2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * FILE: megaraid_sas_fusion.c
23 * Authors: Broadcom Inc.
26 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
29 * Send feedback to: megaraidlinux.pdl@broadcom.com
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>
48 #include <linux/vmalloc.h>
49 #include <linux/workqueue.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_instance *instance);
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 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
89 void megasas_start_timer(struct megasas_instance *instance);
90 extern struct megasas_mgmt_info megasas_mgmt_info;
91 extern unsigned int resetwaittime;
92 extern unsigned int dual_qdepth_disable;
93 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
94 static void megasas_free_reply_fusion(struct megasas_instance *instance);
96 void megasas_configure_queue_sizes(struct megasas_instance *instance);
97 static void megasas_fusion_crash_dump(struct megasas_instance *instance);
98 extern u32 megasas_readl(struct megasas_instance *instance,
99 const volatile void __iomem *addr);
102 * megasas_check_same_4gb_region - check if allocation
103 * crosses same 4GB boundary or not
104 * @instance - adapter's soft instance
105 * start_addr - start address of DMA allocation
106 * size - size of allocation in bytes
107 * return - true : allocation does not cross same
109 * false: allocation crosses same
112 static inline bool megasas_check_same_4gb_region
113 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
117 end_addr = start_addr + size;
119 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
120 dev_err(&instance->pdev->dev,
121 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
122 (unsigned long long)start_addr,
123 (unsigned long long)end_addr);
131 * megasas_enable_intr_fusion - Enables interrupts
132 * @regs: MFI register set
135 megasas_enable_intr_fusion(struct megasas_instance *instance)
137 struct megasas_register_set __iomem *regs;
138 regs = instance->reg_set;
140 instance->mask_interrupts = 0;
141 /* For Thunderbolt/Invader also clear intr on enable */
142 writel(~0, ®s->outbound_intr_status);
143 readl(®s->outbound_intr_status);
145 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
147 /* Dummy readl to force pci flush */
148 readl(®s->outbound_intr_mask);
152 * megasas_disable_intr_fusion - Disables interrupt
153 * @regs: MFI register set
156 megasas_disable_intr_fusion(struct megasas_instance *instance)
158 u32 mask = 0xFFFFFFFF;
160 struct megasas_register_set __iomem *regs;
161 regs = instance->reg_set;
162 instance->mask_interrupts = 1;
164 writel(mask, ®s->outbound_intr_mask);
165 /* Dummy readl to force pci flush */
166 status = readl(®s->outbound_intr_mask);
170 megasas_clear_intr_fusion(struct megasas_instance *instance)
173 struct megasas_register_set __iomem *regs;
174 regs = instance->reg_set;
176 * Check if it is our interrupt
178 status = megasas_readl(instance,
179 ®s->outbound_intr_status);
182 writel(status, ®s->outbound_intr_status);
183 readl(®s->outbound_intr_status);
186 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
193 * megasas_get_cmd_fusion - Get a command from the free pool
194 * @instance: Adapter soft state
196 * Returns a blk_tag indexed mpt frame
198 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
199 *instance, u32 blk_tag)
201 struct fusion_context *fusion;
203 fusion = instance->ctrl_context;
204 return fusion->cmd_list[blk_tag];
208 * megasas_return_cmd_fusion - Return a cmd to free command pool
209 * @instance: Adapter soft state
210 * @cmd: Command packet to be returned to free command pool
212 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
213 struct megasas_cmd_fusion *cmd)
216 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
217 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
218 cmd->cmd_completed = false;
222 * megasas_fire_cmd_fusion - Sends command to the FW
223 * @instance: Adapter soft state
224 * @req_desc: 64bit Request descriptor
230 megasas_fire_cmd_fusion(struct megasas_instance *instance,
231 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
233 #if defined(writeq) && defined(CONFIG_64BIT)
234 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
235 le32_to_cpu(req_desc->u.low));
237 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
240 spin_lock_irqsave(&instance->hba_lock, flags);
241 writel(le32_to_cpu(req_desc->u.low),
242 &instance->reg_set->inbound_low_queue_port);
243 writel(le32_to_cpu(req_desc->u.high),
244 &instance->reg_set->inbound_high_queue_port);
245 spin_unlock_irqrestore(&instance->hba_lock, flags);
250 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
251 * @instance: Adapter soft state
252 * fw_boot_context: Whether this function called during probe or after OCR
254 * This function is only for fusion controllers.
255 * Update host can queue, if firmware downgrade max supported firmware commands.
256 * Firmware upgrade case will be skiped because underlying firmware has
257 * more resource than exposed to the OS.
261 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
263 u16 cur_max_fw_cmds = 0;
264 u16 ldio_threshold = 0;
265 struct megasas_register_set __iomem *reg_set;
267 reg_set = instance->reg_set;
269 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
270 if (instance->adapter_type < VENTURA_SERIES)
272 megasas_readl(instance,
273 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
275 if (dual_qdepth_disable || !cur_max_fw_cmds)
276 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
279 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
281 dev_info(&instance->pdev->dev,
282 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
283 cur_max_fw_cmds, ldio_threshold);
285 if (fw_boot_context == OCR_CONTEXT) {
286 cur_max_fw_cmds = cur_max_fw_cmds - 1;
287 if (cur_max_fw_cmds < instance->max_fw_cmds) {
288 instance->cur_can_queue =
289 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
290 MEGASAS_FUSION_IOCTL_CMDS);
291 instance->host->can_queue = instance->cur_can_queue;
292 instance->ldio_threshold = ldio_threshold;
295 instance->max_fw_cmds = cur_max_fw_cmds;
296 instance->ldio_threshold = ldio_threshold;
299 instance->max_fw_cmds = min(instance->max_fw_cmds,
300 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
302 * Reduce the max supported cmds by 1. This is to ensure that the
303 * reply_q_sz (1 more than the max cmd that driver may send)
304 * does not exceed max cmds that the FW can support
306 instance->max_fw_cmds = instance->max_fw_cmds-1;
310 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
311 * @instance: Adapter soft state
314 megasas_free_cmds_fusion(struct megasas_instance *instance)
317 struct fusion_context *fusion = instance->ctrl_context;
318 struct megasas_cmd_fusion *cmd;
321 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
322 fusion->sense_phys_addr);
325 if (fusion->cmd_list) {
326 for (i = 0; i < instance->max_mpt_cmds; i++) {
327 cmd = fusion->cmd_list[i];
330 dma_pool_free(fusion->sg_dma_pool,
332 cmd->sg_frame_phys_addr);
336 kfree(fusion->cmd_list);
339 if (fusion->sg_dma_pool) {
340 dma_pool_destroy(fusion->sg_dma_pool);
341 fusion->sg_dma_pool = NULL;
343 if (fusion->sense_dma_pool) {
344 dma_pool_destroy(fusion->sense_dma_pool);
345 fusion->sense_dma_pool = NULL;
349 /* Reply Frame, Desc*/
350 if (instance->is_rdpq)
351 megasas_free_rdpq_fusion(instance);
353 megasas_free_reply_fusion(instance);
355 /* Request Frame, Desc*/
356 if (fusion->req_frames_desc)
357 dma_free_coherent(&instance->pdev->dev,
358 fusion->request_alloc_sz, fusion->req_frames_desc,
359 fusion->req_frames_desc_phys);
360 if (fusion->io_request_frames)
361 dma_pool_free(fusion->io_request_frames_pool,
362 fusion->io_request_frames,
363 fusion->io_request_frames_phys);
364 if (fusion->io_request_frames_pool) {
365 dma_pool_destroy(fusion->io_request_frames_pool);
366 fusion->io_request_frames_pool = NULL;
371 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
372 * @instance: Adapter soft state
375 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
379 struct fusion_context *fusion;
380 struct megasas_cmd_fusion *cmd;
384 fusion = instance->ctrl_context;
385 max_cmd = instance->max_fw_cmds;
386 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
388 fusion->sg_dma_pool =
389 dma_pool_create("mr_sg", &instance->pdev->dev,
390 instance->max_chain_frame_sz,
391 MR_DEFAULT_NVME_PAGE_SIZE, 0);
392 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
393 fusion->sense_dma_pool =
394 dma_pool_create("mr_sense", &instance->pdev->dev,
397 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
398 dev_err(&instance->pdev->dev,
399 "Failed from %s %d\n", __func__, __LINE__);
403 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
404 GFP_KERNEL, &fusion->sense_phys_addr);
405 if (!fusion->sense) {
406 dev_err(&instance->pdev->dev,
407 "failed from %s %d\n", __func__, __LINE__);
411 /* sense buffer, request frame and reply desc pool requires to be in
412 * same 4 gb region. Below function will check this.
413 * In case of failure, new pci pool will be created with updated
415 * Older allocation and pool will be destroyed.
416 * Alignment will be used such a way that next allocation if success,
417 * will always meet same 4gb region requirement.
418 * Actual requirement is not alignment, but we need start and end of
419 * DMA address must have same upper 32 bit address.
422 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
424 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
425 fusion->sense_phys_addr);
426 fusion->sense = NULL;
427 dma_pool_destroy(fusion->sense_dma_pool);
429 fusion->sense_dma_pool =
430 dma_pool_create("mr_sense_align", &instance->pdev->dev,
431 sense_sz, roundup_pow_of_two(sense_sz),
433 if (!fusion->sense_dma_pool) {
434 dev_err(&instance->pdev->dev,
435 "Failed from %s %d\n", __func__, __LINE__);
438 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
440 &fusion->sense_phys_addr);
441 if (!fusion->sense) {
442 dev_err(&instance->pdev->dev,
443 "failed from %s %d\n", __func__, __LINE__);
449 * Allocate and attach a frame to each of the commands in cmd_list
451 for (i = 0; i < max_cmd; i++) {
452 cmd = fusion->cmd_list[i];
453 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
454 GFP_KERNEL, &cmd->sg_frame_phys_addr);
456 offset = SCSI_SENSE_BUFFERSIZE * i;
457 cmd->sense = (u8 *)fusion->sense + offset;
458 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
460 if (!cmd->sg_frame) {
461 dev_err(&instance->pdev->dev,
462 "Failed from %s %d\n", __func__, __LINE__);
467 /* create sense buffer for the raid 1/10 fp */
468 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
469 cmd = fusion->cmd_list[i];
470 offset = SCSI_SENSE_BUFFERSIZE * i;
471 cmd->sense = (u8 *)fusion->sense + offset;
472 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
480 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
482 u32 max_mpt_cmd, i, j;
483 struct fusion_context *fusion;
485 fusion = instance->ctrl_context;
487 max_mpt_cmd = instance->max_mpt_cmds;
490 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
491 * Allocate the dynamic array first and then allocate individual
495 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
497 if (!fusion->cmd_list) {
498 dev_err(&instance->pdev->dev,
499 "Failed from %s %d\n", __func__, __LINE__);
503 for (i = 0; i < max_mpt_cmd; i++) {
504 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
506 if (!fusion->cmd_list[i]) {
507 for (j = 0; j < i; j++)
508 kfree(fusion->cmd_list[j]);
509 kfree(fusion->cmd_list);
510 dev_err(&instance->pdev->dev,
511 "Failed from %s %d\n", __func__, __LINE__);
519 megasas_alloc_request_fusion(struct megasas_instance *instance)
521 struct fusion_context *fusion;
523 fusion = instance->ctrl_context;
526 fusion->io_request_frames_pool =
527 dma_pool_create("mr_ioreq", &instance->pdev->dev,
528 fusion->io_frames_alloc_sz, 16, 0);
530 if (!fusion->io_request_frames_pool) {
531 dev_err(&instance->pdev->dev,
532 "Failed from %s %d\n", __func__, __LINE__);
536 fusion->io_request_frames =
537 dma_pool_alloc(fusion->io_request_frames_pool,
538 GFP_KERNEL, &fusion->io_request_frames_phys);
539 if (!fusion->io_request_frames) {
540 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
541 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
542 dma_pool_destroy(fusion->io_request_frames_pool);
543 megasas_configure_queue_sizes(instance);
546 dev_err(&instance->pdev->dev,
547 "Failed from %s %d\n", __func__, __LINE__);
552 if (!megasas_check_same_4gb_region(instance,
553 fusion->io_request_frames_phys,
554 fusion->io_frames_alloc_sz)) {
555 dma_pool_free(fusion->io_request_frames_pool,
556 fusion->io_request_frames,
557 fusion->io_request_frames_phys);
558 fusion->io_request_frames = NULL;
559 dma_pool_destroy(fusion->io_request_frames_pool);
561 fusion->io_request_frames_pool =
562 dma_pool_create("mr_ioreq_align",
563 &instance->pdev->dev,
564 fusion->io_frames_alloc_sz,
565 roundup_pow_of_two(fusion->io_frames_alloc_sz),
568 if (!fusion->io_request_frames_pool) {
569 dev_err(&instance->pdev->dev,
570 "Failed from %s %d\n", __func__, __LINE__);
574 fusion->io_request_frames =
575 dma_pool_alloc(fusion->io_request_frames_pool,
577 &fusion->io_request_frames_phys);
579 if (!fusion->io_request_frames) {
580 dev_err(&instance->pdev->dev,
581 "Failed from %s %d\n", __func__, __LINE__);
586 fusion->req_frames_desc =
587 dma_alloc_coherent(&instance->pdev->dev,
588 fusion->request_alloc_sz,
589 &fusion->req_frames_desc_phys, GFP_KERNEL);
590 if (!fusion->req_frames_desc) {
591 dev_err(&instance->pdev->dev,
592 "Failed from %s %d\n", __func__, __LINE__);
600 megasas_alloc_reply_fusion(struct megasas_instance *instance)
603 struct fusion_context *fusion;
604 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
605 fusion = instance->ctrl_context;
607 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
608 fusion->reply_frames_desc_pool =
609 dma_pool_create("mr_reply", &instance->pdev->dev,
610 fusion->reply_alloc_sz * count, 16, 0);
612 if (!fusion->reply_frames_desc_pool) {
613 dev_err(&instance->pdev->dev,
614 "Failed from %s %d\n", __func__, __LINE__);
618 fusion->reply_frames_desc[0] =
619 dma_pool_alloc(fusion->reply_frames_desc_pool,
620 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
621 if (!fusion->reply_frames_desc[0]) {
622 dev_err(&instance->pdev->dev,
623 "Failed from %s %d\n", __func__, __LINE__);
627 if (!megasas_check_same_4gb_region(instance,
628 fusion->reply_frames_desc_phys[0],
629 (fusion->reply_alloc_sz * count))) {
630 dma_pool_free(fusion->reply_frames_desc_pool,
631 fusion->reply_frames_desc[0],
632 fusion->reply_frames_desc_phys[0]);
633 fusion->reply_frames_desc[0] = NULL;
634 dma_pool_destroy(fusion->reply_frames_desc_pool);
636 fusion->reply_frames_desc_pool =
637 dma_pool_create("mr_reply_align",
638 &instance->pdev->dev,
639 fusion->reply_alloc_sz * count,
640 roundup_pow_of_two(fusion->reply_alloc_sz * count),
643 if (!fusion->reply_frames_desc_pool) {
644 dev_err(&instance->pdev->dev,
645 "Failed from %s %d\n", __func__, __LINE__);
649 fusion->reply_frames_desc[0] =
650 dma_pool_alloc(fusion->reply_frames_desc_pool,
652 &fusion->reply_frames_desc_phys[0]);
654 if (!fusion->reply_frames_desc[0]) {
655 dev_err(&instance->pdev->dev,
656 "Failed from %s %d\n", __func__, __LINE__);
661 reply_desc = fusion->reply_frames_desc[0];
662 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
663 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
665 /* This is not a rdpq mode, but driver still populate
666 * reply_frame_desc array to use same msix index in ISR path.
668 for (i = 0; i < (count - 1); i++)
669 fusion->reply_frames_desc[i + 1] =
670 fusion->reply_frames_desc[i] +
671 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
677 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
679 int i, j, k, msix_count;
680 struct fusion_context *fusion;
681 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
682 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
683 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
684 u8 dma_alloc_count, abs_index;
685 u32 chunk_size, array_size, offset;
687 fusion = instance->ctrl_context;
688 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
689 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
690 MAX_MSIX_QUEUES_FUSION;
692 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
693 array_size, &fusion->rdpq_phys,
695 if (!fusion->rdpq_virt) {
696 dev_err(&instance->pdev->dev,
697 "Failed from %s %d\n", __func__, __LINE__);
701 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
703 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
704 &instance->pdev->dev,
706 fusion->reply_frames_desc_pool_align =
707 dma_pool_create("mr_rdpq_align",
708 &instance->pdev->dev,
710 roundup_pow_of_two(chunk_size),
713 if (!fusion->reply_frames_desc_pool ||
714 !fusion->reply_frames_desc_pool_align) {
715 dev_err(&instance->pdev->dev,
716 "Failed from %s %d\n", __func__, __LINE__);
721 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
722 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
723 * within 4GB boundary and also reply queues in a set must have same
724 * upper 32-bits in their memory address. so here driver is allocating the
725 * DMA'able memory for reply queues according. Driver uses limitation of
726 * VENTURA_SERIES to manage INVADER_SERIES as well.
728 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
730 for (i = 0; i < dma_alloc_count; i++) {
732 dma_pool_alloc(fusion->reply_frames_desc_pool,
733 GFP_KERNEL, &rdpq_chunk_phys[i]);
734 if (!rdpq_chunk_virt[i]) {
735 dev_err(&instance->pdev->dev,
736 "Failed from %s %d\n", __func__, __LINE__);
739 /* reply desc pool requires to be in same 4 gb region.
740 * Below function will check this.
741 * In case of failure, new pci pool will be created with updated
743 * For RDPQ buffers, driver always allocate two separate pci pool.
744 * Alignment will be used such a way that next allocation if
745 * success, will always meet same 4gb region requirement.
746 * rdpq_tracker keep track of each buffer's physical,
747 * virtual address and pci pool descriptor. It will help driver
748 * while freeing the resources.
751 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
753 dma_pool_free(fusion->reply_frames_desc_pool,
758 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
759 GFP_KERNEL, &rdpq_chunk_phys[i]);
760 if (!rdpq_chunk_virt[i]) {
761 dev_err(&instance->pdev->dev,
762 "Failed from %s %d\n",
766 fusion->rdpq_tracker[i].dma_pool_ptr =
767 fusion->reply_frames_desc_pool_align;
769 fusion->rdpq_tracker[i].dma_pool_ptr =
770 fusion->reply_frames_desc_pool;
773 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
774 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
777 for (k = 0; k < dma_alloc_count; k++) {
778 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
779 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
781 if (abs_index == msix_count)
783 offset = fusion->reply_alloc_sz * i;
784 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
785 cpu_to_le64(rdpq_chunk_phys[k] + offset);
786 fusion->reply_frames_desc_phys[abs_index] =
787 rdpq_chunk_phys[k] + offset;
788 fusion->reply_frames_desc[abs_index] =
789 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
791 reply_desc = fusion->reply_frames_desc[abs_index];
792 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
793 reply_desc->Words = ULLONG_MAX;
801 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
804 struct fusion_context *fusion;
806 fusion = instance->ctrl_context;
808 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
809 if (fusion->rdpq_tracker[i].pool_entry_virt)
810 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
811 fusion->rdpq_tracker[i].pool_entry_virt,
812 fusion->rdpq_tracker[i].pool_entry_phys);
816 dma_pool_destroy(fusion->reply_frames_desc_pool);
817 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
819 if (fusion->rdpq_virt)
820 dma_free_coherent(&instance->pdev->dev,
821 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
822 fusion->rdpq_virt, fusion->rdpq_phys);
826 megasas_free_reply_fusion(struct megasas_instance *instance) {
828 struct fusion_context *fusion;
830 fusion = instance->ctrl_context;
832 if (fusion->reply_frames_desc[0])
833 dma_pool_free(fusion->reply_frames_desc_pool,
834 fusion->reply_frames_desc[0],
835 fusion->reply_frames_desc_phys[0]);
837 dma_pool_destroy(fusion->reply_frames_desc_pool);
843 * megasas_alloc_cmds_fusion - Allocates the command packets
844 * @instance: Adapter soft state
847 * Each frame has a 32-bit field called context. This context is used to get
848 * back the megasas_cmd_fusion from the frame when a frame gets completed
849 * In this driver, the 32 bit values are the indices into an array cmd_list.
850 * This array is used only to look up the megasas_cmd_fusion given the context.
851 * The free commands themselves are maintained in a linked list called cmd_pool.
853 * cmds are formed in the io_request and sg_frame members of the
854 * megasas_cmd_fusion. The context field is used to get a request descriptor
855 * and is used as SMID of the cmd.
856 * SMID value range is from 1 to max_fw_cmds.
859 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
862 struct fusion_context *fusion;
863 struct megasas_cmd_fusion *cmd;
865 dma_addr_t io_req_base_phys;
869 fusion = instance->ctrl_context;
871 if (megasas_alloc_request_fusion(instance))
874 if (instance->is_rdpq) {
875 if (megasas_alloc_rdpq_fusion(instance))
878 if (megasas_alloc_reply_fusion(instance))
881 if (megasas_alloc_cmdlist_fusion(instance))
884 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
885 instance->max_fw_cmds);
887 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
888 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
889 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
892 * Add all the commands to command pool (fusion->cmd_pool)
895 /* SMID 0 is reserved. Set SMID/index from 1 */
896 for (i = 0; i < instance->max_mpt_cmds; i++) {
897 cmd = fusion->cmd_list[i];
898 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
899 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
903 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
904 (i - instance->max_scsi_cmds) :
905 (u32)ULONG_MAX; /* Set to Invalid */
906 cmd->instance = instance;
908 (struct MPI2_RAID_SCSI_IO_REQUEST *)
909 (io_req_base + offset);
910 memset(cmd->io_request, 0,
911 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
912 cmd->io_request_phys_addr = io_req_base_phys + offset;
913 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
916 if (megasas_create_sg_sense_fusion(instance))
922 megasas_free_cmds_fusion(instance);
927 * wait_and_poll - Issues a polling command
928 * @instance: Adapter soft state
929 * @cmd: Command packet to be issued
931 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
934 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
938 struct megasas_header *frame_hdr = &cmd->frame->hdr;
940 u32 msecs = seconds * 1000;
943 * Wait for cmd_status to change
945 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
950 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
952 else if (frame_hdr->cmd_status == MFI_STAT_OK)
959 * megasas_ioc_init_fusion - Initializes the FW
960 * @instance: Adapter soft state
962 * Issues the IOC Init cmd
965 megasas_ioc_init_fusion(struct megasas_instance *instance)
967 struct megasas_init_frame *init_frame;
968 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
969 dma_addr_t ioc_init_handle;
970 struct megasas_cmd *cmd;
971 u8 ret, cur_rdpq_mode;
972 struct fusion_context *fusion;
973 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
975 struct megasas_header *frame_hdr;
976 const char *sys_info;
977 MFI_CAPABILITIES *drv_ops;
980 bool cur_fw_64bit_dma_capable;
982 fusion = instance->ctrl_context;
984 ioc_init_handle = fusion->ioc_init_request_phys;
985 IOCInitMessage = fusion->ioc_init_request;
987 cmd = fusion->ioc_init_cmd;
989 scratch_pad_1 = megasas_readl
990 (instance, &instance->reg_set->outbound_scratch_pad_1);
992 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
994 if (instance->adapter_type == INVADER_SERIES) {
995 cur_fw_64bit_dma_capable =
996 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
998 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
999 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1000 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1001 megaraid_sas_kill_hba(instance);
1007 if (instance->is_rdpq && !cur_rdpq_mode) {
1008 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1009 " from RDPQ mode to non RDPQ mode\n");
1014 instance->fw_sync_cache_support = (scratch_pad_1 &
1015 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1016 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1017 instance->fw_sync_cache_support ? "Yes" : "No");
1019 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1021 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1022 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1023 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1024 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1025 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1027 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1028 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1029 cpu_to_le64(fusion->rdpq_phys) :
1030 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1031 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1032 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1033 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1034 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1035 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1036 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1038 time = ktime_get_real();
1039 /* Convert to milliseconds as per FW requirement */
1040 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1042 init_frame = (struct megasas_init_frame *)cmd->frame;
1043 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1045 frame_hdr = &cmd->frame->hdr;
1046 frame_hdr->cmd_status = 0xFF;
1047 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1049 init_frame->cmd = MFI_CMD_INIT;
1050 init_frame->cmd_status = 0xFF;
1052 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1054 /* driver support Extended MSIX */
1055 if (instance->adapter_type >= INVADER_SERIES)
1056 drv_ops->mfi_capabilities.support_additional_msix = 1;
1057 /* driver supports HA / Remote LUN over Fast Path interface */
1058 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1060 drv_ops->mfi_capabilities.support_max_255lds = 1;
1061 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1062 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1064 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1065 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1067 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1068 if (!dual_qdepth_disable)
1069 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1071 drv_ops->mfi_capabilities.support_qd_throttling = 1;
1072 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1073 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1074 drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
1076 if (instance->consistent_mask_64bit)
1077 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1079 /* Convert capability to LE32 */
1080 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1082 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1083 if (instance->system_info_buf && sys_info) {
1084 memcpy(instance->system_info_buf->systemId, sys_info,
1085 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1086 instance->system_info_buf->systemIdLength =
1087 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1088 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1089 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1092 init_frame->queue_info_new_phys_addr_hi =
1093 cpu_to_le32(upper_32_bits(ioc_init_handle));
1094 init_frame->queue_info_new_phys_addr_lo =
1095 cpu_to_le32(lower_32_bits(ioc_init_handle));
1096 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1098 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1099 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1100 req_desc.MFAIo.RequestFlags =
1101 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1102 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1105 * disable the intr before firing the init frame
1107 instance->instancet->disable_intr(instance);
1109 for (i = 0; i < (10 * 1000); i += 20) {
1110 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1116 megasas_fire_cmd_fusion(instance, &req_desc);
1118 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1120 frame_hdr = &cmd->frame->hdr;
1121 if (frame_hdr->cmd_status != 0) {
1129 dev_err(&instance->pdev->dev,
1130 "Init cmd return status FAILED for SCSI host %d\n",
1131 instance->host->host_no);
1137 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1138 * @instance: Adapter soft state
1139 * @pend: set to 1, if it is pended jbod map.
1141 * Issue Jbod map to the firmware. If it is pended command,
1142 * issue command and return. If it is first instance of jbod map
1143 * issue and receive command.
1146 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1149 struct megasas_cmd *cmd;
1150 struct megasas_dcmd_frame *dcmd;
1151 struct fusion_context *fusion = instance->ctrl_context;
1152 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1153 dma_addr_t pd_seq_h;
1155 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1156 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1157 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
1158 (sizeof(struct MR_PD_CFG_SEQ) *
1159 (MAX_PHYSICAL_DEVICES - 1));
1161 cmd = megasas_get_cmd(instance);
1163 dev_err(&instance->pdev->dev,
1164 "Could not get mfi cmd. Fail from %s %d\n",
1165 __func__, __LINE__);
1169 dcmd = &cmd->frame->dcmd;
1171 memset(pd_sync, 0, pd_seq_map_sz);
1172 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1175 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1176 dcmd->flags = MFI_FRAME_DIR_WRITE;
1177 instance->jbod_seq_cmd = cmd;
1179 dcmd->flags = MFI_FRAME_DIR_READ;
1182 dcmd->cmd = MFI_CMD_DCMD;
1183 dcmd->cmd_status = 0xFF;
1184 dcmd->sge_count = 1;
1187 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1188 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1190 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1193 instance->instancet->issue_dcmd(instance, cmd);
1197 /* Below code is only for non pended DCMD */
1198 if (!instance->mask_interrupts)
1199 ret = megasas_issue_blocked_cmd(instance, cmd,
1200 MFI_IO_TIMEOUT_SECS);
1202 ret = megasas_issue_polled(instance, cmd);
1204 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1205 dev_warn(&instance->pdev->dev,
1206 "driver supports max %d JBOD, but FW reports %d\n",
1207 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1211 if (ret == DCMD_TIMEOUT)
1212 megaraid_sas_kill_hba(instance);
1214 if (ret == DCMD_SUCCESS)
1215 instance->pd_seq_map_id++;
1217 megasas_return_cmd(instance, cmd);
1222 * megasas_get_ld_map_info - Returns FW's ld_map structure
1223 * @instance: Adapter soft state
1224 * @pend: Pend the command or not
1225 * Issues an internal command (DCMD) to get the FW's controller PD
1226 * list structure. This information is mainly used to find out SYSTEM
1227 * supported by the FW.
1228 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1229 * dcmd.mbox.b[0] - number of LDs being sync'd
1230 * dcmd.mbox.b[1] - 0 - complete command immediately.
1231 * - 1 - pend till config change
1232 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1233 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1234 * uses extended struct MR_FW_RAID_MAP_EXT
1237 megasas_get_ld_map_info(struct megasas_instance *instance)
1240 struct megasas_cmd *cmd;
1241 struct megasas_dcmd_frame *dcmd;
1243 dma_addr_t ci_h = 0;
1245 struct fusion_context *fusion;
1247 cmd = megasas_get_cmd(instance);
1250 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1254 fusion = instance->ctrl_context;
1257 megasas_return_cmd(instance, cmd);
1261 dcmd = &cmd->frame->dcmd;
1263 size_map_info = fusion->current_map_sz;
1265 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1266 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1269 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1270 megasas_return_cmd(instance, cmd);
1274 memset(ci, 0, fusion->max_map_sz);
1275 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1276 dcmd->cmd = MFI_CMD_DCMD;
1277 dcmd->cmd_status = 0xFF;
1278 dcmd->sge_count = 1;
1279 dcmd->flags = MFI_FRAME_DIR_READ;
1282 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1283 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1285 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1287 if (!instance->mask_interrupts)
1288 ret = megasas_issue_blocked_cmd(instance, cmd,
1289 MFI_IO_TIMEOUT_SECS);
1291 ret = megasas_issue_polled(instance, cmd);
1293 if (ret == DCMD_TIMEOUT)
1294 megaraid_sas_kill_hba(instance);
1296 megasas_return_cmd(instance, cmd);
1302 megasas_get_map_info(struct megasas_instance *instance)
1304 struct fusion_context *fusion = instance->ctrl_context;
1306 fusion->fast_path_io = 0;
1307 if (!megasas_get_ld_map_info(instance)) {
1308 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1309 fusion->fast_path_io = 1;
1317 * megasas_sync_map_info - Returns FW's ld_map structure
1318 * @instance: Adapter soft state
1320 * Issues an internal command (DCMD) to get the FW's controller PD
1321 * list structure. This information is mainly used to find out SYSTEM
1322 * supported by the FW.
1325 megasas_sync_map_info(struct megasas_instance *instance)
1328 struct megasas_cmd *cmd;
1329 struct megasas_dcmd_frame *dcmd;
1331 struct fusion_context *fusion;
1332 struct MR_LD_TARGET_SYNC *ci = NULL;
1333 struct MR_DRV_RAID_MAP_ALL *map;
1334 struct MR_LD_RAID *raid;
1335 struct MR_LD_TARGET_SYNC *ld_sync;
1336 dma_addr_t ci_h = 0;
1339 cmd = megasas_get_cmd(instance);
1342 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1346 fusion = instance->ctrl_context;
1349 megasas_return_cmd(instance, cmd);
1353 map = fusion->ld_drv_map[instance->map_id & 1];
1355 num_lds = le16_to_cpu(map->raidMap.ldCount);
1357 dcmd = &cmd->frame->dcmd;
1359 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1361 ci = (struct MR_LD_TARGET_SYNC *)
1362 fusion->ld_map[(instance->map_id - 1) & 1];
1363 memset(ci, 0, fusion->max_map_sz);
1365 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1367 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1369 for (i = 0; i < num_lds; i++, ld_sync++) {
1370 raid = MR_LdRaidGet(i, map);
1371 ld_sync->targetId = MR_GetLDTgtId(i, map);
1372 ld_sync->seqNum = raid->seqNum;
1375 size_map_info = fusion->current_map_sz;
1377 dcmd->cmd = MFI_CMD_DCMD;
1378 dcmd->cmd_status = 0xFF;
1379 dcmd->sge_count = 1;
1380 dcmd->flags = MFI_FRAME_DIR_WRITE;
1383 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1384 dcmd->mbox.b[0] = num_lds;
1385 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1386 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1388 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1390 instance->map_update_cmd = cmd;
1392 instance->instancet->issue_dcmd(instance, cmd);
1398 * meagasas_display_intel_branding - Display branding string
1399 * @instance: per adapter object
1404 megasas_display_intel_branding(struct megasas_instance *instance)
1406 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1409 switch (instance->pdev->device) {
1410 case PCI_DEVICE_ID_LSI_INVADER:
1411 switch (instance->pdev->subsystem_device) {
1412 case MEGARAID_INTEL_RS3DC080_SSDID:
1413 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1414 instance->host->host_no,
1415 MEGARAID_INTEL_RS3DC080_BRANDING);
1417 case MEGARAID_INTEL_RS3DC040_SSDID:
1418 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1419 instance->host->host_no,
1420 MEGARAID_INTEL_RS3DC040_BRANDING);
1422 case MEGARAID_INTEL_RS3SC008_SSDID:
1423 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1424 instance->host->host_no,
1425 MEGARAID_INTEL_RS3SC008_BRANDING);
1427 case MEGARAID_INTEL_RS3MC044_SSDID:
1428 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1429 instance->host->host_no,
1430 MEGARAID_INTEL_RS3MC044_BRANDING);
1436 case PCI_DEVICE_ID_LSI_FURY:
1437 switch (instance->pdev->subsystem_device) {
1438 case MEGARAID_INTEL_RS3WC080_SSDID:
1439 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1440 instance->host->host_no,
1441 MEGARAID_INTEL_RS3WC080_BRANDING);
1443 case MEGARAID_INTEL_RS3WC040_SSDID:
1444 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1445 instance->host->host_no,
1446 MEGARAID_INTEL_RS3WC040_BRANDING);
1452 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1453 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1454 switch (instance->pdev->subsystem_device) {
1455 case MEGARAID_INTEL_RMS3BC160_SSDID:
1456 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1457 instance->host->host_no,
1458 MEGARAID_INTEL_RMS3BC160_BRANDING);
1470 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1471 * @instance: Adapter soft state
1473 * return: if success: return 0
1474 * failed: return -ENOMEM
1476 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1478 struct fusion_context *fusion;
1481 fusion = instance->ctrl_context;
1483 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1485 for (i = 0; i < 2; i++) {
1486 fusion->ld_map[i] = NULL;
1488 fusion->ld_drv_map[i] = (void *)
1489 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1490 fusion->drv_map_pages);
1492 if (!fusion->ld_drv_map[i]) {
1493 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1495 if (!fusion->ld_drv_map[i]) {
1496 dev_err(&instance->pdev->dev,
1497 "Could not allocate memory for local map"
1498 " size requested: %d\n",
1499 fusion->drv_map_sz);
1500 goto ld_drv_map_alloc_fail;
1505 for (i = 0; i < 2; i++) {
1506 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1508 &fusion->ld_map_phys[i],
1510 if (!fusion->ld_map[i]) {
1511 dev_err(&instance->pdev->dev,
1512 "Could not allocate memory for map info %s:%d\n",
1513 __func__, __LINE__);
1514 goto ld_map_alloc_fail;
1521 for (i = 0; i < 2; i++) {
1522 if (fusion->ld_map[i])
1523 dma_free_coherent(&instance->pdev->dev,
1526 fusion->ld_map_phys[i]);
1529 ld_drv_map_alloc_fail:
1530 for (i = 0; i < 2; i++) {
1531 if (fusion->ld_drv_map[i]) {
1532 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1533 vfree(fusion->ld_drv_map[i]);
1535 free_pages((ulong)fusion->ld_drv_map[i],
1536 fusion->drv_map_pages);
1544 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1546 * IO request frame queue, set can_queue.
1547 * @instance: Adapter soft state
1551 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1553 struct fusion_context *fusion;
1556 fusion = instance->ctrl_context;
1557 max_cmd = instance->max_fw_cmds;
1559 if (instance->adapter_type >= VENTURA_SERIES)
1560 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1562 instance->max_mpt_cmds = instance->max_fw_cmds;
1564 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1565 instance->cur_can_queue = instance->max_scsi_cmds;
1566 instance->host->can_queue = instance->cur_can_queue;
1568 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1570 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1571 instance->max_mpt_cmds;
1572 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1573 (fusion->reply_q_depth);
1574 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1575 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1576 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1579 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1581 struct fusion_context *fusion;
1582 struct megasas_cmd *cmd;
1584 fusion = instance->ctrl_context;
1586 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1589 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1590 __func__, __LINE__);
1594 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1595 IOC_INIT_FRAME_SIZE,
1596 &cmd->frame_phys_addr, GFP_KERNEL);
1599 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1600 __func__, __LINE__);
1605 fusion->ioc_init_cmd = cmd;
1610 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1611 * @instance: Adapter soft state
1613 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1615 struct fusion_context *fusion;
1617 fusion = instance->ctrl_context;
1619 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1620 dma_free_coherent(&instance->pdev->dev,
1621 IOC_INIT_FRAME_SIZE,
1622 fusion->ioc_init_cmd->frame,
1623 fusion->ioc_init_cmd->frame_phys_addr);
1625 kfree(fusion->ioc_init_cmd);
1629 * megasas_init_adapter_fusion - Initializes the FW
1630 * @instance: Adapter soft state
1632 * This is the main function for initializing firmware.
1635 megasas_init_adapter_fusion(struct megasas_instance *instance)
1637 struct fusion_context *fusion;
1641 fusion = instance->ctrl_context;
1643 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1646 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1648 instance->max_mfi_cmds =
1649 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1651 megasas_configure_queue_sizes(instance);
1653 scratch_pad_1 = megasas_readl(instance,
1654 &instance->reg_set->outbound_scratch_pad_1);
1655 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1656 * Firmware support extended IO chain frame which is 4 times more than
1658 * Legacy Firmware - Frame size is (8 * 128) = 1K
1659 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1661 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1662 instance->max_chain_frame_sz =
1663 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1664 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1666 instance->max_chain_frame_sz =
1667 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1668 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1670 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1671 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1672 instance->max_chain_frame_sz,
1673 MEGASAS_CHAIN_FRAME_SZ_MIN);
1674 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1677 fusion->max_sge_in_main_msg =
1678 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1679 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1681 fusion->max_sge_in_chain =
1682 instance->max_chain_frame_sz
1683 / sizeof(union MPI2_SGE_IO_UNION);
1685 instance->max_num_sge =
1686 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1687 + fusion->max_sge_in_chain - 2);
1689 /* Used for pass thru MFI frame (DCMD) */
1690 fusion->chain_offset_mfi_pthru =
1691 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1693 fusion->chain_offset_io_request =
1694 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1695 sizeof(union MPI2_SGE_IO_UNION))/16;
1697 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1698 for (i = 0 ; i < count; i++)
1699 fusion->last_reply_idx[i] = 0;
1702 * For fusion adapters, 3 commands for IOCTL and 8 commands
1703 * for driver's internal DCMDs.
1705 instance->max_scsi_cmds = instance->max_fw_cmds -
1706 (MEGASAS_FUSION_INTERNAL_CMDS +
1707 MEGASAS_FUSION_IOCTL_CMDS);
1708 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1710 if (megasas_alloc_ioc_init_frame(instance))
1714 * Allocate memory for descriptors
1715 * Create a pool of commands
1717 if (megasas_alloc_cmds(instance))
1718 goto fail_alloc_mfi_cmds;
1719 if (megasas_alloc_cmds_fusion(instance))
1720 goto fail_alloc_cmds;
1722 if (megasas_ioc_init_fusion(instance))
1725 megasas_display_intel_branding(instance);
1726 if (megasas_get_ctrl_info(instance)) {
1727 dev_err(&instance->pdev->dev,
1728 "Could not get controller info. Fail from %s %d\n",
1729 __func__, __LINE__);
1733 instance->flag_ieee = 1;
1734 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1735 fusion->fast_path_io = 0;
1737 if (megasas_allocate_raid_maps(instance))
1740 if (!megasas_get_map_info(instance))
1741 megasas_sync_map_info(instance);
1746 megasas_free_cmds_fusion(instance);
1748 megasas_free_cmds(instance);
1749 fail_alloc_mfi_cmds:
1750 megasas_free_ioc_init_cmd(instance);
1755 * megasas_fault_detect_work - Worker function of
1756 * FW fault handling workqueue.
1759 megasas_fault_detect_work(struct work_struct *work)
1761 struct megasas_instance *instance =
1762 container_of(work, struct megasas_instance,
1763 fw_fault_work.work);
1764 u32 fw_state, dma_state, status;
1766 /* Check the fw state */
1767 fw_state = instance->instancet->read_fw_status_reg(instance) &
1770 if (fw_state == MFI_STATE_FAULT) {
1771 dma_state = instance->instancet->read_fw_status_reg(instance) &
1773 /* Start collecting crash, if DMA bit is done */
1774 if (instance->crash_dump_drv_support &&
1775 instance->crash_dump_app_support && dma_state) {
1776 megasas_fusion_crash_dump(instance);
1778 if (instance->unload == 0) {
1779 status = megasas_reset_fusion(instance->host, 0);
1780 if (status != SUCCESS) {
1781 dev_err(&instance->pdev->dev,
1782 "Failed from %s %d, do not re-arm timer\n",
1783 __func__, __LINE__);
1790 if (instance->fw_fault_work_q)
1791 queue_delayed_work(instance->fw_fault_work_q,
1792 &instance->fw_fault_work,
1793 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1797 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1799 /* Check if the Fault WQ is already started */
1800 if (instance->fw_fault_work_q)
1803 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1805 snprintf(instance->fault_handler_work_q_name,
1806 sizeof(instance->fault_handler_work_q_name),
1807 "poll_megasas%d_status", instance->host->host_no);
1809 instance->fw_fault_work_q =
1810 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1811 if (!instance->fw_fault_work_q) {
1812 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1813 __func__, __LINE__);
1817 queue_delayed_work(instance->fw_fault_work_q,
1818 &instance->fw_fault_work,
1819 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1825 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1827 struct workqueue_struct *wq;
1829 if (instance->fw_fault_work_q) {
1830 wq = instance->fw_fault_work_q;
1831 instance->fw_fault_work_q = NULL;
1832 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1833 flush_workqueue(wq);
1834 destroy_workqueue(wq);
1839 * map_cmd_status - Maps FW cmd status to OS cmd status
1840 * @cmd : Pointer to cmd
1841 * @status : status of cmd returned by FW
1842 * @ext_status : ext status of cmd returned by FW
1846 map_cmd_status(struct fusion_context *fusion,
1847 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1848 u32 data_length, u8 *sense)
1853 cmd_type = megasas_cmd_type(scmd);
1857 scmd->result = DID_OK << 16;
1860 case MFI_STAT_SCSI_IO_FAILED:
1861 case MFI_STAT_LD_INIT_IN_PROGRESS:
1862 scmd->result = (DID_ERROR << 16) | ext_status;
1865 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1867 scmd->result = (DID_OK << 16) | ext_status;
1868 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1869 memset(scmd->sense_buffer, 0,
1870 SCSI_SENSE_BUFFERSIZE);
1871 memcpy(scmd->sense_buffer, sense,
1872 SCSI_SENSE_BUFFERSIZE);
1873 scmd->result |= DRIVER_SENSE << 24;
1877 * If the IO request is partially completed, then MR FW will
1878 * update "io_request->DataLength" field with actual number of
1879 * bytes transferred.Driver will set residual bytes count in
1880 * SCSI command structure.
1882 resid = (scsi_bufflen(scmd) - data_length);
1883 scsi_set_resid(scmd, resid);
1886 ((cmd_type == READ_WRITE_LDIO) ||
1887 (cmd_type == READ_WRITE_SYSPDIO)))
1888 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1889 " requested/completed 0x%x/0x%x\n",
1890 status, scsi_bufflen(scmd), data_length);
1893 case MFI_STAT_LD_OFFLINE:
1894 case MFI_STAT_DEVICE_NOT_FOUND:
1895 scmd->result = DID_BAD_TARGET << 16;
1897 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1898 scmd->result = DID_IMM_RETRY << 16;
1901 scmd->result = DID_ERROR << 16;
1907 * megasas_is_prp_possible -
1908 * Checks if native NVMe PRPs can be built for the IO
1910 * @instance: Adapter soft state
1911 * @scmd: SCSI command from the mid-layer
1912 * @sge_count: scatter gather element count.
1914 * Returns: true: PRPs can be built
1915 * false: IEEE SGLs needs to be built
1918 megasas_is_prp_possible(struct megasas_instance *instance,
1919 struct scsi_cmnd *scmd, int sge_count)
1922 u32 data_length = 0;
1923 struct scatterlist *sg_scmd;
1924 bool build_prp = false;
1925 u32 mr_nvme_pg_size;
1927 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1928 MR_DEFAULT_NVME_PAGE_SIZE);
1929 data_length = scsi_bufflen(scmd);
1930 sg_scmd = scsi_sglist(scmd);
1933 * NVMe uses one PRP for each page (or part of a page)
1934 * look at the data length - if 4 pages or less then IEEE is OK
1935 * if > 5 pages then we need to build a native SGL
1936 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1937 * if this first size in the page is >= the residual beyond 4 pages
1938 * then use IEEE, otherwise use native SGL
1941 if (data_length > (mr_nvme_pg_size * 5)) {
1943 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1944 (data_length <= (mr_nvme_pg_size * 5))) {
1945 /* check if 1st SG entry size is < residual beyond 4 pages */
1946 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1951 * Below code detects gaps/holes in IO data buffers.
1952 * What does holes/gaps mean?
1953 * Any SGE except first one in a SGL starts at non NVME page size
1954 * aligned address OR Any SGE except last one in a SGL ends at
1955 * non NVME page size boundary.
1957 * Driver has already informed block layer by setting boundary rules for
1958 * bio merging done at NVME page size boundary calling kernel API
1959 * blk_queue_virt_boundary inside slave_config.
1960 * Still there is possibility of IO coming with holes to driver because of
1961 * IO merging done by IO scheduler.
1963 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1964 * IO scheduling so no IO merging.
1966 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1967 * then sending IOs with holes.
1969 * Though driver can request block layer to disable IO merging by calling-
1970 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1971 * user may tune sysfs parameter- nomerges again to 0 or 1.
1973 * If in future IO scheduling is enabled with SCSI BLK MQ,
1974 * this algorithm to detect holes will be required in driver
1975 * for SCSI BLK MQ enabled case as well.
1979 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1980 if ((i != 0) && (i != (sge_count - 1))) {
1981 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1982 mega_mod64(sg_dma_address(sg_scmd),
1985 atomic_inc(&instance->sge_holes_type1);
1990 if ((sge_count > 1) && (i == 0)) {
1991 if ((mega_mod64((sg_dma_address(sg_scmd) +
1992 sg_dma_len(sg_scmd)),
1993 mr_nvme_pg_size))) {
1995 atomic_inc(&instance->sge_holes_type2);
2000 if ((sge_count > 1) && (i == (sge_count - 1))) {
2001 if (mega_mod64(sg_dma_address(sg_scmd),
2004 atomic_inc(&instance->sge_holes_type3);
2014 * megasas_make_prp_nvme -
2015 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2017 * @instance: Adapter soft state
2018 * @scmd: SCSI command from the mid-layer
2019 * @sgl_ptr: SGL to be filled in
2020 * @cmd: Fusion command frame
2021 * @sge_count: scatter gather element count.
2023 * Returns: true: PRPs are built
2024 * false: IEEE SGLs needs to be built
2027 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2028 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2029 struct megasas_cmd_fusion *cmd, int sge_count)
2031 int sge_len, offset, num_prp_in_chain = 0;
2032 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2034 dma_addr_t ptr_sgl_phys;
2036 u32 page_mask, page_mask_result;
2037 struct scatterlist *sg_scmd;
2039 bool build_prp = false;
2040 int data_len = scsi_bufflen(scmd);
2041 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2042 MR_DEFAULT_NVME_PAGE_SIZE);
2044 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2050 * Nvme has a very convoluted prp format. One prp is required
2051 * for each page or partial page. Driver need to split up OS sg_list
2052 * entries if it is longer than one page or cross a page
2053 * boundary. Driver also have to insert a PRP list pointer entry as
2054 * the last entry in each physical page of the PRP list.
2056 * NOTE: The first PRP "entry" is actually placed in the first
2057 * SGL entry in the main message as IEEE 64 format. The 2nd
2058 * entry in the main message is the chain element, and the rest
2059 * of the PRP entries are built in the contiguous pcie buffer.
2061 page_mask = mr_nvme_pg_size - 1;
2062 ptr_sgl = (u64 *)cmd->sg_frame;
2063 ptr_sgl_phys = cmd->sg_frame_phys_addr;
2064 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2066 /* Build chain frame element which holds all prps except first*/
2067 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2068 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2070 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2071 main_chain_element->NextChainOffset = 0;
2072 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2073 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2074 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2076 /* Build first prp, sge need not to be page aligned*/
2077 ptr_first_sgl = sgl_ptr;
2078 sg_scmd = scsi_sglist(scmd);
2079 sge_addr = sg_dma_address(sg_scmd);
2080 sge_len = sg_dma_len(sg_scmd);
2082 offset = (u32)(sge_addr & page_mask);
2083 first_prp_len = mr_nvme_pg_size - offset;
2085 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2086 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2088 data_len -= first_prp_len;
2090 if (sge_len > first_prp_len) {
2091 sge_addr += first_prp_len;
2092 sge_len -= first_prp_len;
2093 } else if (sge_len == first_prp_len) {
2094 sg_scmd = sg_next(sg_scmd);
2095 sge_addr = sg_dma_address(sg_scmd);
2096 sge_len = sg_dma_len(sg_scmd);
2100 offset = (u32)(sge_addr & page_mask);
2102 /* Put PRP pointer due to page boundary*/
2103 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2104 if (unlikely(!page_mask_result)) {
2105 scmd_printk(KERN_NOTICE,
2106 scmd, "page boundary ptr_sgl: 0x%p\n",
2109 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2114 *ptr_sgl = cpu_to_le64(sge_addr);
2119 sge_addr += mr_nvme_pg_size;
2120 sge_len -= mr_nvme_pg_size;
2121 data_len -= mr_nvme_pg_size;
2129 sg_scmd = sg_next(sg_scmd);
2130 sge_addr = sg_dma_address(sg_scmd);
2131 sge_len = sg_dma_len(sg_scmd);
2134 main_chain_element->Length =
2135 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2137 atomic_inc(&instance->prp_sgl);
2142 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2143 * @instance: Adapter soft state
2144 * @scp: SCSI command from the mid-layer
2145 * @sgl_ptr: SGL to be filled in
2146 * @cmd: cmd we are working on
2147 * @sge_count sge count
2151 megasas_make_sgl_fusion(struct megasas_instance *instance,
2152 struct scsi_cmnd *scp,
2153 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2154 struct megasas_cmd_fusion *cmd, int sge_count)
2156 int i, sg_processed;
2157 struct scatterlist *os_sgl;
2158 struct fusion_context *fusion;
2160 fusion = instance->ctrl_context;
2162 if (instance->adapter_type >= INVADER_SERIES) {
2163 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2164 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2165 sgl_ptr_end->Flags = 0;
2168 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2169 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2170 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2172 if (instance->adapter_type >= INVADER_SERIES)
2173 if (i == sge_count - 1)
2174 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2176 sg_processed = i + 1;
2178 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2179 (sge_count > fusion->max_sge_in_main_msg)) {
2181 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2182 if (instance->adapter_type >= INVADER_SERIES) {
2183 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2184 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2185 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2186 cmd->io_request->ChainOffset =
2188 chain_offset_io_request;
2190 cmd->io_request->ChainOffset = 0;
2192 cmd->io_request->ChainOffset =
2193 fusion->chain_offset_io_request;
2196 /* Prepare chain element */
2197 sg_chain->NextChainOffset = 0;
2198 if (instance->adapter_type >= INVADER_SERIES)
2199 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2202 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2203 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2204 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2205 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2208 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2209 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2212 atomic_inc(&instance->ieee_sgl);
2216 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2217 * @scp: SCSI command pointer
2218 * @instance: Soft instance of controller
2219 * @cmd: Fusion command pointer
2221 * This function will build sgls based on device type.
2222 * For nvme drives, there is different way of building sgls in nvme native
2223 * format- PRPs(Physical Region Page).
2225 * Returns the number of sg lists actually used, zero if the sg lists
2226 * is NULL, or -ENOMEM if the mapping failed
2229 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2230 struct megasas_cmd_fusion *cmd)
2233 bool build_prp = false;
2234 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2236 sge_count = scsi_dma_map(scp);
2238 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2241 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2242 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2243 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2244 (cmd->pd_interface == NVME_PD))
2245 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2249 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2256 * megasas_set_pd_lba - Sets PD LBA
2258 * @cdb_len: cdb length
2259 * @start_blk: Start block of IO
2261 * Used to set the PD LBA in CDB for FP IOs
2264 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2265 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2266 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2268 struct MR_LD_RAID *raid;
2270 u64 start_blk = io_info->pdBlock;
2271 u8 *cdb = io_request->CDB.CDB32;
2272 u32 num_blocks = io_info->numBlocks;
2273 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2275 /* Check if T10 PI (DIF) is enabled for this LD */
2276 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2277 raid = MR_LdRaidGet(ld, local_map_ptr);
2278 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2279 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2280 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2281 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2283 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2284 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2286 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2287 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2290 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2291 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2292 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2293 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2294 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2295 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2296 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2297 cdb[19] = (u8)(start_blk & 0xff);
2299 /* Logical block reference tag */
2300 io_request->CDB.EEDP32.PrimaryReferenceTag =
2301 cpu_to_be32(ref_tag);
2302 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2303 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2305 /* Transfer length */
2306 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2307 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2308 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2309 cdb[31] = (u8)(num_blocks & 0xff);
2311 /* set SCSI IO EEDPFlags */
2312 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2313 io_request->EEDPFlags = cpu_to_le16(
2314 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2315 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2316 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2317 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2318 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2319 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2321 io_request->EEDPFlags = cpu_to_le16(
2322 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2323 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2325 io_request->Control |= cpu_to_le32((0x4 << 26));
2326 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2328 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2329 if (((cdb_len == 12) || (cdb_len == 16)) &&
2330 (start_blk <= 0xffffffff)) {
2331 if (cdb_len == 16) {
2332 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2337 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2343 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2350 /* Transfer length */
2351 cdb[8] = (u8)(num_blocks & 0xff);
2352 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2354 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2356 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2357 /* Convert to 16 byte CDB for large LBA's */
2360 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2365 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2372 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2379 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2386 /* Transfer length */
2387 cdb[13] = (u8)(num_blocks & 0xff);
2388 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2389 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2390 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2392 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2396 /* Normal case, just load LBA here */
2400 u8 val = cdb[1] & 0xE0;
2401 cdb[3] = (u8)(start_blk & 0xff);
2402 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2403 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2407 cdb[5] = (u8)(start_blk & 0xff);
2408 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2409 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2410 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2413 cdb[5] = (u8)(start_blk & 0xff);
2414 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2415 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2416 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2419 cdb[9] = (u8)(start_blk & 0xff);
2420 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2421 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2422 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2423 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2424 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2425 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2426 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2433 * megasas_stream_detect - stream detection on read and and write IOs
2434 * @instance: Adapter soft state
2435 * @cmd: Command to be prepared
2436 * @io_info: IO Request info
2440 /** stream detection on read and and write IOs */
2441 static void megasas_stream_detect(struct megasas_instance *instance,
2442 struct megasas_cmd_fusion *cmd,
2443 struct IO_REQUEST_INFO *io_info)
2445 struct fusion_context *fusion = instance->ctrl_context;
2446 u32 device_id = io_info->ldTgtId;
2447 struct LD_STREAM_DETECT *current_ld_sd
2448 = fusion->stream_detect_by_ld[device_id];
2449 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2450 u32 shifted_values, unshifted_values;
2451 u32 index_value_mask, shifted_values_mask;
2453 bool is_read_ahead = false;
2454 struct STREAM_DETECT *current_sd;
2455 /* find possible stream */
2456 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2457 stream_num = (*track_stream >>
2458 (i * BITS_PER_INDEX_STREAM)) &
2460 current_sd = ¤t_ld_sd->stream_track[stream_num];
2461 /* if we found a stream, update the raid
2462 * context and also update the mruBitMap
2464 /* boundary condition */
2465 if ((current_sd->next_seq_lba) &&
2466 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2467 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2468 (current_sd->is_read == io_info->isRead)) {
2470 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2471 ((!io_info->isRead) || (!is_read_ahead)))
2473 * Once the API availible we need to change this.
2474 * At this point we are not allowing any gap
2478 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2479 current_sd->next_seq_lba =
2480 io_info->ldStartBlock + io_info->numBlocks;
2482 * update the mruBitMap LRU
2484 shifted_values_mask =
2485 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2486 shifted_values = ((*track_stream & shifted_values_mask)
2487 << BITS_PER_INDEX_STREAM);
2489 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2491 *track_stream & ~(shifted_values_mask |
2494 unshifted_values | shifted_values | stream_num;
2499 * if we did not find any stream, create a new one
2500 * from the least recently used
2502 stream_num = (*track_stream >>
2503 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2505 current_sd = ¤t_ld_sd->stream_track[stream_num];
2506 current_sd->is_read = io_info->isRead;
2507 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2508 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2513 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2514 * affinity (cpu of the controller) and raid_flags in the raid context
2517 * @praid_context: IO RAID context
2518 * @raid: LD raid map
2519 * @fp_possible: Is fast path possible?
2520 * @is_read: Is read IO?
2524 megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2525 struct MR_LD_RAID *raid, bool fp_possible,
2526 u8 is_read, u32 scsi_buff_len)
2528 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2529 struct RAID_CONTEXT_G35 *rctx_g35;
2531 rctx_g35 = &praid_context->raid_context_g35;
2534 if ((raid->cpuAffinity.pdRead.cpu0) &&
2535 (raid->cpuAffinity.pdRead.cpu1))
2536 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2537 else if (raid->cpuAffinity.pdRead.cpu1)
2538 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2540 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2541 (raid->cpuAffinity.pdWrite.cpu1))
2542 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2543 else if (raid->cpuAffinity.pdWrite.cpu1)
2544 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2545 /* Fast path cache by pass capable R0/R1 VD */
2546 if ((raid->level <= 1) &&
2547 (raid->capability.fp_cache_bypass_capable)) {
2548 rctx_g35->routing_flags |=
2549 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2550 rctx_g35->raid_flags =
2551 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2552 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2557 if ((raid->cpuAffinity.ldRead.cpu0) &&
2558 (raid->cpuAffinity.ldRead.cpu1))
2559 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2560 else if (raid->cpuAffinity.ldRead.cpu1)
2561 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2563 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2564 (raid->cpuAffinity.ldWrite.cpu1))
2565 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2566 else if (raid->cpuAffinity.ldWrite.cpu1)
2567 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2569 if (is_stream_detected(rctx_g35) &&
2570 ((raid->level == 5) || (raid->level == 6)) &&
2571 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2572 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2573 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2577 rctx_g35->routing_flags |=
2578 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2580 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2581 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2582 * IO Subtype is not bitmap.
2584 if ((raid->level == 1) && (!is_read)) {
2585 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2586 praid_context->raid_context_g35.raid_flags =
2587 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2588 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2593 * megasas_build_ldio_fusion - Prepares IOs to devices
2594 * @instance: Adapter soft state
2595 * @scp: SCSI command
2596 * @cmd: Command to be prepared
2598 * Prepares the io_request and chain elements (sg_frame) for IO
2599 * The IO can be for PD (Fast Path) or LD
2602 megasas_build_ldio_fusion(struct megasas_instance *instance,
2603 struct scsi_cmnd *scp,
2604 struct megasas_cmd_fusion *cmd)
2608 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2610 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2611 struct IO_REQUEST_INFO io_info;
2612 struct fusion_context *fusion;
2613 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2615 unsigned long spinlock_flags;
2616 struct MR_LD_RAID *raid = NULL;
2617 struct MR_PRIV_DEVICE *mrdev_priv;
2618 struct RAID_CONTEXT *rctx;
2619 struct RAID_CONTEXT_G35 *rctx_g35;
2621 device_id = MEGASAS_DEV_INDEX(scp);
2623 fusion = instance->ctrl_context;
2625 io_request = cmd->io_request;
2626 rctx = &io_request->RaidContext.raid_context;
2627 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2629 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2631 rctx->ex_status = 0;
2635 fp_possible = false;
2638 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2640 if (scp->cmd_len == 6) {
2641 datalength = (u32) scp->cmnd[4];
2642 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2643 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2645 start_lba_lo &= 0x1FFFFF;
2649 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2651 else if (scp->cmd_len == 10) {
2652 datalength = (u32) scp->cmnd[8] |
2653 ((u32) scp->cmnd[7] << 8);
2654 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2655 ((u32) scp->cmnd[3] << 16) |
2656 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2660 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2662 else if (scp->cmd_len == 12) {
2663 datalength = ((u32) scp->cmnd[6] << 24) |
2664 ((u32) scp->cmnd[7] << 16) |
2665 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2666 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2667 ((u32) scp->cmnd[3] << 16) |
2668 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2672 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2674 else if (scp->cmd_len == 16) {
2675 datalength = ((u32) scp->cmnd[10] << 24) |
2676 ((u32) scp->cmnd[11] << 16) |
2677 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2678 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2679 ((u32) scp->cmnd[7] << 16) |
2680 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2682 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2683 ((u32) scp->cmnd[3] << 16) |
2684 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2687 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2688 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2689 io_info.numBlocks = datalength;
2690 io_info.ldTgtId = device_id;
2691 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2692 scsi_buff_len = scsi_bufflen(scp);
2693 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2695 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2698 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2699 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2701 if (ld < instance->fw_supported_vd_count)
2702 raid = MR_LdRaidGet(ld, local_map_ptr);
2704 if (!raid || (!fusion->fast_path_io)) {
2705 rctx->reg_lock_flags = 0;
2706 fp_possible = false;
2708 if (MR_BuildRaidContext(instance, &io_info, rctx,
2709 local_map_ptr, &raidLUN))
2710 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2713 cmd->request_desc->SCSIIO.MSIxIndex =
2714 instance->reply_map[raw_smp_processor_id()];
2716 if (instance->adapter_type >= VENTURA_SERIES) {
2717 /* FP for Optimal raid level 1.
2718 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2719 * are built by the driver as LD I/Os.
2720 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2721 * (there is never a reason to process these as buffered writes)
2722 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2723 * with the SLD bit asserted.
2725 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2726 mrdev_priv = scp->device->hostdata;
2728 if (atomic_inc_return(&instance->fw_outstanding) >
2729 (instance->host->can_queue)) {
2730 fp_possible = false;
2731 atomic_dec(&instance->fw_outstanding);
2732 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2733 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
2734 fp_possible = false;
2735 atomic_dec(&instance->fw_outstanding);
2736 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2737 atomic_set(&mrdev_priv->r1_ldio_hint,
2738 instance->r1_ldio_hint_default);
2743 (io_info.isRead && io_info.ra_capable)) {
2744 spin_lock_irqsave(&instance->stream_lock,
2746 megasas_stream_detect(instance, cmd, &io_info);
2747 spin_unlock_irqrestore(&instance->stream_lock,
2749 /* In ventura if stream detected for a read and it is
2750 * read ahead capable make this IO as LDIO
2752 if (is_stream_detected(rctx_g35))
2753 fp_possible = false;
2756 /* If raid is NULL, set CPU affinity to default CPU0 */
2758 megasas_set_raidflag_cpu_affinity(&io_request->RaidContext,
2759 raid, fp_possible, io_info.isRead,
2762 rctx_g35->routing_flags |=
2763 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2767 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2768 local_map_ptr, start_lba_lo);
2769 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2770 cmd->request_desc->SCSIIO.RequestFlags =
2771 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2772 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2773 if (instance->adapter_type == INVADER_SERIES) {
2774 if (rctx->reg_lock_flags == REGION_TYPE_UNUSED)
2775 cmd->request_desc->SCSIIO.RequestFlags =
2776 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2777 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2778 rctx->type = MPI2_TYPE_CUDA;
2780 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2781 rctx->reg_lock_flags |=
2782 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2783 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2784 } else if (instance->adapter_type >= VENTURA_SERIES) {
2785 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2786 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2787 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2788 io_request->IoFlags |=
2789 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2791 if (fusion->load_balance_info &&
2792 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2795 get_updated_dev_handle(instance,
2796 &fusion->load_balance_info[device_id],
2797 &io_info, local_map_ptr);
2798 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2799 cmd->pd_r1_lb = io_info.pd_after_lb;
2800 if (instance->adapter_type >= VENTURA_SERIES)
2801 rctx_g35->span_arm = io_info.span_arm;
2803 rctx->span_arm = io_info.span_arm;
2806 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2808 if (instance->adapter_type >= VENTURA_SERIES)
2809 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2811 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2813 if ((raidLUN[0] == 1) &&
2814 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2815 instance->dev_handle = !(instance->dev_handle);
2817 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2820 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2821 io_request->DevHandle = io_info.devHandle;
2822 cmd->pd_interface = io_info.pd_interface;
2823 /* populate the LUN field */
2824 memcpy(io_request->LUN, raidLUN, 8);
2826 rctx->timeout_value =
2827 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2828 cmd->request_desc->SCSIIO.RequestFlags =
2829 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2830 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2831 if (instance->adapter_type == INVADER_SERIES) {
2832 if (io_info.do_fp_rlbypass ||
2833 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2834 cmd->request_desc->SCSIIO.RequestFlags =
2835 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2836 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2837 rctx->type = MPI2_TYPE_CUDA;
2838 rctx->reg_lock_flags |=
2839 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2840 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2842 } else if (instance->adapter_type >= VENTURA_SERIES) {
2843 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2844 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2845 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2847 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2848 io_request->DevHandle = cpu_to_le16(device_id);
2854 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2855 * @instance: Adapter soft state
2856 * @scp: SCSI command
2857 * @cmd: Command to be prepared
2859 * Prepares the io_request frame for non-rw io cmds for vd.
2861 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2862 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2865 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2867 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2868 struct fusion_context *fusion = instance->ctrl_context;
2872 struct MR_LD_RAID *raid;
2873 struct RAID_CONTEXT *pRAID_Context;
2876 io_request = cmd->io_request;
2877 device_id = MEGASAS_DEV_INDEX(scmd);
2878 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2879 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2880 /* get RAID_Context pointer */
2881 pRAID_Context = &io_request->RaidContext.raid_context;
2882 /* Check with FW team */
2883 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2884 pRAID_Context->reg_lock_row_lba = 0;
2885 pRAID_Context->reg_lock_length = 0;
2887 if (fusion->fast_path_io && (
2888 device_id < instance->fw_supported_vd_count)) {
2890 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2891 if (ld >= instance->fw_supported_vd_count - 1)
2894 raid = MR_LdRaidGet(ld, local_map_ptr);
2895 if (!(raid->capability.fpNonRWCapable))
2902 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2903 io_request->DevHandle = cpu_to_le16(device_id);
2904 io_request->LUN[1] = scmd->device->lun;
2905 pRAID_Context->timeout_value =
2906 cpu_to_le16 (scmd->request->timeout / HZ);
2907 cmd->request_desc->SCSIIO.RequestFlags =
2908 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2909 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2912 /* set RAID context values */
2913 pRAID_Context->config_seq_num = raid->seqNum;
2914 if (instance->adapter_type < VENTURA_SERIES)
2915 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2916 pRAID_Context->timeout_value =
2917 cpu_to_le16(raid->fpIoTimeoutForLd);
2919 /* get the DevHandle for the PD (since this is
2920 fpNonRWCapable, this is a single disk RAID0) */
2922 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2923 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2924 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2926 /* build request descriptor */
2927 cmd->request_desc->SCSIIO.RequestFlags =
2928 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2929 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2930 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2932 /* populate the LUN field */
2933 memcpy(io_request->LUN, raid->LUN, 8);
2935 /* build the raidScsiIO structure */
2936 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2937 io_request->DevHandle = devHandle;
2942 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2943 * @instance: Adapter soft state
2944 * @scp: SCSI command
2945 * @cmd: Command to be prepared
2946 * @fp_possible: parameter to detect fast path or firmware path io.
2948 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2951 megasas_build_syspd_fusion(struct megasas_instance *instance,
2952 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2956 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2958 u16 os_timeout_value;
2960 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2961 struct RAID_CONTEXT *pRAID_Context;
2962 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
2963 struct MR_PRIV_DEVICE *mr_device_priv_data;
2964 struct fusion_context *fusion = instance->ctrl_context;
2965 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
2967 device_id = MEGASAS_DEV_INDEX(scmd);
2968 pd_index = MEGASAS_PD_INDEX(scmd);
2969 os_timeout_value = scmd->request->timeout / HZ;
2970 mr_device_priv_data = scmd->device->hostdata;
2971 cmd->pd_interface = mr_device_priv_data->interface_type;
2973 io_request = cmd->io_request;
2974 /* get RAID_Context pointer */
2975 pRAID_Context = &io_request->RaidContext.raid_context;
2976 pRAID_Context->reg_lock_flags = 0;
2977 pRAID_Context->reg_lock_row_lba = 0;
2978 pRAID_Context->reg_lock_length = 0;
2979 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2980 io_request->LUN[1] = scmd->device->lun;
2981 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
2982 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2984 /* If FW supports PD sequence number */
2985 if (instance->use_seqnum_jbod_fp &&
2986 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2987 /* TgtId must be incremented by 255 as jbod seq number is index
2990 /* More than 256 PD/JBOD support for Ventura */
2991 if (instance->support_morethan256jbod)
2992 pRAID_Context->virtual_disk_tgt_id =
2993 pd_sync->seq[pd_index].pd_target_id;
2995 pRAID_Context->virtual_disk_tgt_id =
2996 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
2997 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
2998 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
2999 if (instance->adapter_type >= VENTURA_SERIES) {
3000 io_request->RaidContext.raid_context_g35.routing_flags |=
3001 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3002 io_request->RaidContext.raid_context_g35.nseg_type |=
3003 (1 << RAID_CONTEXT_NSEG_SHIFT);
3004 io_request->RaidContext.raid_context_g35.nseg_type |=
3005 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3007 pRAID_Context->type = MPI2_TYPE_CUDA;
3008 pRAID_Context->nseg = 0x1;
3009 pRAID_Context->reg_lock_flags |=
3010 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3012 } else if (fusion->fast_path_io) {
3013 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3014 pRAID_Context->config_seq_num = 0;
3015 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
3016 io_request->DevHandle =
3017 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3019 /* Want to send all IO via FW path */
3020 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3021 pRAID_Context->config_seq_num = 0;
3022 io_request->DevHandle = cpu_to_le16(0xFFFF);
3025 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3027 cmd->request_desc->SCSIIO.MSIxIndex =
3028 instance->reply_map[raw_smp_processor_id()];
3031 /* system pd firmware path */
3032 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3033 cmd->request_desc->SCSIIO.RequestFlags =
3034 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3035 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3036 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3037 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3039 if (os_timeout_value)
3042 /* system pd Fast Path */
3043 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3044 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3046 pRAID_Context->timeout_value =
3047 cpu_to_le16((os_timeout_value > timeout_limit) ?
3048 timeout_limit : os_timeout_value);
3049 if (instance->adapter_type >= INVADER_SERIES)
3050 io_request->IoFlags |=
3051 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3053 cmd->request_desc->SCSIIO.RequestFlags =
3054 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3055 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3060 * megasas_build_io_fusion - Prepares IOs to devices
3061 * @instance: Adapter soft state
3062 * @scp: SCSI command
3063 * @cmd: Command to be prepared
3065 * Invokes helper functions to prepare request frames
3066 * and sets flags appropriate for IO/Non-IO cmd
3069 megasas_build_io_fusion(struct megasas_instance *instance,
3070 struct scsi_cmnd *scp,
3071 struct megasas_cmd_fusion *cmd)
3075 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3076 struct MR_PRIV_DEVICE *mr_device_priv_data;
3077 mr_device_priv_data = scp->device->hostdata;
3079 /* Zero out some fields so they don't get reused */
3080 memset(io_request->LUN, 0x0, 8);
3081 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3082 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3083 io_request->EEDPFlags = 0;
3084 io_request->Control = 0;
3085 io_request->EEDPBlockSize = 0;
3086 io_request->ChainOffset = 0;
3087 io_request->RaidContext.raid_context.raid_flags = 0;
3088 io_request->RaidContext.raid_context.type = 0;
3089 io_request->RaidContext.raid_context.nseg = 0;
3091 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3093 * Just the CDB length,rest of the Flags are zero
3094 * This will be modified for FP in build_ldio_fusion
3096 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3098 switch (cmd_type = megasas_cmd_type(scp)) {
3099 case READ_WRITE_LDIO:
3100 megasas_build_ldio_fusion(instance, scp, cmd);
3102 case NON_READ_WRITE_LDIO:
3103 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3105 case READ_WRITE_SYSPDIO:
3106 megasas_build_syspd_fusion(instance, scp, cmd, true);
3108 case NON_READ_WRITE_SYSPDIO:
3109 if (instance->secure_jbod_support ||
3110 mr_device_priv_data->is_tm_capable)
3111 megasas_build_syspd_fusion(instance, scp, cmd, false);
3113 megasas_build_syspd_fusion(instance, scp, cmd, true);
3123 sge_count = megasas_make_sgl(instance, scp, cmd);
3125 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3126 dev_err(&instance->pdev->dev,
3127 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3128 __func__, __LINE__, sge_count, instance->max_num_sge);
3132 if (instance->adapter_type >= VENTURA_SERIES) {
3133 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3134 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3135 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3137 /* numSGE store lower 8 bit of sge_count.
3138 * numSGEExt store higher 8 bit of sge_count
3140 io_request->RaidContext.raid_context.num_sge = sge_count;
3141 io_request->RaidContext.raid_context.num_sge_ext =
3142 (u8)(sge_count >> 8);
3145 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3147 if (scp->sc_data_direction == DMA_TO_DEVICE)
3148 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3149 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3150 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3152 io_request->SGLOffset0 =
3153 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3155 io_request->SenseBufferLowAddress =
3156 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3157 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3160 scp->SCp.ptr = (char *)cmd;
3165 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3166 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3169 struct fusion_context *fusion;
3171 fusion = instance->ctrl_context;
3172 p = fusion->req_frames_desc +
3173 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3175 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3179 /* megasas_prepate_secondRaid1_IO
3180 * It prepares the raid 1 second IO
3182 void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3183 struct megasas_cmd_fusion *cmd,
3184 struct megasas_cmd_fusion *r1_cmd)
3186 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3187 struct fusion_context *fusion;
3188 fusion = instance->ctrl_context;
3189 req_desc = cmd->request_desc;
3190 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3191 memcpy(r1_cmd->io_request, cmd->io_request,
3192 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3193 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3194 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3195 /*sense buffer is different for r1 command*/
3196 r1_cmd->io_request->SenseBufferLowAddress =
3197 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3198 r1_cmd->scmd = cmd->scmd;
3199 req_desc2 = megasas_get_request_descriptor(instance,
3200 (r1_cmd->index - 1));
3201 req_desc2->Words = 0;
3202 r1_cmd->request_desc = req_desc2;
3203 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3204 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3205 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3206 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3207 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3208 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3209 cpu_to_le16(r1_cmd->index);
3210 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3211 cpu_to_le16(cmd->index);
3212 /*MSIxIndex of both commands request descriptors should be same*/
3213 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3214 cmd->request_desc->SCSIIO.MSIxIndex;
3215 /*span arm is different for r1 cmd*/
3216 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3217 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3221 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3222 * issuing non IOCTL cmd
3223 * @instance: Adapter soft state
3224 * @scmd: pointer to scsi cmd from OS
3227 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3228 struct scsi_cmnd *scmd)
3230 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3231 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3234 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3235 instance->ldio_threshold &&
3236 (atomic_inc_return(&instance->ldio_outstanding) >
3237 instance->ldio_threshold)) {
3238 atomic_dec(&instance->ldio_outstanding);
3239 return SCSI_MLQUEUE_DEVICE_BUSY;
3242 if (atomic_inc_return(&instance->fw_outstanding) >
3243 instance->host->can_queue) {
3244 atomic_dec(&instance->fw_outstanding);
3245 return SCSI_MLQUEUE_HOST_BUSY;
3248 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3251 atomic_dec(&instance->fw_outstanding);
3252 return SCSI_MLQUEUE_HOST_BUSY;
3257 req_desc = megasas_get_request_descriptor(instance, index-1);
3259 req_desc->Words = 0;
3260 cmd->request_desc = req_desc;
3262 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3263 megasas_return_cmd_fusion(instance, cmd);
3264 dev_err(&instance->pdev->dev, "Error building command\n");
3265 cmd->request_desc = NULL;
3266 atomic_dec(&instance->fw_outstanding);
3267 return SCSI_MLQUEUE_HOST_BUSY;
3270 req_desc = cmd->request_desc;
3271 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3273 if (cmd->io_request->ChainOffset != 0 &&
3274 cmd->io_request->ChainOffset != 0xF)
3275 dev_err(&instance->pdev->dev, "The chain offset value is not "
3276 "correct : %x\n", cmd->io_request->ChainOffset);
3278 * if it is raid 1/10 fp write capable.
3279 * try to get second command from pool and construct it.
3280 * From FW, it has confirmed that lba values of two PDs
3281 * corresponds to single R1/10 LD are always same
3284 /* driver side count always should be less than max_fw_cmds
3285 * to get new command
3287 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3288 r1_cmd = megasas_get_cmd_fusion(instance,
3289 (scmd->request->tag + instance->max_fw_cmds));
3290 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3295 * Issue the command to the FW
3298 megasas_fire_cmd_fusion(instance, req_desc);
3301 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3308 * megasas_complete_r1_command -
3309 * completes R1 FP write commands which has valid peer smid
3310 * @instance: Adapter soft state
3311 * @cmd_fusion: MPT command frame
3315 megasas_complete_r1_command(struct megasas_instance *instance,
3316 struct megasas_cmd_fusion *cmd)
3318 u8 *sense, status, ex_status;
3321 struct fusion_context *fusion;
3322 struct megasas_cmd_fusion *r1_cmd = NULL;
3323 struct scsi_cmnd *scmd_local = NULL;
3324 struct RAID_CONTEXT_G35 *rctx_g35;
3326 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3327 fusion = instance->ctrl_context;
3328 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
3330 r1_cmd = fusion->cmd_list[peer_smid - 1];
3331 scmd_local = cmd->scmd;
3332 status = rctx_g35->status;
3333 ex_status = rctx_g35->ex_status;
3334 data_length = cmd->io_request->DataLength;
3337 cmd->cmd_completed = true;
3339 /* Check if peer command is completed or not*/
3340 if (r1_cmd->cmd_completed) {
3341 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3342 if (rctx_g35->status != MFI_STAT_OK) {
3343 status = rctx_g35->status;
3344 ex_status = rctx_g35->ex_status;
3345 data_length = r1_cmd->io_request->DataLength;
3346 sense = r1_cmd->sense;
3349 megasas_return_cmd_fusion(instance, r1_cmd);
3350 map_cmd_status(fusion, scmd_local, status, ex_status,
3351 le32_to_cpu(data_length), sense);
3352 if (instance->ldio_threshold &&
3353 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3354 atomic_dec(&instance->ldio_outstanding);
3355 scmd_local->SCp.ptr = NULL;
3356 megasas_return_cmd_fusion(instance, cmd);
3357 scsi_dma_unmap(scmd_local);
3358 scmd_local->scsi_done(scmd_local);
3363 * complete_cmd_fusion - Completes command
3364 * @instance: Adapter soft state
3365 * Completes all commands that is in reply descriptor queue
3368 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
3370 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3371 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3372 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3373 struct fusion_context *fusion;
3374 struct megasas_cmd *cmd_mfi;
3375 struct megasas_cmd_fusion *cmd_fusion;
3376 u16 smid, num_completed;
3377 u8 reply_descript_type, *sense, status, extStatus;
3378 u32 device_id, data_length;
3379 union desc_value d_val;
3380 struct LD_LOAD_BALANCE_INFO *lbinfo;
3381 int threshold_reply_count = 0;
3382 struct scsi_cmnd *scmd_local = NULL;
3383 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3384 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3386 fusion = instance->ctrl_context;
3388 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3391 desc = fusion->reply_frames_desc[MSIxIndex] +
3392 fusion->last_reply_idx[MSIxIndex];
3394 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3396 d_val.word = desc->Words;
3398 reply_descript_type = reply_desc->ReplyFlags &
3399 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3401 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3406 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3407 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3409 smid = le16_to_cpu(reply_desc->SMID);
3410 cmd_fusion = fusion->cmd_list[smid - 1];
3411 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3412 cmd_fusion->io_request;
3414 scmd_local = cmd_fusion->scmd;
3415 status = scsi_io_req->RaidContext.raid_context.status;
3416 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3417 sense = cmd_fusion->sense;
3418 data_length = scsi_io_req->DataLength;
3420 switch (scsi_io_req->Function) {
3421 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3422 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3423 cmd_fusion->io_request;
3424 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3425 &mr_tm_req->TmRequest;
3426 dev_dbg(&instance->pdev->dev, "TM completion:"
3427 "type: 0x%x TaskMID: 0x%x\n",
3428 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3429 complete(&cmd_fusion->done);
3431 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3432 /* Update load balancing info */
3433 if (fusion->load_balance_info &&
3434 (cmd_fusion->scmd->SCp.Status &
3435 MEGASAS_LOAD_BALANCE_FLAG)) {
3436 device_id = MEGASAS_DEV_INDEX(scmd_local);
3437 lbinfo = &fusion->load_balance_info[device_id];
3438 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3439 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3441 /* Fall through - and complete IO */
3442 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3443 atomic_dec(&instance->fw_outstanding);
3444 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3445 map_cmd_status(fusion, scmd_local, status,
3446 extStatus, le32_to_cpu(data_length),
3448 if (instance->ldio_threshold &&
3449 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3450 atomic_dec(&instance->ldio_outstanding);
3451 scmd_local->SCp.ptr = NULL;
3452 megasas_return_cmd_fusion(instance, cmd_fusion);
3453 scsi_dma_unmap(scmd_local);
3454 scmd_local->scsi_done(scmd_local);
3455 } else /* Optimal VD - R1 FP command completion. */
3456 megasas_complete_r1_command(instance, cmd_fusion);
3458 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3459 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3460 /* Poll mode. Dummy free.
3461 * In case of Interrupt mode, caller has reverse check.
3463 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3464 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3465 megasas_return_cmd(instance, cmd_mfi);
3467 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3471 fusion->last_reply_idx[MSIxIndex]++;
3472 if (fusion->last_reply_idx[MSIxIndex] >=
3473 fusion->reply_q_depth)
3474 fusion->last_reply_idx[MSIxIndex] = 0;
3476 desc->Words = cpu_to_le64(ULLONG_MAX);
3478 threshold_reply_count++;
3480 /* Get the next reply descriptor */
3481 if (!fusion->last_reply_idx[MSIxIndex])
3482 desc = fusion->reply_frames_desc[MSIxIndex];
3487 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3489 d_val.word = desc->Words;
3491 reply_descript_type = reply_desc->ReplyFlags &
3492 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3494 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3497 * Write to reply post host index register after completing threshold
3498 * number of reply counts and still there are more replies in reply queue
3499 * pending to be completed
3501 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
3502 if (instance->msix_combined)
3503 writel(((MSIxIndex & 0x7) << 24) |
3504 fusion->last_reply_idx[MSIxIndex],
3505 instance->reply_post_host_index_addr[MSIxIndex/8]);
3507 writel((MSIxIndex << 24) |
3508 fusion->last_reply_idx[MSIxIndex],
3509 instance->reply_post_host_index_addr[0]);
3510 threshold_reply_count = 0;
3518 if (instance->msix_combined)
3519 writel(((MSIxIndex & 0x7) << 24) |
3520 fusion->last_reply_idx[MSIxIndex],
3521 instance->reply_post_host_index_addr[MSIxIndex/8]);
3523 writel((MSIxIndex << 24) |
3524 fusion->last_reply_idx[MSIxIndex],
3525 instance->reply_post_host_index_addr[0]);
3526 megasas_check_and_restore_queue_depth(instance);
3531 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3532 * @instance: Adapter soft state
3534 void megasas_sync_irqs(unsigned long instance_addr)
3537 struct megasas_instance *instance =
3538 (struct megasas_instance *)instance_addr;
3540 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3542 for (i = 0; i < count; i++)
3543 synchronize_irq(pci_irq_vector(instance->pdev, i));
3547 * megasas_complete_cmd_dpc_fusion - Completes command
3548 * @instance: Adapter soft state
3550 * Tasklet to complete cmds
3553 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3555 struct megasas_instance *instance =
3556 (struct megasas_instance *)instance_addr;
3557 u32 count, MSIxIndex;
3559 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3561 /* If we have already declared adapter dead, donot complete cmds */
3562 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3565 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3566 complete_cmd_fusion(instance, MSIxIndex);
3570 * megasas_isr_fusion - isr entry point
3572 irqreturn_t megasas_isr_fusion(int irq, void *devp)
3574 struct megasas_irq_context *irq_context = devp;
3575 struct megasas_instance *instance = irq_context->instance;
3578 if (instance->mask_interrupts)
3581 if (!instance->msix_vectors) {
3582 mfiStatus = instance->instancet->clear_intr(instance);
3587 /* If we are resetting, bail */
3588 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3589 instance->instancet->clear_intr(instance);
3593 return complete_cmd_fusion(instance, irq_context->MSIxIndex);
3597 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3598 * @instance: Adapter soft state
3599 * mfi_cmd: megasas_cmd pointer
3603 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3604 struct megasas_cmd *mfi_cmd)
3606 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3607 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3608 struct megasas_cmd_fusion *cmd;
3609 struct fusion_context *fusion;
3610 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3612 fusion = instance->ctrl_context;
3614 cmd = megasas_get_cmd_fusion(instance,
3615 instance->max_scsi_cmds + mfi_cmd->index);
3617 /* Save the smid. To be used for returning the cmd */
3618 mfi_cmd->context.smid = cmd->index;
3621 * For cmds where the flag is set, store the flag and check
3622 * on completion. For cmds with this flag, don't call
3623 * megasas_complete_cmd
3626 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3627 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3629 io_req = cmd->io_request;
3631 if (instance->adapter_type >= INVADER_SERIES) {
3632 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3633 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3634 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3635 sgl_ptr_end->Flags = 0;
3639 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3641 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3642 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3644 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3646 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3648 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3649 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3651 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3655 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3656 * @instance: Adapter soft state
3657 * @cmd: mfi cmd to build
3660 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3661 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3663 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3666 build_mpt_mfi_pass_thru(instance, cmd);
3667 index = cmd->context.smid;
3669 req_desc = megasas_get_request_descriptor(instance, index - 1);
3671 req_desc->Words = 0;
3672 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3673 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3675 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3681 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3682 * @instance: Adapter soft state
3683 * @cmd: mfi cmd pointer
3687 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3688 struct megasas_cmd *cmd)
3690 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3692 req_desc = build_mpt_cmd(instance, cmd);
3694 megasas_fire_cmd_fusion(instance, req_desc);
3699 * megasas_release_fusion - Reverses the FW initialization
3700 * @instance: Adapter soft state
3703 megasas_release_fusion(struct megasas_instance *instance)
3705 megasas_free_ioc_init_cmd(instance);
3706 megasas_free_cmds(instance);
3707 megasas_free_cmds_fusion(instance);
3709 iounmap(instance->reg_set);
3711 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3715 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3716 * @regs: MFI register set
3719 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
3721 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
3725 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3726 * @instance: Controller's soft instance
3727 * return: Number of allocated host crash buffers
3730 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3734 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3735 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3736 if (!instance->crash_buf[i]) {
3737 dev_info(&instance->pdev->dev, "Firmware crash dump "
3738 "memory allocation failed at index %d\n", i);
3742 instance->drv_buf_alloc = i;
3746 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3747 * @instance: Controller's soft instance
3750 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3753 for (i = 0; i < instance->drv_buf_alloc; i++) {
3754 if (instance->crash_buf[i])
3755 vfree(instance->crash_buf[i]);
3757 instance->drv_buf_index = 0;
3758 instance->drv_buf_alloc = 0;
3759 instance->fw_crash_state = UNAVAILABLE;
3760 instance->fw_crash_buffer_size = 0;
3764 * megasas_adp_reset_fusion - For controller reset
3765 * @regs: MFI register set
3768 megasas_adp_reset_fusion(struct megasas_instance *instance,
3769 struct megasas_register_set __iomem *regs)
3771 u32 host_diag, abs_state, retry;
3773 /* Now try to reset the chip */
3774 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3775 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3776 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3777 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3778 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3779 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3780 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3782 /* Check that the diag write enable (DRWE) bit is on */
3783 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3785 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3787 host_diag = megasas_readl(instance,
3788 &instance->reg_set->fusion_host_diag);
3789 if (retry++ == 100) {
3790 dev_warn(&instance->pdev->dev,
3791 "Host diag unlock failed from %s %d\n",
3792 __func__, __LINE__);
3796 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3799 /* Send chip reset command */
3800 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3801 &instance->reg_set->fusion_host_diag);
3804 /* Make sure reset adapter bit is cleared */
3805 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3807 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3809 host_diag = megasas_readl(instance,
3810 &instance->reg_set->fusion_host_diag);
3811 if (retry++ == 1000) {
3812 dev_warn(&instance->pdev->dev,
3813 "Diag reset adapter never cleared %s %d\n",
3814 __func__, __LINE__);
3818 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3821 abs_state = instance->instancet->read_fw_status_reg(instance)
3825 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3827 abs_state = instance->instancet->
3828 read_fw_status_reg(instance) & MFI_STATE_MASK;
3830 if (abs_state <= MFI_STATE_FW_INIT) {
3831 dev_warn(&instance->pdev->dev,
3832 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3833 abs_state, __func__, __LINE__);
3841 * megasas_check_reset_fusion - For controller reset check
3842 * @regs: MFI register set
3845 megasas_check_reset_fusion(struct megasas_instance *instance,
3846 struct megasas_register_set __iomem *regs)
3852 * megasas_trigger_snap_dump - Trigger snap dump in FW
3853 * @instance: Soft instance of adapter
3855 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
3860 if (!instance->disableOnlineCtrlReset) {
3861 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
3862 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
3863 &instance->reg_set->doorbell);
3864 readl(&instance->reg_set->doorbell);
3867 for (j = 0; j < instance->snapdump_wait_time; j++) {
3868 fw_state = instance->instancet->read_fw_status_reg(instance) &
3870 if (fw_state == MFI_STATE_FAULT) {
3871 dev_err(&instance->pdev->dev,
3872 "Found FW in FAULT state, after snap dump trigger\n");
3879 /* This function waits for outstanding commands on fusion to complete */
3880 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
3881 int reason, int *convert)
3883 int i, outstanding, retval = 0, hb_seconds_missed = 0;
3885 u32 waittime_for_io_completion;
3887 waittime_for_io_completion =
3888 min_t(u32, resetwaittime,
3889 (resetwaittime - instance->snapdump_wait_time));
3891 if (reason == MFI_IO_TIMEOUT_OCR) {
3892 dev_info(&instance->pdev->dev,
3893 "MFI command is timed out\n");
3894 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3895 if (instance->snapdump_wait_time)
3896 megasas_trigger_snap_dump(instance);
3901 for (i = 0; i < waittime_for_io_completion; i++) {
3902 /* Check if firmware is in fault state */
3903 fw_state = instance->instancet->read_fw_status_reg(instance) &
3905 if (fw_state == MFI_STATE_FAULT) {
3906 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
3907 " will reset adapter scsi%d.\n",
3908 instance->host->host_no);
3909 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3910 if (instance->requestorId && reason) {
3911 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3912 " state while polling during"
3913 " I/O timeout handling for %d\n",
3914 instance->host->host_no);
3923 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
3924 if (instance->requestorId && !reason) {
3929 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
3930 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
3931 if (instance->hb_host_mem->HB.fwCounter !=
3932 instance->hb_host_mem->HB.driverCounter) {
3933 instance->hb_host_mem->HB.driverCounter =
3934 instance->hb_host_mem->HB.fwCounter;
3935 hb_seconds_missed = 0;
3937 hb_seconds_missed++;
3938 if (hb_seconds_missed ==
3939 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
3940 dev_warn(&instance->pdev->dev, "SR-IOV:"
3941 " Heartbeat never completed "
3942 " while polling during I/O "
3943 " timeout handling for "
3945 instance->host->host_no);
3953 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3954 outstanding = atomic_read(&instance->fw_outstanding);
3958 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
3959 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
3960 "commands to complete for scsi%d\n", i,
3961 outstanding, instance->host->host_no);
3966 if (instance->snapdump_wait_time) {
3967 megasas_trigger_snap_dump(instance);
3972 if (atomic_read(&instance->fw_outstanding)) {
3973 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
3974 "will reset adapter scsi%d.\n",
3975 instance->host->host_no);
3984 void megasas_reset_reply_desc(struct megasas_instance *instance)
3987 struct fusion_context *fusion;
3988 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3990 fusion = instance->ctrl_context;
3991 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3992 for (i = 0 ; i < count ; i++) {
3993 fusion->last_reply_idx[i] = 0;
3994 reply_desc = fusion->reply_frames_desc[i];
3995 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3996 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4001 * megasas_refire_mgmt_cmd : Re-fire management commands
4002 * @instance: Controller's soft instance
4004 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
4007 struct megasas_cmd_fusion *cmd_fusion;
4008 struct fusion_context *fusion;
4009 struct megasas_cmd *cmd_mfi;
4010 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4012 bool refire_cmd = 0;
4016 fusion = instance->ctrl_context;
4018 /* Re-fire management commands.
4019 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4021 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4022 cmd_fusion = fusion->cmd_list[j];
4023 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4024 smid = le16_to_cpu(cmd_mfi->context.smid);
4025 result = REFIRE_CMD;
4030 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4032 switch (cmd_mfi->frame->hdr.cmd) {
4034 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4035 /* Do not refire shutdown command */
4036 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4037 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4038 result = COMPLETE_CMD;
4042 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4043 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4044 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4047 result = RETURN_CMD;
4051 if (!instance->support_nvme_passthru) {
4052 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4053 result = COMPLETE_CMD;
4063 megasas_fire_cmd_fusion(instance, req_desc);
4066 megasas_return_cmd(instance, cmd_mfi);
4069 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4076 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4077 * @instance: per adapter struct
4078 * @channel: the channel assigned by the OS
4079 * @id: the id assigned by the OS
4081 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4084 static int megasas_track_scsiio(struct megasas_instance *instance,
4085 int id, int channel)
4088 struct megasas_cmd_fusion *cmd_fusion;
4089 struct fusion_context *fusion;
4090 fusion = instance->ctrl_context;
4092 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4093 cmd_fusion = fusion->cmd_list[i];
4094 if (cmd_fusion->scmd &&
4095 (cmd_fusion->scmd->device->id == id &&
4096 cmd_fusion->scmd->device->channel == channel)) {
4097 dev_info(&instance->pdev->dev,
4098 "SCSI commands pending to target"
4099 "channel %d id %d \tSMID: 0x%x\n",
4100 channel, id, cmd_fusion->index);
4101 scsi_print_command(cmd_fusion->scmd);
4107 return found ? FAILED : SUCCESS;
4111 * megasas_tm_response_code - translation of device response code
4112 * @ioc: per adapter object
4113 * @mpi_reply: MPI reply returned by firmware
4118 megasas_tm_response_code(struct megasas_instance *instance,
4119 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4123 switch (mpi_reply->ResponseCode) {
4124 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4125 desc = "task management request completed";
4127 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4128 desc = "invalid frame";
4130 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4131 desc = "task management request not supported";
4133 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4134 desc = "task management request failed";
4136 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4137 desc = "task management request succeeded";
4139 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4140 desc = "invalid lun";
4143 desc = "overlapped tag attempted";
4145 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4146 desc = "task queued, however not sent to target";
4152 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4153 mpi_reply->ResponseCode, desc);
4154 dev_dbg(&instance->pdev->dev,
4155 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4156 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4157 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4158 mpi_reply->Function, mpi_reply->TaskType,
4159 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4163 * megasas_issue_tm - main routine for sending tm requests
4164 * @instance: per adapter struct
4165 * @device_handle: device handle
4166 * @channel: the channel assigned by the OS
4167 * @id: the id assigned by the OS
4168 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4169 * @smid_task: smid assigned to the task
4170 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4173 * MegaRaid use MPT interface for Task Magement request.
4174 * A generic API for sending task management requests to firmware.
4176 * Return SUCCESS or FAILED.
4179 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4180 uint channel, uint id, u16 smid_task, u8 type,
4181 struct MR_PRIV_DEVICE *mr_device_priv_data)
4183 struct MR_TASK_MANAGE_REQUEST *mr_request;
4184 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4185 unsigned long timeleft;
4186 struct megasas_cmd_fusion *cmd_fusion;
4187 struct megasas_cmd *cmd_mfi;
4188 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4189 struct fusion_context *fusion = NULL;
4190 struct megasas_cmd_fusion *scsi_lookup;
4192 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4193 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4195 fusion = instance->ctrl_context;
4197 cmd_mfi = megasas_get_cmd(instance);
4200 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4201 __func__, __LINE__);
4205 cmd_fusion = megasas_get_cmd_fusion(instance,
4206 instance->max_scsi_cmds + cmd_mfi->index);
4208 /* Save the smid. To be used for returning the cmd */
4209 cmd_mfi->context.smid = cmd_fusion->index;
4211 req_desc = megasas_get_request_descriptor(instance,
4212 (cmd_fusion->index - 1));
4214 cmd_fusion->request_desc = req_desc;
4215 req_desc->Words = 0;
4217 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4218 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4219 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4220 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4221 mpi_request->DevHandle = cpu_to_le16(device_handle);
4222 mpi_request->TaskType = type;
4223 mpi_request->TaskMID = cpu_to_le16(smid_task);
4224 mpi_request->LUN[1] = 0;
4227 req_desc = cmd_fusion->request_desc;
4228 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4229 req_desc->HighPriority.RequestFlags =
4230 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4231 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4232 req_desc->HighPriority.MSIxIndex = 0;
4233 req_desc->HighPriority.LMID = 0;
4234 req_desc->HighPriority.Reserved1 = 0;
4236 if (channel < MEGASAS_MAX_PD_CHANNELS)
4237 mr_request->tmReqFlags.isTMForPD = 1;
4239 mr_request->tmReqFlags.isTMForLD = 1;
4241 init_completion(&cmd_fusion->done);
4242 megasas_fire_cmd_fusion(instance, req_desc);
4245 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4246 timeout = mr_device_priv_data->task_abort_tmo;
4248 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4249 timeout = mr_device_priv_data->target_reset_tmo;
4253 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4256 dev_err(&instance->pdev->dev,
4257 "task mgmt type 0x%x timed out\n", type);
4258 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
4259 mutex_unlock(&instance->reset_mutex);
4260 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4261 mutex_lock(&instance->reset_mutex);
4265 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4266 megasas_tm_response_code(instance, mpi_reply);
4268 megasas_return_cmd(instance, cmd_mfi);
4271 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4272 scsi_lookup = fusion->cmd_list[smid_task - 1];
4274 if (scsi_lookup->scmd == NULL)
4277 instance->instancet->disable_intr(instance);
4278 megasas_sync_irqs((unsigned long)instance);
4279 instance->instancet->enable_intr(instance);
4280 if (scsi_lookup->scmd == NULL)
4286 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4287 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4289 instance->instancet->disable_intr(instance);
4290 megasas_sync_irqs((unsigned long)instance);
4291 rc = megasas_track_scsiio(instance, id, channel);
4292 instance->instancet->enable_intr(instance);
4295 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4296 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4308 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4309 * @instance: per adapter struct
4311 * Return Non Zero index, if SMID found in outstanding commands
4313 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4316 struct megasas_instance *instance;
4317 struct megasas_cmd_fusion *cmd_fusion;
4318 struct fusion_context *fusion;
4320 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4322 fusion = instance->ctrl_context;
4324 for (i = 0; i < instance->max_scsi_cmds; i++) {
4325 cmd_fusion = fusion->cmd_list[i];
4326 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4327 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4328 " SMID: %d\n", cmd_fusion->index);
4329 ret = cmd_fusion->index;
4338 * megasas_get_tm_devhandle - Get devhandle for TM request
4339 * @sdev- OS provided scsi device
4341 * Returns- devhandle/targetID of SCSI device
4343 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4347 struct megasas_instance *instance;
4348 struct fusion_context *fusion;
4349 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4350 u16 devhandle = (u16)ULONG_MAX;
4352 instance = (struct megasas_instance *)sdev->host->hostdata;
4353 fusion = instance->ctrl_context;
4355 if (!MEGASAS_IS_LOGICAL(sdev)) {
4356 if (instance->use_seqnum_jbod_fp) {
4357 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4359 pd_sync = (void *)fusion->pd_seq_sync
4360 [(instance->pd_seq_map_id - 1) & 1];
4361 devhandle = pd_sync->seq[pd_index].devHandle;
4363 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4364 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4366 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4368 devhandle = device_id;
4375 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4376 * @scmd : pointer to scsi command object
4378 * Return SUCCESS, if command aborted else FAILED
4381 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4383 struct megasas_instance *instance;
4384 u16 smid, devhandle;
4386 struct MR_PRIV_DEVICE *mr_device_priv_data;
4387 mr_device_priv_data = scmd->device->hostdata;
4389 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4391 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
4392 scsi_print_command(scmd);
4394 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4395 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4396 "SCSI host:%d\n", instance->host->host_no);
4401 if (!mr_device_priv_data) {
4402 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4403 "scmd(%p)\n", scmd);
4404 scmd->result = DID_NO_CONNECT << 16;
4409 if (!mr_device_priv_data->is_tm_capable) {
4414 mutex_lock(&instance->reset_mutex);
4416 smid = megasas_fusion_smid_lookup(scmd);
4420 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4421 " issued is not found in oustanding commands\n");
4422 mutex_unlock(&instance->reset_mutex);
4426 devhandle = megasas_get_tm_devhandle(scmd->device);
4428 if (devhandle == (u16)ULONG_MAX) {
4430 sdev_printk(KERN_INFO, scmd->device,
4431 "task abort issued for invalid devhandle\n");
4432 mutex_unlock(&instance->reset_mutex);
4435 sdev_printk(KERN_INFO, scmd->device,
4436 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
4439 mr_device_priv_data->tm_busy = 1;
4440 ret = megasas_issue_tm(instance, devhandle,
4441 scmd->device->channel, scmd->device->id, smid,
4442 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4443 mr_device_priv_data);
4444 mr_device_priv_data->tm_busy = 0;
4446 mutex_unlock(&instance->reset_mutex);
4448 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4449 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4455 * megasas_reset_target_fusion : target reset function for fusion adapters
4456 * scmd: SCSI command pointer
4458 * Returns SUCCESS if all commands associated with target aborted else FAILED
4461 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4464 struct megasas_instance *instance;
4467 struct MR_PRIV_DEVICE *mr_device_priv_data;
4468 mr_device_priv_data = scmd->device->hostdata;
4470 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4472 sdev_printk(KERN_INFO, scmd->device,
4473 "target reset called for scmd(%p)\n", scmd);
4475 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4476 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4477 "SCSI host:%d\n", instance->host->host_no);
4482 if (!mr_device_priv_data) {
4483 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4484 "scmd(%p)\n", scmd);
4485 scmd->result = DID_NO_CONNECT << 16;
4490 if (!mr_device_priv_data->is_tm_capable) {
4495 mutex_lock(&instance->reset_mutex);
4496 devhandle = megasas_get_tm_devhandle(scmd->device);
4498 if (devhandle == (u16)ULONG_MAX) {
4500 sdev_printk(KERN_INFO, scmd->device,
4501 "target reset issued for invalid devhandle\n");
4502 mutex_unlock(&instance->reset_mutex);
4506 sdev_printk(KERN_INFO, scmd->device,
4507 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4509 mr_device_priv_data->tm_busy = 1;
4510 ret = megasas_issue_tm(instance, devhandle,
4511 scmd->device->channel, scmd->device->id, 0,
4512 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4513 mr_device_priv_data);
4514 mr_device_priv_data->tm_busy = 0;
4515 mutex_unlock(&instance->reset_mutex);
4517 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4518 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4523 /*SRIOV get other instance in cluster if any*/
4524 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4528 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4529 if (megasas_mgmt_info.instance[i] &&
4530 (megasas_mgmt_info.instance[i] != instance) &&
4531 megasas_mgmt_info.instance[i]->requestorId &&
4532 megasas_mgmt_info.instance[i]->peerIsPresent &&
4533 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4534 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4535 return megasas_mgmt_info.instance[i];
4540 /* Check for a second path that is currently UP */
4541 int megasas_check_mpio_paths(struct megasas_instance *instance,
4542 struct scsi_cmnd *scmd)
4544 struct megasas_instance *peer_instance = NULL;
4545 int retval = (DID_REQUEUE << 16);
4547 if (instance->peerIsPresent) {
4548 peer_instance = megasas_get_peer_instance(instance);
4549 if ((peer_instance) &&
4550 (atomic_read(&peer_instance->adprecovery) ==
4551 MEGASAS_HBA_OPERATIONAL))
4552 retval = (DID_NO_CONNECT << 16);
4557 /* Core fusion reset function */
4558 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4560 int retval = SUCCESS, i, j, convert = 0;
4561 struct megasas_instance *instance;
4562 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4563 struct fusion_context *fusion;
4564 u32 abs_state, status_reg, reset_adapter;
4565 u32 io_timeout_in_crash_mode = 0;
4566 struct scsi_cmnd *scmd_local = NULL;
4567 struct scsi_device *sdev;
4568 int ret_target_prop = DCMD_FAILED;
4569 bool is_target_prop = false;
4571 instance = (struct megasas_instance *)shost->hostdata;
4572 fusion = instance->ctrl_context;
4574 mutex_lock(&instance->reset_mutex);
4576 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4577 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4578 "returning FAILED for scsi%d.\n",
4579 instance->host->host_no);
4580 mutex_unlock(&instance->reset_mutex);
4583 status_reg = instance->instancet->read_fw_status_reg(instance);
4584 abs_state = status_reg & MFI_STATE_MASK;
4586 /* IO timeout detected, forcibly put FW in FAULT state */
4587 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4588 instance->crash_dump_app_support && reason) {
4589 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4590 "forcibly FAULT Firmware\n");
4591 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4592 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4593 writel(status_reg | MFI_STATE_FORCE_OCR,
4594 &instance->reg_set->doorbell);
4595 readl(&instance->reg_set->doorbell);
4596 mutex_unlock(&instance->reset_mutex);
4599 io_timeout_in_crash_mode++;
4600 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4601 "seconds for crash dump collection and OCR "
4602 "to be done\n", (io_timeout_in_crash_mode * 3));
4603 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4604 (io_timeout_in_crash_mode < 80));
4606 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4607 dev_info(&instance->pdev->dev, "OCR done for IO "
4611 dev_info(&instance->pdev->dev, "Controller is not "
4612 "operational after 240 seconds wait for IO "
4613 "timeout case in FW crash dump mode\n do "
4614 "OCR/kill adapter\n");
4615 retval = megasas_reset_fusion(shost, 0);
4620 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4621 del_timer_sync(&instance->sriov_heartbeat_timer);
4622 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4623 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4624 instance->instancet->disable_intr(instance);
4625 megasas_sync_irqs((unsigned long)instance);
4627 /* First try waiting for commands to complete */
4628 if (megasas_wait_for_outstanding_fusion(instance, reason,
4630 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4631 dev_warn(&instance->pdev->dev, "resetting fusion "
4632 "adapter scsi%d.\n", instance->host->host_no);
4636 if (megasas_dbg_lvl & OCR_LOGS)
4637 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4639 /* Now return commands back to the OS */
4640 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4641 cmd_fusion = fusion->cmd_list[i];
4642 /*check for extra commands issued by driver*/
4643 if (instance->adapter_type >= VENTURA_SERIES) {
4644 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4645 megasas_return_cmd_fusion(instance, r1_cmd);
4647 scmd_local = cmd_fusion->scmd;
4648 if (cmd_fusion->scmd) {
4649 if (megasas_dbg_lvl & OCR_LOGS) {
4650 sdev_printk(KERN_INFO,
4651 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4653 scsi_print_command(cmd_fusion->scmd);
4656 scmd_local->result =
4657 megasas_check_mpio_paths(instance,
4659 if (instance->ldio_threshold &&
4660 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4661 atomic_dec(&instance->ldio_outstanding);
4662 megasas_return_cmd_fusion(instance, cmd_fusion);
4663 scsi_dma_unmap(scmd_local);
4664 scmd_local->scsi_done(scmd_local);
4668 atomic_set(&instance->fw_outstanding, 0);
4670 status_reg = instance->instancet->read_fw_status_reg(instance);
4671 abs_state = status_reg & MFI_STATE_MASK;
4672 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4673 if (instance->disableOnlineCtrlReset ||
4674 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4675 /* Reset not supported, kill adapter */
4676 dev_warn(&instance->pdev->dev, "Reset not supported"
4677 ", killing adapter scsi%d.\n",
4678 instance->host->host_no);
4679 megaraid_sas_kill_hba(instance);
4680 instance->skip_heartbeat_timer_del = 1;
4685 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4686 if (instance->requestorId && !reason) {
4687 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4688 goto transition_to_ready;
4691 /* Now try to reset the chip */
4692 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
4694 if (instance->instancet->adp_reset
4695 (instance, instance->reg_set))
4697 transition_to_ready:
4698 /* Wait for FW to become ready */
4699 if (megasas_transition_to_ready(instance, 1)) {
4700 dev_warn(&instance->pdev->dev,
4701 "Failed to transition controller to ready for "
4702 "scsi%d.\n", instance->host->host_no);
4703 if (instance->requestorId && !reason)
4704 goto fail_kill_adapter;
4708 megasas_reset_reply_desc(instance);
4709 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4711 if (megasas_ioc_init_fusion(instance)) {
4712 if (instance->requestorId && !reason)
4713 goto fail_kill_adapter;
4718 if (megasas_get_ctrl_info(instance)) {
4719 dev_info(&instance->pdev->dev,
4720 "Failed from %s %d\n",
4721 __func__, __LINE__);
4722 megaraid_sas_kill_hba(instance);
4727 megasas_refire_mgmt_cmd(instance);
4729 /* Reset load balance info */
4730 if (fusion->load_balance_info)
4731 memset(fusion->load_balance_info, 0,
4732 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4733 MAX_LOGICAL_DRIVES_EXT));
4735 if (!megasas_get_map_info(instance))
4736 megasas_sync_map_info(instance);
4738 megasas_setup_jbod_map(instance);
4740 /* reset stream detection array */
4741 if (instance->adapter_type >= VENTURA_SERIES) {
4742 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4743 memset(fusion->stream_detect_by_ld[j],
4744 0, sizeof(struct LD_STREAM_DETECT));
4745 fusion->stream_detect_by_ld[j]->mru_bit_map
4750 clear_bit(MEGASAS_FUSION_IN_RESET,
4751 &instance->reset_flags);
4752 instance->instancet->enable_intr(instance);
4754 shost_for_each_device(sdev, shost) {
4755 if ((instance->tgt_prop) &&
4756 (instance->nvme_page_size))
4757 ret_target_prop = megasas_get_target_prop(instance, sdev);
4759 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4760 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4763 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4765 dev_info(&instance->pdev->dev, "Interrupts are enabled and"
4766 " controller is OPERATIONAL for scsi:%d\n",
4767 instance->host->host_no);
4769 /* Restart SR-IOV heartbeat */
4770 if (instance->requestorId) {
4771 if (!megasas_sriov_start_heartbeat(instance, 0))
4772 megasas_start_timer(instance);
4774 instance->skip_heartbeat_timer_del = 1;
4777 if (instance->crash_dump_drv_support &&
4778 instance->crash_dump_app_support)
4779 megasas_set_crash_dump_params(instance,
4780 MR_CRASH_BUF_TURN_ON);
4782 megasas_set_crash_dump_params(instance,
4783 MR_CRASH_BUF_TURN_OFF);
4785 if (instance->snapdump_wait_time) {
4786 megasas_get_snapdump_properties(instance);
4787 dev_info(&instance->pdev->dev,
4788 "Snap dump wait time\t: %d\n",
4789 instance->snapdump_wait_time);
4794 /* Adapter reset completed successfully */
4795 dev_warn(&instance->pdev->dev,
4796 "Reset successful for scsi%d.\n",
4797 instance->host->host_no);
4802 /* Reset failed, kill the adapter */
4803 dev_warn(&instance->pdev->dev, "Reset failed, killing "
4804 "adapter scsi%d.\n", instance->host->host_no);
4805 megaraid_sas_kill_hba(instance);
4806 instance->skip_heartbeat_timer_del = 1;
4809 /* For VF: Restart HB timer if we didn't OCR */
4810 if (instance->requestorId) {
4811 megasas_start_timer(instance);
4813 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4814 instance->instancet->enable_intr(instance);
4815 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4818 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4819 mutex_unlock(&instance->reset_mutex);
4823 /* Fusion Crash dump collection */
4824 void megasas_fusion_crash_dump(struct megasas_instance *instance)
4827 u8 partial_copy = 0;
4831 status_reg = instance->instancet->read_fw_status_reg(instance);
4834 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4835 * to host crash buffers
4837 if (instance->drv_buf_index == 0) {
4838 /* Buffer is already allocated for old Crash dump.
4839 * Do OCR and do not wait for crash dump collection
4841 if (instance->drv_buf_alloc) {
4842 dev_info(&instance->pdev->dev, "earlier crash dump is "
4843 "not yet copied by application, ignoring this "
4844 "crash dump and initiating OCR\n");
4845 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4847 &instance->reg_set->outbound_scratch_pad_0);
4848 readl(&instance->reg_set->outbound_scratch_pad_0);
4851 megasas_alloc_host_crash_buffer(instance);
4852 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4853 "allocated: %d\n", instance->drv_buf_alloc);
4856 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
4857 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
4858 if (!(status_reg & MFI_STATE_DMADONE)) {
4860 * Next crash dump buffer is not yet DMA'd by FW
4861 * Check after 10ms. Wait for 1 second for FW to
4862 * post the next buffer. If not bail out.
4865 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4866 status_reg = instance->instancet->read_fw_status_reg(
4872 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
4873 dev_info(&instance->pdev->dev,
4874 "Driver is done copying the buffer: %d\n",
4875 instance->drv_buf_alloc);
4876 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4880 memcpy(instance->crash_buf[instance->drv_buf_index],
4881 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4882 instance->drv_buf_index++;
4883 status_reg &= ~MFI_STATE_DMADONE;
4886 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4887 readl(&instance->reg_set->outbound_scratch_pad_0);
4889 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4890 status_reg = instance->instancet->read_fw_status_reg(instance);
4893 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4894 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4895 "of copied buffers: %d\n", instance->drv_buf_index);
4896 instance->fw_crash_buffer_size = instance->drv_buf_index;
4897 instance->fw_crash_state = AVAILABLE;
4898 instance->drv_buf_index = 0;
4899 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4900 readl(&instance->reg_set->outbound_scratch_pad_0);
4902 megasas_reset_fusion(instance->host, 0);
4907 /* Fusion OCR work queue */
4908 void megasas_fusion_ocr_wq(struct work_struct *work)
4910 struct megasas_instance *instance =
4911 container_of(work, struct megasas_instance, work_init);
4913 megasas_reset_fusion(instance->host, 0);
4916 /* Allocate fusion context */
4918 megasas_alloc_fusion_context(struct megasas_instance *instance)
4920 struct fusion_context *fusion;
4922 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
4924 if (!instance->ctrl_context) {
4925 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4926 __func__, __LINE__);
4930 fusion = instance->ctrl_context;
4932 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4933 sizeof(LD_SPAN_INFO));
4934 fusion->log_to_span =
4935 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4936 fusion->log_to_span_pages);
4937 if (!fusion->log_to_span) {
4938 fusion->log_to_span =
4939 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4940 sizeof(LD_SPAN_INFO)));
4941 if (!fusion->log_to_span) {
4942 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4943 __func__, __LINE__);
4948 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4949 sizeof(struct LD_LOAD_BALANCE_INFO));
4950 fusion->load_balance_info =
4951 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4952 fusion->load_balance_info_pages);
4953 if (!fusion->load_balance_info) {
4954 fusion->load_balance_info =
4955 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4956 sizeof(struct LD_LOAD_BALANCE_INFO)));
4957 if (!fusion->load_balance_info)
4958 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4959 "continuing without Load Balance support\n");
4966 megasas_free_fusion_context(struct megasas_instance *instance)
4968 struct fusion_context *fusion = instance->ctrl_context;
4971 if (fusion->load_balance_info) {
4972 if (is_vmalloc_addr(fusion->load_balance_info))
4973 vfree(fusion->load_balance_info);
4975 free_pages((ulong)fusion->load_balance_info,
4976 fusion->load_balance_info_pages);
4979 if (fusion->log_to_span) {
4980 if (is_vmalloc_addr(fusion->log_to_span))
4981 vfree(fusion->log_to_span);
4983 free_pages((ulong)fusion->log_to_span,
4984 fusion->log_to_span_pages);
4991 struct megasas_instance_template megasas_instance_template_fusion = {
4992 .enable_intr = megasas_enable_intr_fusion,
4993 .disable_intr = megasas_disable_intr_fusion,
4994 .clear_intr = megasas_clear_intr_fusion,
4995 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4996 .adp_reset = megasas_adp_reset_fusion,
4997 .check_reset = megasas_check_reset_fusion,
4998 .service_isr = megasas_isr_fusion,
4999 .tasklet = megasas_complete_cmd_dpc_fusion,
5000 .init_adapter = megasas_init_adapter_fusion,
5001 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5002 .issue_dcmd = megasas_issue_dcmd_fusion,