2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * FILE: megaraid_sas_fusion.c
22 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/uaccess.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
51 #include <scsi/scsi.h>
52 #include <scsi/scsi_cmnd.h>
53 #include <scsi/scsi_device.h>
54 #include <scsi/scsi_host.h>
55 #include <scsi/scsi_dbg.h>
56 #include <linux/dmi.h>
58 #include "megaraid_sas_fusion.h"
59 #include "megaraid_sas.h"
62 extern void megasas_free_cmds(struct megasas_instance *instance);
63 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
66 megasas_complete_cmd(struct megasas_instance *instance,
67 struct megasas_cmd *cmd, u8 alt_status);
69 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
73 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
74 int megasas_alloc_cmds(struct megasas_instance *instance);
76 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
78 megasas_issue_polled(struct megasas_instance *instance,
79 struct megasas_cmd *cmd);
81 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
83 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
84 void megaraid_sas_kill_hba(struct megasas_instance *instance);
86 extern u32 megasas_dbg_lvl;
87 void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
88 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
90 void megasas_start_timer(struct megasas_instance *instance,
91 struct timer_list *timer,
92 void *fn, unsigned long interval);
93 extern struct megasas_mgmt_info megasas_mgmt_info;
94 extern int resetwaittime;
99 * megasas_enable_intr_fusion - Enables interrupts
100 * @regs: MFI register set
103 megasas_enable_intr_fusion(struct megasas_instance *instance)
105 struct megasas_register_set __iomem *regs;
106 regs = instance->reg_set;
108 instance->mask_interrupts = 0;
109 /* For Thunderbolt/Invader also clear intr on enable */
110 writel(~0, ®s->outbound_intr_status);
111 readl(®s->outbound_intr_status);
113 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
115 /* Dummy readl to force pci flush */
116 readl(®s->outbound_intr_mask);
120 * megasas_disable_intr_fusion - Disables interrupt
121 * @regs: MFI register set
124 megasas_disable_intr_fusion(struct megasas_instance *instance)
126 u32 mask = 0xFFFFFFFF;
128 struct megasas_register_set __iomem *regs;
129 regs = instance->reg_set;
130 instance->mask_interrupts = 1;
132 writel(mask, ®s->outbound_intr_mask);
133 /* Dummy readl to force pci flush */
134 status = readl(®s->outbound_intr_mask);
138 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
142 * Check if it is our interrupt
144 status = readl(®s->outbound_intr_status);
147 writel(status, ®s->outbound_intr_status);
148 readl(®s->outbound_intr_status);
151 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
158 * megasas_get_cmd_fusion - Get a command from the free pool
159 * @instance: Adapter soft state
161 * Returns a blk_tag indexed mpt frame
163 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
164 *instance, u32 blk_tag)
166 struct fusion_context *fusion;
168 fusion = instance->ctrl_context;
169 return fusion->cmd_list[blk_tag];
173 * megasas_return_cmd_fusion - Return a cmd to free command pool
174 * @instance: Adapter soft state
175 * @cmd: Command packet to be returned to free command pool
177 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
178 struct megasas_cmd_fusion *cmd)
181 memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
185 * megasas_fire_cmd_fusion - Sends command to the FW
188 megasas_fire_cmd_fusion(struct megasas_instance *instance,
189 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
191 #if defined(writeq) && defined(CONFIG_64BIT)
192 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
193 le32_to_cpu(req_desc->u.low));
195 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
199 spin_lock_irqsave(&instance->hba_lock, flags);
200 writel(le32_to_cpu(req_desc->u.low),
201 &instance->reg_set->inbound_low_queue_port);
202 writel(le32_to_cpu(req_desc->u.high),
203 &instance->reg_set->inbound_high_queue_port);
204 spin_unlock_irqrestore(&instance->hba_lock, flags);
210 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
211 * @instance: Adapter soft state
213 static void megasas_teardown_frame_pool_fusion(
214 struct megasas_instance *instance)
217 struct fusion_context *fusion = instance->ctrl_context;
219 u16 max_cmd = instance->max_fw_cmds;
221 struct megasas_cmd_fusion *cmd;
223 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
224 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
225 "sense pool : %p\n", fusion->sg_dma_pool,
226 fusion->sense_dma_pool);
231 * Return all frames to pool
233 for (i = 0; i < max_cmd; i++) {
235 cmd = fusion->cmd_list[i];
238 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
239 cmd->sg_frame_phys_addr);
242 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
243 cmd->sense_phys_addr);
247 * Now destroy the pool itself
249 pci_pool_destroy(fusion->sg_dma_pool);
250 pci_pool_destroy(fusion->sense_dma_pool);
252 fusion->sg_dma_pool = NULL;
253 fusion->sense_dma_pool = NULL;
257 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
258 * @instance: Adapter soft state
261 megasas_free_cmds_fusion(struct megasas_instance *instance)
264 struct fusion_context *fusion = instance->ctrl_context;
266 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
269 req_sz = fusion->request_alloc_sz;
270 reply_sz = fusion->reply_alloc_sz;
271 io_frames_sz = fusion->io_frames_alloc_sz;
273 max_cmds = instance->max_fw_cmds;
275 /* Free descriptors and request Frames memory */
276 if (fusion->req_frames_desc)
277 dma_free_coherent(&instance->pdev->dev, req_sz,
278 fusion->req_frames_desc,
279 fusion->req_frames_desc_phys);
281 if (fusion->reply_frames_desc) {
282 pci_pool_free(fusion->reply_frames_desc_pool,
283 fusion->reply_frames_desc,
284 fusion->reply_frames_desc_phys);
285 pci_pool_destroy(fusion->reply_frames_desc_pool);
288 if (fusion->io_request_frames) {
289 pci_pool_free(fusion->io_request_frames_pool,
290 fusion->io_request_frames,
291 fusion->io_request_frames_phys);
292 pci_pool_destroy(fusion->io_request_frames_pool);
295 /* Free the Fusion frame pool */
296 megasas_teardown_frame_pool_fusion(instance);
298 /* Free all the commands in the cmd_list */
299 for (i = 0; i < max_cmds; i++)
300 kfree(fusion->cmd_list[i]);
302 /* Free the cmd_list buffer itself */
303 kfree(fusion->cmd_list);
304 fusion->cmd_list = NULL;
309 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
310 * @instance: Adapter soft state
313 static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
317 struct fusion_context *fusion;
318 struct megasas_cmd_fusion *cmd;
319 u32 total_sz_chain_frame;
321 fusion = instance->ctrl_context;
322 max_cmd = instance->max_fw_cmds;
324 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
327 * Use DMA pool facility provided by PCI layer
330 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
332 total_sz_chain_frame, 4,
334 if (!fusion->sg_dma_pool) {
335 printk(KERN_DEBUG "megasas: failed to setup request pool "
339 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
341 SCSI_SENSE_BUFFERSIZE, 64, 0);
343 if (!fusion->sense_dma_pool) {
344 printk(KERN_DEBUG "megasas: failed to setup sense pool "
346 pci_pool_destroy(fusion->sg_dma_pool);
347 fusion->sg_dma_pool = NULL;
352 * Allocate and attach a frame to each of the commands in cmd_list
354 for (i = 0; i < max_cmd; i++) {
356 cmd = fusion->cmd_list[i];
358 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
360 &cmd->sg_frame_phys_addr);
362 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
363 GFP_KERNEL, &cmd->sense_phys_addr);
365 * megasas_teardown_frame_pool_fusion() takes care of freeing
366 * whatever has been allocated
368 if (!cmd->sg_frame || !cmd->sense) {
369 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
370 megasas_teardown_frame_pool_fusion(instance);
378 * megasas_alloc_cmds_fusion - Allocates the command packets
379 * @instance: Adapter soft state
382 * Each frame has a 32-bit field called context. This context is used to get
383 * back the megasas_cmd_fusion from the frame when a frame gets completed
384 * In this driver, the 32 bit values are the indices into an array cmd_list.
385 * This array is used only to look up the megasas_cmd_fusion given the context.
386 * The free commands themselves are maintained in a linked list called cmd_pool.
388 * cmds are formed in the io_request and sg_frame members of the
389 * megasas_cmd_fusion. The context field is used to get a request descriptor
390 * and is used as SMID of the cmd.
391 * SMID value range is from 1 to max_fw_cmds.
394 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
397 u32 max_cmd, io_frames_sz;
398 struct fusion_context *fusion;
399 struct megasas_cmd_fusion *cmd;
400 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
402 dma_addr_t io_req_base_phys;
405 fusion = instance->ctrl_context;
407 max_cmd = instance->max_fw_cmds;
409 fusion->req_frames_desc =
410 dma_alloc_coherent(&instance->pdev->dev,
411 fusion->request_alloc_sz,
412 &fusion->req_frames_desc_phys, GFP_KERNEL);
414 if (!fusion->req_frames_desc) {
415 printk(KERN_ERR "megasas; Could not allocate memory for "
420 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
421 fusion->reply_frames_desc_pool =
422 pci_pool_create("reply_frames pool", instance->pdev,
423 fusion->reply_alloc_sz * count, 16, 0);
425 if (!fusion->reply_frames_desc_pool) {
426 printk(KERN_ERR "megasas; Could not allocate memory for "
427 "reply_frame pool\n");
428 goto fail_reply_desc;
431 fusion->reply_frames_desc =
432 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
433 &fusion->reply_frames_desc_phys);
434 if (!fusion->reply_frames_desc) {
435 printk(KERN_ERR "megasas; Could not allocate memory for "
436 "reply_frame pool\n");
437 pci_pool_destroy(fusion->reply_frames_desc_pool);
438 goto fail_reply_desc;
441 reply_desc = fusion->reply_frames_desc;
442 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
443 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
445 io_frames_sz = fusion->io_frames_alloc_sz;
447 fusion->io_request_frames_pool =
448 pci_pool_create("io_request_frames pool", instance->pdev,
449 fusion->io_frames_alloc_sz, 16, 0);
451 if (!fusion->io_request_frames_pool) {
452 printk(KERN_ERR "megasas: Could not allocate memory for "
453 "io_request_frame pool\n");
457 fusion->io_request_frames =
458 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
459 &fusion->io_request_frames_phys);
460 if (!fusion->io_request_frames) {
461 printk(KERN_ERR "megasas: Could not allocate memory for "
462 "io_request_frames frames\n");
463 pci_pool_destroy(fusion->io_request_frames_pool);
468 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
469 * Allocate the dynamic array first and then allocate individual
472 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
473 * max_cmd, GFP_KERNEL);
475 if (!fusion->cmd_list) {
476 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
477 "memory for cmd_list_fusion\n");
481 max_cmd = instance->max_fw_cmds;
482 for (i = 0; i < max_cmd; i++) {
483 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
485 if (!fusion->cmd_list[i]) {
486 printk(KERN_ERR "Could not alloc cmd list fusion\n");
488 for (j = 0; j < i; j++)
489 kfree(fusion->cmd_list[j]);
491 kfree(fusion->cmd_list);
492 fusion->cmd_list = NULL;
497 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
498 io_req_base = fusion->io_request_frames +
499 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
500 io_req_base_phys = fusion->io_request_frames_phys +
501 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
504 * Add all the commands to command pool (fusion->cmd_pool)
507 /* SMID 0 is reserved. Set SMID/index from 1 */
508 for (i = 0; i < max_cmd; i++) {
509 cmd = fusion->cmd_list[i];
510 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
511 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
514 cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
515 (i - instance->max_scsi_cmds) :
516 (u32)ULONG_MAX; /* Set to Invalid */
517 cmd->instance = instance;
519 (struct MPI2_RAID_SCSI_IO_REQUEST *)
520 (io_req_base + offset);
521 memset(cmd->io_request, 0,
522 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
523 cmd->io_request_phys_addr = io_req_base_phys + offset;
527 * Create a frame pool and assign one frame to each cmd
529 if (megasas_create_frame_pool_fusion(instance)) {
530 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
531 megasas_free_cmds_fusion(instance);
538 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
539 fusion->io_request_frames_phys);
540 pci_pool_destroy(fusion->io_request_frames_pool);
542 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
543 fusion->reply_frames_desc,
544 fusion->reply_frames_desc_phys);
545 pci_pool_free(fusion->reply_frames_desc_pool,
546 fusion->reply_frames_desc,
547 fusion->reply_frames_desc_phys);
548 pci_pool_destroy(fusion->reply_frames_desc_pool);
551 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
552 fusion->req_frames_desc,
553 fusion->req_frames_desc_phys);
559 * wait_and_poll - Issues a polling command
560 * @instance: Adapter soft state
561 * @cmd: Command packet to be issued
563 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
566 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
570 struct megasas_header *frame_hdr = &cmd->frame->hdr;
571 struct fusion_context *fusion;
573 u32 msecs = seconds * 1000;
575 fusion = instance->ctrl_context;
577 * Wait for cmd_status to change
579 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
584 if (frame_hdr->cmd_status == 0xff)
587 return (frame_hdr->cmd_status == MFI_STAT_OK) ?
592 * megasas_ioc_init_fusion - Initializes the FW
593 * @instance: Adapter soft state
595 * Issues the IOC Init cmd
598 megasas_ioc_init_fusion(struct megasas_instance *instance)
600 struct megasas_init_frame *init_frame;
601 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
602 dma_addr_t ioc_init_handle;
603 struct megasas_cmd *cmd;
605 struct fusion_context *fusion;
606 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
608 struct megasas_header *frame_hdr;
609 const char *sys_info;
611 fusion = instance->ctrl_context;
613 cmd = megasas_get_cmd(instance);
616 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
622 dma_alloc_coherent(&instance->pdev->dev,
623 sizeof(struct MPI2_IOC_INIT_REQUEST),
624 &ioc_init_handle, GFP_KERNEL);
626 if (!IOCInitMessage) {
627 printk(KERN_ERR "Could not allocate memory for "
633 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
635 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
636 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
637 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
638 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
639 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
641 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
642 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
643 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
644 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
645 init_frame = (struct megasas_init_frame *)cmd->frame;
646 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
648 frame_hdr = &cmd->frame->hdr;
649 frame_hdr->cmd_status = 0xFF;
650 frame_hdr->flags = cpu_to_le16(
651 le16_to_cpu(frame_hdr->flags) |
652 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
654 init_frame->cmd = MFI_CMD_INIT;
655 init_frame->cmd_status = 0xFF;
657 /* driver support Extended MSIX */
658 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
659 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
660 init_frame->driver_operations.
661 mfi_capabilities.support_additional_msix = 1;
662 /* driver supports HA / Remote LUN over Fast Path interface */
663 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
665 init_frame->driver_operations.mfi_capabilities.support_max_255lds
667 init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
669 init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw
671 /* Convert capability to LE32 */
672 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
674 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
675 if (instance->system_info_buf && sys_info) {
676 memcpy(instance->system_info_buf->systemId, sys_info,
677 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
678 instance->system_info_buf->systemIdLength =
679 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
680 init_frame->system_info_lo = instance->system_info_h;
681 init_frame->system_info_hi = 0;
684 init_frame->queue_info_new_phys_addr_hi =
685 cpu_to_le32(upper_32_bits(ioc_init_handle));
686 init_frame->queue_info_new_phys_addr_lo =
687 cpu_to_le32(lower_32_bits(ioc_init_handle));
688 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
690 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
691 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
692 req_desc.MFAIo.RequestFlags =
693 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
694 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
697 * disable the intr before firing the init frame
699 instance->instancet->disable_intr(instance);
701 for (i = 0; i < (10 * 1000); i += 20) {
702 if (readl(&instance->reg_set->doorbell) & 1)
708 megasas_fire_cmd_fusion(instance, &req_desc);
710 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
712 frame_hdr = &cmd->frame->hdr;
713 if (frame_hdr->cmd_status != 0) {
717 printk(KERN_ERR "megasas:IOC Init cmd success\n");
722 megasas_return_cmd(instance, cmd);
724 dma_free_coherent(&instance->pdev->dev,
725 sizeof(struct MPI2_IOC_INIT_REQUEST),
726 IOCInitMessage, ioc_init_handle);
732 * megasas_get_ld_map_info - Returns FW's ld_map structure
733 * @instance: Adapter soft state
734 * @pend: Pend the command or not
735 * Issues an internal command (DCMD) to get the FW's controller PD
736 * list structure. This information is mainly used to find out SYSTEM
737 * supported by the FW.
738 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
739 * dcmd.mbox.b[0] - number of LDs being sync'd
740 * dcmd.mbox.b[1] - 0 - complete command immediately.
741 * - 1 - pend till config change
742 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
743 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
744 * uses extended struct MR_FW_RAID_MAP_EXT
747 megasas_get_ld_map_info(struct megasas_instance *instance)
750 struct megasas_cmd *cmd;
751 struct megasas_dcmd_frame *dcmd;
755 struct fusion_context *fusion;
757 cmd = megasas_get_cmd(instance);
760 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
764 fusion = instance->ctrl_context;
767 megasas_return_cmd(instance, cmd);
771 dcmd = &cmd->frame->dcmd;
773 size_map_info = fusion->current_map_sz;
775 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
776 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
779 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
780 megasas_return_cmd(instance, cmd);
784 memset(ci, 0, fusion->max_map_sz);
785 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
787 dev_dbg(&instance->pdev->dev,
788 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
789 __func__, cpu_to_le32(size_map_info));
791 dcmd->cmd = MFI_CMD_DCMD;
792 dcmd->cmd_status = 0xFF;
794 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
797 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
798 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
799 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
800 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
802 if (instance->ctrl_context && !instance->mask_interrupts)
803 ret = megasas_issue_blocked_cmd(instance, cmd,
804 MEGASAS_BLOCKED_CMD_TIMEOUT);
806 ret = megasas_issue_polled(instance, cmd);
808 megasas_return_cmd(instance, cmd);
814 megasas_get_map_info(struct megasas_instance *instance)
816 struct fusion_context *fusion = instance->ctrl_context;
818 fusion->fast_path_io = 0;
819 if (!megasas_get_ld_map_info(instance)) {
820 if (MR_ValidateMapInfo(instance)) {
821 fusion->fast_path_io = 1;
829 * megasas_sync_map_info - Returns FW's ld_map structure
830 * @instance: Adapter soft state
832 * Issues an internal command (DCMD) to get the FW's controller PD
833 * list structure. This information is mainly used to find out SYSTEM
834 * supported by the FW.
837 megasas_sync_map_info(struct megasas_instance *instance)
840 struct megasas_cmd *cmd;
841 struct megasas_dcmd_frame *dcmd;
842 u32 size_sync_info, num_lds;
843 struct fusion_context *fusion;
844 struct MR_LD_TARGET_SYNC *ci = NULL;
845 struct MR_DRV_RAID_MAP_ALL *map;
846 struct MR_LD_RAID *raid;
847 struct MR_LD_TARGET_SYNC *ld_sync;
851 cmd = megasas_get_cmd(instance);
854 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
859 fusion = instance->ctrl_context;
862 megasas_return_cmd(instance, cmd);
866 map = fusion->ld_drv_map[instance->map_id & 1];
868 num_lds = le16_to_cpu(map->raidMap.ldCount);
870 dcmd = &cmd->frame->dcmd;
872 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
874 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
876 ci = (struct MR_LD_TARGET_SYNC *)
877 fusion->ld_map[(instance->map_id - 1) & 1];
878 memset(ci, 0, fusion->max_map_sz);
880 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
882 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
884 for (i = 0; i < num_lds; i++, ld_sync++) {
885 raid = MR_LdRaidGet(i, map);
886 ld_sync->targetId = MR_GetLDTgtId(i, map);
887 ld_sync->seqNum = raid->seqNum;
890 size_map_info = fusion->current_map_sz;
892 dcmd->cmd = MFI_CMD_DCMD;
893 dcmd->cmd_status = 0xFF;
895 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
898 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
899 dcmd->mbox.b[0] = num_lds;
900 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
901 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
902 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
903 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
905 instance->map_update_cmd = cmd;
907 instance->instancet->issue_dcmd(instance, cmd);
913 * meagasas_display_intel_branding - Display branding string
914 * @instance: per adapter object
919 megasas_display_intel_branding(struct megasas_instance *instance)
921 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
924 switch (instance->pdev->device) {
925 case PCI_DEVICE_ID_LSI_INVADER:
926 switch (instance->pdev->subsystem_device) {
927 case MEGARAID_INTEL_RS3DC080_SSDID:
928 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
929 instance->host->host_no,
930 MEGARAID_INTEL_RS3DC080_BRANDING);
932 case MEGARAID_INTEL_RS3DC040_SSDID:
933 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
934 instance->host->host_no,
935 MEGARAID_INTEL_RS3DC040_BRANDING);
937 case MEGARAID_INTEL_RS3SC008_SSDID:
938 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
939 instance->host->host_no,
940 MEGARAID_INTEL_RS3SC008_BRANDING);
942 case MEGARAID_INTEL_RS3MC044_SSDID:
943 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
944 instance->host->host_no,
945 MEGARAID_INTEL_RS3MC044_BRANDING);
951 case PCI_DEVICE_ID_LSI_FURY:
952 switch (instance->pdev->subsystem_device) {
953 case MEGARAID_INTEL_RS3WC080_SSDID:
954 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
955 instance->host->host_no,
956 MEGARAID_INTEL_RS3WC080_BRANDING);
958 case MEGARAID_INTEL_RS3WC040_SSDID:
959 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
960 instance->host->host_no,
961 MEGARAID_INTEL_RS3WC040_BRANDING);
973 * megasas_init_adapter_fusion - Initializes the FW
974 * @instance: Adapter soft state
976 * This is the main function for initializing firmware.
979 megasas_init_adapter_fusion(struct megasas_instance *instance)
981 struct megasas_register_set __iomem *reg_set;
982 struct fusion_context *fusion;
986 fusion = instance->ctrl_context;
988 reg_set = instance->reg_set;
991 * Get various operational parameters from status register
993 instance->max_fw_cmds =
994 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
995 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
998 * Reduce the max supported cmds by 1. This is to ensure that the
999 * reply_q_sz (1 more than the max cmd that driver may send)
1000 * does not exceed max cmds that the FW can support
1002 instance->max_fw_cmds = instance->max_fw_cmds-1;
1005 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1007 instance->max_mfi_cmds =
1008 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1010 max_cmd = instance->max_fw_cmds;
1012 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
1014 fusion->request_alloc_sz =
1015 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1016 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1017 *(fusion->reply_q_depth);
1018 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1019 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1020 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1022 fusion->max_sge_in_main_msg =
1023 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1024 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1026 fusion->max_sge_in_chain =
1027 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
1029 instance->max_num_sge = rounddown_pow_of_two(
1030 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
1032 /* Used for pass thru MFI frame (DCMD) */
1033 fusion->chain_offset_mfi_pthru =
1034 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1036 fusion->chain_offset_io_request =
1037 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1038 sizeof(union MPI2_SGE_IO_UNION))/16;
1040 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1041 for (i = 0 ; i < count; i++)
1042 fusion->last_reply_idx[i] = 0;
1045 * For fusion adapters, 3 commands for IOCTL and 5 commands
1046 * for driver's internal DCMDs.
1048 instance->max_scsi_cmds = instance->max_fw_cmds -
1049 (MEGASAS_FUSION_INTERNAL_CMDS +
1050 MEGASAS_FUSION_IOCTL_CMDS);
1051 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1054 * Allocate memory for descriptors
1055 * Create a pool of commands
1057 if (megasas_alloc_cmds(instance))
1058 goto fail_alloc_mfi_cmds;
1059 if (megasas_alloc_cmds_fusion(instance))
1060 goto fail_alloc_cmds;
1062 if (megasas_ioc_init_fusion(instance))
1065 megasas_display_intel_branding(instance);
1066 if (megasas_get_ctrl_info(instance)) {
1067 dev_err(&instance->pdev->dev,
1068 "Could not get controller info. Fail from %s %d\n",
1069 __func__, __LINE__);
1073 instance->flag_ieee = 1;
1074 fusion->fast_path_io = 0;
1076 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1077 for (i = 0; i < 2; i++) {
1078 fusion->ld_map[i] = NULL;
1079 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1080 fusion->drv_map_pages);
1081 if (!fusion->ld_drv_map[i]) {
1082 dev_err(&instance->pdev->dev, "Could not allocate "
1083 "memory for local map info for %d pages\n",
1084 fusion->drv_map_pages);
1086 free_pages((ulong)fusion->ld_drv_map[0],
1087 fusion->drv_map_pages);
1090 memset(fusion->ld_drv_map[i], 0,
1091 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
1094 for (i = 0; i < 2; i++) {
1095 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1097 &fusion->ld_map_phys[i],
1099 if (!fusion->ld_map[i]) {
1100 printk(KERN_ERR "megasas: Could not allocate memory "
1106 if (!megasas_get_map_info(instance))
1107 megasas_sync_map_info(instance);
1113 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
1114 fusion->ld_map[0], fusion->ld_map_phys[0]);
1116 megasas_free_cmds_fusion(instance);
1118 megasas_free_cmds(instance);
1119 fail_alloc_mfi_cmds:
1124 * map_cmd_status - Maps FW cmd status to OS cmd status
1125 * @cmd : Pointer to cmd
1126 * @status : status of cmd returned by FW
1127 * @ext_status : ext status of cmd returned by FW
1131 map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1137 cmd->scmd->result = DID_OK << 16;
1140 case MFI_STAT_SCSI_IO_FAILED:
1141 case MFI_STAT_LD_INIT_IN_PROGRESS:
1142 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1145 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1147 cmd->scmd->result = (DID_OK << 16) | ext_status;
1148 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1149 memset(cmd->scmd->sense_buffer, 0,
1150 SCSI_SENSE_BUFFERSIZE);
1151 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1152 SCSI_SENSE_BUFFERSIZE);
1153 cmd->scmd->result |= DRIVER_SENSE << 24;
1157 case MFI_STAT_LD_OFFLINE:
1158 case MFI_STAT_DEVICE_NOT_FOUND:
1159 cmd->scmd->result = DID_BAD_TARGET << 16;
1161 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1162 cmd->scmd->result = DID_IMM_RETRY << 16;
1165 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1166 cmd->scmd->result = DID_ERROR << 16;
1172 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1173 * @instance: Adapter soft state
1174 * @scp: SCSI command from the mid-layer
1175 * @sgl_ptr: SGL to be filled in
1176 * @cmd: cmd we are working on
1178 * If successful, this function returns the number of SG elements.
1181 megasas_make_sgl_fusion(struct megasas_instance *instance,
1182 struct scsi_cmnd *scp,
1183 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1184 struct megasas_cmd_fusion *cmd)
1186 int i, sg_processed, sge_count;
1187 struct scatterlist *os_sgl;
1188 struct fusion_context *fusion;
1190 fusion = instance->ctrl_context;
1192 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1193 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1194 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1195 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1196 sgl_ptr_end->Flags = 0;
1199 sge_count = scsi_dma_map(scp);
1201 BUG_ON(sge_count < 0);
1203 if (sge_count > instance->max_num_sge || !sge_count)
1206 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1207 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1208 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1210 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1211 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1212 if (i == sge_count - 1)
1213 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1217 sg_processed = i + 1;
1219 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1220 (sge_count > fusion->max_sge_in_main_msg)) {
1222 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1223 if ((instance->pdev->device ==
1224 PCI_DEVICE_ID_LSI_INVADER) ||
1225 (instance->pdev->device ==
1226 PCI_DEVICE_ID_LSI_FURY)) {
1227 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1228 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1229 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1230 cmd->io_request->ChainOffset =
1232 chain_offset_io_request;
1234 cmd->io_request->ChainOffset = 0;
1236 cmd->io_request->ChainOffset =
1237 fusion->chain_offset_io_request;
1240 /* Prepare chain element */
1241 sg_chain->NextChainOffset = 0;
1242 if ((instance->pdev->device ==
1243 PCI_DEVICE_ID_LSI_INVADER) ||
1244 (instance->pdev->device ==
1245 PCI_DEVICE_ID_LSI_FURY))
1246 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1249 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1250 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1251 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1252 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1255 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1256 memset(sgl_ptr, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
1264 * megasas_set_pd_lba - Sets PD LBA
1266 * @cdb_len: cdb length
1267 * @start_blk: Start block of IO
1269 * Used to set the PD LBA in CDB for FP IOs
1272 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1273 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1274 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1276 struct MR_LD_RAID *raid;
1278 u64 start_blk = io_info->pdBlock;
1279 u8 *cdb = io_request->CDB.CDB32;
1280 u32 num_blocks = io_info->numBlocks;
1281 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1283 /* Check if T10 PI (DIF) is enabled for this LD */
1284 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1285 raid = MR_LdRaidGet(ld, local_map_ptr);
1286 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1287 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1288 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1289 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1291 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1292 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1294 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1295 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1298 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1299 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1300 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1301 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1302 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1303 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1304 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1305 cdb[19] = (u8)(start_blk & 0xff);
1307 /* Logical block reference tag */
1308 io_request->CDB.EEDP32.PrimaryReferenceTag =
1309 cpu_to_be32(ref_tag);
1310 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
1311 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1313 /* Transfer length */
1314 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1315 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1316 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1317 cdb[31] = (u8)(num_blocks & 0xff);
1319 /* set SCSI IO EEDPFlags */
1320 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1321 io_request->EEDPFlags = cpu_to_le16(
1322 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1323 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1324 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1325 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1326 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1328 io_request->EEDPFlags = cpu_to_le16(
1329 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1330 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1332 io_request->Control |= cpu_to_le32((0x4 << 26));
1333 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1335 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1336 if (((cdb_len == 12) || (cdb_len == 16)) &&
1337 (start_blk <= 0xffffffff)) {
1338 if (cdb_len == 16) {
1339 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1344 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1350 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1357 /* Transfer length */
1358 cdb[8] = (u8)(num_blocks & 0xff);
1359 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1361 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1363 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1364 /* Convert to 16 byte CDB for large LBA's */
1367 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1372 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1379 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1386 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1393 /* Transfer length */
1394 cdb[13] = (u8)(num_blocks & 0xff);
1395 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1396 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1397 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1399 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1403 /* Normal case, just load LBA here */
1407 u8 val = cdb[1] & 0xE0;
1408 cdb[3] = (u8)(start_blk & 0xff);
1409 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1410 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1414 cdb[5] = (u8)(start_blk & 0xff);
1415 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1416 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1417 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1420 cdb[5] = (u8)(start_blk & 0xff);
1421 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1422 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1423 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1426 cdb[9] = (u8)(start_blk & 0xff);
1427 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1428 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1429 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1430 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1431 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1432 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1433 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1440 * megasas_build_ldio_fusion - Prepares IOs to devices
1441 * @instance: Adapter soft state
1442 * @scp: SCSI command
1443 * @cmd: Command to be prepared
1445 * Prepares the io_request and chain elements (sg_frame) for IO
1446 * The IO can be for PD (Fast Path) or LD
1449 megasas_build_ldio_fusion(struct megasas_instance *instance,
1450 struct scsi_cmnd *scp,
1451 struct megasas_cmd_fusion *cmd)
1454 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
1455 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1456 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1457 struct IO_REQUEST_INFO io_info;
1458 struct fusion_context *fusion;
1459 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1462 device_id = MEGASAS_DEV_INDEX(scp);
1464 fusion = instance->ctrl_context;
1466 io_request = cmd->io_request;
1467 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
1468 io_request->RaidContext.status = 0;
1469 io_request->RaidContext.exStatus = 0;
1471 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1478 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1480 if (scp->cmd_len == 6) {
1481 datalength = (u32) scp->cmnd[4];
1482 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1483 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1485 start_lba_lo &= 0x1FFFFF;
1489 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1491 else if (scp->cmd_len == 10) {
1492 datalength = (u32) scp->cmnd[8] |
1493 ((u32) scp->cmnd[7] << 8);
1494 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1495 ((u32) scp->cmnd[3] << 16) |
1496 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1500 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1502 else if (scp->cmd_len == 12) {
1503 datalength = ((u32) scp->cmnd[6] << 24) |
1504 ((u32) scp->cmnd[7] << 16) |
1505 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1506 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1507 ((u32) scp->cmnd[3] << 16) |
1508 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1512 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1514 else if (scp->cmd_len == 16) {
1515 datalength = ((u32) scp->cmnd[10] << 24) |
1516 ((u32) scp->cmnd[11] << 16) |
1517 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1518 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1519 ((u32) scp->cmnd[7] << 16) |
1520 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1522 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1523 ((u32) scp->cmnd[3] << 16) |
1524 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1527 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1528 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1529 io_info.numBlocks = datalength;
1530 io_info.ldTgtId = device_id;
1531 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
1533 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1536 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1538 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1539 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
1540 io_request->RaidContext.regLockFlags = 0;
1543 if (MR_BuildRaidContext(instance, &io_info,
1544 &io_request->RaidContext,
1545 local_map_ptr, &raidLUN))
1546 fp_possible = io_info.fpOkForIo;
1549 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
1550 id by default, not CPU group id, otherwise all MSI-X queues won't
1552 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1553 raw_smp_processor_id() % instance->msix_vectors : 0;
1556 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1557 local_map_ptr, start_lba_lo);
1558 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1559 cmd->request_desc->SCSIIO.RequestFlags =
1560 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1561 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1562 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1563 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1564 if (io_request->RaidContext.regLockFlags ==
1566 cmd->request_desc->SCSIIO.RequestFlags =
1567 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1568 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1569 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1570 io_request->RaidContext.nseg = 0x1;
1571 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1572 io_request->RaidContext.regLockFlags |=
1573 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1574 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1576 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1579 get_updated_dev_handle(instance,
1580 &fusion->load_balance_info[device_id],
1582 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1583 cmd->pd_r1_lb = io_info.pd_after_lb;
1585 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1587 if ((raidLUN[0] == 1) &&
1588 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 2)) {
1589 instance->dev_handle = !(instance->dev_handle);
1591 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
1594 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1595 io_request->DevHandle = io_info.devHandle;
1596 /* populate the LUN field */
1597 memcpy(io_request->LUN, raidLUN, 8);
1599 io_request->RaidContext.timeoutValue =
1600 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
1601 cmd->request_desc->SCSIIO.RequestFlags =
1602 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1603 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1604 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1605 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1606 if (io_request->RaidContext.regLockFlags ==
1608 cmd->request_desc->SCSIIO.RequestFlags =
1609 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1610 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1611 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1612 io_request->RaidContext.regLockFlags |=
1613 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1614 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1615 io_request->RaidContext.nseg = 0x1;
1617 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1618 io_request->DevHandle = cpu_to_le16(device_id);
1623 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
1624 * @instance: Adapter soft state
1625 * @scp: SCSI command
1626 * @cmd: Command to be prepared
1628 * Prepares the io_request frame for non-rw io cmds for vd.
1630 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1631 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
1634 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1636 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1637 struct fusion_context *fusion = instance->ctrl_context;
1641 struct MR_LD_RAID *raid;
1642 struct RAID_CONTEXT *pRAID_Context;
1645 io_request = cmd->io_request;
1646 device_id = MEGASAS_DEV_INDEX(scmd);
1647 pd_index = MEGASAS_PD_INDEX(scmd);
1648 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1649 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1650 /* get RAID_Context pointer */
1651 pRAID_Context = &io_request->RaidContext;
1652 /* Check with FW team */
1653 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1654 pRAID_Context->regLockRowLBA = 0;
1655 pRAID_Context->regLockLength = 0;
1657 if (fusion->fast_path_io && (
1658 device_id < instance->fw_supported_vd_count)) {
1660 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1661 if (ld >= instance->fw_supported_vd_count)
1664 raid = MR_LdRaidGet(ld, local_map_ptr);
1665 if (!(raid->capability.fpNonRWCapable))
1671 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1672 io_request->DevHandle = cpu_to_le16(device_id);
1673 io_request->LUN[1] = scmd->device->lun;
1674 pRAID_Context->timeoutValue =
1675 cpu_to_le16 (scmd->request->timeout / HZ);
1676 cmd->request_desc->SCSIIO.RequestFlags =
1677 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1678 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1681 /* set RAID context values */
1682 pRAID_Context->configSeqNum = raid->seqNum;
1683 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1684 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
1686 /* get the DevHandle for the PD (since this is
1687 fpNonRWCapable, this is a single disk RAID0) */
1689 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1690 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1691 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1693 /* build request descriptor */
1694 cmd->request_desc->SCSIIO.RequestFlags =
1695 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1696 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1697 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1699 /* populate the LUN field */
1700 memcpy(io_request->LUN, raid->LUN, 8);
1702 /* build the raidScsiIO structure */
1703 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1704 io_request->DevHandle = devHandle;
1709 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1710 * @instance: Adapter soft state
1711 * @scp: SCSI command
1712 * @cmd: Command to be prepared
1713 * @fp_possible: parameter to detect fast path or firmware path io.
1715 * Prepares the io_request frame for rw/non-rw io cmds for syspds
1718 megasas_build_syspd_fusion(struct megasas_instance *instance,
1719 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1722 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1724 u16 os_timeout_value;
1726 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1727 struct RAID_CONTEXT *pRAID_Context;
1728 struct fusion_context *fusion = instance->ctrl_context;
1730 device_id = MEGASAS_DEV_INDEX(scmd);
1731 pd_index = MEGASAS_PD_INDEX(scmd);
1732 os_timeout_value = scmd->request->timeout / HZ;
1734 io_request = cmd->io_request;
1735 /* get RAID_Context pointer */
1736 pRAID_Context = &io_request->RaidContext;
1737 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1738 io_request->LUN[1] = scmd->device->lun;
1739 pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1740 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1742 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1743 pRAID_Context->configSeqNum = 0;
1744 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1745 io_request->DevHandle =
1746 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1748 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1749 cmd->request_desc->SCSIIO.MSIxIndex =
1750 instance->msix_vectors ?
1751 (raw_smp_processor_id() % instance->msix_vectors) : 0;
1755 /* system pd firmware path */
1756 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1757 cmd->request_desc->SCSIIO.RequestFlags =
1758 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1759 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1760 pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
1762 /* system pd Fast Path */
1763 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1764 pRAID_Context->regLockFlags = 0;
1765 pRAID_Context->regLockRowLBA = 0;
1766 pRAID_Context->regLockLength = 0;
1767 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1769 pRAID_Context->timeoutValue =
1770 cpu_to_le16((os_timeout_value > timeout_limit) ?
1771 timeout_limit : os_timeout_value);
1772 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1773 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1774 cmd->request_desc->SCSIIO.RequestFlags |=
1775 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1776 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1777 pRAID_Context->Type = MPI2_TYPE_CUDA;
1778 pRAID_Context->nseg = 0x1;
1779 io_request->IoFlags |=
1780 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1782 cmd->request_desc->SCSIIO.RequestFlags =
1783 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1784 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1789 * megasas_build_io_fusion - Prepares IOs to devices
1790 * @instance: Adapter soft state
1791 * @scp: SCSI command
1792 * @cmd: Command to be prepared
1794 * Invokes helper functions to prepare request frames
1795 * and sets flags appropriate for IO/Non-IO cmd
1798 megasas_build_io_fusion(struct megasas_instance *instance,
1799 struct scsi_cmnd *scp,
1800 struct megasas_cmd_fusion *cmd)
1804 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1806 /* Zero out some fields so they don't get reused */
1807 memset(io_request->LUN, 0x0, 8);
1808 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1809 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1810 io_request->EEDPFlags = 0;
1811 io_request->Control = 0;
1812 io_request->EEDPBlockSize = 0;
1813 io_request->ChainOffset = 0;
1814 io_request->RaidContext.RAIDFlags = 0;
1815 io_request->RaidContext.Type = 0;
1816 io_request->RaidContext.nseg = 0;
1818 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1820 * Just the CDB length,rest of the Flags are zero
1821 * This will be modified for FP in build_ldio_fusion
1823 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
1825 switch (cmd_type = megasas_cmd_type(scp)) {
1826 case READ_WRITE_LDIO:
1827 megasas_build_ldio_fusion(instance, scp, cmd);
1829 case NON_READ_WRITE_LDIO:
1830 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1832 case READ_WRITE_SYSPDIO:
1833 case NON_READ_WRITE_SYSPDIO:
1834 if (instance->secure_jbod_support &&
1835 (cmd_type == NON_READ_WRITE_SYSPDIO))
1836 megasas_build_syspd_fusion(instance, scp, cmd, 0);
1838 megasas_build_syspd_fusion(instance, scp, cmd, 1);
1849 megasas_make_sgl_fusion(instance, scp,
1850 (struct MPI25_IEEE_SGE_CHAIN64 *)
1851 &io_request->SGL, cmd);
1853 if (sge_count > instance->max_num_sge) {
1854 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1855 "max (0x%x) allowed\n", sge_count,
1856 instance->max_num_sge);
1860 io_request->RaidContext.numSGE = sge_count;
1862 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
1864 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1865 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
1866 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1867 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
1869 io_request->SGLOffset0 =
1870 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1872 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
1873 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1876 scp->SCp.ptr = (char *)cmd;
1881 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1882 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1885 struct fusion_context *fusion;
1887 if (index >= instance->max_fw_cmds) {
1888 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1889 "descriptor for scsi%d\n", index,
1890 instance->host->host_no);
1893 fusion = instance->ctrl_context;
1894 p = fusion->req_frames_desc
1895 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1897 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1901 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1902 * issuing non IOCTL cmd
1903 * @instance: Adapter soft state
1904 * @scmd: pointer to scsi cmd from OS
1907 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1908 struct scsi_cmnd *scmd)
1910 struct megasas_cmd_fusion *cmd;
1911 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1913 struct fusion_context *fusion;
1915 fusion = instance->ctrl_context;
1917 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
1921 req_desc = megasas_get_request_descriptor(instance, index-1);
1925 req_desc->Words = 0;
1926 cmd->request_desc = req_desc;
1928 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1929 megasas_return_cmd_fusion(instance, cmd);
1930 printk(KERN_ERR "megasas: Error building command.\n");
1931 cmd->request_desc = NULL;
1935 req_desc = cmd->request_desc;
1936 req_desc->SCSIIO.SMID = cpu_to_le16(index);
1938 if (cmd->io_request->ChainOffset != 0 &&
1939 cmd->io_request->ChainOffset != 0xF)
1940 printk(KERN_ERR "megasas: The chain offset value is not "
1941 "correct : %x\n", cmd->io_request->ChainOffset);
1944 * Issue the command to the FW
1946 atomic_inc(&instance->fw_outstanding);
1948 megasas_fire_cmd_fusion(instance, req_desc);
1954 * complete_cmd_fusion - Completes command
1955 * @instance: Adapter soft state
1956 * Completes all commands that is in reply descriptor queue
1959 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
1961 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1962 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1963 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1964 struct fusion_context *fusion;
1965 struct megasas_cmd *cmd_mfi;
1966 struct megasas_cmd_fusion *cmd_fusion;
1967 u16 smid, num_completed;
1968 u8 reply_descript_type;
1969 u32 status, extStatus, device_id;
1970 union desc_value d_val;
1971 struct LD_LOAD_BALANCE_INFO *lbinfo;
1972 int threshold_reply_count = 0;
1973 struct scsi_cmnd *scmd_local = NULL;
1975 fusion = instance->ctrl_context;
1977 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1980 desc = fusion->reply_frames_desc;
1981 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1982 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1983 fusion->last_reply_idx[MSIxIndex];
1985 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1987 d_val.word = desc->Words;
1989 reply_descript_type = reply_desc->ReplyFlags &
1990 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1992 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1997 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
1998 d_val.u.high != cpu_to_le32(UINT_MAX)) {
1999 smid = le16_to_cpu(reply_desc->SMID);
2001 cmd_fusion = fusion->cmd_list[smid - 1];
2004 (struct MPI2_RAID_SCSI_IO_REQUEST *)
2005 cmd_fusion->io_request;
2007 if (cmd_fusion->scmd)
2008 cmd_fusion->scmd->SCp.ptr = NULL;
2010 scmd_local = cmd_fusion->scmd;
2011 status = scsi_io_req->RaidContext.status;
2012 extStatus = scsi_io_req->RaidContext.exStatus;
2014 switch (scsi_io_req->Function) {
2015 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
2016 /* Update load balancing info */
2017 device_id = MEGASAS_DEV_INDEX(scmd_local);
2018 lbinfo = &fusion->load_balance_info[device_id];
2019 if (cmd_fusion->scmd->SCp.Status &
2020 MEGASAS_LOAD_BALANCE_FLAG) {
2021 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
2022 cmd_fusion->scmd->SCp.Status &=
2023 ~MEGASAS_LOAD_BALANCE_FLAG;
2025 if (reply_descript_type ==
2026 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2027 if (megasas_dbg_lvl == 5)
2028 printk(KERN_ERR "\nmegasas: FAST Path "
2031 /* Fall thru and complete IO */
2032 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2033 /* Map the FW Cmd Status */
2034 map_cmd_status(cmd_fusion, status, extStatus);
2035 scsi_io_req->RaidContext.status = 0;
2036 scsi_io_req->RaidContext.exStatus = 0;
2037 megasas_return_cmd_fusion(instance, cmd_fusion);
2038 scsi_dma_unmap(scmd_local);
2039 scmd_local->scsi_done(scmd_local);
2040 atomic_dec(&instance->fw_outstanding);
2043 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2044 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2046 /* Poll mode. Dummy free.
2047 * In case of Interrupt mode, caller has reverse check.
2049 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2050 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2051 megasas_return_cmd(instance, cmd_mfi);
2053 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2057 fusion->last_reply_idx[MSIxIndex]++;
2058 if (fusion->last_reply_idx[MSIxIndex] >=
2059 fusion->reply_q_depth)
2060 fusion->last_reply_idx[MSIxIndex] = 0;
2062 desc->Words = cpu_to_le64(ULLONG_MAX);
2064 threshold_reply_count++;
2066 /* Get the next reply descriptor */
2067 if (!fusion->last_reply_idx[MSIxIndex])
2068 desc = fusion->reply_frames_desc +
2069 ((MSIxIndex * fusion->reply_alloc_sz)/
2070 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
2075 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2077 d_val.word = desc->Words;
2079 reply_descript_type = reply_desc->ReplyFlags &
2080 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2082 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2085 * Write to reply post host index register after completing threshold
2086 * number of reply counts and still there are more replies in reply queue
2087 * pending to be completed
2089 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2090 if ((instance->pdev->device ==
2091 PCI_DEVICE_ID_LSI_INVADER) ||
2092 (instance->pdev->device ==
2093 PCI_DEVICE_ID_LSI_FURY))
2094 writel(((MSIxIndex & 0x7) << 24) |
2095 fusion->last_reply_idx[MSIxIndex],
2096 instance->reply_post_host_index_addr[MSIxIndex/8]);
2098 writel((MSIxIndex << 24) |
2099 fusion->last_reply_idx[MSIxIndex],
2100 instance->reply_post_host_index_addr[0]);
2101 threshold_reply_count = 0;
2109 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2110 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2111 writel(((MSIxIndex & 0x7) << 24) |
2112 fusion->last_reply_idx[MSIxIndex],
2113 instance->reply_post_host_index_addr[MSIxIndex/8]);
2115 writel((MSIxIndex << 24) |
2116 fusion->last_reply_idx[MSIxIndex],
2117 instance->reply_post_host_index_addr[0]);
2118 megasas_check_and_restore_queue_depth(instance);
2123 * megasas_complete_cmd_dpc_fusion - Completes command
2124 * @instance: Adapter soft state
2126 * Tasklet to complete cmds
2129 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2131 struct megasas_instance *instance =
2132 (struct megasas_instance *)instance_addr;
2133 unsigned long flags;
2134 u32 count, MSIxIndex;
2136 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2138 /* If we have already declared adapter dead, donot complete cmds */
2139 spin_lock_irqsave(&instance->hba_lock, flags);
2140 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2141 spin_unlock_irqrestore(&instance->hba_lock, flags);
2144 spin_unlock_irqrestore(&instance->hba_lock, flags);
2146 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2147 complete_cmd_fusion(instance, MSIxIndex);
2151 * megasas_isr_fusion - isr entry point
2153 irqreturn_t megasas_isr_fusion(int irq, void *devp)
2155 struct megasas_irq_context *irq_context = devp;
2156 struct megasas_instance *instance = irq_context->instance;
2157 u32 mfiStatus, fw_state, dma_state;
2159 if (instance->mask_interrupts)
2162 if (!instance->msix_vectors) {
2163 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2168 /* If we are resetting, bail */
2169 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2170 instance->instancet->clear_intr(instance->reg_set);
2174 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
2175 instance->instancet->clear_intr(instance->reg_set);
2176 /* If we didn't complete any commands, check for FW fault */
2177 fw_state = instance->instancet->read_fw_status_reg(
2178 instance->reg_set) & MFI_STATE_MASK;
2179 dma_state = instance->instancet->read_fw_status_reg
2180 (instance->reg_set) & MFI_STATE_DMADONE;
2181 if (instance->crash_dump_drv_support &&
2182 instance->crash_dump_app_support) {
2183 /* Start collecting crash, if DMA bit is done */
2184 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2185 schedule_work(&instance->crash_init);
2186 else if (fw_state == MFI_STATE_FAULT)
2187 schedule_work(&instance->work_init);
2188 } else if (fw_state == MFI_STATE_FAULT) {
2189 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2190 "for scsi%d\n", instance->host->host_no);
2191 schedule_work(&instance->work_init);
2199 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2200 * @instance: Adapter soft state
2201 * mfi_cmd: megasas_cmd pointer
2205 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2206 struct megasas_cmd *mfi_cmd)
2208 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2209 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2210 struct megasas_cmd_fusion *cmd;
2211 struct fusion_context *fusion;
2212 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2214 fusion = instance->ctrl_context;
2216 cmd = megasas_get_cmd_fusion(instance,
2217 instance->max_scsi_cmds + mfi_cmd->index);
2219 /* Save the smid. To be used for returning the cmd */
2220 mfi_cmd->context.smid = cmd->index;
2223 * For cmds where the flag is set, store the flag and check
2224 * on completion. For cmds with this flag, don't call
2225 * megasas_complete_cmd
2228 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
2229 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
2231 io_req = cmd->io_request;
2233 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2234 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
2235 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2236 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2237 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2238 sgl_ptr_end->Flags = 0;
2242 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2244 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2245 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2247 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2249 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
2251 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2252 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2254 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
2260 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2261 * @instance: Adapter soft state
2262 * @cmd: mfi cmd to build
2265 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2266 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2268 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2271 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2272 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2276 index = cmd->context.smid;
2278 req_desc = megasas_get_request_descriptor(instance, index - 1);
2283 req_desc->Words = 0;
2284 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2285 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2287 req_desc->SCSIIO.SMID = cpu_to_le16(index);
2293 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2294 * @instance: Adapter soft state
2295 * @cmd: mfi cmd pointer
2299 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2300 struct megasas_cmd *cmd)
2302 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2304 req_desc = build_mpt_cmd(instance, cmd);
2306 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2309 megasas_fire_cmd_fusion(instance, req_desc);
2313 * megasas_release_fusion - Reverses the FW initialization
2314 * @instance: Adapter soft state
2317 megasas_release_fusion(struct megasas_instance *instance)
2319 megasas_free_cmds(instance);
2320 megasas_free_cmds_fusion(instance);
2322 iounmap(instance->reg_set);
2324 pci_release_selected_regions(instance->pdev, instance->bar);
2328 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2329 * @regs: MFI register set
2332 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2334 return readl(&(regs)->outbound_scratch_pad);
2338 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2339 * @instance: Controller's soft instance
2340 * return: Number of allocated host crash buffers
2343 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2347 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2348 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2349 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2350 instance->crash_buf_pages);
2351 if (!instance->crash_buf[i]) {
2352 dev_info(&instance->pdev->dev, "Firmware crash dump "
2353 "memory allocation failed at index %d\n", i);
2356 memset(instance->crash_buf[i], 0,
2357 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
2359 instance->drv_buf_alloc = i;
2363 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2364 * @instance: Controller's soft instance
2367 megasas_free_host_crash_buffer(struct megasas_instance *instance)
2371 for (i = 0; i < instance->drv_buf_alloc; i++) {
2372 if (instance->crash_buf[i])
2373 free_pages((ulong)instance->crash_buf[i],
2374 instance->crash_buf_pages);
2376 instance->drv_buf_index = 0;
2377 instance->drv_buf_alloc = 0;
2378 instance->fw_crash_state = UNAVAILABLE;
2379 instance->fw_crash_buffer_size = 0;
2383 * megasas_adp_reset_fusion - For controller reset
2384 * @regs: MFI register set
2387 megasas_adp_reset_fusion(struct megasas_instance *instance,
2388 struct megasas_register_set __iomem *regs)
2394 * megasas_check_reset_fusion - For controller reset check
2395 * @regs: MFI register set
2398 megasas_check_reset_fusion(struct megasas_instance *instance,
2399 struct megasas_register_set __iomem *regs)
2404 /* This function waits for outstanding commands on fusion to complete */
2405 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2406 int iotimeout, int *convert)
2408 int i, outstanding, retval = 0, hb_seconds_missed = 0;
2411 for (i = 0; i < resetwaittime; i++) {
2412 /* Check if firmware is in fault state */
2413 fw_state = instance->instancet->read_fw_status_reg(
2414 instance->reg_set) & MFI_STATE_MASK;
2415 if (fw_state == MFI_STATE_FAULT) {
2416 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2417 " will reset adapter scsi%d.\n",
2418 instance->host->host_no);
2422 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2423 if (instance->requestorId && !iotimeout) {
2428 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2429 if (instance->requestorId && iotimeout) {
2430 if (instance->hb_host_mem->HB.fwCounter !=
2431 instance->hb_host_mem->HB.driverCounter) {
2432 instance->hb_host_mem->HB.driverCounter =
2433 instance->hb_host_mem->HB.fwCounter;
2434 hb_seconds_missed = 0;
2436 hb_seconds_missed++;
2437 if (hb_seconds_missed ==
2438 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2439 printk(KERN_WARNING "megasas: SR-IOV:"
2440 " Heartbeat never completed "
2441 " while polling during I/O "
2442 " timeout handling for "
2444 instance->host->host_no);
2452 outstanding = atomic_read(&instance->fw_outstanding);
2456 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2457 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2458 "commands to complete for scsi%d\n", i,
2459 outstanding, instance->host->host_no);
2460 megasas_complete_cmd_dpc_fusion(
2461 (unsigned long)instance);
2466 if (atomic_read(&instance->fw_outstanding)) {
2467 printk("megaraid_sas: pending commands remain after waiting, "
2468 "will reset adapter scsi%d.\n",
2469 instance->host->host_no);
2476 void megasas_reset_reply_desc(struct megasas_instance *instance)
2479 struct fusion_context *fusion;
2480 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2482 fusion = instance->ctrl_context;
2483 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2484 for (i = 0 ; i < count ; i++)
2485 fusion->last_reply_idx[i] = 0;
2486 reply_desc = fusion->reply_frames_desc;
2487 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
2488 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
2492 * megasas_refire_mgmt_cmd : Re-fire management commands
2493 * @instance: Controller's soft instance
2495 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2498 struct megasas_cmd_fusion *cmd_fusion;
2499 struct fusion_context *fusion;
2500 struct megasas_cmd *cmd_mfi;
2501 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2504 fusion = instance->ctrl_context;
2506 /* Re-fire management commands.
2507 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2509 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2510 cmd_fusion = fusion->cmd_list[j];
2511 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2512 smid = le16_to_cpu(cmd_mfi->context.smid);
2516 req_desc = megasas_get_request_descriptor
2517 (instance, smid - 1);
2518 if (req_desc && (cmd_mfi->frame->dcmd.opcode !=
2519 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)))
2520 megasas_fire_cmd_fusion(instance, req_desc);
2522 megasas_return_cmd(instance, cmd_mfi);
2526 /* Check for a second path that is currently UP */
2527 int megasas_check_mpio_paths(struct megasas_instance *instance,
2528 struct scsi_cmnd *scmd)
2530 int i, j, retval = (DID_RESET << 16);
2532 if (instance->mpio && instance->requestorId) {
2533 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2534 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2535 if (megasas_mgmt_info.instance[i] &&
2536 (megasas_mgmt_info.instance[i] != instance) &&
2537 megasas_mgmt_info.instance[i]->mpio &&
2538 megasas_mgmt_info.instance[i]->requestorId
2540 (megasas_mgmt_info.instance[i]->ld_ids[j]
2541 == scmd->device->id)) {
2542 retval = (DID_NO_CONNECT << 16);
2550 /* Core fusion reset function */
2551 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2553 int retval = SUCCESS, i, retry = 0, convert = 0;
2554 struct megasas_instance *instance;
2555 struct megasas_cmd_fusion *cmd_fusion;
2556 struct fusion_context *fusion;
2557 u32 host_diag, abs_state, status_reg, reset_adapter;
2558 u32 io_timeout_in_crash_mode = 0;
2559 struct scsi_cmnd *scmd_local = NULL;
2561 instance = (struct megasas_instance *)shost->hostdata;
2562 fusion = instance->ctrl_context;
2564 mutex_lock(&instance->reset_mutex);
2566 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2567 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2568 "returning FAILED for scsi%d.\n",
2569 instance->host->host_no);
2570 mutex_unlock(&instance->reset_mutex);
2573 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2574 abs_state = status_reg & MFI_STATE_MASK;
2576 /* IO timeout detected, forcibly put FW in FAULT state */
2577 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2578 instance->crash_dump_app_support && iotimeout) {
2579 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2580 "forcibly FAULT Firmware\n");
2581 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2582 status_reg = readl(&instance->reg_set->doorbell);
2583 writel(status_reg | MFI_STATE_FORCE_OCR,
2584 &instance->reg_set->doorbell);
2585 readl(&instance->reg_set->doorbell);
2586 mutex_unlock(&instance->reset_mutex);
2589 io_timeout_in_crash_mode++;
2590 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2591 "seconds for crash dump collection and OCR "
2592 "to be done\n", (io_timeout_in_crash_mode * 3));
2593 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2594 (io_timeout_in_crash_mode < 80));
2596 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2597 dev_info(&instance->pdev->dev, "OCR done for IO "
2601 dev_info(&instance->pdev->dev, "Controller is not "
2602 "operational after 240 seconds wait for IO "
2603 "timeout case in FW crash dump mode\n do "
2604 "OCR/kill adapter\n");
2605 retval = megasas_reset_fusion(shost, 0);
2610 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2611 del_timer_sync(&instance->sriov_heartbeat_timer);
2612 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2613 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
2614 instance->instancet->disable_intr(instance);
2617 /* First try waiting for commands to complete */
2618 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2620 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2621 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2622 "adapter scsi%d.\n", instance->host->host_no);
2626 /* Now return commands back to the OS */
2627 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
2628 cmd_fusion = fusion->cmd_list[i];
2629 scmd_local = cmd_fusion->scmd;
2630 if (cmd_fusion->scmd) {
2631 scmd_local->result =
2632 megasas_check_mpio_paths(instance,
2634 megasas_return_cmd_fusion(instance, cmd_fusion);
2635 scsi_dma_unmap(scmd_local);
2636 scmd_local->scsi_done(scmd_local);
2637 atomic_dec(&instance->fw_outstanding);
2641 status_reg = instance->instancet->read_fw_status_reg(
2643 abs_state = status_reg & MFI_STATE_MASK;
2644 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2645 if (instance->disableOnlineCtrlReset ||
2646 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
2647 /* Reset not supported, kill adapter */
2648 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2649 ", killing adapter scsi%d.\n",
2650 instance->host->host_no);
2651 megaraid_sas_kill_hba(instance);
2652 instance->skip_heartbeat_timer_del = 1;
2657 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2658 if (instance->requestorId && !iotimeout) {
2659 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2660 /* Look for a late HB update after VF settle time */
2661 if (abs_state == MFI_STATE_OPERATIONAL &&
2662 (instance->hb_host_mem->HB.fwCounter !=
2663 instance->hb_host_mem->HB.driverCounter)) {
2664 instance->hb_host_mem->HB.driverCounter =
2665 instance->hb_host_mem->HB.fwCounter;
2666 printk(KERN_WARNING "megasas: SR-IOV:"
2667 "Late FW heartbeat update for "
2669 instance->host->host_no);
2671 /* In VF mode, first poll for FW ready */
2673 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2676 instance->instancet->
2679 abs_state = status_reg &
2681 if (abs_state == MFI_STATE_READY) {
2682 printk(KERN_WARNING "megasas"
2683 ": SR-IOV: FW was found"
2684 "to be in ready state "
2686 instance->host->host_no);
2691 if (abs_state != MFI_STATE_READY) {
2692 printk(KERN_WARNING "megasas: SR-IOV: "
2693 "FW not in ready state after %d"
2694 " seconds for scsi%d, status_reg = "
2696 MEGASAS_RESET_WAIT_TIME,
2697 instance->host->host_no,
2699 megaraid_sas_kill_hba(instance);
2700 instance->skip_heartbeat_timer_del = 1;
2701 instance->adprecovery =
2702 MEGASAS_HW_CRITICAL_ERROR;
2709 /* Now try to reset the chip */
2710 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2711 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2712 &instance->reg_set->fusion_seq_offset);
2713 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2714 &instance->reg_set->fusion_seq_offset);
2715 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2716 &instance->reg_set->fusion_seq_offset);
2717 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2718 &instance->reg_set->fusion_seq_offset);
2719 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2720 &instance->reg_set->fusion_seq_offset);
2721 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2722 &instance->reg_set->fusion_seq_offset);
2723 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2724 &instance->reg_set->fusion_seq_offset);
2726 /* Check that the diag write enable (DRWE) bit is on */
2727 host_diag = readl(&instance->reg_set->fusion_host_diag);
2729 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2732 readl(&instance->reg_set->fusion_host_diag);
2733 if (retry++ == 100) {
2734 printk(KERN_WARNING "megaraid_sas: "
2735 "Host diag unlock failed! "
2737 instance->host->host_no);
2741 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2744 /* Send chip reset command */
2745 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2746 &instance->reg_set->fusion_host_diag);
2749 /* Make sure reset adapter bit is cleared */
2750 host_diag = readl(&instance->reg_set->fusion_host_diag);
2752 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2755 readl(&instance->reg_set->fusion_host_diag);
2756 if (retry++ == 1000) {
2757 printk(KERN_WARNING "megaraid_sas: "
2758 "Diag reset adapter never "
2759 "cleared for scsi%d!\n",
2760 instance->host->host_no);
2764 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2768 instance->instancet->read_fw_status_reg(
2769 instance->reg_set) & MFI_STATE_MASK;
2772 while ((abs_state <= MFI_STATE_FW_INIT) &&
2776 instance->instancet->read_fw_status_reg(
2777 instance->reg_set) & MFI_STATE_MASK;
2779 if (abs_state <= MFI_STATE_FW_INIT) {
2780 printk(KERN_WARNING "megaraid_sas: firmware "
2781 "state < MFI_STATE_FW_INIT, state = "
2782 "0x%x for scsi%d\n", abs_state,
2783 instance->host->host_no);
2787 /* Wait for FW to become ready */
2788 if (megasas_transition_to_ready(instance, 1)) {
2789 printk(KERN_WARNING "megaraid_sas: Failed to "
2790 "transition controller to ready "
2792 instance->host->host_no);
2796 megasas_reset_reply_desc(instance);
2797 if (megasas_ioc_init_fusion(instance)) {
2798 printk(KERN_WARNING "megaraid_sas: "
2799 "megasas_ioc_init_fusion() failed!"
2801 instance->host->host_no);
2805 megasas_refire_mgmt_cmd(instance);
2807 if (megasas_get_ctrl_info(instance)) {
2808 dev_info(&instance->pdev->dev,
2809 "Failed from %s %d\n",
2810 __func__, __LINE__);
2811 megaraid_sas_kill_hba(instance);
2814 /* Reset load balance info */
2815 memset(fusion->load_balance_info, 0,
2816 sizeof(struct LD_LOAD_BALANCE_INFO)
2817 *MAX_LOGICAL_DRIVES_EXT);
2819 if (!megasas_get_map_info(instance))
2820 megasas_sync_map_info(instance);
2822 clear_bit(MEGASAS_FUSION_IN_RESET,
2823 &instance->reset_flags);
2824 instance->instancet->enable_intr(instance);
2825 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2827 /* Restart SR-IOV heartbeat */
2828 if (instance->requestorId) {
2829 if (!megasas_sriov_start_heartbeat(instance, 0))
2830 megasas_start_timer(instance,
2831 &instance->sriov_heartbeat_timer,
2832 megasas_sriov_heartbeat_handler,
2833 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2835 instance->skip_heartbeat_timer_del = 1;
2838 /* Adapter reset completed successfully */
2839 printk(KERN_WARNING "megaraid_sas: Reset "
2840 "successful for scsi%d.\n",
2841 instance->host->host_no);
2843 if (instance->crash_dump_drv_support &&
2844 instance->crash_dump_app_support)
2845 megasas_set_crash_dump_params(instance,
2846 MR_CRASH_BUF_TURN_ON);
2848 megasas_set_crash_dump_params(instance,
2849 MR_CRASH_BUF_TURN_OFF);
2854 /* Reset failed, kill the adapter */
2855 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2856 "adapter scsi%d.\n", instance->host->host_no);
2857 megaraid_sas_kill_hba(instance);
2858 instance->skip_heartbeat_timer_del = 1;
2861 /* For VF: Restart HB timer if we didn't OCR */
2862 if (instance->requestorId) {
2863 megasas_start_timer(instance,
2864 &instance->sriov_heartbeat_timer,
2865 megasas_sriov_heartbeat_handler,
2866 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2868 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2869 instance->instancet->enable_intr(instance);
2870 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2873 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2874 mutex_unlock(&instance->reset_mutex);
2878 /* Fusion Crash dump collection work queue */
2879 void megasas_fusion_crash_dump_wq(struct work_struct *work)
2881 struct megasas_instance *instance =
2882 container_of(work, struct megasas_instance, crash_init);
2884 u8 partial_copy = 0;
2887 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2890 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2891 * to host crash buffers
2893 if (instance->drv_buf_index == 0) {
2894 /* Buffer is already allocated for old Crash dump.
2895 * Do OCR and do not wait for crash dump collection
2897 if (instance->drv_buf_alloc) {
2898 dev_info(&instance->pdev->dev, "earlier crash dump is "
2899 "not yet copied by application, ignoring this "
2900 "crash dump and initiating OCR\n");
2901 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2903 &instance->reg_set->outbound_scratch_pad);
2904 readl(&instance->reg_set->outbound_scratch_pad);
2907 megasas_alloc_host_crash_buffer(instance);
2908 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2909 "allocated: %d\n", instance->drv_buf_alloc);
2913 * Driver has allocated max buffers, which can be allocated
2914 * and FW has more crash dump data, then driver will
2917 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2918 dev_info(&instance->pdev->dev, "Driver is done copying "
2919 "the buffer: %d\n", instance->drv_buf_alloc);
2920 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2923 memcpy(instance->crash_buf[instance->drv_buf_index],
2924 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2925 instance->drv_buf_index++;
2926 status_reg &= ~MFI_STATE_DMADONE;
2929 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2930 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2931 "of copied buffers: %d\n", instance->drv_buf_index);
2932 instance->fw_crash_buffer_size = instance->drv_buf_index;
2933 instance->fw_crash_state = AVAILABLE;
2934 instance->drv_buf_index = 0;
2935 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2936 readl(&instance->reg_set->outbound_scratch_pad);
2938 megasas_reset_fusion(instance->host, 0);
2940 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2941 readl(&instance->reg_set->outbound_scratch_pad);
2946 /* Fusion OCR work queue */
2947 void megasas_fusion_ocr_wq(struct work_struct *work)
2949 struct megasas_instance *instance =
2950 container_of(work, struct megasas_instance, work_init);
2952 megasas_reset_fusion(instance->host, 0);
2955 struct megasas_instance_template megasas_instance_template_fusion = {
2956 .enable_intr = megasas_enable_intr_fusion,
2957 .disable_intr = megasas_disable_intr_fusion,
2958 .clear_intr = megasas_clear_intr_fusion,
2959 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2960 .adp_reset = megasas_adp_reset_fusion,
2961 .check_reset = megasas_check_reset_fusion,
2962 .service_isr = megasas_isr_fusion,
2963 .tasklet = megasas_complete_cmd_dpc_fusion,
2964 .init_adapter = megasas_init_adapter_fusion,
2965 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2966 .issue_dcmd = megasas_issue_dcmd_fusion,