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 = readl(®s->outbound_intr_status);
181 writel(status, ®s->outbound_intr_status);
182 readl(®s->outbound_intr_status);
185 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
192 * megasas_get_cmd_fusion - Get a command from the free pool
193 * @instance: Adapter soft state
195 * Returns a blk_tag indexed mpt frame
197 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
198 *instance, u32 blk_tag)
200 struct fusion_context *fusion;
202 fusion = instance->ctrl_context;
203 return fusion->cmd_list[blk_tag];
207 * megasas_return_cmd_fusion - Return a cmd to free command pool
208 * @instance: Adapter soft state
209 * @cmd: Command packet to be returned to free command pool
211 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
212 struct megasas_cmd_fusion *cmd)
215 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
216 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
217 cmd->cmd_completed = false;
221 * megasas_fire_cmd_fusion - Sends command to the FW
222 * @instance: Adapter soft state
223 * @req_desc: 64bit Request descriptor
229 megasas_fire_cmd_fusion(struct megasas_instance *instance,
230 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
232 #if defined(writeq) && defined(CONFIG_64BIT)
233 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
234 le32_to_cpu(req_desc->u.low));
236 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
239 spin_lock_irqsave(&instance->hba_lock, flags);
240 writel(le32_to_cpu(req_desc->u.low),
241 &instance->reg_set->inbound_low_queue_port);
242 writel(le32_to_cpu(req_desc->u.high),
243 &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;
939 struct fusion_context *fusion;
941 u32 msecs = seconds * 1000;
943 fusion = instance->ctrl_context;
945 * Wait for cmd_status to change
947 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
952 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
954 else if (frame_hdr->cmd_status == MFI_STAT_OK)
961 * megasas_ioc_init_fusion - Initializes the FW
962 * @instance: Adapter soft state
964 * Issues the IOC Init cmd
967 megasas_ioc_init_fusion(struct megasas_instance *instance)
969 struct megasas_init_frame *init_frame;
970 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
971 dma_addr_t ioc_init_handle;
972 struct megasas_cmd *cmd;
973 u8 ret, cur_rdpq_mode;
974 struct fusion_context *fusion;
975 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
977 struct megasas_header *frame_hdr;
978 const char *sys_info;
979 MFI_CAPABILITIES *drv_ops;
982 bool cur_fw_64bit_dma_capable;
984 fusion = instance->ctrl_context;
986 ioc_init_handle = fusion->ioc_init_request_phys;
987 IOCInitMessage = fusion->ioc_init_request;
989 cmd = fusion->ioc_init_cmd;
991 scratch_pad_1 = megasas_readl
992 (instance, &instance->reg_set->outbound_scratch_pad_1);
994 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
996 if (instance->adapter_type == INVADER_SERIES) {
997 cur_fw_64bit_dma_capable =
998 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
1000 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1001 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1002 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1003 megaraid_sas_kill_hba(instance);
1009 if (instance->is_rdpq && !cur_rdpq_mode) {
1010 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1011 " from RDPQ mode to non RDPQ mode\n");
1016 instance->fw_sync_cache_support = (scratch_pad_1 &
1017 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1018 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1019 instance->fw_sync_cache_support ? "Yes" : "No");
1021 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1023 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1024 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1025 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1026 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1027 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1029 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1030 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1031 cpu_to_le64(fusion->rdpq_phys) :
1032 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1033 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1034 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1035 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1036 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1037 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1038 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1040 time = ktime_get_real();
1041 /* Convert to milliseconds as per FW requirement */
1042 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1044 init_frame = (struct megasas_init_frame *)cmd->frame;
1045 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1047 frame_hdr = &cmd->frame->hdr;
1048 frame_hdr->cmd_status = 0xFF;
1049 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1051 init_frame->cmd = MFI_CMD_INIT;
1052 init_frame->cmd_status = 0xFF;
1054 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1056 /* driver support Extended MSIX */
1057 if (instance->adapter_type >= INVADER_SERIES)
1058 drv_ops->mfi_capabilities.support_additional_msix = 1;
1059 /* driver supports HA / Remote LUN over Fast Path interface */
1060 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1062 drv_ops->mfi_capabilities.support_max_255lds = 1;
1063 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1064 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1066 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1067 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1069 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1070 if (!dual_qdepth_disable)
1071 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1073 drv_ops->mfi_capabilities.support_qd_throttling = 1;
1074 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1075 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1077 if (instance->consistent_mask_64bit)
1078 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1080 /* Convert capability to LE32 */
1081 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1083 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1084 if (instance->system_info_buf && sys_info) {
1085 memcpy(instance->system_info_buf->systemId, sys_info,
1086 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1087 instance->system_info_buf->systemIdLength =
1088 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1089 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1090 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1093 init_frame->queue_info_new_phys_addr_hi =
1094 cpu_to_le32(upper_32_bits(ioc_init_handle));
1095 init_frame->queue_info_new_phys_addr_lo =
1096 cpu_to_le32(lower_32_bits(ioc_init_handle));
1097 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1099 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1100 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1101 req_desc.MFAIo.RequestFlags =
1102 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1103 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1106 * disable the intr before firing the init frame
1108 instance->instancet->disable_intr(instance);
1110 for (i = 0; i < (10 * 1000); i += 20) {
1111 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1117 megasas_fire_cmd_fusion(instance, &req_desc);
1119 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1121 frame_hdr = &cmd->frame->hdr;
1122 if (frame_hdr->cmd_status != 0) {
1130 dev_err(&instance->pdev->dev,
1131 "Init cmd return status FAILED for SCSI host %d\n",
1132 instance->host->host_no);
1138 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1139 * @instance: Adapter soft state
1140 * @pend: set to 1, if it is pended jbod map.
1142 * Issue Jbod map to the firmware. If it is pended command,
1143 * issue command and return. If it is first instance of jbod map
1144 * issue and receive command.
1147 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1150 struct megasas_cmd *cmd;
1151 struct megasas_dcmd_frame *dcmd;
1152 struct fusion_context *fusion = instance->ctrl_context;
1153 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1154 dma_addr_t pd_seq_h;
1156 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1157 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1158 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
1159 (sizeof(struct MR_PD_CFG_SEQ) *
1160 (MAX_PHYSICAL_DEVICES - 1));
1162 cmd = megasas_get_cmd(instance);
1164 dev_err(&instance->pdev->dev,
1165 "Could not get mfi cmd. Fail from %s %d\n",
1166 __func__, __LINE__);
1170 dcmd = &cmd->frame->dcmd;
1172 memset(pd_sync, 0, pd_seq_map_sz);
1173 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1176 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1177 dcmd->flags = MFI_FRAME_DIR_WRITE;
1178 instance->jbod_seq_cmd = cmd;
1180 dcmd->flags = MFI_FRAME_DIR_READ;
1183 dcmd->cmd = MFI_CMD_DCMD;
1184 dcmd->cmd_status = 0xFF;
1185 dcmd->sge_count = 1;
1188 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1189 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1191 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1194 instance->instancet->issue_dcmd(instance, cmd);
1198 /* Below code is only for non pended DCMD */
1199 if (!instance->mask_interrupts)
1200 ret = megasas_issue_blocked_cmd(instance, cmd,
1201 MFI_IO_TIMEOUT_SECS);
1203 ret = megasas_issue_polled(instance, cmd);
1205 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1206 dev_warn(&instance->pdev->dev,
1207 "driver supports max %d JBOD, but FW reports %d\n",
1208 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1212 if (ret == DCMD_TIMEOUT)
1213 megaraid_sas_kill_hba(instance);
1215 if (ret == DCMD_SUCCESS)
1216 instance->pd_seq_map_id++;
1218 megasas_return_cmd(instance, cmd);
1223 * megasas_get_ld_map_info - Returns FW's ld_map structure
1224 * @instance: Adapter soft state
1225 * @pend: Pend the command or not
1226 * Issues an internal command (DCMD) to get the FW's controller PD
1227 * list structure. This information is mainly used to find out SYSTEM
1228 * supported by the FW.
1229 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1230 * dcmd.mbox.b[0] - number of LDs being sync'd
1231 * dcmd.mbox.b[1] - 0 - complete command immediately.
1232 * - 1 - pend till config change
1233 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1234 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1235 * uses extended struct MR_FW_RAID_MAP_EXT
1238 megasas_get_ld_map_info(struct megasas_instance *instance)
1241 struct megasas_cmd *cmd;
1242 struct megasas_dcmd_frame *dcmd;
1244 dma_addr_t ci_h = 0;
1246 struct fusion_context *fusion;
1248 cmd = megasas_get_cmd(instance);
1251 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1255 fusion = instance->ctrl_context;
1258 megasas_return_cmd(instance, cmd);
1262 dcmd = &cmd->frame->dcmd;
1264 size_map_info = fusion->current_map_sz;
1266 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1267 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1270 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1271 megasas_return_cmd(instance, cmd);
1275 memset(ci, 0, fusion->max_map_sz);
1276 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1277 dcmd->cmd = MFI_CMD_DCMD;
1278 dcmd->cmd_status = 0xFF;
1279 dcmd->sge_count = 1;
1280 dcmd->flags = MFI_FRAME_DIR_READ;
1283 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1284 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1286 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1288 if (!instance->mask_interrupts)
1289 ret = megasas_issue_blocked_cmd(instance, cmd,
1290 MFI_IO_TIMEOUT_SECS);
1292 ret = megasas_issue_polled(instance, cmd);
1294 if (ret == DCMD_TIMEOUT)
1295 megaraid_sas_kill_hba(instance);
1297 megasas_return_cmd(instance, cmd);
1303 megasas_get_map_info(struct megasas_instance *instance)
1305 struct fusion_context *fusion = instance->ctrl_context;
1307 fusion->fast_path_io = 0;
1308 if (!megasas_get_ld_map_info(instance)) {
1309 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1310 fusion->fast_path_io = 1;
1318 * megasas_sync_map_info - Returns FW's ld_map structure
1319 * @instance: Adapter soft state
1321 * Issues an internal command (DCMD) to get the FW's controller PD
1322 * list structure. This information is mainly used to find out SYSTEM
1323 * supported by the FW.
1326 megasas_sync_map_info(struct megasas_instance *instance)
1329 struct megasas_cmd *cmd;
1330 struct megasas_dcmd_frame *dcmd;
1333 struct fusion_context *fusion;
1334 struct MR_LD_TARGET_SYNC *ci = NULL;
1335 struct MR_DRV_RAID_MAP_ALL *map;
1336 struct MR_LD_RAID *raid;
1337 struct MR_LD_TARGET_SYNC *ld_sync;
1338 dma_addr_t ci_h = 0;
1341 cmd = megasas_get_cmd(instance);
1344 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1348 fusion = instance->ctrl_context;
1351 megasas_return_cmd(instance, cmd);
1355 map = fusion->ld_drv_map[instance->map_id & 1];
1357 num_lds = le16_to_cpu(map->raidMap.ldCount);
1359 dcmd = &cmd->frame->dcmd;
1361 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1363 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1365 ci = (struct MR_LD_TARGET_SYNC *)
1366 fusion->ld_map[(instance->map_id - 1) & 1];
1367 memset(ci, 0, fusion->max_map_sz);
1369 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1371 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1373 for (i = 0; i < num_lds; i++, ld_sync++) {
1374 raid = MR_LdRaidGet(i, map);
1375 ld_sync->targetId = MR_GetLDTgtId(i, map);
1376 ld_sync->seqNum = raid->seqNum;
1379 size_map_info = fusion->current_map_sz;
1381 dcmd->cmd = MFI_CMD_DCMD;
1382 dcmd->cmd_status = 0xFF;
1383 dcmd->sge_count = 1;
1384 dcmd->flags = MFI_FRAME_DIR_WRITE;
1387 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1388 dcmd->mbox.b[0] = num_lds;
1389 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1390 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1392 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1394 instance->map_update_cmd = cmd;
1396 instance->instancet->issue_dcmd(instance, cmd);
1402 * meagasas_display_intel_branding - Display branding string
1403 * @instance: per adapter object
1408 megasas_display_intel_branding(struct megasas_instance *instance)
1410 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1413 switch (instance->pdev->device) {
1414 case PCI_DEVICE_ID_LSI_INVADER:
1415 switch (instance->pdev->subsystem_device) {
1416 case MEGARAID_INTEL_RS3DC080_SSDID:
1417 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1418 instance->host->host_no,
1419 MEGARAID_INTEL_RS3DC080_BRANDING);
1421 case MEGARAID_INTEL_RS3DC040_SSDID:
1422 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1423 instance->host->host_no,
1424 MEGARAID_INTEL_RS3DC040_BRANDING);
1426 case MEGARAID_INTEL_RS3SC008_SSDID:
1427 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1428 instance->host->host_no,
1429 MEGARAID_INTEL_RS3SC008_BRANDING);
1431 case MEGARAID_INTEL_RS3MC044_SSDID:
1432 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1433 instance->host->host_no,
1434 MEGARAID_INTEL_RS3MC044_BRANDING);
1440 case PCI_DEVICE_ID_LSI_FURY:
1441 switch (instance->pdev->subsystem_device) {
1442 case MEGARAID_INTEL_RS3WC080_SSDID:
1443 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1444 instance->host->host_no,
1445 MEGARAID_INTEL_RS3WC080_BRANDING);
1447 case MEGARAID_INTEL_RS3WC040_SSDID:
1448 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1449 instance->host->host_no,
1450 MEGARAID_INTEL_RS3WC040_BRANDING);
1456 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1457 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1458 switch (instance->pdev->subsystem_device) {
1459 case MEGARAID_INTEL_RMS3BC160_SSDID:
1460 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1461 instance->host->host_no,
1462 MEGARAID_INTEL_RMS3BC160_BRANDING);
1474 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1475 * @instance: Adapter soft state
1477 * return: if success: return 0
1478 * failed: return -ENOMEM
1480 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1482 struct fusion_context *fusion;
1485 fusion = instance->ctrl_context;
1487 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1489 for (i = 0; i < 2; i++) {
1490 fusion->ld_map[i] = NULL;
1492 fusion->ld_drv_map[i] = (void *)
1493 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1494 fusion->drv_map_pages);
1496 if (!fusion->ld_drv_map[i]) {
1497 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1499 if (!fusion->ld_drv_map[i]) {
1500 dev_err(&instance->pdev->dev,
1501 "Could not allocate memory for local map"
1502 " size requested: %d\n",
1503 fusion->drv_map_sz);
1504 goto ld_drv_map_alloc_fail;
1509 for (i = 0; i < 2; i++) {
1510 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1512 &fusion->ld_map_phys[i],
1514 if (!fusion->ld_map[i]) {
1515 dev_err(&instance->pdev->dev,
1516 "Could not allocate memory for map info %s:%d\n",
1517 __func__, __LINE__);
1518 goto ld_map_alloc_fail;
1525 for (i = 0; i < 2; i++) {
1526 if (fusion->ld_map[i])
1527 dma_free_coherent(&instance->pdev->dev,
1530 fusion->ld_map_phys[i]);
1533 ld_drv_map_alloc_fail:
1534 for (i = 0; i < 2; i++) {
1535 if (fusion->ld_drv_map[i]) {
1536 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1537 vfree(fusion->ld_drv_map[i]);
1539 free_pages((ulong)fusion->ld_drv_map[i],
1540 fusion->drv_map_pages);
1548 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1550 * IO request frame queue, set can_queue.
1551 * @instance: Adapter soft state
1555 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1557 struct fusion_context *fusion;
1560 fusion = instance->ctrl_context;
1561 max_cmd = instance->max_fw_cmds;
1563 if (instance->adapter_type >= VENTURA_SERIES)
1564 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1566 instance->max_mpt_cmds = instance->max_fw_cmds;
1568 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1569 instance->cur_can_queue = instance->max_scsi_cmds;
1570 instance->host->can_queue = instance->cur_can_queue;
1572 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1574 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1575 instance->max_mpt_cmds;
1576 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1577 (fusion->reply_q_depth);
1578 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1579 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1580 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1583 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1585 struct fusion_context *fusion;
1586 struct megasas_cmd *cmd;
1588 fusion = instance->ctrl_context;
1590 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1593 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1594 __func__, __LINE__);
1598 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1599 IOC_INIT_FRAME_SIZE,
1600 &cmd->frame_phys_addr, GFP_KERNEL);
1603 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1604 __func__, __LINE__);
1609 fusion->ioc_init_cmd = cmd;
1614 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1615 * @instance: Adapter soft state
1617 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1619 struct fusion_context *fusion;
1621 fusion = instance->ctrl_context;
1623 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1624 dma_free_coherent(&instance->pdev->dev,
1625 IOC_INIT_FRAME_SIZE,
1626 fusion->ioc_init_cmd->frame,
1627 fusion->ioc_init_cmd->frame_phys_addr);
1629 kfree(fusion->ioc_init_cmd);
1633 * megasas_init_adapter_fusion - Initializes the FW
1634 * @instance: Adapter soft state
1636 * This is the main function for initializing firmware.
1639 megasas_init_adapter_fusion(struct megasas_instance *instance)
1641 struct megasas_register_set __iomem *reg_set;
1642 struct fusion_context *fusion;
1646 fusion = instance->ctrl_context;
1648 reg_set = instance->reg_set;
1650 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1653 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1655 instance->max_mfi_cmds =
1656 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1658 megasas_configure_queue_sizes(instance);
1660 scratch_pad_1 = megasas_readl(instance,
1661 &instance->reg_set->outbound_scratch_pad_1);
1662 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1663 * Firmware support extended IO chain frame which is 4 times more than
1665 * Legacy Firmware - Frame size is (8 * 128) = 1K
1666 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1668 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1669 instance->max_chain_frame_sz =
1670 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1671 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1673 instance->max_chain_frame_sz =
1674 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1675 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1677 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1678 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1679 instance->max_chain_frame_sz,
1680 MEGASAS_CHAIN_FRAME_SZ_MIN);
1681 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1684 fusion->max_sge_in_main_msg =
1685 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1686 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1688 fusion->max_sge_in_chain =
1689 instance->max_chain_frame_sz
1690 / sizeof(union MPI2_SGE_IO_UNION);
1692 instance->max_num_sge =
1693 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1694 + fusion->max_sge_in_chain - 2);
1696 /* Used for pass thru MFI frame (DCMD) */
1697 fusion->chain_offset_mfi_pthru =
1698 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1700 fusion->chain_offset_io_request =
1701 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1702 sizeof(union MPI2_SGE_IO_UNION))/16;
1704 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1705 for (i = 0 ; i < count; i++)
1706 fusion->last_reply_idx[i] = 0;
1709 * For fusion adapters, 3 commands for IOCTL and 8 commands
1710 * for driver's internal DCMDs.
1712 instance->max_scsi_cmds = instance->max_fw_cmds -
1713 (MEGASAS_FUSION_INTERNAL_CMDS +
1714 MEGASAS_FUSION_IOCTL_CMDS);
1715 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1717 if (megasas_alloc_ioc_init_frame(instance))
1721 * Allocate memory for descriptors
1722 * Create a pool of commands
1724 if (megasas_alloc_cmds(instance))
1725 goto fail_alloc_mfi_cmds;
1726 if (megasas_alloc_cmds_fusion(instance))
1727 goto fail_alloc_cmds;
1729 if (megasas_ioc_init_fusion(instance))
1732 megasas_display_intel_branding(instance);
1733 if (megasas_get_ctrl_info(instance)) {
1734 dev_err(&instance->pdev->dev,
1735 "Could not get controller info. Fail from %s %d\n",
1736 __func__, __LINE__);
1740 instance->flag_ieee = 1;
1741 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1742 fusion->fast_path_io = 0;
1744 if (megasas_allocate_raid_maps(instance))
1747 if (!megasas_get_map_info(instance))
1748 megasas_sync_map_info(instance);
1753 megasas_free_cmds_fusion(instance);
1755 megasas_free_cmds(instance);
1756 fail_alloc_mfi_cmds:
1757 megasas_free_ioc_init_cmd(instance);
1762 * megasas_fault_detect_work - Worker function of
1763 * FW fault handling workqueue.
1766 megasas_fault_detect_work(struct work_struct *work)
1768 struct megasas_instance *instance =
1769 container_of(work, struct megasas_instance,
1770 fw_fault_work.work);
1771 u32 fw_state, dma_state, status;
1773 /* Check the fw state */
1774 fw_state = instance->instancet->read_fw_status_reg(instance) &
1777 if (fw_state == MFI_STATE_FAULT) {
1778 dma_state = instance->instancet->read_fw_status_reg(instance) &
1780 /* Start collecting crash, if DMA bit is done */
1781 if (instance->crash_dump_drv_support &&
1782 instance->crash_dump_app_support && dma_state) {
1783 megasas_fusion_crash_dump(instance);
1785 if (instance->unload == 0) {
1786 status = megasas_reset_fusion(instance->host, 0);
1787 if (status != SUCCESS) {
1788 dev_err(&instance->pdev->dev,
1789 "Failed from %s %d, do not re-arm timer\n",
1790 __func__, __LINE__);
1797 if (instance->fw_fault_work_q)
1798 queue_delayed_work(instance->fw_fault_work_q,
1799 &instance->fw_fault_work,
1800 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1804 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1806 /* Check if the Fault WQ is already started */
1807 if (instance->fw_fault_work_q)
1810 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1812 snprintf(instance->fault_handler_work_q_name,
1813 sizeof(instance->fault_handler_work_q_name),
1814 "poll_megasas%d_status", instance->host->host_no);
1816 instance->fw_fault_work_q =
1817 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1818 if (!instance->fw_fault_work_q) {
1819 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1820 __func__, __LINE__);
1824 queue_delayed_work(instance->fw_fault_work_q,
1825 &instance->fw_fault_work,
1826 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1832 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1834 struct workqueue_struct *wq;
1836 if (instance->fw_fault_work_q) {
1837 wq = instance->fw_fault_work_q;
1838 instance->fw_fault_work_q = NULL;
1839 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1840 flush_workqueue(wq);
1841 destroy_workqueue(wq);
1846 * map_cmd_status - Maps FW cmd status to OS cmd status
1847 * @cmd : Pointer to cmd
1848 * @status : status of cmd returned by FW
1849 * @ext_status : ext status of cmd returned by FW
1853 map_cmd_status(struct fusion_context *fusion,
1854 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1855 u32 data_length, u8 *sense)
1860 cmd_type = megasas_cmd_type(scmd);
1864 scmd->result = DID_OK << 16;
1867 case MFI_STAT_SCSI_IO_FAILED:
1868 case MFI_STAT_LD_INIT_IN_PROGRESS:
1869 scmd->result = (DID_ERROR << 16) | ext_status;
1872 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1874 scmd->result = (DID_OK << 16) | ext_status;
1875 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1876 memset(scmd->sense_buffer, 0,
1877 SCSI_SENSE_BUFFERSIZE);
1878 memcpy(scmd->sense_buffer, sense,
1879 SCSI_SENSE_BUFFERSIZE);
1880 scmd->result |= DRIVER_SENSE << 24;
1884 * If the IO request is partially completed, then MR FW will
1885 * update "io_request->DataLength" field with actual number of
1886 * bytes transferred.Driver will set residual bytes count in
1887 * SCSI command structure.
1889 resid = (scsi_bufflen(scmd) - data_length);
1890 scsi_set_resid(scmd, resid);
1893 ((cmd_type == READ_WRITE_LDIO) ||
1894 (cmd_type == READ_WRITE_SYSPDIO)))
1895 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1896 " requested/completed 0x%x/0x%x\n",
1897 status, scsi_bufflen(scmd), data_length);
1900 case MFI_STAT_LD_OFFLINE:
1901 case MFI_STAT_DEVICE_NOT_FOUND:
1902 scmd->result = DID_BAD_TARGET << 16;
1904 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1905 scmd->result = DID_IMM_RETRY << 16;
1908 scmd->result = DID_ERROR << 16;
1914 * megasas_is_prp_possible -
1915 * Checks if native NVMe PRPs can be built for the IO
1917 * @instance: Adapter soft state
1918 * @scmd: SCSI command from the mid-layer
1919 * @sge_count: scatter gather element count.
1921 * Returns: true: PRPs can be built
1922 * false: IEEE SGLs needs to be built
1925 megasas_is_prp_possible(struct megasas_instance *instance,
1926 struct scsi_cmnd *scmd, int sge_count)
1928 struct fusion_context *fusion;
1930 u32 data_length = 0;
1931 struct scatterlist *sg_scmd;
1932 bool build_prp = false;
1933 u32 mr_nvme_pg_size;
1935 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1936 MR_DEFAULT_NVME_PAGE_SIZE);
1937 fusion = instance->ctrl_context;
1938 data_length = scsi_bufflen(scmd);
1939 sg_scmd = scsi_sglist(scmd);
1942 * NVMe uses one PRP for each page (or part of a page)
1943 * look at the data length - if 4 pages or less then IEEE is OK
1944 * if > 5 pages then we need to build a native SGL
1945 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1946 * if this first size in the page is >= the residual beyond 4 pages
1947 * then use IEEE, otherwise use native SGL
1950 if (data_length > (mr_nvme_pg_size * 5)) {
1952 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1953 (data_length <= (mr_nvme_pg_size * 5))) {
1954 /* check if 1st SG entry size is < residual beyond 4 pages */
1955 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1960 * Below code detects gaps/holes in IO data buffers.
1961 * What does holes/gaps mean?
1962 * Any SGE except first one in a SGL starts at non NVME page size
1963 * aligned address OR Any SGE except last one in a SGL ends at
1964 * non NVME page size boundary.
1966 * Driver has already informed block layer by setting boundary rules for
1967 * bio merging done at NVME page size boundary calling kernel API
1968 * blk_queue_virt_boundary inside slave_config.
1969 * Still there is possibility of IO coming with holes to driver because of
1970 * IO merging done by IO scheduler.
1972 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1973 * IO scheduling so no IO merging.
1975 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1976 * then sending IOs with holes.
1978 * Though driver can request block layer to disable IO merging by calling-
1979 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1980 * user may tune sysfs parameter- nomerges again to 0 or 1.
1982 * If in future IO scheduling is enabled with SCSI BLK MQ,
1983 * this algorithm to detect holes will be required in driver
1984 * for SCSI BLK MQ enabled case as well.
1988 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1989 if ((i != 0) && (i != (sge_count - 1))) {
1990 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1991 mega_mod64(sg_dma_address(sg_scmd),
1994 atomic_inc(&instance->sge_holes_type1);
1999 if ((sge_count > 1) && (i == 0)) {
2000 if ((mega_mod64((sg_dma_address(sg_scmd) +
2001 sg_dma_len(sg_scmd)),
2002 mr_nvme_pg_size))) {
2004 atomic_inc(&instance->sge_holes_type2);
2009 if ((sge_count > 1) && (i == (sge_count - 1))) {
2010 if (mega_mod64(sg_dma_address(sg_scmd),
2013 atomic_inc(&instance->sge_holes_type3);
2023 * megasas_make_prp_nvme -
2024 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2026 * @instance: Adapter soft state
2027 * @scmd: SCSI command from the mid-layer
2028 * @sgl_ptr: SGL to be filled in
2029 * @cmd: Fusion command frame
2030 * @sge_count: scatter gather element count.
2032 * Returns: true: PRPs are built
2033 * false: IEEE SGLs needs to be built
2036 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2037 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2038 struct megasas_cmd_fusion *cmd, int sge_count)
2040 int sge_len, offset, num_prp_in_chain = 0;
2041 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2043 dma_addr_t ptr_sgl_phys;
2045 u32 page_mask, page_mask_result;
2046 struct scatterlist *sg_scmd;
2048 bool build_prp = false;
2049 int data_len = scsi_bufflen(scmd);
2050 struct fusion_context *fusion;
2051 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2052 MR_DEFAULT_NVME_PAGE_SIZE);
2054 fusion = instance->ctrl_context;
2056 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2062 * Nvme has a very convoluted prp format. One prp is required
2063 * for each page or partial page. Driver need to split up OS sg_list
2064 * entries if it is longer than one page or cross a page
2065 * boundary. Driver also have to insert a PRP list pointer entry as
2066 * the last entry in each physical page of the PRP list.
2068 * NOTE: The first PRP "entry" is actually placed in the first
2069 * SGL entry in the main message as IEEE 64 format. The 2nd
2070 * entry in the main message is the chain element, and the rest
2071 * of the PRP entries are built in the contiguous pcie buffer.
2073 page_mask = mr_nvme_pg_size - 1;
2074 ptr_sgl = (u64 *)cmd->sg_frame;
2075 ptr_sgl_phys = cmd->sg_frame_phys_addr;
2076 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2078 /* Build chain frame element which holds all prps except first*/
2079 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2080 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2082 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2083 main_chain_element->NextChainOffset = 0;
2084 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2085 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2086 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2088 /* Build first prp, sge need not to be page aligned*/
2089 ptr_first_sgl = sgl_ptr;
2090 sg_scmd = scsi_sglist(scmd);
2091 sge_addr = sg_dma_address(sg_scmd);
2092 sge_len = sg_dma_len(sg_scmd);
2094 offset = (u32)(sge_addr & page_mask);
2095 first_prp_len = mr_nvme_pg_size - offset;
2097 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2098 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2100 data_len -= first_prp_len;
2102 if (sge_len > first_prp_len) {
2103 sge_addr += first_prp_len;
2104 sge_len -= first_prp_len;
2105 } else if (sge_len == first_prp_len) {
2106 sg_scmd = sg_next(sg_scmd);
2107 sge_addr = sg_dma_address(sg_scmd);
2108 sge_len = sg_dma_len(sg_scmd);
2112 offset = (u32)(sge_addr & page_mask);
2114 /* Put PRP pointer due to page boundary*/
2115 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2116 if (unlikely(!page_mask_result)) {
2117 scmd_printk(KERN_NOTICE,
2118 scmd, "page boundary ptr_sgl: 0x%p\n",
2121 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2126 *ptr_sgl = cpu_to_le64(sge_addr);
2131 sge_addr += mr_nvme_pg_size;
2132 sge_len -= mr_nvme_pg_size;
2133 data_len -= mr_nvme_pg_size;
2141 sg_scmd = sg_next(sg_scmd);
2142 sge_addr = sg_dma_address(sg_scmd);
2143 sge_len = sg_dma_len(sg_scmd);
2146 main_chain_element->Length =
2147 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2149 atomic_inc(&instance->prp_sgl);
2154 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2155 * @instance: Adapter soft state
2156 * @scp: SCSI command from the mid-layer
2157 * @sgl_ptr: SGL to be filled in
2158 * @cmd: cmd we are working on
2159 * @sge_count sge count
2163 megasas_make_sgl_fusion(struct megasas_instance *instance,
2164 struct scsi_cmnd *scp,
2165 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2166 struct megasas_cmd_fusion *cmd, int sge_count)
2168 int i, sg_processed;
2169 struct scatterlist *os_sgl;
2170 struct fusion_context *fusion;
2172 fusion = instance->ctrl_context;
2174 if (instance->adapter_type >= INVADER_SERIES) {
2175 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2176 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2177 sgl_ptr_end->Flags = 0;
2180 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2181 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2182 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2184 if (instance->adapter_type >= INVADER_SERIES)
2185 if (i == sge_count - 1)
2186 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2188 sg_processed = i + 1;
2190 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2191 (sge_count > fusion->max_sge_in_main_msg)) {
2193 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2194 if (instance->adapter_type >= INVADER_SERIES) {
2195 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2196 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2197 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2198 cmd->io_request->ChainOffset =
2200 chain_offset_io_request;
2202 cmd->io_request->ChainOffset = 0;
2204 cmd->io_request->ChainOffset =
2205 fusion->chain_offset_io_request;
2208 /* Prepare chain element */
2209 sg_chain->NextChainOffset = 0;
2210 if (instance->adapter_type >= INVADER_SERIES)
2211 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2214 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2215 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2216 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2217 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2220 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2221 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2224 atomic_inc(&instance->ieee_sgl);
2228 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2229 * @scp: SCSI command pointer
2230 * @instance: Soft instance of controller
2231 * @cmd: Fusion command pointer
2233 * This function will build sgls based on device type.
2234 * For nvme drives, there is different way of building sgls in nvme native
2235 * format- PRPs(Physical Region Page).
2237 * Returns the number of sg lists actually used, zero if the sg lists
2238 * is NULL, or -ENOMEM if the mapping failed
2241 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2242 struct megasas_cmd_fusion *cmd)
2245 bool build_prp = false;
2246 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2248 sge_count = scsi_dma_map(scp);
2250 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2253 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2254 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2255 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2256 (cmd->pd_interface == NVME_PD))
2257 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2261 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2268 * megasas_set_pd_lba - Sets PD LBA
2270 * @cdb_len: cdb length
2271 * @start_blk: Start block of IO
2273 * Used to set the PD LBA in CDB for FP IOs
2276 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2277 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2278 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2280 struct MR_LD_RAID *raid;
2282 u64 start_blk = io_info->pdBlock;
2283 u8 *cdb = io_request->CDB.CDB32;
2284 u32 num_blocks = io_info->numBlocks;
2285 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2287 /* Check if T10 PI (DIF) is enabled for this LD */
2288 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2289 raid = MR_LdRaidGet(ld, local_map_ptr);
2290 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2291 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2292 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2293 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2295 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2296 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2298 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2299 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2302 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2303 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2304 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2305 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2306 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2307 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2308 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2309 cdb[19] = (u8)(start_blk & 0xff);
2311 /* Logical block reference tag */
2312 io_request->CDB.EEDP32.PrimaryReferenceTag =
2313 cpu_to_be32(ref_tag);
2314 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2315 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2317 /* Transfer length */
2318 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2319 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2320 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2321 cdb[31] = (u8)(num_blocks & 0xff);
2323 /* set SCSI IO EEDPFlags */
2324 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2325 io_request->EEDPFlags = cpu_to_le16(
2326 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2327 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2328 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2329 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2330 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2331 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2333 io_request->EEDPFlags = cpu_to_le16(
2334 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2335 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2337 io_request->Control |= cpu_to_le32((0x4 << 26));
2338 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2340 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2341 if (((cdb_len == 12) || (cdb_len == 16)) &&
2342 (start_blk <= 0xffffffff)) {
2343 if (cdb_len == 16) {
2344 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2349 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2355 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2362 /* Transfer length */
2363 cdb[8] = (u8)(num_blocks & 0xff);
2364 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2366 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2368 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2369 /* Convert to 16 byte CDB for large LBA's */
2372 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2377 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2384 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2391 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2398 /* Transfer length */
2399 cdb[13] = (u8)(num_blocks & 0xff);
2400 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2401 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2402 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2404 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2408 /* Normal case, just load LBA here */
2412 u8 val = cdb[1] & 0xE0;
2413 cdb[3] = (u8)(start_blk & 0xff);
2414 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2415 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2419 cdb[5] = (u8)(start_blk & 0xff);
2420 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2421 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2422 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2425 cdb[5] = (u8)(start_blk & 0xff);
2426 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2427 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2428 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2431 cdb[9] = (u8)(start_blk & 0xff);
2432 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2433 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2434 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2435 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2436 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2437 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2438 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2445 * megasas_stream_detect - stream detection on read and and write IOs
2446 * @instance: Adapter soft state
2447 * @cmd: Command to be prepared
2448 * @io_info: IO Request info
2452 /** stream detection on read and and write IOs */
2453 static void megasas_stream_detect(struct megasas_instance *instance,
2454 struct megasas_cmd_fusion *cmd,
2455 struct IO_REQUEST_INFO *io_info)
2457 struct fusion_context *fusion = instance->ctrl_context;
2458 u32 device_id = io_info->ldTgtId;
2459 struct LD_STREAM_DETECT *current_ld_sd
2460 = fusion->stream_detect_by_ld[device_id];
2461 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2462 u32 shifted_values, unshifted_values;
2463 u32 index_value_mask, shifted_values_mask;
2465 bool is_read_ahead = false;
2466 struct STREAM_DETECT *current_sd;
2467 /* find possible stream */
2468 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2469 stream_num = (*track_stream >>
2470 (i * BITS_PER_INDEX_STREAM)) &
2472 current_sd = ¤t_ld_sd->stream_track[stream_num];
2473 /* if we found a stream, update the raid
2474 * context and also update the mruBitMap
2476 /* boundary condition */
2477 if ((current_sd->next_seq_lba) &&
2478 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2479 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2480 (current_sd->is_read == io_info->isRead)) {
2482 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2483 ((!io_info->isRead) || (!is_read_ahead)))
2485 * Once the API availible we need to change this.
2486 * At this point we are not allowing any gap
2490 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2491 current_sd->next_seq_lba =
2492 io_info->ldStartBlock + io_info->numBlocks;
2494 * update the mruBitMap LRU
2496 shifted_values_mask =
2497 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2498 shifted_values = ((*track_stream & shifted_values_mask)
2499 << BITS_PER_INDEX_STREAM);
2501 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2503 *track_stream & ~(shifted_values_mask |
2506 unshifted_values | shifted_values | stream_num;
2511 * if we did not find any stream, create a new one
2512 * from the least recently used
2514 stream_num = (*track_stream >>
2515 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2517 current_sd = ¤t_ld_sd->stream_track[stream_num];
2518 current_sd->is_read = io_info->isRead;
2519 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2520 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2525 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2526 * affinity (cpu of the controller) and raid_flags in the raid context
2529 * @praid_context: IO RAID context
2530 * @raid: LD raid map
2531 * @fp_possible: Is fast path possible?
2532 * @is_read: Is read IO?
2536 megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2537 struct MR_LD_RAID *raid, bool fp_possible,
2538 u8 is_read, u32 scsi_buff_len)
2540 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2541 struct RAID_CONTEXT_G35 *rctx_g35;
2543 rctx_g35 = &praid_context->raid_context_g35;
2546 if ((raid->cpuAffinity.pdRead.cpu0) &&
2547 (raid->cpuAffinity.pdRead.cpu1))
2548 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2549 else if (raid->cpuAffinity.pdRead.cpu1)
2550 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2552 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2553 (raid->cpuAffinity.pdWrite.cpu1))
2554 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2555 else if (raid->cpuAffinity.pdWrite.cpu1)
2556 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2557 /* Fast path cache by pass capable R0/R1 VD */
2558 if ((raid->level <= 1) &&
2559 (raid->capability.fp_cache_bypass_capable)) {
2560 rctx_g35->routing_flags |=
2561 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2562 rctx_g35->raid_flags =
2563 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2564 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2569 if ((raid->cpuAffinity.ldRead.cpu0) &&
2570 (raid->cpuAffinity.ldRead.cpu1))
2571 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2572 else if (raid->cpuAffinity.ldRead.cpu1)
2573 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2575 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2576 (raid->cpuAffinity.ldWrite.cpu1))
2577 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2578 else if (raid->cpuAffinity.ldWrite.cpu1)
2579 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2581 if (is_stream_detected(rctx_g35) &&
2582 ((raid->level == 5) || (raid->level == 6)) &&
2583 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2584 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2585 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2589 rctx_g35->routing_flags |=
2590 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2592 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2593 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2594 * IO Subtype is not bitmap.
2596 if ((raid->level == 1) && (!is_read)) {
2597 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2598 praid_context->raid_context_g35.raid_flags =
2599 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2600 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2605 * megasas_build_ldio_fusion - Prepares IOs to devices
2606 * @instance: Adapter soft state
2607 * @scp: SCSI command
2608 * @cmd: Command to be prepared
2610 * Prepares the io_request and chain elements (sg_frame) for IO
2611 * The IO can be for PD (Fast Path) or LD
2614 megasas_build_ldio_fusion(struct megasas_instance *instance,
2615 struct scsi_cmnd *scp,
2616 struct megasas_cmd_fusion *cmd)
2620 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2622 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2623 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2624 struct IO_REQUEST_INFO io_info;
2625 struct fusion_context *fusion;
2626 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2628 unsigned long spinlock_flags;
2629 struct MR_LD_RAID *raid = NULL;
2630 struct MR_PRIV_DEVICE *mrdev_priv;
2631 struct RAID_CONTEXT *rctx;
2632 struct RAID_CONTEXT_G35 *rctx_g35;
2634 device_id = MEGASAS_DEV_INDEX(scp);
2636 fusion = instance->ctrl_context;
2638 io_request = cmd->io_request;
2639 rctx = &io_request->RaidContext.raid_context;
2640 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2642 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2644 rctx->ex_status = 0;
2646 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2650 fp_possible = false;
2653 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2655 if (scp->cmd_len == 6) {
2656 datalength = (u32) scp->cmnd[4];
2657 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2658 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2660 start_lba_lo &= 0x1FFFFF;
2664 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2666 else if (scp->cmd_len == 10) {
2667 datalength = (u32) scp->cmnd[8] |
2668 ((u32) scp->cmnd[7] << 8);
2669 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2670 ((u32) scp->cmnd[3] << 16) |
2671 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2675 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2677 else if (scp->cmd_len == 12) {
2678 datalength = ((u32) scp->cmnd[6] << 24) |
2679 ((u32) scp->cmnd[7] << 16) |
2680 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2681 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2682 ((u32) scp->cmnd[3] << 16) |
2683 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2687 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2689 else if (scp->cmd_len == 16) {
2690 datalength = ((u32) scp->cmnd[10] << 24) |
2691 ((u32) scp->cmnd[11] << 16) |
2692 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2693 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2694 ((u32) scp->cmnd[7] << 16) |
2695 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2697 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2698 ((u32) scp->cmnd[3] << 16) |
2699 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2702 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2703 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2704 io_info.numBlocks = datalength;
2705 io_info.ldTgtId = device_id;
2706 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2707 scsi_buff_len = scsi_bufflen(scp);
2708 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2710 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2713 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2714 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2716 if (ld < instance->fw_supported_vd_count)
2717 raid = MR_LdRaidGet(ld, local_map_ptr);
2719 if (!raid || (!fusion->fast_path_io)) {
2720 rctx->reg_lock_flags = 0;
2721 fp_possible = false;
2723 if (MR_BuildRaidContext(instance, &io_info, rctx,
2724 local_map_ptr, &raidLUN))
2725 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2728 cmd->request_desc->SCSIIO.MSIxIndex =
2729 instance->reply_map[raw_smp_processor_id()];
2731 if (instance->adapter_type >= VENTURA_SERIES) {
2732 /* FP for Optimal raid level 1.
2733 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2734 * are built by the driver as LD I/Os.
2735 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2736 * (there is never a reason to process these as buffered writes)
2737 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2738 * with the SLD bit asserted.
2740 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2741 mrdev_priv = scp->device->hostdata;
2743 if (atomic_inc_return(&instance->fw_outstanding) >
2744 (instance->host->can_queue)) {
2745 fp_possible = false;
2746 atomic_dec(&instance->fw_outstanding);
2747 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2748 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
2749 fp_possible = false;
2750 atomic_dec(&instance->fw_outstanding);
2751 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2752 atomic_set(&mrdev_priv->r1_ldio_hint,
2753 instance->r1_ldio_hint_default);
2758 (io_info.isRead && io_info.ra_capable)) {
2759 spin_lock_irqsave(&instance->stream_lock,
2761 megasas_stream_detect(instance, cmd, &io_info);
2762 spin_unlock_irqrestore(&instance->stream_lock,
2764 /* In ventura if stream detected for a read and it is
2765 * read ahead capable make this IO as LDIO
2767 if (is_stream_detected(rctx_g35))
2768 fp_possible = false;
2771 /* If raid is NULL, set CPU affinity to default CPU0 */
2773 megasas_set_raidflag_cpu_affinity(&io_request->RaidContext,
2774 raid, fp_possible, io_info.isRead,
2777 rctx_g35->routing_flags |=
2778 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2782 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2783 local_map_ptr, start_lba_lo);
2784 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2785 cmd->request_desc->SCSIIO.RequestFlags =
2786 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2787 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2788 if (instance->adapter_type == INVADER_SERIES) {
2789 if (rctx->reg_lock_flags == REGION_TYPE_UNUSED)
2790 cmd->request_desc->SCSIIO.RequestFlags =
2791 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2792 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2793 rctx->type = MPI2_TYPE_CUDA;
2795 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2796 rctx->reg_lock_flags |=
2797 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2798 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2799 } else if (instance->adapter_type >= VENTURA_SERIES) {
2800 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2801 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2802 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2803 io_request->IoFlags |=
2804 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2806 if (fusion->load_balance_info &&
2807 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2810 get_updated_dev_handle(instance,
2811 &fusion->load_balance_info[device_id],
2812 &io_info, local_map_ptr);
2813 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2814 cmd->pd_r1_lb = io_info.pd_after_lb;
2815 if (instance->adapter_type >= VENTURA_SERIES)
2816 rctx_g35->span_arm = io_info.span_arm;
2818 rctx->span_arm = io_info.span_arm;
2821 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2823 if (instance->adapter_type >= VENTURA_SERIES)
2824 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2826 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2828 if ((raidLUN[0] == 1) &&
2829 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2830 instance->dev_handle = !(instance->dev_handle);
2832 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2835 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2836 io_request->DevHandle = io_info.devHandle;
2837 cmd->pd_interface = io_info.pd_interface;
2838 /* populate the LUN field */
2839 memcpy(io_request->LUN, raidLUN, 8);
2841 rctx->timeout_value =
2842 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2843 cmd->request_desc->SCSIIO.RequestFlags =
2844 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2845 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2846 if (instance->adapter_type == INVADER_SERIES) {
2847 if (io_info.do_fp_rlbypass ||
2848 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2849 cmd->request_desc->SCSIIO.RequestFlags =
2850 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2851 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2852 rctx->type = MPI2_TYPE_CUDA;
2853 rctx->reg_lock_flags |=
2854 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2855 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2857 } else if (instance->adapter_type >= VENTURA_SERIES) {
2858 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2859 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2860 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2862 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2863 io_request->DevHandle = cpu_to_le16(device_id);
2869 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2870 * @instance: Adapter soft state
2871 * @scp: SCSI command
2872 * @cmd: Command to be prepared
2874 * Prepares the io_request frame for non-rw io cmds for vd.
2876 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2877 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2880 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2882 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2883 struct fusion_context *fusion = instance->ctrl_context;
2887 struct MR_LD_RAID *raid;
2888 struct RAID_CONTEXT *pRAID_Context;
2891 io_request = cmd->io_request;
2892 device_id = MEGASAS_DEV_INDEX(scmd);
2893 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2894 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2895 /* get RAID_Context pointer */
2896 pRAID_Context = &io_request->RaidContext.raid_context;
2897 /* Check with FW team */
2898 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2899 pRAID_Context->reg_lock_row_lba = 0;
2900 pRAID_Context->reg_lock_length = 0;
2902 if (fusion->fast_path_io && (
2903 device_id < instance->fw_supported_vd_count)) {
2905 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2906 if (ld >= instance->fw_supported_vd_count - 1)
2909 raid = MR_LdRaidGet(ld, local_map_ptr);
2910 if (!(raid->capability.fpNonRWCapable))
2917 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2918 io_request->DevHandle = cpu_to_le16(device_id);
2919 io_request->LUN[1] = scmd->device->lun;
2920 pRAID_Context->timeout_value =
2921 cpu_to_le16 (scmd->request->timeout / HZ);
2922 cmd->request_desc->SCSIIO.RequestFlags =
2923 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2924 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2927 /* set RAID context values */
2928 pRAID_Context->config_seq_num = raid->seqNum;
2929 if (instance->adapter_type < VENTURA_SERIES)
2930 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2931 pRAID_Context->timeout_value =
2932 cpu_to_le16(raid->fpIoTimeoutForLd);
2934 /* get the DevHandle for the PD (since this is
2935 fpNonRWCapable, this is a single disk RAID0) */
2937 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2938 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2939 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2941 /* build request descriptor */
2942 cmd->request_desc->SCSIIO.RequestFlags =
2943 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2944 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2945 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2947 /* populate the LUN field */
2948 memcpy(io_request->LUN, raid->LUN, 8);
2950 /* build the raidScsiIO structure */
2951 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2952 io_request->DevHandle = devHandle;
2957 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2958 * @instance: Adapter soft state
2959 * @scp: SCSI command
2960 * @cmd: Command to be prepared
2961 * @fp_possible: parameter to detect fast path or firmware path io.
2963 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2966 megasas_build_syspd_fusion(struct megasas_instance *instance,
2967 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2971 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2973 u16 os_timeout_value;
2975 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2976 struct RAID_CONTEXT *pRAID_Context;
2977 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
2978 struct MR_PRIV_DEVICE *mr_device_priv_data;
2979 struct fusion_context *fusion = instance->ctrl_context;
2980 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
2982 device_id = MEGASAS_DEV_INDEX(scmd);
2983 pd_index = MEGASAS_PD_INDEX(scmd);
2984 os_timeout_value = scmd->request->timeout / HZ;
2985 mr_device_priv_data = scmd->device->hostdata;
2986 cmd->pd_interface = mr_device_priv_data->interface_type;
2988 io_request = cmd->io_request;
2989 /* get RAID_Context pointer */
2990 pRAID_Context = &io_request->RaidContext.raid_context;
2991 pRAID_Context->reg_lock_flags = 0;
2992 pRAID_Context->reg_lock_row_lba = 0;
2993 pRAID_Context->reg_lock_length = 0;
2994 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2995 io_request->LUN[1] = scmd->device->lun;
2996 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
2997 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2999 /* If FW supports PD sequence number */
3000 if (instance->use_seqnum_jbod_fp &&
3001 instance->pd_list[pd_index].driveType == TYPE_DISK) {
3002 /* TgtId must be incremented by 255 as jbod seq number is index
3005 /* More than 256 PD/JBOD support for Ventura */
3006 if (instance->support_morethan256jbod)
3007 pRAID_Context->virtual_disk_tgt_id =
3008 pd_sync->seq[pd_index].pd_target_id;
3010 pRAID_Context->virtual_disk_tgt_id =
3011 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
3012 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
3013 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
3014 if (instance->adapter_type >= VENTURA_SERIES) {
3015 io_request->RaidContext.raid_context_g35.routing_flags |=
3016 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3017 io_request->RaidContext.raid_context_g35.nseg_type |=
3018 (1 << RAID_CONTEXT_NSEG_SHIFT);
3019 io_request->RaidContext.raid_context_g35.nseg_type |=
3020 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3022 pRAID_Context->type = MPI2_TYPE_CUDA;
3023 pRAID_Context->nseg = 0x1;
3024 pRAID_Context->reg_lock_flags |=
3025 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3027 } else if (fusion->fast_path_io) {
3028 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3029 pRAID_Context->config_seq_num = 0;
3030 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
3031 io_request->DevHandle =
3032 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3034 /* Want to send all IO via FW path */
3035 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3036 pRAID_Context->config_seq_num = 0;
3037 io_request->DevHandle = cpu_to_le16(0xFFFF);
3040 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3042 cmd->request_desc->SCSIIO.MSIxIndex =
3043 instance->reply_map[raw_smp_processor_id()];
3046 /* system pd firmware path */
3047 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3048 cmd->request_desc->SCSIIO.RequestFlags =
3049 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3050 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3051 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3052 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3054 if (os_timeout_value)
3057 /* system pd Fast Path */
3058 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3059 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3061 pRAID_Context->timeout_value =
3062 cpu_to_le16((os_timeout_value > timeout_limit) ?
3063 timeout_limit : os_timeout_value);
3064 if (instance->adapter_type >= INVADER_SERIES)
3065 io_request->IoFlags |=
3066 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3068 cmd->request_desc->SCSIIO.RequestFlags =
3069 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3070 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3075 * megasas_build_io_fusion - Prepares IOs to devices
3076 * @instance: Adapter soft state
3077 * @scp: SCSI command
3078 * @cmd: Command to be prepared
3080 * Invokes helper functions to prepare request frames
3081 * and sets flags appropriate for IO/Non-IO cmd
3084 megasas_build_io_fusion(struct megasas_instance *instance,
3085 struct scsi_cmnd *scp,
3086 struct megasas_cmd_fusion *cmd)
3090 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3091 struct MR_PRIV_DEVICE *mr_device_priv_data;
3092 mr_device_priv_data = scp->device->hostdata;
3094 /* Zero out some fields so they don't get reused */
3095 memset(io_request->LUN, 0x0, 8);
3096 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3097 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3098 io_request->EEDPFlags = 0;
3099 io_request->Control = 0;
3100 io_request->EEDPBlockSize = 0;
3101 io_request->ChainOffset = 0;
3102 io_request->RaidContext.raid_context.raid_flags = 0;
3103 io_request->RaidContext.raid_context.type = 0;
3104 io_request->RaidContext.raid_context.nseg = 0;
3106 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3108 * Just the CDB length,rest of the Flags are zero
3109 * This will be modified for FP in build_ldio_fusion
3111 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3113 switch (cmd_type = megasas_cmd_type(scp)) {
3114 case READ_WRITE_LDIO:
3115 megasas_build_ldio_fusion(instance, scp, cmd);
3117 case NON_READ_WRITE_LDIO:
3118 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3120 case READ_WRITE_SYSPDIO:
3121 megasas_build_syspd_fusion(instance, scp, cmd, true);
3123 case NON_READ_WRITE_SYSPDIO:
3124 if (instance->secure_jbod_support ||
3125 mr_device_priv_data->is_tm_capable)
3126 megasas_build_syspd_fusion(instance, scp, cmd, false);
3128 megasas_build_syspd_fusion(instance, scp, cmd, true);
3138 sge_count = megasas_make_sgl(instance, scp, cmd);
3140 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3141 dev_err(&instance->pdev->dev,
3142 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3143 __func__, __LINE__, sge_count, instance->max_num_sge);
3147 if (instance->adapter_type >= VENTURA_SERIES) {
3148 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3149 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3150 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3152 /* numSGE store lower 8 bit of sge_count.
3153 * numSGEExt store higher 8 bit of sge_count
3155 io_request->RaidContext.raid_context.num_sge = sge_count;
3156 io_request->RaidContext.raid_context.num_sge_ext =
3157 (u8)(sge_count >> 8);
3160 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3162 if (scp->sc_data_direction == DMA_TO_DEVICE)
3163 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3164 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3165 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3167 io_request->SGLOffset0 =
3168 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3170 io_request->SenseBufferLowAddress =
3171 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3172 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3175 scp->SCp.ptr = (char *)cmd;
3180 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3181 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3184 struct fusion_context *fusion;
3186 fusion = instance->ctrl_context;
3187 p = fusion->req_frames_desc +
3188 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3190 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3194 /* megasas_prepate_secondRaid1_IO
3195 * It prepares the raid 1 second IO
3197 void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3198 struct megasas_cmd_fusion *cmd,
3199 struct megasas_cmd_fusion *r1_cmd)
3201 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3202 struct fusion_context *fusion;
3203 fusion = instance->ctrl_context;
3204 req_desc = cmd->request_desc;
3205 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3206 memcpy(r1_cmd->io_request, cmd->io_request,
3207 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3208 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3209 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3210 /*sense buffer is different for r1 command*/
3211 r1_cmd->io_request->SenseBufferLowAddress =
3212 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3213 r1_cmd->scmd = cmd->scmd;
3214 req_desc2 = megasas_get_request_descriptor(instance,
3215 (r1_cmd->index - 1));
3216 req_desc2->Words = 0;
3217 r1_cmd->request_desc = req_desc2;
3218 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3219 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3220 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3221 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3222 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3223 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3224 cpu_to_le16(r1_cmd->index);
3225 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3226 cpu_to_le16(cmd->index);
3227 /*MSIxIndex of both commands request descriptors should be same*/
3228 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3229 cmd->request_desc->SCSIIO.MSIxIndex;
3230 /*span arm is different for r1 cmd*/
3231 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3232 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3236 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3237 * issuing non IOCTL cmd
3238 * @instance: Adapter soft state
3239 * @scmd: pointer to scsi cmd from OS
3242 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3243 struct scsi_cmnd *scmd)
3245 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3246 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3248 struct fusion_context *fusion;
3250 fusion = instance->ctrl_context;
3252 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3253 instance->ldio_threshold &&
3254 (atomic_inc_return(&instance->ldio_outstanding) >
3255 instance->ldio_threshold)) {
3256 atomic_dec(&instance->ldio_outstanding);
3257 return SCSI_MLQUEUE_DEVICE_BUSY;
3260 if (atomic_inc_return(&instance->fw_outstanding) >
3261 instance->host->can_queue) {
3262 atomic_dec(&instance->fw_outstanding);
3263 return SCSI_MLQUEUE_HOST_BUSY;
3266 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3269 atomic_dec(&instance->fw_outstanding);
3270 return SCSI_MLQUEUE_HOST_BUSY;
3275 req_desc = megasas_get_request_descriptor(instance, index-1);
3277 req_desc->Words = 0;
3278 cmd->request_desc = req_desc;
3280 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3281 megasas_return_cmd_fusion(instance, cmd);
3282 dev_err(&instance->pdev->dev, "Error building command\n");
3283 cmd->request_desc = NULL;
3284 atomic_dec(&instance->fw_outstanding);
3285 return SCSI_MLQUEUE_HOST_BUSY;
3288 req_desc = cmd->request_desc;
3289 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3291 if (cmd->io_request->ChainOffset != 0 &&
3292 cmd->io_request->ChainOffset != 0xF)
3293 dev_err(&instance->pdev->dev, "The chain offset value is not "
3294 "correct : %x\n", cmd->io_request->ChainOffset);
3296 * if it is raid 1/10 fp write capable.
3297 * try to get second command from pool and construct it.
3298 * From FW, it has confirmed that lba values of two PDs
3299 * corresponds to single R1/10 LD are always same
3302 /* driver side count always should be less than max_fw_cmds
3303 * to get new command
3305 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3306 r1_cmd = megasas_get_cmd_fusion(instance,
3307 (scmd->request->tag + instance->max_fw_cmds));
3308 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3313 * Issue the command to the FW
3316 megasas_fire_cmd_fusion(instance, req_desc);
3319 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3326 * megasas_complete_r1_command -
3327 * completes R1 FP write commands which has valid peer smid
3328 * @instance: Adapter soft state
3329 * @cmd_fusion: MPT command frame
3333 megasas_complete_r1_command(struct megasas_instance *instance,
3334 struct megasas_cmd_fusion *cmd)
3336 u8 *sense, status, ex_status;
3339 struct fusion_context *fusion;
3340 struct megasas_cmd_fusion *r1_cmd = NULL;
3341 struct scsi_cmnd *scmd_local = NULL;
3342 struct RAID_CONTEXT_G35 *rctx_g35;
3344 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3345 fusion = instance->ctrl_context;
3346 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
3348 r1_cmd = fusion->cmd_list[peer_smid - 1];
3349 scmd_local = cmd->scmd;
3350 status = rctx_g35->status;
3351 ex_status = rctx_g35->ex_status;
3352 data_length = cmd->io_request->DataLength;
3355 cmd->cmd_completed = true;
3357 /* Check if peer command is completed or not*/
3358 if (r1_cmd->cmd_completed) {
3359 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3360 if (rctx_g35->status != MFI_STAT_OK) {
3361 status = rctx_g35->status;
3362 ex_status = rctx_g35->ex_status;
3363 data_length = r1_cmd->io_request->DataLength;
3364 sense = r1_cmd->sense;
3367 megasas_return_cmd_fusion(instance, r1_cmd);
3368 map_cmd_status(fusion, scmd_local, status, ex_status,
3369 le32_to_cpu(data_length), sense);
3370 if (instance->ldio_threshold &&
3371 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3372 atomic_dec(&instance->ldio_outstanding);
3373 scmd_local->SCp.ptr = NULL;
3374 megasas_return_cmd_fusion(instance, cmd);
3375 scsi_dma_unmap(scmd_local);
3376 scmd_local->scsi_done(scmd_local);
3381 * complete_cmd_fusion - Completes command
3382 * @instance: Adapter soft state
3383 * Completes all commands that is in reply descriptor queue
3386 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
3388 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3389 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3390 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3391 struct fusion_context *fusion;
3392 struct megasas_cmd *cmd_mfi;
3393 struct megasas_cmd_fusion *cmd_fusion;
3394 u16 smid, num_completed;
3395 u8 reply_descript_type, *sense, status, extStatus;
3396 u32 device_id, data_length;
3397 union desc_value d_val;
3398 struct LD_LOAD_BALANCE_INFO *lbinfo;
3399 int threshold_reply_count = 0;
3400 struct scsi_cmnd *scmd_local = NULL;
3401 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3402 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3404 fusion = instance->ctrl_context;
3406 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3409 desc = fusion->reply_frames_desc[MSIxIndex] +
3410 fusion->last_reply_idx[MSIxIndex];
3412 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3414 d_val.word = desc->Words;
3416 reply_descript_type = reply_desc->ReplyFlags &
3417 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3419 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3424 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3425 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3427 smid = le16_to_cpu(reply_desc->SMID);
3428 cmd_fusion = fusion->cmd_list[smid - 1];
3429 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3430 cmd_fusion->io_request;
3432 scmd_local = cmd_fusion->scmd;
3433 status = scsi_io_req->RaidContext.raid_context.status;
3434 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3435 sense = cmd_fusion->sense;
3436 data_length = scsi_io_req->DataLength;
3438 switch (scsi_io_req->Function) {
3439 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3440 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3441 cmd_fusion->io_request;
3442 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3443 &mr_tm_req->TmRequest;
3444 dev_dbg(&instance->pdev->dev, "TM completion:"
3445 "type: 0x%x TaskMID: 0x%x\n",
3446 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3447 complete(&cmd_fusion->done);
3449 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3450 /* Update load balancing info */
3451 if (fusion->load_balance_info &&
3452 (cmd_fusion->scmd->SCp.Status &
3453 MEGASAS_LOAD_BALANCE_FLAG)) {
3454 device_id = MEGASAS_DEV_INDEX(scmd_local);
3455 lbinfo = &fusion->load_balance_info[device_id];
3456 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3457 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3459 /* Fall through - and complete IO */
3460 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3461 atomic_dec(&instance->fw_outstanding);
3462 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3463 map_cmd_status(fusion, scmd_local, status,
3464 extStatus, le32_to_cpu(data_length),
3466 if (instance->ldio_threshold &&
3467 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3468 atomic_dec(&instance->ldio_outstanding);
3469 scmd_local->SCp.ptr = NULL;
3470 megasas_return_cmd_fusion(instance, cmd_fusion);
3471 scsi_dma_unmap(scmd_local);
3472 scmd_local->scsi_done(scmd_local);
3473 } else /* Optimal VD - R1 FP command completion. */
3474 megasas_complete_r1_command(instance, cmd_fusion);
3476 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3477 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3478 /* Poll mode. Dummy free.
3479 * In case of Interrupt mode, caller has reverse check.
3481 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3482 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3483 megasas_return_cmd(instance, cmd_mfi);
3485 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3489 fusion->last_reply_idx[MSIxIndex]++;
3490 if (fusion->last_reply_idx[MSIxIndex] >=
3491 fusion->reply_q_depth)
3492 fusion->last_reply_idx[MSIxIndex] = 0;
3494 desc->Words = cpu_to_le64(ULLONG_MAX);
3496 threshold_reply_count++;
3498 /* Get the next reply descriptor */
3499 if (!fusion->last_reply_idx[MSIxIndex])
3500 desc = fusion->reply_frames_desc[MSIxIndex];
3505 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3507 d_val.word = desc->Words;
3509 reply_descript_type = reply_desc->ReplyFlags &
3510 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3512 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3515 * Write to reply post host index register after completing threshold
3516 * number of reply counts and still there are more replies in reply queue
3517 * pending to be completed
3519 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
3520 if (instance->msix_combined)
3521 writel(((MSIxIndex & 0x7) << 24) |
3522 fusion->last_reply_idx[MSIxIndex],
3523 instance->reply_post_host_index_addr[MSIxIndex/8]);
3525 writel((MSIxIndex << 24) |
3526 fusion->last_reply_idx[MSIxIndex],
3527 instance->reply_post_host_index_addr[0]);
3528 threshold_reply_count = 0;
3536 if (instance->msix_combined)
3537 writel(((MSIxIndex & 0x7) << 24) |
3538 fusion->last_reply_idx[MSIxIndex],
3539 instance->reply_post_host_index_addr[MSIxIndex/8]);
3541 writel((MSIxIndex << 24) |
3542 fusion->last_reply_idx[MSIxIndex],
3543 instance->reply_post_host_index_addr[0]);
3544 megasas_check_and_restore_queue_depth(instance);
3549 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3550 * @instance: Adapter soft state
3552 void megasas_sync_irqs(unsigned long instance_addr)
3555 struct megasas_instance *instance =
3556 (struct megasas_instance *)instance_addr;
3558 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3560 for (i = 0; i < count; i++)
3561 synchronize_irq(pci_irq_vector(instance->pdev, i));
3565 * megasas_complete_cmd_dpc_fusion - Completes command
3566 * @instance: Adapter soft state
3568 * Tasklet to complete cmds
3571 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3573 struct megasas_instance *instance =
3574 (struct megasas_instance *)instance_addr;
3575 u32 count, MSIxIndex;
3577 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3579 /* If we have already declared adapter dead, donot complete cmds */
3580 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3583 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3584 complete_cmd_fusion(instance, MSIxIndex);
3588 * megasas_isr_fusion - isr entry point
3590 irqreturn_t megasas_isr_fusion(int irq, void *devp)
3592 struct megasas_irq_context *irq_context = devp;
3593 struct megasas_instance *instance = irq_context->instance;
3596 if (instance->mask_interrupts)
3599 if (!instance->msix_vectors) {
3600 mfiStatus = instance->instancet->clear_intr(instance);
3605 /* If we are resetting, bail */
3606 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3607 instance->instancet->clear_intr(instance);
3611 return complete_cmd_fusion(instance, irq_context->MSIxIndex);
3615 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3616 * @instance: Adapter soft state
3617 * mfi_cmd: megasas_cmd pointer
3621 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3622 struct megasas_cmd *mfi_cmd)
3624 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3625 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3626 struct megasas_cmd_fusion *cmd;
3627 struct fusion_context *fusion;
3628 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3630 fusion = instance->ctrl_context;
3632 cmd = megasas_get_cmd_fusion(instance,
3633 instance->max_scsi_cmds + mfi_cmd->index);
3635 /* Save the smid. To be used for returning the cmd */
3636 mfi_cmd->context.smid = cmd->index;
3639 * For cmds where the flag is set, store the flag and check
3640 * on completion. For cmds with this flag, don't call
3641 * megasas_complete_cmd
3644 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3645 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3647 io_req = cmd->io_request;
3649 if (instance->adapter_type >= INVADER_SERIES) {
3650 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3651 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3652 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3653 sgl_ptr_end->Flags = 0;
3657 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3659 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3660 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3662 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3664 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3666 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3667 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3669 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3673 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3674 * @instance: Adapter soft state
3675 * @cmd: mfi cmd to build
3678 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3679 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3681 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3684 build_mpt_mfi_pass_thru(instance, cmd);
3685 index = cmd->context.smid;
3687 req_desc = megasas_get_request_descriptor(instance, index - 1);
3689 req_desc->Words = 0;
3690 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3691 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3693 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3699 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3700 * @instance: Adapter soft state
3701 * @cmd: mfi cmd pointer
3705 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3706 struct megasas_cmd *cmd)
3708 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3710 req_desc = build_mpt_cmd(instance, cmd);
3712 megasas_fire_cmd_fusion(instance, req_desc);
3717 * megasas_release_fusion - Reverses the FW initialization
3718 * @instance: Adapter soft state
3721 megasas_release_fusion(struct megasas_instance *instance)
3723 megasas_free_ioc_init_cmd(instance);
3724 megasas_free_cmds(instance);
3725 megasas_free_cmds_fusion(instance);
3727 iounmap(instance->reg_set);
3729 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3733 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3734 * @regs: MFI register set
3737 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
3739 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
3743 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3744 * @instance: Controller's soft instance
3745 * return: Number of allocated host crash buffers
3748 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3752 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3753 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3754 if (!instance->crash_buf[i]) {
3755 dev_info(&instance->pdev->dev, "Firmware crash dump "
3756 "memory allocation failed at index %d\n", i);
3760 instance->drv_buf_alloc = i;
3764 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3765 * @instance: Controller's soft instance
3768 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3771 for (i = 0; i < instance->drv_buf_alloc; i++) {
3772 if (instance->crash_buf[i])
3773 vfree(instance->crash_buf[i]);
3775 instance->drv_buf_index = 0;
3776 instance->drv_buf_alloc = 0;
3777 instance->fw_crash_state = UNAVAILABLE;
3778 instance->fw_crash_buffer_size = 0;
3782 * megasas_adp_reset_fusion - For controller reset
3783 * @regs: MFI register set
3786 megasas_adp_reset_fusion(struct megasas_instance *instance,
3787 struct megasas_register_set __iomem *regs)
3789 u32 host_diag, abs_state, retry;
3791 /* Now try to reset the chip */
3792 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3793 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3794 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3795 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3796 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3797 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3798 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3800 /* Check that the diag write enable (DRWE) bit is on */
3801 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3803 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3805 host_diag = megasas_readl(instance,
3806 &instance->reg_set->fusion_host_diag);
3807 if (retry++ == 100) {
3808 dev_warn(&instance->pdev->dev,
3809 "Host diag unlock failed from %s %d\n",
3810 __func__, __LINE__);
3814 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3817 /* Send chip reset command */
3818 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3819 &instance->reg_set->fusion_host_diag);
3822 /* Make sure reset adapter bit is cleared */
3823 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3825 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3827 host_diag = megasas_readl(instance,
3828 &instance->reg_set->fusion_host_diag);
3829 if (retry++ == 1000) {
3830 dev_warn(&instance->pdev->dev,
3831 "Diag reset adapter never cleared %s %d\n",
3832 __func__, __LINE__);
3836 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3839 abs_state = instance->instancet->read_fw_status_reg(instance)
3843 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3845 abs_state = instance->instancet->
3846 read_fw_status_reg(instance) & MFI_STATE_MASK;
3848 if (abs_state <= MFI_STATE_FW_INIT) {
3849 dev_warn(&instance->pdev->dev,
3850 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3851 abs_state, __func__, __LINE__);
3859 * megasas_check_reset_fusion - For controller reset check
3860 * @regs: MFI register set
3863 megasas_check_reset_fusion(struct megasas_instance *instance,
3864 struct megasas_register_set __iomem *regs)
3870 * megasas_trigger_snap_dump - Trigger snap dump in FW
3871 * @instance: Soft instance of adapter
3873 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
3878 if (!instance->disableOnlineCtrlReset) {
3879 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
3880 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
3881 &instance->reg_set->doorbell);
3882 readl(&instance->reg_set->doorbell);
3885 for (j = 0; j < instance->snapdump_wait_time; j++) {
3886 fw_state = instance->instancet->read_fw_status_reg(instance) &
3888 if (fw_state == MFI_STATE_FAULT) {
3889 dev_err(&instance->pdev->dev,
3890 "Found FW in FAULT state, after snap dump trigger\n");
3897 /* This function waits for outstanding commands on fusion to complete */
3898 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
3899 int reason, int *convert)
3901 int i, outstanding, retval = 0, hb_seconds_missed = 0;
3903 u32 waittime_for_io_completion;
3905 waittime_for_io_completion =
3906 min_t(u32, resetwaittime,
3907 (resetwaittime - instance->snapdump_wait_time));
3909 if (reason == MFI_IO_TIMEOUT_OCR) {
3910 dev_info(&instance->pdev->dev,
3911 "MFI command is timed out\n");
3912 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3913 if (instance->snapdump_wait_time)
3914 megasas_trigger_snap_dump(instance);
3919 for (i = 0; i < waittime_for_io_completion; i++) {
3920 /* Check if firmware is in fault state */
3921 fw_state = instance->instancet->read_fw_status_reg(instance) &
3923 if (fw_state == MFI_STATE_FAULT) {
3924 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
3925 " will reset adapter scsi%d.\n",
3926 instance->host->host_no);
3927 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3928 if (instance->requestorId && reason) {
3929 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3930 " state while polling during"
3931 " I/O timeout handling for %d\n",
3932 instance->host->host_no);
3941 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
3942 if (instance->requestorId && !reason) {
3947 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
3948 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
3949 if (instance->hb_host_mem->HB.fwCounter !=
3950 instance->hb_host_mem->HB.driverCounter) {
3951 instance->hb_host_mem->HB.driverCounter =
3952 instance->hb_host_mem->HB.fwCounter;
3953 hb_seconds_missed = 0;
3955 hb_seconds_missed++;
3956 if (hb_seconds_missed ==
3957 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
3958 dev_warn(&instance->pdev->dev, "SR-IOV:"
3959 " Heartbeat never completed "
3960 " while polling during I/O "
3961 " timeout handling for "
3963 instance->host->host_no);
3971 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3972 outstanding = atomic_read(&instance->fw_outstanding);
3976 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
3977 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
3978 "commands to complete for scsi%d\n", i,
3979 outstanding, instance->host->host_no);
3984 if (instance->snapdump_wait_time) {
3985 megasas_trigger_snap_dump(instance);
3990 if (atomic_read(&instance->fw_outstanding)) {
3991 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
3992 "will reset adapter scsi%d.\n",
3993 instance->host->host_no);
4002 void megasas_reset_reply_desc(struct megasas_instance *instance)
4005 struct fusion_context *fusion;
4006 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4008 fusion = instance->ctrl_context;
4009 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4010 for (i = 0 ; i < count ; i++) {
4011 fusion->last_reply_idx[i] = 0;
4012 reply_desc = fusion->reply_frames_desc[i];
4013 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4014 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4019 * megasas_refire_mgmt_cmd : Re-fire management commands
4020 * @instance: Controller's soft instance
4022 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
4025 struct megasas_cmd_fusion *cmd_fusion;
4026 struct fusion_context *fusion;
4027 struct megasas_cmd *cmd_mfi;
4028 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4030 bool refire_cmd = 0;
4034 fusion = instance->ctrl_context;
4036 /* Re-fire management commands.
4037 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4039 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4040 cmd_fusion = fusion->cmd_list[j];
4041 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4042 smid = le16_to_cpu(cmd_mfi->context.smid);
4043 result = REFIRE_CMD;
4048 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4050 switch (cmd_mfi->frame->hdr.cmd) {
4052 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4053 /* Do not refire shutdown command */
4054 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4055 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4056 result = COMPLETE_CMD;
4060 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4061 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4062 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4065 result = RETURN_CMD;
4069 if (!instance->support_nvme_passthru) {
4070 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4071 result = COMPLETE_CMD;
4081 megasas_fire_cmd_fusion(instance, req_desc);
4084 megasas_return_cmd(instance, cmd_mfi);
4087 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4094 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4095 * @instance: per adapter struct
4096 * @channel: the channel assigned by the OS
4097 * @id: the id assigned by the OS
4099 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4102 static int megasas_track_scsiio(struct megasas_instance *instance,
4103 int id, int channel)
4106 struct megasas_cmd_fusion *cmd_fusion;
4107 struct fusion_context *fusion;
4108 fusion = instance->ctrl_context;
4110 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4111 cmd_fusion = fusion->cmd_list[i];
4112 if (cmd_fusion->scmd &&
4113 (cmd_fusion->scmd->device->id == id &&
4114 cmd_fusion->scmd->device->channel == channel)) {
4115 dev_info(&instance->pdev->dev,
4116 "SCSI commands pending to target"
4117 "channel %d id %d \tSMID: 0x%x\n",
4118 channel, id, cmd_fusion->index);
4119 scsi_print_command(cmd_fusion->scmd);
4125 return found ? FAILED : SUCCESS;
4129 * megasas_tm_response_code - translation of device response code
4130 * @ioc: per adapter object
4131 * @mpi_reply: MPI reply returned by firmware
4136 megasas_tm_response_code(struct megasas_instance *instance,
4137 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4141 switch (mpi_reply->ResponseCode) {
4142 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4143 desc = "task management request completed";
4145 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4146 desc = "invalid frame";
4148 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4149 desc = "task management request not supported";
4151 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4152 desc = "task management request failed";
4154 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4155 desc = "task management request succeeded";
4157 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4158 desc = "invalid lun";
4161 desc = "overlapped tag attempted";
4163 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4164 desc = "task queued, however not sent to target";
4170 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4171 mpi_reply->ResponseCode, desc);
4172 dev_dbg(&instance->pdev->dev,
4173 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4174 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4175 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4176 mpi_reply->Function, mpi_reply->TaskType,
4177 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4181 * megasas_issue_tm - main routine for sending tm requests
4182 * @instance: per adapter struct
4183 * @device_handle: device handle
4184 * @channel: the channel assigned by the OS
4185 * @id: the id assigned by the OS
4186 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4187 * @smid_task: smid assigned to the task
4188 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4191 * MegaRaid use MPT interface for Task Magement request.
4192 * A generic API for sending task management requests to firmware.
4194 * Return SUCCESS or FAILED.
4197 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4198 uint channel, uint id, u16 smid_task, u8 type,
4199 struct MR_PRIV_DEVICE *mr_device_priv_data)
4201 struct MR_TASK_MANAGE_REQUEST *mr_request;
4202 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4203 unsigned long timeleft;
4204 struct megasas_cmd_fusion *cmd_fusion;
4205 struct megasas_cmd *cmd_mfi;
4206 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4207 struct fusion_context *fusion = NULL;
4208 struct megasas_cmd_fusion *scsi_lookup;
4210 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4211 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4213 fusion = instance->ctrl_context;
4215 cmd_mfi = megasas_get_cmd(instance);
4218 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4219 __func__, __LINE__);
4223 cmd_fusion = megasas_get_cmd_fusion(instance,
4224 instance->max_scsi_cmds + cmd_mfi->index);
4226 /* Save the smid. To be used for returning the cmd */
4227 cmd_mfi->context.smid = cmd_fusion->index;
4229 req_desc = megasas_get_request_descriptor(instance,
4230 (cmd_fusion->index - 1));
4232 cmd_fusion->request_desc = req_desc;
4233 req_desc->Words = 0;
4235 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4236 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4237 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4238 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4239 mpi_request->DevHandle = cpu_to_le16(device_handle);
4240 mpi_request->TaskType = type;
4241 mpi_request->TaskMID = cpu_to_le16(smid_task);
4242 mpi_request->LUN[1] = 0;
4245 req_desc = cmd_fusion->request_desc;
4246 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4247 req_desc->HighPriority.RequestFlags =
4248 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4249 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4250 req_desc->HighPriority.MSIxIndex = 0;
4251 req_desc->HighPriority.LMID = 0;
4252 req_desc->HighPriority.Reserved1 = 0;
4254 if (channel < MEGASAS_MAX_PD_CHANNELS)
4255 mr_request->tmReqFlags.isTMForPD = 1;
4257 mr_request->tmReqFlags.isTMForLD = 1;
4259 init_completion(&cmd_fusion->done);
4260 megasas_fire_cmd_fusion(instance, req_desc);
4263 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4264 timeout = mr_device_priv_data->task_abort_tmo;
4266 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4267 timeout = mr_device_priv_data->target_reset_tmo;
4271 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4274 dev_err(&instance->pdev->dev,
4275 "task mgmt type 0x%x timed out\n", type);
4276 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
4277 mutex_unlock(&instance->reset_mutex);
4278 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4279 mutex_lock(&instance->reset_mutex);
4283 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4284 megasas_tm_response_code(instance, mpi_reply);
4286 megasas_return_cmd(instance, cmd_mfi);
4289 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4290 scsi_lookup = fusion->cmd_list[smid_task - 1];
4292 if (scsi_lookup->scmd == NULL)
4295 instance->instancet->disable_intr(instance);
4296 megasas_sync_irqs((unsigned long)instance);
4297 instance->instancet->enable_intr(instance);
4298 if (scsi_lookup->scmd == NULL)
4304 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4305 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4307 instance->instancet->disable_intr(instance);
4308 megasas_sync_irqs((unsigned long)instance);
4309 rc = megasas_track_scsiio(instance, id, channel);
4310 instance->instancet->enable_intr(instance);
4313 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4314 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4326 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4327 * @instance: per adapter struct
4329 * Return Non Zero index, if SMID found in outstanding commands
4331 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4334 struct megasas_instance *instance;
4335 struct megasas_cmd_fusion *cmd_fusion;
4336 struct fusion_context *fusion;
4338 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4340 fusion = instance->ctrl_context;
4342 for (i = 0; i < instance->max_scsi_cmds; i++) {
4343 cmd_fusion = fusion->cmd_list[i];
4344 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4345 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4346 " SMID: %d\n", cmd_fusion->index);
4347 ret = cmd_fusion->index;
4356 * megasas_get_tm_devhandle - Get devhandle for TM request
4357 * @sdev- OS provided scsi device
4359 * Returns- devhandle/targetID of SCSI device
4361 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4365 struct megasas_instance *instance;
4366 struct fusion_context *fusion;
4367 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4368 u16 devhandle = (u16)ULONG_MAX;
4370 instance = (struct megasas_instance *)sdev->host->hostdata;
4371 fusion = instance->ctrl_context;
4373 if (!MEGASAS_IS_LOGICAL(sdev)) {
4374 if (instance->use_seqnum_jbod_fp) {
4375 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4377 pd_sync = (void *)fusion->pd_seq_sync
4378 [(instance->pd_seq_map_id - 1) & 1];
4379 devhandle = pd_sync->seq[pd_index].devHandle;
4381 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4382 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4384 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4386 devhandle = device_id;
4393 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4394 * @scmd : pointer to scsi command object
4396 * Return SUCCESS, if command aborted else FAILED
4399 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4401 struct megasas_instance *instance;
4402 u16 smid, devhandle;
4403 struct fusion_context *fusion;
4405 struct MR_PRIV_DEVICE *mr_device_priv_data;
4406 mr_device_priv_data = scmd->device->hostdata;
4409 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4410 fusion = instance->ctrl_context;
4412 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
4413 scsi_print_command(scmd);
4415 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4416 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4417 "SCSI host:%d\n", instance->host->host_no);
4422 if (!mr_device_priv_data) {
4423 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4424 "scmd(%p)\n", scmd);
4425 scmd->result = DID_NO_CONNECT << 16;
4431 if (!mr_device_priv_data->is_tm_capable) {
4436 mutex_lock(&instance->reset_mutex);
4438 smid = megasas_fusion_smid_lookup(scmd);
4442 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4443 " issued is not found in oustanding commands\n");
4444 mutex_unlock(&instance->reset_mutex);
4448 devhandle = megasas_get_tm_devhandle(scmd->device);
4450 if (devhandle == (u16)ULONG_MAX) {
4452 sdev_printk(KERN_INFO, scmd->device,
4453 "task abort issued for invalid devhandle\n");
4454 mutex_unlock(&instance->reset_mutex);
4457 sdev_printk(KERN_INFO, scmd->device,
4458 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
4461 mr_device_priv_data->tm_busy = 1;
4462 ret = megasas_issue_tm(instance, devhandle,
4463 scmd->device->channel, scmd->device->id, smid,
4464 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4465 mr_device_priv_data);
4466 mr_device_priv_data->tm_busy = 0;
4468 mutex_unlock(&instance->reset_mutex);
4470 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4471 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4477 * megasas_reset_target_fusion : target reset function for fusion adapters
4478 * scmd: SCSI command pointer
4480 * Returns SUCCESS if all commands associated with target aborted else FAILED
4483 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4486 struct megasas_instance *instance;
4489 struct fusion_context *fusion;
4490 struct MR_PRIV_DEVICE *mr_device_priv_data;
4491 mr_device_priv_data = scmd->device->hostdata;
4493 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4494 fusion = instance->ctrl_context;
4496 sdev_printk(KERN_INFO, scmd->device,
4497 "target reset called for scmd(%p)\n", scmd);
4499 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4500 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4501 "SCSI host:%d\n", instance->host->host_no);
4506 if (!mr_device_priv_data) {
4507 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4508 "scmd(%p)\n", scmd);
4509 scmd->result = DID_NO_CONNECT << 16;
4515 if (!mr_device_priv_data->is_tm_capable) {
4520 mutex_lock(&instance->reset_mutex);
4521 devhandle = megasas_get_tm_devhandle(scmd->device);
4523 if (devhandle == (u16)ULONG_MAX) {
4525 sdev_printk(KERN_INFO, scmd->device,
4526 "target reset issued for invalid devhandle\n");
4527 mutex_unlock(&instance->reset_mutex);
4531 sdev_printk(KERN_INFO, scmd->device,
4532 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4534 mr_device_priv_data->tm_busy = 1;
4535 ret = megasas_issue_tm(instance, devhandle,
4536 scmd->device->channel, scmd->device->id, 0,
4537 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4538 mr_device_priv_data);
4539 mr_device_priv_data->tm_busy = 0;
4540 mutex_unlock(&instance->reset_mutex);
4542 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4543 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4548 /*SRIOV get other instance in cluster if any*/
4549 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4553 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4554 if (megasas_mgmt_info.instance[i] &&
4555 (megasas_mgmt_info.instance[i] != instance) &&
4556 megasas_mgmt_info.instance[i]->requestorId &&
4557 megasas_mgmt_info.instance[i]->peerIsPresent &&
4558 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4559 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4560 return megasas_mgmt_info.instance[i];
4565 /* Check for a second path that is currently UP */
4566 int megasas_check_mpio_paths(struct megasas_instance *instance,
4567 struct scsi_cmnd *scmd)
4569 struct megasas_instance *peer_instance = NULL;
4570 int retval = (DID_REQUEUE << 16);
4572 if (instance->peerIsPresent) {
4573 peer_instance = megasas_get_peer_instance(instance);
4574 if ((peer_instance) &&
4575 (atomic_read(&peer_instance->adprecovery) ==
4576 MEGASAS_HBA_OPERATIONAL))
4577 retval = (DID_NO_CONNECT << 16);
4582 /* Core fusion reset function */
4583 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4585 int retval = SUCCESS, i, j, convert = 0;
4586 struct megasas_instance *instance;
4587 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4588 struct fusion_context *fusion;
4589 u32 abs_state, status_reg, reset_adapter;
4590 u32 io_timeout_in_crash_mode = 0;
4591 struct scsi_cmnd *scmd_local = NULL;
4592 struct scsi_device *sdev;
4593 int ret_target_prop = DCMD_FAILED;
4594 bool is_target_prop = false;
4596 instance = (struct megasas_instance *)shost->hostdata;
4597 fusion = instance->ctrl_context;
4599 mutex_lock(&instance->reset_mutex);
4601 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4602 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4603 "returning FAILED for scsi%d.\n",
4604 instance->host->host_no);
4605 mutex_unlock(&instance->reset_mutex);
4608 status_reg = instance->instancet->read_fw_status_reg(instance);
4609 abs_state = status_reg & MFI_STATE_MASK;
4611 /* IO timeout detected, forcibly put FW in FAULT state */
4612 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4613 instance->crash_dump_app_support && reason) {
4614 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4615 "forcibly FAULT Firmware\n");
4616 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4617 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4618 writel(status_reg | MFI_STATE_FORCE_OCR,
4619 &instance->reg_set->doorbell);
4620 readl(&instance->reg_set->doorbell);
4621 mutex_unlock(&instance->reset_mutex);
4624 io_timeout_in_crash_mode++;
4625 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4626 "seconds for crash dump collection and OCR "
4627 "to be done\n", (io_timeout_in_crash_mode * 3));
4628 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4629 (io_timeout_in_crash_mode < 80));
4631 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4632 dev_info(&instance->pdev->dev, "OCR done for IO "
4636 dev_info(&instance->pdev->dev, "Controller is not "
4637 "operational after 240 seconds wait for IO "
4638 "timeout case in FW crash dump mode\n do "
4639 "OCR/kill adapter\n");
4640 retval = megasas_reset_fusion(shost, 0);
4645 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4646 del_timer_sync(&instance->sriov_heartbeat_timer);
4647 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4648 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4649 instance->instancet->disable_intr(instance);
4650 megasas_sync_irqs((unsigned long)instance);
4652 /* First try waiting for commands to complete */
4653 if (megasas_wait_for_outstanding_fusion(instance, reason,
4655 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4656 dev_warn(&instance->pdev->dev, "resetting fusion "
4657 "adapter scsi%d.\n", instance->host->host_no);
4661 if (megasas_dbg_lvl & OCR_LOGS)
4662 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4664 /* Now return commands back to the OS */
4665 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4666 cmd_fusion = fusion->cmd_list[i];
4667 /*check for extra commands issued by driver*/
4668 if (instance->adapter_type >= VENTURA_SERIES) {
4669 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4670 megasas_return_cmd_fusion(instance, r1_cmd);
4672 scmd_local = cmd_fusion->scmd;
4673 if (cmd_fusion->scmd) {
4674 if (megasas_dbg_lvl & OCR_LOGS) {
4675 sdev_printk(KERN_INFO,
4676 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4678 scsi_print_command(cmd_fusion->scmd);
4681 scmd_local->result =
4682 megasas_check_mpio_paths(instance,
4684 if (instance->ldio_threshold &&
4685 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4686 atomic_dec(&instance->ldio_outstanding);
4687 megasas_return_cmd_fusion(instance, cmd_fusion);
4688 scsi_dma_unmap(scmd_local);
4689 scmd_local->scsi_done(scmd_local);
4693 atomic_set(&instance->fw_outstanding, 0);
4695 status_reg = instance->instancet->read_fw_status_reg(instance);
4696 abs_state = status_reg & MFI_STATE_MASK;
4697 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4698 if (instance->disableOnlineCtrlReset ||
4699 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4700 /* Reset not supported, kill adapter */
4701 dev_warn(&instance->pdev->dev, "Reset not supported"
4702 ", killing adapter scsi%d.\n",
4703 instance->host->host_no);
4704 megaraid_sas_kill_hba(instance);
4705 instance->skip_heartbeat_timer_del = 1;
4710 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4711 if (instance->requestorId && !reason) {
4712 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4713 goto transition_to_ready;
4716 /* Now try to reset the chip */
4717 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
4719 if (instance->instancet->adp_reset
4720 (instance, instance->reg_set))
4722 transition_to_ready:
4723 /* Wait for FW to become ready */
4724 if (megasas_transition_to_ready(instance, 1)) {
4725 dev_warn(&instance->pdev->dev,
4726 "Failed to transition controller to ready for "
4727 "scsi%d.\n", instance->host->host_no);
4728 if (instance->requestorId && !reason)
4729 goto fail_kill_adapter;
4733 megasas_reset_reply_desc(instance);
4734 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4736 if (megasas_ioc_init_fusion(instance)) {
4737 if (instance->requestorId && !reason)
4738 goto fail_kill_adapter;
4743 if (megasas_get_ctrl_info(instance)) {
4744 dev_info(&instance->pdev->dev,
4745 "Failed from %s %d\n",
4746 __func__, __LINE__);
4747 megaraid_sas_kill_hba(instance);
4752 megasas_refire_mgmt_cmd(instance);
4754 /* Reset load balance info */
4755 if (fusion->load_balance_info)
4756 memset(fusion->load_balance_info, 0,
4757 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4758 MAX_LOGICAL_DRIVES_EXT));
4760 if (!megasas_get_map_info(instance))
4761 megasas_sync_map_info(instance);
4763 megasas_setup_jbod_map(instance);
4765 /* reset stream detection array */
4766 if (instance->adapter_type >= VENTURA_SERIES) {
4767 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4768 memset(fusion->stream_detect_by_ld[j],
4769 0, sizeof(struct LD_STREAM_DETECT));
4770 fusion->stream_detect_by_ld[j]->mru_bit_map
4775 clear_bit(MEGASAS_FUSION_IN_RESET,
4776 &instance->reset_flags);
4777 instance->instancet->enable_intr(instance);
4779 shost_for_each_device(sdev, shost) {
4780 if ((instance->tgt_prop) &&
4781 (instance->nvme_page_size))
4782 ret_target_prop = megasas_get_target_prop(instance, sdev);
4784 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4785 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4788 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4790 dev_info(&instance->pdev->dev, "Interrupts are enabled and"
4791 " controller is OPERATIONAL for scsi:%d\n",
4792 instance->host->host_no);
4794 /* Restart SR-IOV heartbeat */
4795 if (instance->requestorId) {
4796 if (!megasas_sriov_start_heartbeat(instance, 0))
4797 megasas_start_timer(instance);
4799 instance->skip_heartbeat_timer_del = 1;
4802 if (instance->crash_dump_drv_support &&
4803 instance->crash_dump_app_support)
4804 megasas_set_crash_dump_params(instance,
4805 MR_CRASH_BUF_TURN_ON);
4807 megasas_set_crash_dump_params(instance,
4808 MR_CRASH_BUF_TURN_OFF);
4810 if (instance->snapdump_wait_time) {
4811 megasas_get_snapdump_properties(instance);
4812 dev_info(&instance->pdev->dev,
4813 "Snap dump wait time\t: %d\n",
4814 instance->snapdump_wait_time);
4819 /* Adapter reset completed successfully */
4820 dev_warn(&instance->pdev->dev,
4821 "Reset successful for scsi%d.\n",
4822 instance->host->host_no);
4827 /* Reset failed, kill the adapter */
4828 dev_warn(&instance->pdev->dev, "Reset failed, killing "
4829 "adapter scsi%d.\n", instance->host->host_no);
4830 megaraid_sas_kill_hba(instance);
4831 instance->skip_heartbeat_timer_del = 1;
4834 /* For VF: Restart HB timer if we didn't OCR */
4835 if (instance->requestorId) {
4836 megasas_start_timer(instance);
4838 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4839 instance->instancet->enable_intr(instance);
4840 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4843 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4844 mutex_unlock(&instance->reset_mutex);
4848 /* Fusion Crash dump collection */
4849 void megasas_fusion_crash_dump(struct megasas_instance *instance)
4852 u8 partial_copy = 0;
4856 status_reg = instance->instancet->read_fw_status_reg(instance);
4859 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4860 * to host crash buffers
4862 if (instance->drv_buf_index == 0) {
4863 /* Buffer is already allocated for old Crash dump.
4864 * Do OCR and do not wait for crash dump collection
4866 if (instance->drv_buf_alloc) {
4867 dev_info(&instance->pdev->dev, "earlier crash dump is "
4868 "not yet copied by application, ignoring this "
4869 "crash dump and initiating OCR\n");
4870 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4872 &instance->reg_set->outbound_scratch_pad_0);
4873 readl(&instance->reg_set->outbound_scratch_pad_0);
4876 megasas_alloc_host_crash_buffer(instance);
4877 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4878 "allocated: %d\n", instance->drv_buf_alloc);
4881 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
4882 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
4883 if (!(status_reg & MFI_STATE_DMADONE)) {
4885 * Next crash dump buffer is not yet DMA'd by FW
4886 * Check after 10ms. Wait for 1 second for FW to
4887 * post the next buffer. If not bail out.
4890 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4891 status_reg = instance->instancet->read_fw_status_reg(
4897 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
4898 dev_info(&instance->pdev->dev,
4899 "Driver is done copying the buffer: %d\n",
4900 instance->drv_buf_alloc);
4901 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4905 memcpy(instance->crash_buf[instance->drv_buf_index],
4906 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4907 instance->drv_buf_index++;
4908 status_reg &= ~MFI_STATE_DMADONE;
4911 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4912 readl(&instance->reg_set->outbound_scratch_pad_0);
4914 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4915 status_reg = instance->instancet->read_fw_status_reg(instance);
4918 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4919 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4920 "of copied buffers: %d\n", instance->drv_buf_index);
4921 instance->fw_crash_buffer_size = instance->drv_buf_index;
4922 instance->fw_crash_state = AVAILABLE;
4923 instance->drv_buf_index = 0;
4924 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4925 readl(&instance->reg_set->outbound_scratch_pad_0);
4927 megasas_reset_fusion(instance->host, 0);
4932 /* Fusion OCR work queue */
4933 void megasas_fusion_ocr_wq(struct work_struct *work)
4935 struct megasas_instance *instance =
4936 container_of(work, struct megasas_instance, work_init);
4938 megasas_reset_fusion(instance->host, 0);
4941 /* Allocate fusion context */
4943 megasas_alloc_fusion_context(struct megasas_instance *instance)
4945 struct fusion_context *fusion;
4947 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
4949 if (!instance->ctrl_context) {
4950 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4951 __func__, __LINE__);
4955 fusion = instance->ctrl_context;
4957 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4958 sizeof(LD_SPAN_INFO));
4959 fusion->log_to_span =
4960 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4961 fusion->log_to_span_pages);
4962 if (!fusion->log_to_span) {
4963 fusion->log_to_span =
4964 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4965 sizeof(LD_SPAN_INFO)));
4966 if (!fusion->log_to_span) {
4967 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4968 __func__, __LINE__);
4973 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4974 sizeof(struct LD_LOAD_BALANCE_INFO));
4975 fusion->load_balance_info =
4976 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4977 fusion->load_balance_info_pages);
4978 if (!fusion->load_balance_info) {
4979 fusion->load_balance_info =
4980 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4981 sizeof(struct LD_LOAD_BALANCE_INFO)));
4982 if (!fusion->load_balance_info)
4983 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4984 "continuing without Load Balance support\n");
4991 megasas_free_fusion_context(struct megasas_instance *instance)
4993 struct fusion_context *fusion = instance->ctrl_context;
4996 if (fusion->load_balance_info) {
4997 if (is_vmalloc_addr(fusion->load_balance_info))
4998 vfree(fusion->load_balance_info);
5000 free_pages((ulong)fusion->load_balance_info,
5001 fusion->load_balance_info_pages);
5004 if (fusion->log_to_span) {
5005 if (is_vmalloc_addr(fusion->log_to_span))
5006 vfree(fusion->log_to_span);
5008 free_pages((ulong)fusion->log_to_span,
5009 fusion->log_to_span_pages);
5016 struct megasas_instance_template megasas_instance_template_fusion = {
5017 .enable_intr = megasas_enable_intr_fusion,
5018 .disable_intr = megasas_disable_intr_fusion,
5019 .clear_intr = megasas_clear_intr_fusion,
5020 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5021 .adp_reset = megasas_adp_reset_fusion,
5022 .check_reset = megasas_check_reset_fusion,
5023 .service_isr = megasas_isr_fusion,
5024 .tasklet = megasas_complete_cmd_dpc_fusion,
5025 .init_adapter = megasas_init_adapter_fusion,
5026 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5027 .issue_dcmd = megasas_issue_dcmd_fusion,