2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * FILE: megaraid_sas_fusion.c
22 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/uaccess.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
50 #include <linux/vmalloc.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_dbg.h>
57 #include <linux/dmi.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
63 extern void megasas_free_cmds(struct megasas_instance *instance);
64 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
67 megasas_complete_cmd(struct megasas_instance *instance,
68 struct megasas_cmd *cmd, u8 alt_status);
70 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
74 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
75 int megasas_alloc_cmds(struct megasas_instance *instance);
77 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
79 megasas_issue_polled(struct megasas_instance *instance,
80 struct megasas_cmd *cmd);
82 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
84 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
85 void megaraid_sas_kill_hba(struct megasas_instance *instance);
87 extern u32 megasas_dbg_lvl;
88 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
90 void megasas_start_timer(struct megasas_instance *instance);
91 extern struct megasas_mgmt_info megasas_mgmt_info;
92 extern unsigned int resetwaittime;
93 extern unsigned int dual_qdepth_disable;
94 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
95 static void megasas_free_reply_fusion(struct megasas_instance *instance);
97 void megasas_configure_queue_sizes(struct megasas_instance *instance);
100 * megasas_check_same_4gb_region - check if allocation
101 * crosses same 4GB boundary or not
102 * @instance - adapter's soft instance
103 * start_addr - start address of DMA allocation
104 * size - size of allocation in bytes
105 * return - true : allocation does not cross same
107 * false: allocation crosses same
110 static inline bool megasas_check_same_4gb_region
111 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
115 end_addr = start_addr + size;
117 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
118 dev_err(&instance->pdev->dev,
119 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
120 (unsigned long long)start_addr,
121 (unsigned long long)end_addr);
129 * megasas_enable_intr_fusion - Enables interrupts
130 * @regs: MFI register set
133 megasas_enable_intr_fusion(struct megasas_instance *instance)
135 struct megasas_register_set __iomem *regs;
136 regs = instance->reg_set;
138 instance->mask_interrupts = 0;
139 /* For Thunderbolt/Invader also clear intr on enable */
140 writel(~0, ®s->outbound_intr_status);
141 readl(®s->outbound_intr_status);
143 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
145 /* Dummy readl to force pci flush */
146 readl(®s->outbound_intr_mask);
150 * megasas_disable_intr_fusion - Disables interrupt
151 * @regs: MFI register set
154 megasas_disable_intr_fusion(struct megasas_instance *instance)
156 u32 mask = 0xFFFFFFFF;
158 struct megasas_register_set __iomem *regs;
159 regs = instance->reg_set;
160 instance->mask_interrupts = 1;
162 writel(mask, ®s->outbound_intr_mask);
163 /* Dummy readl to force pci flush */
164 status = readl(®s->outbound_intr_mask);
168 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
172 * Check if it is our interrupt
174 status = readl(®s->outbound_intr_status);
177 writel(status, ®s->outbound_intr_status);
178 readl(®s->outbound_intr_status);
181 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
188 * megasas_get_cmd_fusion - Get a command from the free pool
189 * @instance: Adapter soft state
191 * Returns a blk_tag indexed mpt frame
193 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
194 *instance, u32 blk_tag)
196 struct fusion_context *fusion;
198 fusion = instance->ctrl_context;
199 return fusion->cmd_list[blk_tag];
203 * megasas_return_cmd_fusion - Return a cmd to free command pool
204 * @instance: Adapter soft state
205 * @cmd: Command packet to be returned to free command pool
207 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
208 struct megasas_cmd_fusion *cmd)
211 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
212 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
213 cmd->cmd_completed = false;
217 * megasas_fire_cmd_fusion - Sends command to the FW
218 * @instance: Adapter soft state
219 * @req_desc: 64bit Request descriptor
225 megasas_fire_cmd_fusion(struct megasas_instance *instance,
226 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
228 #if defined(writeq) && defined(CONFIG_64BIT)
229 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
230 le32_to_cpu(req_desc->u.low));
232 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
235 spin_lock_irqsave(&instance->hba_lock, flags);
236 writel(le32_to_cpu(req_desc->u.low),
237 &instance->reg_set->inbound_low_queue_port);
238 writel(le32_to_cpu(req_desc->u.high),
239 &instance->reg_set->inbound_high_queue_port);
241 spin_unlock_irqrestore(&instance->hba_lock, flags);
246 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
247 * @instance: Adapter soft state
248 * fw_boot_context: Whether this function called during probe or after OCR
250 * This function is only for fusion controllers.
251 * Update host can queue, if firmware downgrade max supported firmware commands.
252 * Firmware upgrade case will be skiped because underlying firmware has
253 * more resource than exposed to the OS.
257 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
259 u16 cur_max_fw_cmds = 0;
260 u16 ldio_threshold = 0;
261 struct megasas_register_set __iomem *reg_set;
263 reg_set = instance->reg_set;
265 /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
266 if (instance->adapter_type < VENTURA_SERIES)
268 readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
270 if (dual_qdepth_disable || !cur_max_fw_cmds)
271 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
274 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
276 dev_info(&instance->pdev->dev,
277 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
278 cur_max_fw_cmds, ldio_threshold);
280 if (fw_boot_context == OCR_CONTEXT) {
281 cur_max_fw_cmds = cur_max_fw_cmds - 1;
282 if (cur_max_fw_cmds < instance->max_fw_cmds) {
283 instance->cur_can_queue =
284 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
285 MEGASAS_FUSION_IOCTL_CMDS);
286 instance->host->can_queue = instance->cur_can_queue;
287 instance->ldio_threshold = ldio_threshold;
290 instance->max_fw_cmds = cur_max_fw_cmds;
291 instance->ldio_threshold = ldio_threshold;
294 instance->max_fw_cmds = min(instance->max_fw_cmds,
295 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
297 * Reduce the max supported cmds by 1. This is to ensure that the
298 * reply_q_sz (1 more than the max cmd that driver may send)
299 * does not exceed max cmds that the FW can support
301 instance->max_fw_cmds = instance->max_fw_cmds-1;
305 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
306 * @instance: Adapter soft state
309 megasas_free_cmds_fusion(struct megasas_instance *instance)
312 struct fusion_context *fusion = instance->ctrl_context;
313 struct megasas_cmd_fusion *cmd;
316 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
317 fusion->sense_phys_addr);
320 if (fusion->cmd_list) {
321 for (i = 0; i < instance->max_mpt_cmds; i++) {
322 cmd = fusion->cmd_list[i];
325 dma_pool_free(fusion->sg_dma_pool,
327 cmd->sg_frame_phys_addr);
331 kfree(fusion->cmd_list);
334 if (fusion->sg_dma_pool) {
335 dma_pool_destroy(fusion->sg_dma_pool);
336 fusion->sg_dma_pool = NULL;
338 if (fusion->sense_dma_pool) {
339 dma_pool_destroy(fusion->sense_dma_pool);
340 fusion->sense_dma_pool = NULL;
344 /* Reply Frame, Desc*/
345 if (instance->is_rdpq)
346 megasas_free_rdpq_fusion(instance);
348 megasas_free_reply_fusion(instance);
350 /* Request Frame, Desc*/
351 if (fusion->req_frames_desc)
352 dma_free_coherent(&instance->pdev->dev,
353 fusion->request_alloc_sz, fusion->req_frames_desc,
354 fusion->req_frames_desc_phys);
355 if (fusion->io_request_frames)
356 dma_pool_free(fusion->io_request_frames_pool,
357 fusion->io_request_frames,
358 fusion->io_request_frames_phys);
359 if (fusion->io_request_frames_pool) {
360 dma_pool_destroy(fusion->io_request_frames_pool);
361 fusion->io_request_frames_pool = NULL;
366 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
367 * @instance: Adapter soft state
370 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
374 struct fusion_context *fusion;
375 struct megasas_cmd_fusion *cmd;
379 fusion = instance->ctrl_context;
380 max_cmd = instance->max_fw_cmds;
381 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
383 fusion->sg_dma_pool =
384 dma_pool_create("mr_sg", &instance->pdev->dev,
385 instance->max_chain_frame_sz,
386 MR_DEFAULT_NVME_PAGE_SIZE, 0);
387 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
388 fusion->sense_dma_pool =
389 dma_pool_create("mr_sense", &instance->pdev->dev,
392 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
393 dev_err(&instance->pdev->dev,
394 "Failed from %s %d\n", __func__, __LINE__);
398 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
399 GFP_KERNEL, &fusion->sense_phys_addr);
400 if (!fusion->sense) {
401 dev_err(&instance->pdev->dev,
402 "failed from %s %d\n", __func__, __LINE__);
406 /* sense buffer, request frame and reply desc pool requires to be in
407 * same 4 gb region. Below function will check this.
408 * In case of failure, new pci pool will be created with updated
410 * Older allocation and pool will be destroyed.
411 * Alignment will be used such a way that next allocation if success,
412 * will always meet same 4gb region requirement.
413 * Actual requirement is not alignment, but we need start and end of
414 * DMA address must have same upper 32 bit address.
417 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
419 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
420 fusion->sense_phys_addr);
421 fusion->sense = NULL;
422 dma_pool_destroy(fusion->sense_dma_pool);
424 fusion->sense_dma_pool =
425 dma_pool_create("mr_sense_align", &instance->pdev->dev,
426 sense_sz, roundup_pow_of_two(sense_sz),
428 if (!fusion->sense_dma_pool) {
429 dev_err(&instance->pdev->dev,
430 "Failed from %s %d\n", __func__, __LINE__);
433 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
435 &fusion->sense_phys_addr);
436 if (!fusion->sense) {
437 dev_err(&instance->pdev->dev,
438 "failed from %s %d\n", __func__, __LINE__);
444 * Allocate and attach a frame to each of the commands in cmd_list
446 for (i = 0; i < max_cmd; i++) {
447 cmd = fusion->cmd_list[i];
448 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
449 GFP_KERNEL, &cmd->sg_frame_phys_addr);
451 offset = SCSI_SENSE_BUFFERSIZE * i;
452 cmd->sense = (u8 *)fusion->sense + offset;
453 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
455 if (!cmd->sg_frame) {
456 dev_err(&instance->pdev->dev,
457 "Failed from %s %d\n", __func__, __LINE__);
462 /* create sense buffer for the raid 1/10 fp */
463 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
464 cmd = fusion->cmd_list[i];
465 offset = SCSI_SENSE_BUFFERSIZE * i;
466 cmd->sense = (u8 *)fusion->sense + offset;
467 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
475 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
477 u32 max_mpt_cmd, i, j;
478 struct fusion_context *fusion;
480 fusion = instance->ctrl_context;
482 max_mpt_cmd = instance->max_mpt_cmds;
485 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
486 * Allocate the dynamic array first and then allocate individual
490 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
492 if (!fusion->cmd_list) {
493 dev_err(&instance->pdev->dev,
494 "Failed from %s %d\n", __func__, __LINE__);
498 for (i = 0; i < max_mpt_cmd; i++) {
499 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
501 if (!fusion->cmd_list[i]) {
502 for (j = 0; j < i; j++)
503 kfree(fusion->cmd_list[j]);
504 kfree(fusion->cmd_list);
505 dev_err(&instance->pdev->dev,
506 "Failed from %s %d\n", __func__, __LINE__);
514 megasas_alloc_request_fusion(struct megasas_instance *instance)
516 struct fusion_context *fusion;
518 fusion = instance->ctrl_context;
521 fusion->io_request_frames_pool =
522 dma_pool_create("mr_ioreq", &instance->pdev->dev,
523 fusion->io_frames_alloc_sz, 16, 0);
525 if (!fusion->io_request_frames_pool) {
526 dev_err(&instance->pdev->dev,
527 "Failed from %s %d\n", __func__, __LINE__);
531 fusion->io_request_frames =
532 dma_pool_alloc(fusion->io_request_frames_pool,
533 GFP_KERNEL | __GFP_NOWARN,
534 &fusion->io_request_frames_phys);
535 if (!fusion->io_request_frames) {
536 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
537 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
538 dma_pool_destroy(fusion->io_request_frames_pool);
539 megasas_configure_queue_sizes(instance);
542 dev_err(&instance->pdev->dev,
543 "Failed from %s %d\n", __func__, __LINE__);
548 if (!megasas_check_same_4gb_region(instance,
549 fusion->io_request_frames_phys,
550 fusion->io_frames_alloc_sz)) {
551 dma_pool_free(fusion->io_request_frames_pool,
552 fusion->io_request_frames,
553 fusion->io_request_frames_phys);
554 fusion->io_request_frames = NULL;
555 dma_pool_destroy(fusion->io_request_frames_pool);
557 fusion->io_request_frames_pool =
558 dma_pool_create("mr_ioreq_align",
559 &instance->pdev->dev,
560 fusion->io_frames_alloc_sz,
561 roundup_pow_of_two(fusion->io_frames_alloc_sz),
564 if (!fusion->io_request_frames_pool) {
565 dev_err(&instance->pdev->dev,
566 "Failed from %s %d\n", __func__, __LINE__);
570 fusion->io_request_frames =
571 dma_pool_alloc(fusion->io_request_frames_pool,
572 GFP_KERNEL | __GFP_NOWARN,
573 &fusion->io_request_frames_phys);
575 if (!fusion->io_request_frames) {
576 dev_err(&instance->pdev->dev,
577 "Failed from %s %d\n", __func__, __LINE__);
582 fusion->req_frames_desc =
583 dma_alloc_coherent(&instance->pdev->dev,
584 fusion->request_alloc_sz,
585 &fusion->req_frames_desc_phys, GFP_KERNEL);
586 if (!fusion->req_frames_desc) {
587 dev_err(&instance->pdev->dev,
588 "Failed from %s %d\n", __func__, __LINE__);
596 megasas_alloc_reply_fusion(struct megasas_instance *instance)
599 struct fusion_context *fusion;
600 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
601 fusion = instance->ctrl_context;
603 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
604 fusion->reply_frames_desc_pool =
605 dma_pool_create("mr_reply", &instance->pdev->dev,
606 fusion->reply_alloc_sz * count, 16, 0);
608 if (!fusion->reply_frames_desc_pool) {
609 dev_err(&instance->pdev->dev,
610 "Failed from %s %d\n", __func__, __LINE__);
614 fusion->reply_frames_desc[0] =
615 dma_pool_alloc(fusion->reply_frames_desc_pool,
616 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
617 if (!fusion->reply_frames_desc[0]) {
618 dev_err(&instance->pdev->dev,
619 "Failed from %s %d\n", __func__, __LINE__);
623 if (!megasas_check_same_4gb_region(instance,
624 fusion->reply_frames_desc_phys[0],
625 (fusion->reply_alloc_sz * count))) {
626 dma_pool_free(fusion->reply_frames_desc_pool,
627 fusion->reply_frames_desc[0],
628 fusion->reply_frames_desc_phys[0]);
629 fusion->reply_frames_desc[0] = NULL;
630 dma_pool_destroy(fusion->reply_frames_desc_pool);
632 fusion->reply_frames_desc_pool =
633 dma_pool_create("mr_reply_align",
634 &instance->pdev->dev,
635 fusion->reply_alloc_sz * count,
636 roundup_pow_of_two(fusion->reply_alloc_sz * count),
639 if (!fusion->reply_frames_desc_pool) {
640 dev_err(&instance->pdev->dev,
641 "Failed from %s %d\n", __func__, __LINE__);
645 fusion->reply_frames_desc[0] =
646 dma_pool_alloc(fusion->reply_frames_desc_pool,
648 &fusion->reply_frames_desc_phys[0]);
650 if (!fusion->reply_frames_desc[0]) {
651 dev_err(&instance->pdev->dev,
652 "Failed from %s %d\n", __func__, __LINE__);
657 reply_desc = fusion->reply_frames_desc[0];
658 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
659 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
661 /* This is not a rdpq mode, but driver still populate
662 * reply_frame_desc array to use same msix index in ISR path.
664 for (i = 0; i < (count - 1); i++)
665 fusion->reply_frames_desc[i + 1] =
666 fusion->reply_frames_desc[i] +
667 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
673 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
675 int i, j, k, msix_count;
676 struct fusion_context *fusion;
677 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
678 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
679 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
680 u8 dma_alloc_count, abs_index;
681 u32 chunk_size, array_size, offset;
683 fusion = instance->ctrl_context;
684 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
685 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
686 MAX_MSIX_QUEUES_FUSION;
688 fusion->rdpq_virt = pci_zalloc_consistent(instance->pdev, array_size,
690 if (!fusion->rdpq_virt) {
691 dev_err(&instance->pdev->dev,
692 "Failed from %s %d\n", __func__, __LINE__);
696 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
698 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
699 &instance->pdev->dev,
701 fusion->reply_frames_desc_pool_align =
702 dma_pool_create("mr_rdpq_align",
703 &instance->pdev->dev,
705 roundup_pow_of_two(chunk_size),
708 if (!fusion->reply_frames_desc_pool ||
709 !fusion->reply_frames_desc_pool_align) {
710 dev_err(&instance->pdev->dev,
711 "Failed from %s %d\n", __func__, __LINE__);
716 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
717 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
718 * within 4GB boundary and also reply queues in a set must have same
719 * upper 32-bits in their memory address. so here driver is allocating the
720 * DMA'able memory for reply queues according. Driver uses limitation of
721 * VENTURA_SERIES to manage INVADER_SERIES as well.
723 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
725 for (i = 0; i < dma_alloc_count; i++) {
727 dma_pool_alloc(fusion->reply_frames_desc_pool,
728 GFP_KERNEL, &rdpq_chunk_phys[i]);
729 if (!rdpq_chunk_virt[i]) {
730 dev_err(&instance->pdev->dev,
731 "Failed from %s %d\n", __func__, __LINE__);
734 /* reply desc pool requires to be in same 4 gb region.
735 * Below function will check this.
736 * In case of failure, new pci pool will be created with updated
738 * For RDPQ buffers, driver always allocate two separate pci pool.
739 * Alignment will be used such a way that next allocation if
740 * success, will always meet same 4gb region requirement.
741 * rdpq_tracker keep track of each buffer's physical,
742 * virtual address and pci pool descriptor. It will help driver
743 * while freeing the resources.
746 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
748 dma_pool_free(fusion->reply_frames_desc_pool,
753 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
754 GFP_KERNEL, &rdpq_chunk_phys[i]);
755 if (!rdpq_chunk_virt[i]) {
756 dev_err(&instance->pdev->dev,
757 "Failed from %s %d\n",
761 fusion->rdpq_tracker[i].dma_pool_ptr =
762 fusion->reply_frames_desc_pool_align;
764 fusion->rdpq_tracker[i].dma_pool_ptr =
765 fusion->reply_frames_desc_pool;
768 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
769 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
772 for (k = 0; k < dma_alloc_count; k++) {
773 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
774 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
776 if (abs_index == msix_count)
778 offset = fusion->reply_alloc_sz * i;
779 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
780 cpu_to_le64(rdpq_chunk_phys[k] + offset);
781 fusion->reply_frames_desc_phys[abs_index] =
782 rdpq_chunk_phys[k] + offset;
783 fusion->reply_frames_desc[abs_index] =
784 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
786 reply_desc = fusion->reply_frames_desc[abs_index];
787 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
788 reply_desc->Words = ULLONG_MAX;
796 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
799 struct fusion_context *fusion;
801 fusion = instance->ctrl_context;
803 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
804 if (fusion->rdpq_tracker[i].pool_entry_virt)
805 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
806 fusion->rdpq_tracker[i].pool_entry_virt,
807 fusion->rdpq_tracker[i].pool_entry_phys);
811 if (fusion->reply_frames_desc_pool)
812 dma_pool_destroy(fusion->reply_frames_desc_pool);
813 if (fusion->reply_frames_desc_pool_align)
814 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
816 if (fusion->rdpq_virt)
817 pci_free_consistent(instance->pdev,
818 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
819 fusion->rdpq_virt, fusion->rdpq_phys);
823 megasas_free_reply_fusion(struct megasas_instance *instance) {
825 struct fusion_context *fusion;
827 fusion = instance->ctrl_context;
829 if (fusion->reply_frames_desc[0])
830 dma_pool_free(fusion->reply_frames_desc_pool,
831 fusion->reply_frames_desc[0],
832 fusion->reply_frames_desc_phys[0]);
834 if (fusion->reply_frames_desc_pool)
835 dma_pool_destroy(fusion->reply_frames_desc_pool);
841 * megasas_alloc_cmds_fusion - Allocates the command packets
842 * @instance: Adapter soft state
845 * Each frame has a 32-bit field called context. This context is used to get
846 * back the megasas_cmd_fusion from the frame when a frame gets completed
847 * In this driver, the 32 bit values are the indices into an array cmd_list.
848 * This array is used only to look up the megasas_cmd_fusion given the context.
849 * The free commands themselves are maintained in a linked list called cmd_pool.
851 * cmds are formed in the io_request and sg_frame members of the
852 * megasas_cmd_fusion. The context field is used to get a request descriptor
853 * and is used as SMID of the cmd.
854 * SMID value range is from 1 to max_fw_cmds.
857 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
860 struct fusion_context *fusion;
861 struct megasas_cmd_fusion *cmd;
863 dma_addr_t io_req_base_phys;
867 fusion = instance->ctrl_context;
869 if (megasas_alloc_request_fusion(instance))
872 if (instance->is_rdpq) {
873 if (megasas_alloc_rdpq_fusion(instance))
876 if (megasas_alloc_reply_fusion(instance))
879 if (megasas_alloc_cmdlist_fusion(instance))
882 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
883 instance->max_fw_cmds);
885 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
886 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
887 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
890 * Add all the commands to command pool (fusion->cmd_pool)
893 /* SMID 0 is reserved. Set SMID/index from 1 */
894 for (i = 0; i < instance->max_mpt_cmds; i++) {
895 cmd = fusion->cmd_list[i];
896 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
897 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
901 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
902 (i - instance->max_scsi_cmds) :
903 (u32)ULONG_MAX; /* Set to Invalid */
904 cmd->instance = instance;
906 (struct MPI2_RAID_SCSI_IO_REQUEST *)
907 (io_req_base + offset);
908 memset(cmd->io_request, 0,
909 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
910 cmd->io_request_phys_addr = io_req_base_phys + offset;
911 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
914 if (megasas_create_sg_sense_fusion(instance))
920 megasas_free_cmds_fusion(instance);
925 * wait_and_poll - Issues a polling command
926 * @instance: Adapter soft state
927 * @cmd: Command packet to be issued
929 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
932 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
936 struct megasas_header *frame_hdr = &cmd->frame->hdr;
937 struct fusion_context *fusion;
939 u32 msecs = seconds * 1000;
941 fusion = instance->ctrl_context;
943 * Wait for cmd_status to change
945 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
950 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
952 else if (frame_hdr->cmd_status == MFI_STAT_OK)
959 * megasas_ioc_init_fusion - Initializes the FW
960 * @instance: Adapter soft state
962 * Issues the IOC Init cmd
965 megasas_ioc_init_fusion(struct megasas_instance *instance)
967 struct megasas_init_frame *init_frame;
968 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
969 dma_addr_t ioc_init_handle;
970 struct megasas_cmd *cmd;
971 u8 ret, cur_rdpq_mode;
972 struct fusion_context *fusion;
973 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
975 struct megasas_header *frame_hdr;
976 const char *sys_info;
977 MFI_CAPABILITIES *drv_ops;
980 bool cur_fw_64bit_dma_capable;
982 fusion = instance->ctrl_context;
984 ioc_init_handle = fusion->ioc_init_request_phys;
985 IOCInitMessage = fusion->ioc_init_request;
987 cmd = fusion->ioc_init_cmd;
989 scratch_pad_2 = readl
990 (&instance->reg_set->outbound_scratch_pad_2);
992 cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
994 if (instance->adapter_type == INVADER_SERIES) {
995 cur_fw_64bit_dma_capable =
996 (scratch_pad_2 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
998 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
999 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1000 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1001 megaraid_sas_kill_hba(instance);
1007 if (instance->is_rdpq && !cur_rdpq_mode) {
1008 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1009 " from RDPQ mode to non RDPQ mode\n");
1014 instance->fw_sync_cache_support = (scratch_pad_2 &
1015 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1016 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1017 instance->fw_sync_cache_support ? "Yes" : "No");
1019 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1021 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1022 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1023 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1024 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1025 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1027 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1028 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1029 cpu_to_le64(fusion->rdpq_phys) :
1030 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1031 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1032 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1033 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1034 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1035 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1036 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1038 time = ktime_get_real();
1039 /* Convert to milliseconds as per FW requirement */
1040 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1042 init_frame = (struct megasas_init_frame *)cmd->frame;
1043 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1045 frame_hdr = &cmd->frame->hdr;
1046 frame_hdr->cmd_status = 0xFF;
1047 frame_hdr->flags = cpu_to_le16(
1048 le16_to_cpu(frame_hdr->flags) |
1049 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 (readl(&instance->reg_set->doorbell) & 1)
1117 megasas_fire_cmd_fusion(instance, &req_desc);
1119 wait_and_poll(instance, cmd, MFI_POLL_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 -
1569 (MEGASAS_FUSION_INTERNAL_CMDS +
1570 MEGASAS_FUSION_IOCTL_CMDS);
1571 instance->cur_can_queue = instance->max_scsi_cmds;
1572 instance->host->can_queue = instance->cur_can_queue;
1574 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1576 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1577 instance->max_mpt_cmds;
1578 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1579 (fusion->reply_q_depth);
1580 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1581 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1582 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1585 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1587 struct fusion_context *fusion;
1588 struct megasas_cmd *cmd;
1590 fusion = instance->ctrl_context;
1592 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1595 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1596 __func__, __LINE__);
1600 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1601 IOC_INIT_FRAME_SIZE,
1602 &cmd->frame_phys_addr, GFP_KERNEL);
1605 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1606 __func__, __LINE__);
1611 fusion->ioc_init_cmd = cmd;
1616 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1617 * @instance: Adapter soft state
1619 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1621 struct fusion_context *fusion;
1623 fusion = instance->ctrl_context;
1625 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1626 dma_free_coherent(&instance->pdev->dev,
1627 IOC_INIT_FRAME_SIZE,
1628 fusion->ioc_init_cmd->frame,
1629 fusion->ioc_init_cmd->frame_phys_addr);
1631 if (fusion->ioc_init_cmd)
1632 kfree(fusion->ioc_init_cmd);
1636 * megasas_init_adapter_fusion - Initializes the FW
1637 * @instance: Adapter soft state
1639 * This is the main function for initializing firmware.
1642 megasas_init_adapter_fusion(struct megasas_instance *instance)
1644 struct megasas_register_set __iomem *reg_set;
1645 struct fusion_context *fusion;
1649 fusion = instance->ctrl_context;
1651 reg_set = instance->reg_set;
1653 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1656 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1658 instance->max_mfi_cmds =
1659 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1661 megasas_configure_queue_sizes(instance);
1663 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1664 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1665 * Firmware support extended IO chain frame which is 4 times more than
1667 * Legacy Firmware - Frame size is (8 * 128) = 1K
1668 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1670 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1671 instance->max_chain_frame_sz =
1672 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1673 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1675 instance->max_chain_frame_sz =
1676 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1677 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1679 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1680 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1681 instance->max_chain_frame_sz,
1682 MEGASAS_CHAIN_FRAME_SZ_MIN);
1683 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1686 fusion->max_sge_in_main_msg =
1687 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1688 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1690 fusion->max_sge_in_chain =
1691 instance->max_chain_frame_sz
1692 / sizeof(union MPI2_SGE_IO_UNION);
1694 instance->max_num_sge =
1695 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1696 + fusion->max_sge_in_chain - 2);
1698 /* Used for pass thru MFI frame (DCMD) */
1699 fusion->chain_offset_mfi_pthru =
1700 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1702 fusion->chain_offset_io_request =
1703 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1704 sizeof(union MPI2_SGE_IO_UNION))/16;
1706 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1707 for (i = 0 ; i < count; i++)
1708 fusion->last_reply_idx[i] = 0;
1711 * For fusion adapters, 3 commands for IOCTL and 8 commands
1712 * for driver's internal DCMDs.
1714 instance->max_scsi_cmds = instance->max_fw_cmds -
1715 (MEGASAS_FUSION_INTERNAL_CMDS +
1716 MEGASAS_FUSION_IOCTL_CMDS);
1717 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1719 if (megasas_alloc_ioc_init_frame(instance))
1723 * Allocate memory for descriptors
1724 * Create a pool of commands
1726 if (megasas_alloc_cmds(instance))
1727 goto fail_alloc_mfi_cmds;
1728 if (megasas_alloc_cmds_fusion(instance))
1729 goto fail_alloc_cmds;
1731 if (megasas_ioc_init_fusion(instance))
1734 megasas_display_intel_branding(instance);
1735 if (megasas_get_ctrl_info(instance)) {
1736 dev_err(&instance->pdev->dev,
1737 "Could not get controller info. Fail from %s %d\n",
1738 __func__, __LINE__);
1742 instance->flag_ieee = 1;
1743 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1744 fusion->fast_path_io = 0;
1746 if (megasas_allocate_raid_maps(instance))
1749 if (!megasas_get_map_info(instance))
1750 megasas_sync_map_info(instance);
1755 megasas_free_cmds_fusion(instance);
1757 megasas_free_cmds(instance);
1758 fail_alloc_mfi_cmds:
1759 megasas_free_ioc_init_cmd(instance);
1764 * map_cmd_status - Maps FW cmd status to OS cmd status
1765 * @cmd : Pointer to cmd
1766 * @status : status of cmd returned by FW
1767 * @ext_status : ext status of cmd returned by FW
1771 map_cmd_status(struct fusion_context *fusion,
1772 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1773 u32 data_length, u8 *sense)
1778 cmd_type = megasas_cmd_type(scmd);
1782 scmd->result = DID_OK << 16;
1785 case MFI_STAT_SCSI_IO_FAILED:
1786 case MFI_STAT_LD_INIT_IN_PROGRESS:
1787 scmd->result = (DID_ERROR << 16) | ext_status;
1790 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1792 scmd->result = (DID_OK << 16) | ext_status;
1793 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1794 memset(scmd->sense_buffer, 0,
1795 SCSI_SENSE_BUFFERSIZE);
1796 memcpy(scmd->sense_buffer, sense,
1797 SCSI_SENSE_BUFFERSIZE);
1798 scmd->result |= DRIVER_SENSE << 24;
1802 * If the IO request is partially completed, then MR FW will
1803 * update "io_request->DataLength" field with actual number of
1804 * bytes transferred.Driver will set residual bytes count in
1805 * SCSI command structure.
1807 resid = (scsi_bufflen(scmd) - data_length);
1808 scsi_set_resid(scmd, resid);
1811 ((cmd_type == READ_WRITE_LDIO) ||
1812 (cmd_type == READ_WRITE_SYSPDIO)))
1813 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1814 " requested/completed 0x%x/0x%x\n",
1815 status, scsi_bufflen(scmd), data_length);
1818 case MFI_STAT_LD_OFFLINE:
1819 case MFI_STAT_DEVICE_NOT_FOUND:
1820 scmd->result = DID_BAD_TARGET << 16;
1822 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1823 scmd->result = DID_IMM_RETRY << 16;
1826 scmd->result = DID_ERROR << 16;
1832 * megasas_is_prp_possible -
1833 * Checks if native NVMe PRPs can be built for the IO
1835 * @instance: Adapter soft state
1836 * @scmd: SCSI command from the mid-layer
1837 * @sge_count: scatter gather element count.
1839 * Returns: true: PRPs can be built
1840 * false: IEEE SGLs needs to be built
1843 megasas_is_prp_possible(struct megasas_instance *instance,
1844 struct scsi_cmnd *scmd, int sge_count)
1846 struct fusion_context *fusion;
1848 u32 data_length = 0;
1849 struct scatterlist *sg_scmd;
1850 bool build_prp = false;
1851 u32 mr_nvme_pg_size;
1853 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1854 MR_DEFAULT_NVME_PAGE_SIZE);
1855 fusion = instance->ctrl_context;
1856 data_length = scsi_bufflen(scmd);
1857 sg_scmd = scsi_sglist(scmd);
1860 * NVMe uses one PRP for each page (or part of a page)
1861 * look at the data length - if 4 pages or less then IEEE is OK
1862 * if > 5 pages then we need to build a native SGL
1863 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1864 * if this first size in the page is >= the residual beyond 4 pages
1865 * then use IEEE, otherwise use native SGL
1868 if (data_length > (mr_nvme_pg_size * 5)) {
1870 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1871 (data_length <= (mr_nvme_pg_size * 5))) {
1872 /* check if 1st SG entry size is < residual beyond 4 pages */
1873 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1878 * Below code detects gaps/holes in IO data buffers.
1879 * What does holes/gaps mean?
1880 * Any SGE except first one in a SGL starts at non NVME page size
1881 * aligned address OR Any SGE except last one in a SGL ends at
1882 * non NVME page size boundary.
1884 * Driver has already informed block layer by setting boundary rules for
1885 * bio merging done at NVME page size boundary calling kernel API
1886 * blk_queue_virt_boundary inside slave_config.
1887 * Still there is possibility of IO coming with holes to driver because of
1888 * IO merging done by IO scheduler.
1890 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1891 * IO scheduling so no IO merging.
1893 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1894 * then sending IOs with holes.
1896 * Though driver can request block layer to disable IO merging by calling-
1897 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1898 * user may tune sysfs parameter- nomerges again to 0 or 1.
1900 * If in future IO scheduling is enabled with SCSI BLK MQ,
1901 * this algorithm to detect holes will be required in driver
1902 * for SCSI BLK MQ enabled case as well.
1906 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1907 if ((i != 0) && (i != (sge_count - 1))) {
1908 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1909 mega_mod64(sg_dma_address(sg_scmd),
1912 atomic_inc(&instance->sge_holes_type1);
1917 if ((sge_count > 1) && (i == 0)) {
1918 if ((mega_mod64((sg_dma_address(sg_scmd) +
1919 sg_dma_len(sg_scmd)),
1920 mr_nvme_pg_size))) {
1922 atomic_inc(&instance->sge_holes_type2);
1927 if ((sge_count > 1) && (i == (sge_count - 1))) {
1928 if (mega_mod64(sg_dma_address(sg_scmd),
1931 atomic_inc(&instance->sge_holes_type3);
1941 * megasas_make_prp_nvme -
1942 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1944 * @instance: Adapter soft state
1945 * @scmd: SCSI command from the mid-layer
1946 * @sgl_ptr: SGL to be filled in
1947 * @cmd: Fusion command frame
1948 * @sge_count: scatter gather element count.
1950 * Returns: true: PRPs are built
1951 * false: IEEE SGLs needs to be built
1954 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
1955 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1956 struct megasas_cmd_fusion *cmd, int sge_count)
1958 int sge_len, offset, num_prp_in_chain = 0;
1959 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
1961 dma_addr_t ptr_sgl_phys;
1963 u32 page_mask, page_mask_result;
1964 struct scatterlist *sg_scmd;
1966 bool build_prp = false;
1967 int data_len = scsi_bufflen(scmd);
1968 struct fusion_context *fusion;
1969 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1970 MR_DEFAULT_NVME_PAGE_SIZE);
1972 fusion = instance->ctrl_context;
1974 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
1980 * Nvme has a very convoluted prp format. One prp is required
1981 * for each page or partial page. Driver need to split up OS sg_list
1982 * entries if it is longer than one page or cross a page
1983 * boundary. Driver also have to insert a PRP list pointer entry as
1984 * the last entry in each physical page of the PRP list.
1986 * NOTE: The first PRP "entry" is actually placed in the first
1987 * SGL entry in the main message as IEEE 64 format. The 2nd
1988 * entry in the main message is the chain element, and the rest
1989 * of the PRP entries are built in the contiguous pcie buffer.
1991 page_mask = mr_nvme_pg_size - 1;
1992 ptr_sgl = (u64 *)cmd->sg_frame;
1993 ptr_sgl_phys = cmd->sg_frame_phys_addr;
1994 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
1996 /* Build chain frame element which holds all prps except first*/
1997 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
1998 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2000 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2001 main_chain_element->NextChainOffset = 0;
2002 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2003 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2004 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2006 /* Build first prp, sge need not to be page aligned*/
2007 ptr_first_sgl = sgl_ptr;
2008 sg_scmd = scsi_sglist(scmd);
2009 sge_addr = sg_dma_address(sg_scmd);
2010 sge_len = sg_dma_len(sg_scmd);
2012 offset = (u32)(sge_addr & page_mask);
2013 first_prp_len = mr_nvme_pg_size - offset;
2015 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2016 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2018 data_len -= first_prp_len;
2020 if (sge_len > first_prp_len) {
2021 sge_addr += first_prp_len;
2022 sge_len -= first_prp_len;
2023 } else if (sge_len == first_prp_len) {
2024 sg_scmd = sg_next(sg_scmd);
2025 sge_addr = sg_dma_address(sg_scmd);
2026 sge_len = sg_dma_len(sg_scmd);
2030 offset = (u32)(sge_addr & page_mask);
2032 /* Put PRP pointer due to page boundary*/
2033 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2034 if (unlikely(!page_mask_result)) {
2035 scmd_printk(KERN_NOTICE,
2036 scmd, "page boundary ptr_sgl: 0x%p\n",
2039 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2044 *ptr_sgl = cpu_to_le64(sge_addr);
2049 sge_addr += mr_nvme_pg_size;
2050 sge_len -= mr_nvme_pg_size;
2051 data_len -= mr_nvme_pg_size;
2059 sg_scmd = sg_next(sg_scmd);
2060 sge_addr = sg_dma_address(sg_scmd);
2061 sge_len = sg_dma_len(sg_scmd);
2064 main_chain_element->Length =
2065 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2067 atomic_inc(&instance->prp_sgl);
2072 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2073 * @instance: Adapter soft state
2074 * @scp: SCSI command from the mid-layer
2075 * @sgl_ptr: SGL to be filled in
2076 * @cmd: cmd we are working on
2077 * @sge_count sge count
2081 megasas_make_sgl_fusion(struct megasas_instance *instance,
2082 struct scsi_cmnd *scp,
2083 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2084 struct megasas_cmd_fusion *cmd, int sge_count)
2086 int i, sg_processed;
2087 struct scatterlist *os_sgl;
2088 struct fusion_context *fusion;
2090 fusion = instance->ctrl_context;
2092 if (instance->adapter_type >= INVADER_SERIES) {
2093 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2094 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2095 sgl_ptr_end->Flags = 0;
2098 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2099 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2100 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2102 if (instance->adapter_type >= INVADER_SERIES)
2103 if (i == sge_count - 1)
2104 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2106 sg_processed = i + 1;
2108 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2109 (sge_count > fusion->max_sge_in_main_msg)) {
2111 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2112 if (instance->adapter_type >= INVADER_SERIES) {
2113 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2114 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2115 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2116 cmd->io_request->ChainOffset =
2118 chain_offset_io_request;
2120 cmd->io_request->ChainOffset = 0;
2122 cmd->io_request->ChainOffset =
2123 fusion->chain_offset_io_request;
2126 /* Prepare chain element */
2127 sg_chain->NextChainOffset = 0;
2128 if (instance->adapter_type >= INVADER_SERIES)
2129 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2132 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2133 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2134 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2135 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2138 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2139 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2142 atomic_inc(&instance->ieee_sgl);
2146 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2147 * @scp: SCSI command pointer
2148 * @instance: Soft instance of controller
2149 * @cmd: Fusion command pointer
2151 * This function will build sgls based on device type.
2152 * For nvme drives, there is different way of building sgls in nvme native
2153 * format- PRPs(Physical Region Page).
2155 * Returns the number of sg lists actually used, zero if the sg lists
2156 * is NULL, or -ENOMEM if the mapping failed
2159 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2160 struct megasas_cmd_fusion *cmd)
2163 bool build_prp = false;
2164 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2166 sge_count = scsi_dma_map(scp);
2168 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2171 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2172 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2173 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2174 (cmd->pd_interface == NVME_PD))
2175 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2179 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2186 * megasas_set_pd_lba - Sets PD LBA
2188 * @cdb_len: cdb length
2189 * @start_blk: Start block of IO
2191 * Used to set the PD LBA in CDB for FP IOs
2194 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2195 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2196 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2198 struct MR_LD_RAID *raid;
2200 u64 start_blk = io_info->pdBlock;
2201 u8 *cdb = io_request->CDB.CDB32;
2202 u32 num_blocks = io_info->numBlocks;
2203 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2205 /* Check if T10 PI (DIF) is enabled for this LD */
2206 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2207 raid = MR_LdRaidGet(ld, local_map_ptr);
2208 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2209 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2210 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2211 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2213 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2214 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2216 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2217 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2220 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2221 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2222 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2223 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2224 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2225 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2226 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2227 cdb[19] = (u8)(start_blk & 0xff);
2229 /* Logical block reference tag */
2230 io_request->CDB.EEDP32.PrimaryReferenceTag =
2231 cpu_to_be32(ref_tag);
2232 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2233 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2235 /* Transfer length */
2236 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2237 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2238 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2239 cdb[31] = (u8)(num_blocks & 0xff);
2241 /* set SCSI IO EEDPFlags */
2242 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
2243 io_request->EEDPFlags = cpu_to_le16(
2244 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2245 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2246 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2247 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2248 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2249 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2251 io_request->EEDPFlags = cpu_to_le16(
2252 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2253 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2255 io_request->Control |= cpu_to_le32((0x4 << 26));
2256 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2258 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2259 if (((cdb_len == 12) || (cdb_len == 16)) &&
2260 (start_blk <= 0xffffffff)) {
2261 if (cdb_len == 16) {
2262 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2267 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2273 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2280 /* Transfer length */
2281 cdb[8] = (u8)(num_blocks & 0xff);
2282 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2284 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2286 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2287 /* Convert to 16 byte CDB for large LBA's */
2290 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2295 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2302 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2309 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2316 /* Transfer length */
2317 cdb[13] = (u8)(num_blocks & 0xff);
2318 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2319 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2320 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2322 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2326 /* Normal case, just load LBA here */
2330 u8 val = cdb[1] & 0xE0;
2331 cdb[3] = (u8)(start_blk & 0xff);
2332 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2333 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2337 cdb[5] = (u8)(start_blk & 0xff);
2338 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2339 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2340 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2343 cdb[5] = (u8)(start_blk & 0xff);
2344 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2345 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2346 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2349 cdb[9] = (u8)(start_blk & 0xff);
2350 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2351 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2352 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2353 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2354 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2355 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2356 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2363 * megasas_stream_detect - stream detection on read and and write IOs
2364 * @instance: Adapter soft state
2365 * @cmd: Command to be prepared
2366 * @io_info: IO Request info
2370 /** stream detection on read and and write IOs */
2371 static void megasas_stream_detect(struct megasas_instance *instance,
2372 struct megasas_cmd_fusion *cmd,
2373 struct IO_REQUEST_INFO *io_info)
2375 struct fusion_context *fusion = instance->ctrl_context;
2376 u32 device_id = io_info->ldTgtId;
2377 struct LD_STREAM_DETECT *current_ld_sd
2378 = fusion->stream_detect_by_ld[device_id];
2379 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2380 u32 shifted_values, unshifted_values;
2381 u32 index_value_mask, shifted_values_mask;
2383 bool is_read_ahead = false;
2384 struct STREAM_DETECT *current_sd;
2385 /* find possible stream */
2386 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2387 stream_num = (*track_stream >>
2388 (i * BITS_PER_INDEX_STREAM)) &
2390 current_sd = ¤t_ld_sd->stream_track[stream_num];
2391 /* if we found a stream, update the raid
2392 * context and also update the mruBitMap
2394 /* boundary condition */
2395 if ((current_sd->next_seq_lba) &&
2396 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2397 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2398 (current_sd->is_read == io_info->isRead)) {
2400 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2401 ((!io_info->isRead) || (!is_read_ahead)))
2403 * Once the API availible we need to change this.
2404 * At this point we are not allowing any gap
2408 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2409 current_sd->next_seq_lba =
2410 io_info->ldStartBlock + io_info->numBlocks;
2412 * update the mruBitMap LRU
2414 shifted_values_mask =
2415 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2416 shifted_values = ((*track_stream & shifted_values_mask)
2417 << BITS_PER_INDEX_STREAM);
2419 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2421 *track_stream & ~(shifted_values_mask |
2424 unshifted_values | shifted_values | stream_num;
2429 * if we did not find any stream, create a new one
2430 * from the least recently used
2432 stream_num = (*track_stream >>
2433 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2435 current_sd = ¤t_ld_sd->stream_track[stream_num];
2436 current_sd->is_read = io_info->isRead;
2437 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2438 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2443 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2444 * affinity (cpu of the controller) and raid_flags in the raid context
2447 * @praid_context: IO RAID context
2448 * @raid: LD raid map
2449 * @fp_possible: Is fast path possible?
2450 * @is_read: Is read IO?
2454 megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2455 struct MR_LD_RAID *raid, bool fp_possible,
2456 u8 is_read, u32 scsi_buff_len)
2458 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2459 struct RAID_CONTEXT_G35 *rctx_g35;
2461 rctx_g35 = &praid_context->raid_context_g35;
2464 if ((raid->cpuAffinity.pdRead.cpu0) &&
2465 (raid->cpuAffinity.pdRead.cpu1))
2466 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2467 else if (raid->cpuAffinity.pdRead.cpu1)
2468 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2470 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2471 (raid->cpuAffinity.pdWrite.cpu1))
2472 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2473 else if (raid->cpuAffinity.pdWrite.cpu1)
2474 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2475 /* Fast path cache by pass capable R0/R1 VD */
2476 if ((raid->level <= 1) &&
2477 (raid->capability.fp_cache_bypass_capable)) {
2478 rctx_g35->routing_flags |=
2479 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2480 rctx_g35->raid_flags =
2481 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2482 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2487 if ((raid->cpuAffinity.ldRead.cpu0) &&
2488 (raid->cpuAffinity.ldRead.cpu1))
2489 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2490 else if (raid->cpuAffinity.ldRead.cpu1)
2491 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2493 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2494 (raid->cpuAffinity.ldWrite.cpu1))
2495 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2496 else if (raid->cpuAffinity.ldWrite.cpu1)
2497 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2499 if (is_stream_detected(rctx_g35) &&
2500 ((raid->level == 5) || (raid->level == 6)) &&
2501 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2502 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2503 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2507 rctx_g35->routing_flags |=
2508 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2510 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2511 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2512 * IO Subtype is not bitmap.
2514 if ((raid->level == 1) && (!is_read)) {
2515 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2516 praid_context->raid_context_g35.raid_flags =
2517 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2518 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2523 * megasas_build_ldio_fusion - Prepares IOs to devices
2524 * @instance: Adapter soft state
2525 * @scp: SCSI command
2526 * @cmd: Command to be prepared
2528 * Prepares the io_request and chain elements (sg_frame) for IO
2529 * The IO can be for PD (Fast Path) or LD
2532 megasas_build_ldio_fusion(struct megasas_instance *instance,
2533 struct scsi_cmnd *scp,
2534 struct megasas_cmd_fusion *cmd)
2538 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2540 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2541 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2542 struct IO_REQUEST_INFO io_info;
2543 struct fusion_context *fusion;
2544 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2546 unsigned long spinlock_flags;
2547 union RAID_CONTEXT_UNION *praid_context;
2548 struct MR_LD_RAID *raid = NULL;
2549 struct MR_PRIV_DEVICE *mrdev_priv;
2551 device_id = MEGASAS_DEV_INDEX(scp);
2553 fusion = instance->ctrl_context;
2555 io_request = cmd->io_request;
2556 io_request->RaidContext.raid_context.virtual_disk_tgt_id =
2557 cpu_to_le16(device_id);
2558 io_request->RaidContext.raid_context.status = 0;
2559 io_request->RaidContext.raid_context.ex_status = 0;
2561 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2565 fp_possible = false;
2568 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2570 if (scp->cmd_len == 6) {
2571 datalength = (u32) scp->cmnd[4];
2572 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2573 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2575 start_lba_lo &= 0x1FFFFF;
2579 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2581 else if (scp->cmd_len == 10) {
2582 datalength = (u32) scp->cmnd[8] |
2583 ((u32) scp->cmnd[7] << 8);
2584 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2585 ((u32) scp->cmnd[3] << 16) |
2586 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2590 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2592 else if (scp->cmd_len == 12) {
2593 datalength = ((u32) scp->cmnd[6] << 24) |
2594 ((u32) scp->cmnd[7] << 16) |
2595 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2596 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2597 ((u32) scp->cmnd[3] << 16) |
2598 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2602 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2604 else if (scp->cmd_len == 16) {
2605 datalength = ((u32) scp->cmnd[10] << 24) |
2606 ((u32) scp->cmnd[11] << 16) |
2607 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2608 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2609 ((u32) scp->cmnd[7] << 16) |
2610 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2612 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2613 ((u32) scp->cmnd[3] << 16) |
2614 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2617 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2618 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2619 io_info.numBlocks = datalength;
2620 io_info.ldTgtId = device_id;
2621 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2622 scsi_buff_len = scsi_bufflen(scp);
2623 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2625 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2628 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2629 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2631 if (ld < instance->fw_supported_vd_count)
2632 raid = MR_LdRaidGet(ld, local_map_ptr);
2634 if (!raid || (!fusion->fast_path_io)) {
2635 io_request->RaidContext.raid_context.reg_lock_flags = 0;
2636 fp_possible = false;
2638 if (MR_BuildRaidContext(instance, &io_info,
2639 &io_request->RaidContext.raid_context,
2640 local_map_ptr, &raidLUN))
2641 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2644 cmd->request_desc->SCSIIO.MSIxIndex =
2645 instance->reply_map[raw_smp_processor_id()];
2647 praid_context = &io_request->RaidContext;
2649 if (instance->adapter_type == VENTURA_SERIES) {
2650 /* FP for Optimal raid level 1.
2651 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2652 * are built by the driver as LD I/Os.
2653 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2654 * (there is never a reason to process these as buffered writes)
2655 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2656 * with the SLD bit asserted.
2658 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2659 mrdev_priv = scp->device->hostdata;
2661 if (atomic_inc_return(&instance->fw_outstanding) >
2662 (instance->host->can_queue)) {
2663 fp_possible = false;
2664 atomic_dec(&instance->fw_outstanding);
2665 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2666 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
2667 fp_possible = false;
2668 atomic_dec(&instance->fw_outstanding);
2669 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2670 atomic_set(&mrdev_priv->r1_ldio_hint,
2671 instance->r1_ldio_hint_default);
2676 (io_info.isRead && io_info.ra_capable)) {
2677 spin_lock_irqsave(&instance->stream_lock,
2679 megasas_stream_detect(instance, cmd, &io_info);
2680 spin_unlock_irqrestore(&instance->stream_lock,
2682 /* In ventura if stream detected for a read and it is
2683 * read ahead capable make this IO as LDIO
2685 if (is_stream_detected(&io_request->RaidContext.raid_context_g35))
2686 fp_possible = false;
2689 /* If raid is NULL, set CPU affinity to default CPU0 */
2691 megasas_set_raidflag_cpu_affinity(praid_context,
2692 raid, fp_possible, io_info.isRead,
2695 praid_context->raid_context_g35.routing_flags |=
2696 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2700 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2701 local_map_ptr, start_lba_lo);
2702 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2703 cmd->request_desc->SCSIIO.RequestFlags =
2704 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2705 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2706 if (instance->adapter_type == INVADER_SERIES) {
2707 if (io_request->RaidContext.raid_context.reg_lock_flags ==
2709 cmd->request_desc->SCSIIO.RequestFlags =
2710 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2711 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2712 io_request->RaidContext.raid_context.type
2714 io_request->RaidContext.raid_context.nseg = 0x1;
2715 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2716 io_request->RaidContext.raid_context.reg_lock_flags |=
2717 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2718 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2719 } else if (instance->adapter_type == VENTURA_SERIES) {
2720 io_request->RaidContext.raid_context_g35.nseg_type |=
2721 (1 << RAID_CONTEXT_NSEG_SHIFT);
2722 io_request->RaidContext.raid_context_g35.nseg_type |=
2723 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2724 io_request->RaidContext.raid_context_g35.routing_flags |=
2725 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2726 io_request->IoFlags |=
2727 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2729 if (fusion->load_balance_info &&
2730 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2733 get_updated_dev_handle(instance,
2734 &fusion->load_balance_info[device_id],
2735 &io_info, local_map_ptr);
2736 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2737 cmd->pd_r1_lb = io_info.pd_after_lb;
2738 if (instance->adapter_type == VENTURA_SERIES)
2739 io_request->RaidContext.raid_context_g35.span_arm
2742 io_request->RaidContext.raid_context.span_arm
2746 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2748 if (instance->adapter_type == VENTURA_SERIES)
2749 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2751 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2753 if ((raidLUN[0] == 1) &&
2754 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2755 instance->dev_handle = !(instance->dev_handle);
2757 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2760 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2761 io_request->DevHandle = io_info.devHandle;
2762 cmd->pd_interface = io_info.pd_interface;
2763 /* populate the LUN field */
2764 memcpy(io_request->LUN, raidLUN, 8);
2766 io_request->RaidContext.raid_context.timeout_value =
2767 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2768 cmd->request_desc->SCSIIO.RequestFlags =
2769 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2770 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2771 if (instance->adapter_type == INVADER_SERIES) {
2772 if (io_info.do_fp_rlbypass ||
2773 (io_request->RaidContext.raid_context.reg_lock_flags
2774 == REGION_TYPE_UNUSED))
2775 cmd->request_desc->SCSIIO.RequestFlags =
2776 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2777 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2778 io_request->RaidContext.raid_context.type
2780 io_request->RaidContext.raid_context.reg_lock_flags |=
2781 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2782 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2783 io_request->RaidContext.raid_context.nseg = 0x1;
2784 } else if (instance->adapter_type == VENTURA_SERIES) {
2785 io_request->RaidContext.raid_context_g35.routing_flags |=
2786 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2787 io_request->RaidContext.raid_context_g35.nseg_type |=
2788 (1 << RAID_CONTEXT_NSEG_SHIFT);
2789 io_request->RaidContext.raid_context_g35.nseg_type |=
2790 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2792 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2793 io_request->DevHandle = cpu_to_le16(device_id);
2799 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2800 * @instance: Adapter soft state
2801 * @scp: SCSI command
2802 * @cmd: Command to be prepared
2804 * Prepares the io_request frame for non-rw io cmds for vd.
2806 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2807 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2810 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2812 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2813 struct fusion_context *fusion = instance->ctrl_context;
2817 struct MR_LD_RAID *raid;
2818 struct RAID_CONTEXT *pRAID_Context;
2821 io_request = cmd->io_request;
2822 device_id = MEGASAS_DEV_INDEX(scmd);
2823 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2824 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2825 /* get RAID_Context pointer */
2826 pRAID_Context = &io_request->RaidContext.raid_context;
2827 /* Check with FW team */
2828 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2829 pRAID_Context->reg_lock_row_lba = 0;
2830 pRAID_Context->reg_lock_length = 0;
2832 if (fusion->fast_path_io && (
2833 device_id < instance->fw_supported_vd_count)) {
2835 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2836 if (ld >= instance->fw_supported_vd_count - 1)
2839 raid = MR_LdRaidGet(ld, local_map_ptr);
2840 if (!(raid->capability.fpNonRWCapable))
2847 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2848 io_request->DevHandle = cpu_to_le16(device_id);
2849 io_request->LUN[1] = scmd->device->lun;
2850 pRAID_Context->timeout_value =
2851 cpu_to_le16 (scmd->request->timeout / HZ);
2852 cmd->request_desc->SCSIIO.RequestFlags =
2853 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2854 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2857 /* set RAID context values */
2858 pRAID_Context->config_seq_num = raid->seqNum;
2859 if (instance->adapter_type != VENTURA_SERIES)
2860 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2861 pRAID_Context->timeout_value =
2862 cpu_to_le16(raid->fpIoTimeoutForLd);
2864 /* get the DevHandle for the PD (since this is
2865 fpNonRWCapable, this is a single disk RAID0) */
2867 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2868 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2869 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2871 /* build request descriptor */
2872 cmd->request_desc->SCSIIO.RequestFlags =
2873 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2874 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2875 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2877 /* populate the LUN field */
2878 memcpy(io_request->LUN, raid->LUN, 8);
2880 /* build the raidScsiIO structure */
2881 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2882 io_request->DevHandle = devHandle;
2887 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2888 * @instance: Adapter soft state
2889 * @scp: SCSI command
2890 * @cmd: Command to be prepared
2891 * @fp_possible: parameter to detect fast path or firmware path io.
2893 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2896 megasas_build_syspd_fusion(struct megasas_instance *instance,
2897 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2901 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2903 u16 os_timeout_value;
2905 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2906 struct RAID_CONTEXT *pRAID_Context;
2907 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
2908 struct MR_PRIV_DEVICE *mr_device_priv_data;
2909 struct fusion_context *fusion = instance->ctrl_context;
2910 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
2912 device_id = MEGASAS_DEV_INDEX(scmd);
2913 pd_index = MEGASAS_PD_INDEX(scmd);
2914 os_timeout_value = scmd->request->timeout / HZ;
2915 mr_device_priv_data = scmd->device->hostdata;
2916 cmd->pd_interface = mr_device_priv_data->interface_type;
2918 io_request = cmd->io_request;
2919 /* get RAID_Context pointer */
2920 pRAID_Context = &io_request->RaidContext.raid_context;
2921 pRAID_Context->reg_lock_flags = 0;
2922 pRAID_Context->reg_lock_row_lba = 0;
2923 pRAID_Context->reg_lock_length = 0;
2924 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2925 io_request->LUN[1] = scmd->device->lun;
2926 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
2927 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2929 /* If FW supports PD sequence number */
2930 if (instance->use_seqnum_jbod_fp &&
2931 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2932 /* TgtId must be incremented by 255 as jbod seq number is index
2935 /* More than 256 PD/JBOD support for Ventura */
2936 if (instance->support_morethan256jbod)
2937 pRAID_Context->virtual_disk_tgt_id =
2938 pd_sync->seq[pd_index].pd_target_id;
2940 pRAID_Context->virtual_disk_tgt_id =
2941 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
2942 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
2943 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
2944 if (instance->adapter_type == VENTURA_SERIES) {
2945 io_request->RaidContext.raid_context_g35.routing_flags |=
2946 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2947 io_request->RaidContext.raid_context_g35.nseg_type |=
2948 (1 << RAID_CONTEXT_NSEG_SHIFT);
2949 io_request->RaidContext.raid_context_g35.nseg_type |=
2950 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2952 pRAID_Context->type = MPI2_TYPE_CUDA;
2953 pRAID_Context->nseg = 0x1;
2954 pRAID_Context->reg_lock_flags |=
2955 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
2957 } else if (fusion->fast_path_io) {
2958 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2959 pRAID_Context->config_seq_num = 0;
2960 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2961 io_request->DevHandle =
2962 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
2964 /* Want to send all IO via FW path */
2965 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2966 pRAID_Context->config_seq_num = 0;
2967 io_request->DevHandle = cpu_to_le16(0xFFFF);
2970 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
2972 cmd->request_desc->SCSIIO.MSIxIndex =
2973 instance->reply_map[raw_smp_processor_id()];
2976 /* system pd firmware path */
2977 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2978 cmd->request_desc->SCSIIO.RequestFlags =
2979 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2980 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2981 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
2982 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2984 if (os_timeout_value)
2987 /* system pd Fast Path */
2988 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2989 timeout_limit = (scmd->device->type == TYPE_DISK) ?
2991 pRAID_Context->timeout_value =
2992 cpu_to_le16((os_timeout_value > timeout_limit) ?
2993 timeout_limit : os_timeout_value);
2994 if (instance->adapter_type >= INVADER_SERIES)
2995 io_request->IoFlags |=
2996 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2998 cmd->request_desc->SCSIIO.RequestFlags =
2999 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3000 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3005 * megasas_build_io_fusion - Prepares IOs to devices
3006 * @instance: Adapter soft state
3007 * @scp: SCSI command
3008 * @cmd: Command to be prepared
3010 * Invokes helper functions to prepare request frames
3011 * and sets flags appropriate for IO/Non-IO cmd
3014 megasas_build_io_fusion(struct megasas_instance *instance,
3015 struct scsi_cmnd *scp,
3016 struct megasas_cmd_fusion *cmd)
3020 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3021 struct MR_PRIV_DEVICE *mr_device_priv_data;
3022 mr_device_priv_data = scp->device->hostdata;
3024 /* Zero out some fields so they don't get reused */
3025 memset(io_request->LUN, 0x0, 8);
3026 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3027 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3028 io_request->EEDPFlags = 0;
3029 io_request->Control = 0;
3030 io_request->EEDPBlockSize = 0;
3031 io_request->ChainOffset = 0;
3032 io_request->RaidContext.raid_context.raid_flags = 0;
3033 io_request->RaidContext.raid_context.type = 0;
3034 io_request->RaidContext.raid_context.nseg = 0;
3036 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3038 * Just the CDB length,rest of the Flags are zero
3039 * This will be modified for FP in build_ldio_fusion
3041 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3043 switch (cmd_type = megasas_cmd_type(scp)) {
3044 case READ_WRITE_LDIO:
3045 megasas_build_ldio_fusion(instance, scp, cmd);
3047 case NON_READ_WRITE_LDIO:
3048 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3050 case READ_WRITE_SYSPDIO:
3051 megasas_build_syspd_fusion(instance, scp, cmd, true);
3053 case NON_READ_WRITE_SYSPDIO:
3054 if (instance->secure_jbod_support ||
3055 mr_device_priv_data->is_tm_capable)
3056 megasas_build_syspd_fusion(instance, scp, cmd, false);
3058 megasas_build_syspd_fusion(instance, scp, cmd, true);
3068 sge_count = megasas_make_sgl(instance, scp, cmd);
3070 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3071 dev_err(&instance->pdev->dev,
3072 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3073 __func__, __LINE__, sge_count, instance->max_num_sge);
3077 if (instance->adapter_type == VENTURA_SERIES) {
3078 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3079 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3080 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3082 /* numSGE store lower 8 bit of sge_count.
3083 * numSGEExt store higher 8 bit of sge_count
3085 io_request->RaidContext.raid_context.num_sge = sge_count;
3086 io_request->RaidContext.raid_context.num_sge_ext =
3087 (u8)(sge_count >> 8);
3090 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3092 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
3093 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3094 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
3095 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3097 io_request->SGLOffset0 =
3098 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3100 io_request->SenseBufferLowAddress =
3101 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3102 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3105 scp->SCp.ptr = (char *)cmd;
3110 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3111 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3114 struct fusion_context *fusion;
3116 fusion = instance->ctrl_context;
3117 p = fusion->req_frames_desc +
3118 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3120 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3124 /* megasas_prepate_secondRaid1_IO
3125 * It prepares the raid 1 second IO
3127 void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3128 struct megasas_cmd_fusion *cmd,
3129 struct megasas_cmd_fusion *r1_cmd)
3131 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3132 struct fusion_context *fusion;
3133 fusion = instance->ctrl_context;
3134 req_desc = cmd->request_desc;
3135 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3136 memcpy(r1_cmd->io_request, cmd->io_request,
3137 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3138 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3139 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3140 /*sense buffer is different for r1 command*/
3141 r1_cmd->io_request->SenseBufferLowAddress =
3142 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3143 r1_cmd->scmd = cmd->scmd;
3144 req_desc2 = megasas_get_request_descriptor(instance,
3145 (r1_cmd->index - 1));
3146 req_desc2->Words = 0;
3147 r1_cmd->request_desc = req_desc2;
3148 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3149 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3150 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3151 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3152 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3153 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3154 cpu_to_le16(r1_cmd->index);
3155 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3156 cpu_to_le16(cmd->index);
3157 /*MSIxIndex of both commands request descriptors should be same*/
3158 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3159 cmd->request_desc->SCSIIO.MSIxIndex;
3160 /*span arm is different for r1 cmd*/
3161 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3162 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3166 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3167 * issuing non IOCTL cmd
3168 * @instance: Adapter soft state
3169 * @scmd: pointer to scsi cmd from OS
3172 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3173 struct scsi_cmnd *scmd)
3175 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3176 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3178 struct fusion_context *fusion;
3180 fusion = instance->ctrl_context;
3182 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3183 instance->ldio_threshold &&
3184 (atomic_inc_return(&instance->ldio_outstanding) >
3185 instance->ldio_threshold)) {
3186 atomic_dec(&instance->ldio_outstanding);
3187 return SCSI_MLQUEUE_DEVICE_BUSY;
3190 if (atomic_inc_return(&instance->fw_outstanding) >
3191 instance->host->can_queue) {
3192 atomic_dec(&instance->fw_outstanding);
3193 return SCSI_MLQUEUE_HOST_BUSY;
3196 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3199 atomic_dec(&instance->fw_outstanding);
3200 return SCSI_MLQUEUE_HOST_BUSY;
3205 req_desc = megasas_get_request_descriptor(instance, index-1);
3207 req_desc->Words = 0;
3208 cmd->request_desc = req_desc;
3210 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3211 megasas_return_cmd_fusion(instance, cmd);
3212 dev_err(&instance->pdev->dev, "Error building command\n");
3213 cmd->request_desc = NULL;
3214 atomic_dec(&instance->fw_outstanding);
3215 return SCSI_MLQUEUE_HOST_BUSY;
3218 req_desc = cmd->request_desc;
3219 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3221 if (cmd->io_request->ChainOffset != 0 &&
3222 cmd->io_request->ChainOffset != 0xF)
3223 dev_err(&instance->pdev->dev, "The chain offset value is not "
3224 "correct : %x\n", cmd->io_request->ChainOffset);
3226 * if it is raid 1/10 fp write capable.
3227 * try to get second command from pool and construct it.
3228 * From FW, it has confirmed that lba values of two PDs
3229 * corresponds to single R1/10 LD are always same
3232 /* driver side count always should be less than max_fw_cmds
3233 * to get new command
3235 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3236 r1_cmd = megasas_get_cmd_fusion(instance,
3237 (scmd->request->tag + instance->max_fw_cmds));
3238 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3243 * Issue the command to the FW
3246 megasas_fire_cmd_fusion(instance, req_desc);
3249 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3256 * megasas_complete_r1_command -
3257 * completes R1 FP write commands which has valid peer smid
3258 * @instance: Adapter soft state
3259 * @cmd_fusion: MPT command frame
3263 megasas_complete_r1_command(struct megasas_instance *instance,
3264 struct megasas_cmd_fusion *cmd)
3266 u8 *sense, status, ex_status;
3269 struct fusion_context *fusion;
3270 struct megasas_cmd_fusion *r1_cmd = NULL;
3271 struct scsi_cmnd *scmd_local = NULL;
3272 struct RAID_CONTEXT_G35 *rctx_g35;
3274 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3275 fusion = instance->ctrl_context;
3276 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
3278 r1_cmd = fusion->cmd_list[peer_smid - 1];
3279 scmd_local = cmd->scmd;
3280 status = rctx_g35->status;
3281 ex_status = rctx_g35->ex_status;
3282 data_length = cmd->io_request->DataLength;
3285 cmd->cmd_completed = true;
3287 /* Check if peer command is completed or not*/
3288 if (r1_cmd->cmd_completed) {
3289 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3290 if (rctx_g35->status != MFI_STAT_OK) {
3291 status = rctx_g35->status;
3292 ex_status = rctx_g35->ex_status;
3293 data_length = r1_cmd->io_request->DataLength;
3294 sense = r1_cmd->sense;
3297 megasas_return_cmd_fusion(instance, r1_cmd);
3298 map_cmd_status(fusion, scmd_local, status, ex_status,
3299 le32_to_cpu(data_length), sense);
3300 if (instance->ldio_threshold &&
3301 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3302 atomic_dec(&instance->ldio_outstanding);
3303 scmd_local->SCp.ptr = NULL;
3304 megasas_return_cmd_fusion(instance, cmd);
3305 scsi_dma_unmap(scmd_local);
3306 scmd_local->scsi_done(scmd_local);
3311 * complete_cmd_fusion - Completes command
3312 * @instance: Adapter soft state
3313 * Completes all commands that is in reply descriptor queue
3316 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
3318 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3319 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3320 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3321 struct fusion_context *fusion;
3322 struct megasas_cmd *cmd_mfi;
3323 struct megasas_cmd_fusion *cmd_fusion;
3324 u16 smid, num_completed;
3325 u8 reply_descript_type, *sense, status, extStatus;
3326 u32 device_id, data_length;
3327 union desc_value d_val;
3328 struct LD_LOAD_BALANCE_INFO *lbinfo;
3329 int threshold_reply_count = 0;
3330 struct scsi_cmnd *scmd_local = NULL;
3331 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3332 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3334 fusion = instance->ctrl_context;
3336 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3339 desc = fusion->reply_frames_desc[MSIxIndex] +
3340 fusion->last_reply_idx[MSIxIndex];
3342 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3344 d_val.word = desc->Words;
3346 reply_descript_type = reply_desc->ReplyFlags &
3347 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3349 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3354 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3355 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3357 smid = le16_to_cpu(reply_desc->SMID);
3358 cmd_fusion = fusion->cmd_list[smid - 1];
3359 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3360 cmd_fusion->io_request;
3362 scmd_local = cmd_fusion->scmd;
3363 status = scsi_io_req->RaidContext.raid_context.status;
3364 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3365 sense = cmd_fusion->sense;
3366 data_length = scsi_io_req->DataLength;
3368 switch (scsi_io_req->Function) {
3369 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3370 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3371 cmd_fusion->io_request;
3372 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3373 &mr_tm_req->TmRequest;
3374 dev_dbg(&instance->pdev->dev, "TM completion:"
3375 "type: 0x%x TaskMID: 0x%x\n",
3376 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3377 complete(&cmd_fusion->done);
3379 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3380 /* Update load balancing info */
3381 if (fusion->load_balance_info &&
3382 (cmd_fusion->scmd->SCp.Status &
3383 MEGASAS_LOAD_BALANCE_FLAG)) {
3384 device_id = MEGASAS_DEV_INDEX(scmd_local);
3385 lbinfo = &fusion->load_balance_info[device_id];
3386 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3387 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3389 //Fall thru and complete IO
3390 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3391 atomic_dec(&instance->fw_outstanding);
3392 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3393 map_cmd_status(fusion, scmd_local, status,
3394 extStatus, le32_to_cpu(data_length),
3396 if (instance->ldio_threshold &&
3397 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3398 atomic_dec(&instance->ldio_outstanding);
3399 scmd_local->SCp.ptr = NULL;
3400 megasas_return_cmd_fusion(instance, cmd_fusion);
3401 scsi_dma_unmap(scmd_local);
3402 scmd_local->scsi_done(scmd_local);
3403 } else /* Optimal VD - R1 FP command completion. */
3404 megasas_complete_r1_command(instance, cmd_fusion);
3406 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3407 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3408 /* Poll mode. Dummy free.
3409 * In case of Interrupt mode, caller has reverse check.
3411 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3412 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3413 megasas_return_cmd(instance, cmd_mfi);
3415 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3419 fusion->last_reply_idx[MSIxIndex]++;
3420 if (fusion->last_reply_idx[MSIxIndex] >=
3421 fusion->reply_q_depth)
3422 fusion->last_reply_idx[MSIxIndex] = 0;
3424 desc->Words = cpu_to_le64(ULLONG_MAX);
3426 threshold_reply_count++;
3428 /* Get the next reply descriptor */
3429 if (!fusion->last_reply_idx[MSIxIndex])
3430 desc = fusion->reply_frames_desc[MSIxIndex];
3435 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3437 d_val.word = desc->Words;
3439 reply_descript_type = reply_desc->ReplyFlags &
3440 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3442 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3445 * Write to reply post host index register after completing threshold
3446 * number of reply counts and still there are more replies in reply queue
3447 * pending to be completed
3449 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
3450 if (instance->msix_combined)
3451 writel(((MSIxIndex & 0x7) << 24) |
3452 fusion->last_reply_idx[MSIxIndex],
3453 instance->reply_post_host_index_addr[MSIxIndex/8]);
3455 writel((MSIxIndex << 24) |
3456 fusion->last_reply_idx[MSIxIndex],
3457 instance->reply_post_host_index_addr[0]);
3458 threshold_reply_count = 0;
3466 if (instance->msix_combined)
3467 writel(((MSIxIndex & 0x7) << 24) |
3468 fusion->last_reply_idx[MSIxIndex],
3469 instance->reply_post_host_index_addr[MSIxIndex/8]);
3471 writel((MSIxIndex << 24) |
3472 fusion->last_reply_idx[MSIxIndex],
3473 instance->reply_post_host_index_addr[0]);
3474 megasas_check_and_restore_queue_depth(instance);
3479 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3480 * @instance: Adapter soft state
3482 void megasas_sync_irqs(unsigned long instance_addr)
3485 struct megasas_instance *instance =
3486 (struct megasas_instance *)instance_addr;
3488 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3490 for (i = 0; i < count; i++)
3491 synchronize_irq(pci_irq_vector(instance->pdev, i));
3495 * megasas_complete_cmd_dpc_fusion - Completes command
3496 * @instance: Adapter soft state
3498 * Tasklet to complete cmds
3501 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3503 struct megasas_instance *instance =
3504 (struct megasas_instance *)instance_addr;
3505 unsigned long flags;
3506 u32 count, MSIxIndex;
3508 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3510 /* If we have already declared adapter dead, donot complete cmds */
3511 spin_lock_irqsave(&instance->hba_lock, flags);
3512 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
3513 spin_unlock_irqrestore(&instance->hba_lock, flags);
3516 spin_unlock_irqrestore(&instance->hba_lock, flags);
3518 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3519 complete_cmd_fusion(instance, MSIxIndex);
3523 * megasas_isr_fusion - isr entry point
3525 irqreturn_t megasas_isr_fusion(int irq, void *devp)
3527 struct megasas_irq_context *irq_context = devp;
3528 struct megasas_instance *instance = irq_context->instance;
3529 u32 mfiStatus, fw_state, dma_state;
3531 if (instance->mask_interrupts)
3534 if (!instance->msix_vectors) {
3535 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3540 /* If we are resetting, bail */
3541 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3542 instance->instancet->clear_intr(instance->reg_set);
3546 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
3547 instance->instancet->clear_intr(instance->reg_set);
3548 /* If we didn't complete any commands, check for FW fault */
3549 fw_state = instance->instancet->read_fw_status_reg(
3550 instance->reg_set) & MFI_STATE_MASK;
3551 dma_state = instance->instancet->read_fw_status_reg
3552 (instance->reg_set) & MFI_STATE_DMADONE;
3553 if (instance->crash_dump_drv_support &&
3554 instance->crash_dump_app_support) {
3555 /* Start collecting crash, if DMA bit is done */
3556 if ((fw_state == MFI_STATE_FAULT) && dma_state)
3557 schedule_work(&instance->crash_init);
3558 else if (fw_state == MFI_STATE_FAULT) {
3559 if (instance->unload == 0)
3560 schedule_work(&instance->work_init);
3562 } else if (fw_state == MFI_STATE_FAULT) {
3563 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
3564 "for scsi%d\n", instance->host->host_no);
3565 if (instance->unload == 0)
3566 schedule_work(&instance->work_init);
3574 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3575 * @instance: Adapter soft state
3576 * mfi_cmd: megasas_cmd pointer
3580 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3581 struct megasas_cmd *mfi_cmd)
3583 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3584 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3585 struct megasas_cmd_fusion *cmd;
3586 struct fusion_context *fusion;
3587 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3589 fusion = instance->ctrl_context;
3591 cmd = megasas_get_cmd_fusion(instance,
3592 instance->max_scsi_cmds + mfi_cmd->index);
3594 /* Save the smid. To be used for returning the cmd */
3595 mfi_cmd->context.smid = cmd->index;
3598 * For cmds where the flag is set, store the flag and check
3599 * on completion. For cmds with this flag, don't call
3600 * megasas_complete_cmd
3603 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3604 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3606 io_req = cmd->io_request;
3608 if (instance->adapter_type >= INVADER_SERIES) {
3609 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3610 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3611 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3612 sgl_ptr_end->Flags = 0;
3616 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3618 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3619 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3621 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3623 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3625 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3626 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3628 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3632 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3633 * @instance: Adapter soft state
3634 * @cmd: mfi cmd to build
3637 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3638 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3640 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3643 build_mpt_mfi_pass_thru(instance, cmd);
3644 index = cmd->context.smid;
3646 req_desc = megasas_get_request_descriptor(instance, index - 1);
3648 req_desc->Words = 0;
3649 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3650 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3652 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3658 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3659 * @instance: Adapter soft state
3660 * @cmd: mfi cmd pointer
3664 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3665 struct megasas_cmd *cmd)
3667 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3669 req_desc = build_mpt_cmd(instance, cmd);
3671 megasas_fire_cmd_fusion(instance, req_desc);
3676 * megasas_release_fusion - Reverses the FW initialization
3677 * @instance: Adapter soft state
3680 megasas_release_fusion(struct megasas_instance *instance)
3682 megasas_free_ioc_init_cmd(instance);
3683 megasas_free_cmds(instance);
3684 megasas_free_cmds_fusion(instance);
3686 iounmap(instance->reg_set);
3688 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3692 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3693 * @regs: MFI register set
3696 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3698 return readl(&(regs)->outbound_scratch_pad);
3702 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3703 * @instance: Controller's soft instance
3704 * return: Number of allocated host crash buffers
3707 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3711 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3712 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3713 if (!instance->crash_buf[i]) {
3714 dev_info(&instance->pdev->dev, "Firmware crash dump "
3715 "memory allocation failed at index %d\n", i);
3719 instance->drv_buf_alloc = i;
3723 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3724 * @instance: Controller's soft instance
3727 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3730 for (i = 0; i < instance->drv_buf_alloc; i++) {
3731 if (instance->crash_buf[i])
3732 vfree(instance->crash_buf[i]);
3734 instance->drv_buf_index = 0;
3735 instance->drv_buf_alloc = 0;
3736 instance->fw_crash_state = UNAVAILABLE;
3737 instance->fw_crash_buffer_size = 0;
3741 * megasas_adp_reset_fusion - For controller reset
3742 * @regs: MFI register set
3745 megasas_adp_reset_fusion(struct megasas_instance *instance,
3746 struct megasas_register_set __iomem *regs)
3748 u32 host_diag, abs_state, retry;
3750 /* Now try to reset the chip */
3751 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3752 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3753 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3754 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3755 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3756 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3757 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3759 /* Check that the diag write enable (DRWE) bit is on */
3760 host_diag = readl(&instance->reg_set->fusion_host_diag);
3762 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3764 host_diag = readl(&instance->reg_set->fusion_host_diag);
3765 if (retry++ == 100) {
3766 dev_warn(&instance->pdev->dev,
3767 "Host diag unlock failed from %s %d\n",
3768 __func__, __LINE__);
3772 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3775 /* Send chip reset command */
3776 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3777 &instance->reg_set->fusion_host_diag);
3780 /* Make sure reset adapter bit is cleared */
3781 host_diag = readl(&instance->reg_set->fusion_host_diag);
3783 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3785 host_diag = readl(&instance->reg_set->fusion_host_diag);
3786 if (retry++ == 1000) {
3787 dev_warn(&instance->pdev->dev,
3788 "Diag reset adapter never cleared %s %d\n",
3789 __func__, __LINE__);
3793 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3796 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3800 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3802 abs_state = instance->instancet->
3803 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3805 if (abs_state <= MFI_STATE_FW_INIT) {
3806 dev_warn(&instance->pdev->dev,
3807 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3808 abs_state, __func__, __LINE__);
3816 * megasas_check_reset_fusion - For controller reset check
3817 * @regs: MFI register set
3820 megasas_check_reset_fusion(struct megasas_instance *instance,
3821 struct megasas_register_set __iomem *regs)
3826 /* This function waits for outstanding commands on fusion to complete */
3827 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
3828 int reason, int *convert)
3830 int i, outstanding, retval = 0, hb_seconds_missed = 0;
3833 for (i = 0; i < resetwaittime; i++) {
3834 /* Check if firmware is in fault state */
3835 fw_state = instance->instancet->read_fw_status_reg(
3836 instance->reg_set) & MFI_STATE_MASK;
3837 if (fw_state == MFI_STATE_FAULT) {
3838 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
3839 " will reset adapter scsi%d.\n",
3840 instance->host->host_no);
3841 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3842 if (instance->requestorId && reason) {
3843 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3844 " state while polling during"
3845 " I/O timeout handling for %d\n",
3846 instance->host->host_no);
3854 if (reason == MFI_IO_TIMEOUT_OCR) {
3855 dev_info(&instance->pdev->dev,
3856 "MFI IO is timed out, initiating OCR\n");
3857 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3862 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
3863 if (instance->requestorId && !reason) {
3868 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
3869 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
3870 if (instance->hb_host_mem->HB.fwCounter !=
3871 instance->hb_host_mem->HB.driverCounter) {
3872 instance->hb_host_mem->HB.driverCounter =
3873 instance->hb_host_mem->HB.fwCounter;
3874 hb_seconds_missed = 0;
3876 hb_seconds_missed++;
3877 if (hb_seconds_missed ==
3878 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
3879 dev_warn(&instance->pdev->dev, "SR-IOV:"
3880 " Heartbeat never completed "
3881 " while polling during I/O "
3882 " timeout handling for "
3884 instance->host->host_no);
3892 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3893 outstanding = atomic_read(&instance->fw_outstanding);
3897 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
3898 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
3899 "commands to complete for scsi%d\n", i,
3900 outstanding, instance->host->host_no);
3905 if (atomic_read(&instance->fw_outstanding)) {
3906 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
3907 "will reset adapter scsi%d.\n",
3908 instance->host->host_no);
3916 void megasas_reset_reply_desc(struct megasas_instance *instance)
3919 struct fusion_context *fusion;
3920 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3922 fusion = instance->ctrl_context;
3923 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3924 for (i = 0 ; i < count ; i++) {
3925 fusion->last_reply_idx[i] = 0;
3926 reply_desc = fusion->reply_frames_desc[i];
3927 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3928 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
3933 * megasas_refire_mgmt_cmd : Re-fire management commands
3934 * @instance: Controller's soft instance
3936 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
3939 struct megasas_cmd_fusion *cmd_fusion;
3940 struct fusion_context *fusion;
3941 struct megasas_cmd *cmd_mfi;
3942 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3943 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3945 bool refire_cmd = 0;
3949 fusion = instance->ctrl_context;
3951 /* Re-fire management commands.
3952 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
3954 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
3955 cmd_fusion = fusion->cmd_list[j];
3956 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3957 smid = le16_to_cpu(cmd_mfi->context.smid);
3958 result = REFIRE_CMD;
3963 req_desc = megasas_get_request_descriptor(instance, smid - 1);
3965 switch (cmd_mfi->frame->hdr.cmd) {
3967 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
3968 /* Do not refire shutdown command */
3969 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
3970 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
3971 result = COMPLETE_CMD;
3975 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
3976 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3977 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
3980 result = RETURN_CMD;
3984 if (!instance->support_nvme_passthru) {
3985 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
3986 result = COMPLETE_CMD;
3994 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3995 cmd_fusion->io_request;
3996 if (scsi_io_req->Function == MPI2_FUNCTION_SCSI_TASK_MGMT)
3997 result = RETURN_CMD;
4001 megasas_fire_cmd_fusion(instance, req_desc);
4004 megasas_return_cmd(instance, cmd_mfi);
4007 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4014 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4015 * @instance: per adapter struct
4016 * @channel: the channel assigned by the OS
4017 * @id: the id assigned by the OS
4019 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4022 static int megasas_track_scsiio(struct megasas_instance *instance,
4023 int id, int channel)
4026 struct megasas_cmd_fusion *cmd_fusion;
4027 struct fusion_context *fusion;
4028 fusion = instance->ctrl_context;
4030 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4031 cmd_fusion = fusion->cmd_list[i];
4032 if (cmd_fusion->scmd &&
4033 (cmd_fusion->scmd->device->id == id &&
4034 cmd_fusion->scmd->device->channel == channel)) {
4035 dev_info(&instance->pdev->dev,
4036 "SCSI commands pending to target"
4037 "channel %d id %d \tSMID: 0x%x\n",
4038 channel, id, cmd_fusion->index);
4039 scsi_print_command(cmd_fusion->scmd);
4045 return found ? FAILED : SUCCESS;
4049 * megasas_tm_response_code - translation of device response code
4050 * @ioc: per adapter object
4051 * @mpi_reply: MPI reply returned by firmware
4056 megasas_tm_response_code(struct megasas_instance *instance,
4057 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4061 switch (mpi_reply->ResponseCode) {
4062 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4063 desc = "task management request completed";
4065 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4066 desc = "invalid frame";
4068 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4069 desc = "task management request not supported";
4071 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4072 desc = "task management request failed";
4074 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4075 desc = "task management request succeeded";
4077 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4078 desc = "invalid lun";
4081 desc = "overlapped tag attempted";
4083 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4084 desc = "task queued, however not sent to target";
4090 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4091 mpi_reply->ResponseCode, desc);
4092 dev_dbg(&instance->pdev->dev,
4093 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4094 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4095 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4096 mpi_reply->Function, mpi_reply->TaskType,
4097 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4101 * megasas_issue_tm - main routine for sending tm requests
4102 * @instance: per adapter struct
4103 * @device_handle: device handle
4104 * @channel: the channel assigned by the OS
4105 * @id: the id assigned by the OS
4106 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4107 * @smid_task: smid assigned to the task
4108 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4111 * MegaRaid use MPT interface for Task Magement request.
4112 * A generic API for sending task management requests to firmware.
4114 * Return SUCCESS or FAILED.
4117 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4118 uint channel, uint id, u16 smid_task, u8 type,
4119 struct MR_PRIV_DEVICE *mr_device_priv_data)
4121 struct MR_TASK_MANAGE_REQUEST *mr_request;
4122 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4123 unsigned long timeleft;
4124 struct megasas_cmd_fusion *cmd_fusion;
4125 struct megasas_cmd *cmd_mfi;
4126 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4127 struct fusion_context *fusion = NULL;
4128 struct megasas_cmd_fusion *scsi_lookup;
4130 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4131 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4133 fusion = instance->ctrl_context;
4135 cmd_mfi = megasas_get_cmd(instance);
4138 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4139 __func__, __LINE__);
4143 cmd_fusion = megasas_get_cmd_fusion(instance,
4144 instance->max_scsi_cmds + cmd_mfi->index);
4146 /* Save the smid. To be used for returning the cmd */
4147 cmd_mfi->context.smid = cmd_fusion->index;
4149 req_desc = megasas_get_request_descriptor(instance,
4150 (cmd_fusion->index - 1));
4152 cmd_fusion->request_desc = req_desc;
4153 req_desc->Words = 0;
4155 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4156 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4157 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4158 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4159 mpi_request->DevHandle = cpu_to_le16(device_handle);
4160 mpi_request->TaskType = type;
4161 mpi_request->TaskMID = cpu_to_le16(smid_task);
4162 mpi_request->LUN[1] = 0;
4165 req_desc = cmd_fusion->request_desc;
4166 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4167 req_desc->HighPriority.RequestFlags =
4168 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4169 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4170 req_desc->HighPriority.MSIxIndex = 0;
4171 req_desc->HighPriority.LMID = 0;
4172 req_desc->HighPriority.Reserved1 = 0;
4174 if (channel < MEGASAS_MAX_PD_CHANNELS)
4175 mr_request->tmReqFlags.isTMForPD = 1;
4177 mr_request->tmReqFlags.isTMForLD = 1;
4179 init_completion(&cmd_fusion->done);
4180 megasas_fire_cmd_fusion(instance, req_desc);
4183 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4184 timeout = mr_device_priv_data->task_abort_tmo;
4186 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4187 timeout = mr_device_priv_data->target_reset_tmo;
4191 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4194 dev_err(&instance->pdev->dev,
4195 "task mgmt type 0x%x timed out\n", type);
4196 mutex_unlock(&instance->reset_mutex);
4197 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4198 mutex_lock(&instance->reset_mutex);
4202 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4203 megasas_tm_response_code(instance, mpi_reply);
4205 megasas_return_cmd(instance, cmd_mfi);
4208 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4209 scsi_lookup = fusion->cmd_list[smid_task - 1];
4211 if (scsi_lookup->scmd == NULL)
4214 instance->instancet->disable_intr(instance);
4215 megasas_sync_irqs((unsigned long)instance);
4216 instance->instancet->enable_intr(instance);
4217 if (scsi_lookup->scmd == NULL)
4223 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4224 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4226 instance->instancet->disable_intr(instance);
4227 megasas_sync_irqs((unsigned long)instance);
4228 rc = megasas_track_scsiio(instance, id, channel);
4229 instance->instancet->enable_intr(instance);
4232 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4233 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4245 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4246 * @instance: per adapter struct
4248 * Return Non Zero index, if SMID found in outstanding commands
4250 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4253 struct megasas_instance *instance;
4254 struct megasas_cmd_fusion *cmd_fusion;
4255 struct fusion_context *fusion;
4257 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4259 fusion = instance->ctrl_context;
4261 for (i = 0; i < instance->max_scsi_cmds; i++) {
4262 cmd_fusion = fusion->cmd_list[i];
4263 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4264 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4265 " SMID: %d\n", cmd_fusion->index);
4266 ret = cmd_fusion->index;
4275 * megasas_get_tm_devhandle - Get devhandle for TM request
4276 * @sdev- OS provided scsi device
4278 * Returns- devhandle/targetID of SCSI device
4280 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4284 struct megasas_instance *instance;
4285 struct fusion_context *fusion;
4286 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4287 u16 devhandle = (u16)ULONG_MAX;
4289 instance = (struct megasas_instance *)sdev->host->hostdata;
4290 fusion = instance->ctrl_context;
4292 if (!MEGASAS_IS_LOGICAL(sdev)) {
4293 if (instance->use_seqnum_jbod_fp) {
4294 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4296 pd_sync = (void *)fusion->pd_seq_sync
4297 [(instance->pd_seq_map_id - 1) & 1];
4298 devhandle = pd_sync->seq[pd_index].devHandle;
4300 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4301 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4303 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4305 devhandle = device_id;
4312 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4313 * @scmd : pointer to scsi command object
4315 * Return SUCCESS, if command aborted else FAILED
4318 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4320 struct megasas_instance *instance;
4321 u16 smid, devhandle;
4322 struct fusion_context *fusion;
4324 struct MR_PRIV_DEVICE *mr_device_priv_data;
4325 mr_device_priv_data = scmd->device->hostdata;
4328 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4329 fusion = instance->ctrl_context;
4331 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
4332 scsi_print_command(scmd);
4334 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4335 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4336 "SCSI host:%d\n", instance->host->host_no);
4341 if (!mr_device_priv_data) {
4342 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4343 "scmd(%p)\n", scmd);
4344 scmd->result = DID_NO_CONNECT << 16;
4350 if (!mr_device_priv_data->is_tm_capable) {
4355 mutex_lock(&instance->reset_mutex);
4357 smid = megasas_fusion_smid_lookup(scmd);
4361 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4362 " issued is not found in oustanding commands\n");
4363 mutex_unlock(&instance->reset_mutex);
4367 devhandle = megasas_get_tm_devhandle(scmd->device);
4369 if (devhandle == (u16)ULONG_MAX) {
4371 sdev_printk(KERN_INFO, scmd->device,
4372 "task abort issued for invalid devhandle\n");
4373 mutex_unlock(&instance->reset_mutex);
4376 sdev_printk(KERN_INFO, scmd->device,
4377 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
4380 mr_device_priv_data->tm_busy = 1;
4381 ret = megasas_issue_tm(instance, devhandle,
4382 scmd->device->channel, scmd->device->id, smid,
4383 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4384 mr_device_priv_data);
4385 mr_device_priv_data->tm_busy = 0;
4387 mutex_unlock(&instance->reset_mutex);
4389 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4390 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4396 * megasas_reset_target_fusion : target reset function for fusion adapters
4397 * scmd: SCSI command pointer
4399 * Returns SUCCESS if all commands associated with target aborted else FAILED
4402 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4405 struct megasas_instance *instance;
4408 struct fusion_context *fusion;
4409 struct MR_PRIV_DEVICE *mr_device_priv_data;
4410 mr_device_priv_data = scmd->device->hostdata;
4412 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4413 fusion = instance->ctrl_context;
4415 sdev_printk(KERN_INFO, scmd->device,
4416 "target reset called for scmd(%p)\n", scmd);
4418 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4419 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4420 "SCSI host:%d\n", instance->host->host_no);
4425 if (!mr_device_priv_data) {
4426 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4427 "scmd(%p)\n", scmd);
4428 scmd->result = DID_NO_CONNECT << 16;
4434 if (!mr_device_priv_data->is_tm_capable) {
4439 mutex_lock(&instance->reset_mutex);
4440 devhandle = megasas_get_tm_devhandle(scmd->device);
4442 if (devhandle == (u16)ULONG_MAX) {
4444 sdev_printk(KERN_INFO, scmd->device,
4445 "target reset issued for invalid devhandle\n");
4446 mutex_unlock(&instance->reset_mutex);
4450 sdev_printk(KERN_INFO, scmd->device,
4451 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4453 mr_device_priv_data->tm_busy = 1;
4454 ret = megasas_issue_tm(instance, devhandle,
4455 scmd->device->channel, scmd->device->id, 0,
4456 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4457 mr_device_priv_data);
4458 mr_device_priv_data->tm_busy = 0;
4459 mutex_unlock(&instance->reset_mutex);
4461 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4462 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4467 /*SRIOV get other instance in cluster if any*/
4468 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4472 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4473 if (megasas_mgmt_info.instance[i] &&
4474 (megasas_mgmt_info.instance[i] != instance) &&
4475 megasas_mgmt_info.instance[i]->requestorId &&
4476 megasas_mgmt_info.instance[i]->peerIsPresent &&
4477 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4478 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4479 return megasas_mgmt_info.instance[i];
4484 /* Check for a second path that is currently UP */
4485 int megasas_check_mpio_paths(struct megasas_instance *instance,
4486 struct scsi_cmnd *scmd)
4488 struct megasas_instance *peer_instance = NULL;
4489 int retval = (DID_REQUEUE << 16);
4491 if (instance->peerIsPresent) {
4492 peer_instance = megasas_get_peer_instance(instance);
4493 if ((peer_instance) &&
4494 (atomic_read(&peer_instance->adprecovery) ==
4495 MEGASAS_HBA_OPERATIONAL))
4496 retval = (DID_NO_CONNECT << 16);
4501 /* Core fusion reset function */
4502 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4504 int retval = SUCCESS, i, j, convert = 0;
4505 struct megasas_instance *instance;
4506 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4507 struct fusion_context *fusion;
4508 u32 abs_state, status_reg, reset_adapter;
4509 u32 io_timeout_in_crash_mode = 0;
4510 struct scsi_cmnd *scmd_local = NULL;
4511 struct scsi_device *sdev;
4512 int ret_target_prop = DCMD_FAILED;
4513 bool is_target_prop = false;
4515 instance = (struct megasas_instance *)shost->hostdata;
4516 fusion = instance->ctrl_context;
4518 mutex_lock(&instance->reset_mutex);
4520 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4521 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4522 "returning FAILED for scsi%d.\n",
4523 instance->host->host_no);
4524 mutex_unlock(&instance->reset_mutex);
4527 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4528 abs_state = status_reg & MFI_STATE_MASK;
4530 /* IO timeout detected, forcibly put FW in FAULT state */
4531 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4532 instance->crash_dump_app_support && reason) {
4533 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4534 "forcibly FAULT Firmware\n");
4535 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4536 status_reg = readl(&instance->reg_set->doorbell);
4537 writel(status_reg | MFI_STATE_FORCE_OCR,
4538 &instance->reg_set->doorbell);
4539 readl(&instance->reg_set->doorbell);
4540 mutex_unlock(&instance->reset_mutex);
4543 io_timeout_in_crash_mode++;
4544 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4545 "seconds for crash dump collection and OCR "
4546 "to be done\n", (io_timeout_in_crash_mode * 3));
4547 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4548 (io_timeout_in_crash_mode < 80));
4550 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4551 dev_info(&instance->pdev->dev, "OCR done for IO "
4555 dev_info(&instance->pdev->dev, "Controller is not "
4556 "operational after 240 seconds wait for IO "
4557 "timeout case in FW crash dump mode\n do "
4558 "OCR/kill adapter\n");
4559 retval = megasas_reset_fusion(shost, 0);
4564 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4565 del_timer_sync(&instance->sriov_heartbeat_timer);
4566 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4567 set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4568 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4569 instance->instancet->disable_intr(instance);
4570 megasas_sync_irqs((unsigned long)instance);
4572 /* First try waiting for commands to complete */
4573 if (megasas_wait_for_outstanding_fusion(instance, reason,
4575 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4576 dev_warn(&instance->pdev->dev, "resetting fusion "
4577 "adapter scsi%d.\n", instance->host->host_no);
4581 if (megasas_dbg_lvl & OCR_LOGS)
4582 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4584 /* Now return commands back to the OS */
4585 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4586 cmd_fusion = fusion->cmd_list[i];
4587 /*check for extra commands issued by driver*/
4588 if (instance->adapter_type == VENTURA_SERIES) {
4589 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4590 megasas_return_cmd_fusion(instance, r1_cmd);
4592 scmd_local = cmd_fusion->scmd;
4593 if (cmd_fusion->scmd) {
4594 if (megasas_dbg_lvl & OCR_LOGS) {
4595 sdev_printk(KERN_INFO,
4596 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4598 scsi_print_command(cmd_fusion->scmd);
4601 scmd_local->result =
4602 megasas_check_mpio_paths(instance,
4604 if (instance->ldio_threshold &&
4605 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4606 atomic_dec(&instance->ldio_outstanding);
4607 megasas_return_cmd_fusion(instance, cmd_fusion);
4608 scsi_dma_unmap(scmd_local);
4609 scmd_local->scsi_done(scmd_local);
4613 atomic_set(&instance->fw_outstanding, 0);
4615 status_reg = instance->instancet->read_fw_status_reg(
4617 abs_state = status_reg & MFI_STATE_MASK;
4618 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4619 if (instance->disableOnlineCtrlReset ||
4620 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4621 /* Reset not supported, kill adapter */
4622 dev_warn(&instance->pdev->dev, "Reset not supported"
4623 ", killing adapter scsi%d.\n",
4624 instance->host->host_no);
4625 megaraid_sas_kill_hba(instance);
4626 instance->skip_heartbeat_timer_del = 1;
4631 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4632 if (instance->requestorId && !reason) {
4633 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4634 goto transition_to_ready;
4637 /* Now try to reset the chip */
4638 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
4640 if (instance->instancet->adp_reset
4641 (instance, instance->reg_set))
4643 transition_to_ready:
4644 /* Wait for FW to become ready */
4645 if (megasas_transition_to_ready(instance, 1)) {
4646 dev_warn(&instance->pdev->dev,
4647 "Failed to transition controller to ready for "
4648 "scsi%d.\n", instance->host->host_no);
4649 if (instance->requestorId && !reason)
4650 goto fail_kill_adapter;
4654 megasas_reset_reply_desc(instance);
4655 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4657 if (megasas_ioc_init_fusion(instance)) {
4658 if (instance->requestorId && !reason)
4659 goto fail_kill_adapter;
4664 if (megasas_get_ctrl_info(instance)) {
4665 dev_info(&instance->pdev->dev,
4666 "Failed from %s %d\n",
4667 __func__, __LINE__);
4668 megaraid_sas_kill_hba(instance);
4673 megasas_refire_mgmt_cmd(instance);
4675 /* Reset load balance info */
4676 if (fusion->load_balance_info)
4677 memset(fusion->load_balance_info, 0,
4678 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4679 MAX_LOGICAL_DRIVES_EXT));
4681 if (!megasas_get_map_info(instance))
4682 megasas_sync_map_info(instance);
4684 megasas_setup_jbod_map(instance);
4686 /* reset stream detection array */
4687 if (instance->adapter_type == VENTURA_SERIES) {
4688 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4689 memset(fusion->stream_detect_by_ld[j],
4690 0, sizeof(struct LD_STREAM_DETECT));
4691 fusion->stream_detect_by_ld[j]->mru_bit_map
4696 clear_bit(MEGASAS_FUSION_IN_RESET,
4697 &instance->reset_flags);
4698 instance->instancet->enable_intr(instance);
4700 shost_for_each_device(sdev, shost) {
4701 if ((instance->tgt_prop) &&
4702 (instance->nvme_page_size))
4703 ret_target_prop = megasas_get_target_prop(instance, sdev);
4705 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4706 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4709 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4711 dev_info(&instance->pdev->dev, "Interrupts are enabled and"
4712 " controller is OPERATIONAL for scsi:%d\n",
4713 instance->host->host_no);
4715 /* Restart SR-IOV heartbeat */
4716 if (instance->requestorId) {
4717 if (!megasas_sriov_start_heartbeat(instance, 0))
4718 megasas_start_timer(instance);
4720 instance->skip_heartbeat_timer_del = 1;
4723 if (instance->crash_dump_drv_support &&
4724 instance->crash_dump_app_support)
4725 megasas_set_crash_dump_params(instance,
4726 MR_CRASH_BUF_TURN_ON);
4728 megasas_set_crash_dump_params(instance,
4729 MR_CRASH_BUF_TURN_OFF);
4733 /* Adapter reset completed successfully */
4734 dev_warn(&instance->pdev->dev,
4735 "Reset successful for scsi%d.\n",
4736 instance->host->host_no);
4741 /* Reset failed, kill the adapter */
4742 dev_warn(&instance->pdev->dev, "Reset failed, killing "
4743 "adapter scsi%d.\n", instance->host->host_no);
4744 megaraid_sas_kill_hba(instance);
4745 instance->skip_heartbeat_timer_del = 1;
4748 /* For VF: Restart HB timer if we didn't OCR */
4749 if (instance->requestorId) {
4750 megasas_start_timer(instance);
4752 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4753 instance->instancet->enable_intr(instance);
4754 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4757 clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4758 mutex_unlock(&instance->reset_mutex);
4762 /* Fusion Crash dump collection work queue */
4763 void megasas_fusion_crash_dump_wq(struct work_struct *work)
4765 struct megasas_instance *instance =
4766 container_of(work, struct megasas_instance, crash_init);
4768 u8 partial_copy = 0;
4771 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4774 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4775 * to host crash buffers
4777 if (instance->drv_buf_index == 0) {
4778 /* Buffer is already allocated for old Crash dump.
4779 * Do OCR and do not wait for crash dump collection
4781 if (instance->drv_buf_alloc) {
4782 dev_info(&instance->pdev->dev, "earlier crash dump is "
4783 "not yet copied by application, ignoring this "
4784 "crash dump and initiating OCR\n");
4785 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4787 &instance->reg_set->outbound_scratch_pad);
4788 readl(&instance->reg_set->outbound_scratch_pad);
4791 megasas_alloc_host_crash_buffer(instance);
4792 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4793 "allocated: %d\n", instance->drv_buf_alloc);
4797 * Driver has allocated max buffers, which can be allocated
4798 * and FW has more crash dump data, then driver will
4801 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
4802 dev_info(&instance->pdev->dev, "Driver is done copying "
4803 "the buffer: %d\n", instance->drv_buf_alloc);
4804 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4807 memcpy(instance->crash_buf[instance->drv_buf_index],
4808 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4809 instance->drv_buf_index++;
4810 status_reg &= ~MFI_STATE_DMADONE;
4813 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4814 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4815 "of copied buffers: %d\n", instance->drv_buf_index);
4816 instance->fw_crash_buffer_size = instance->drv_buf_index;
4817 instance->fw_crash_state = AVAILABLE;
4818 instance->drv_buf_index = 0;
4819 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4820 readl(&instance->reg_set->outbound_scratch_pad);
4822 megasas_reset_fusion(instance->host, 0);
4824 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4825 readl(&instance->reg_set->outbound_scratch_pad);
4830 /* Fusion OCR work queue */
4831 void megasas_fusion_ocr_wq(struct work_struct *work)
4833 struct megasas_instance *instance =
4834 container_of(work, struct megasas_instance, work_init);
4836 megasas_reset_fusion(instance->host, 0);
4839 /* Allocate fusion context */
4841 megasas_alloc_fusion_context(struct megasas_instance *instance)
4843 struct fusion_context *fusion;
4845 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
4847 if (!instance->ctrl_context) {
4848 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4849 __func__, __LINE__);
4853 fusion = instance->ctrl_context;
4855 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4856 sizeof(LD_SPAN_INFO));
4857 fusion->log_to_span =
4858 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4859 fusion->log_to_span_pages);
4860 if (!fusion->log_to_span) {
4861 fusion->log_to_span =
4862 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4863 sizeof(LD_SPAN_INFO)));
4864 if (!fusion->log_to_span) {
4865 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4866 __func__, __LINE__);
4871 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4872 sizeof(struct LD_LOAD_BALANCE_INFO));
4873 fusion->load_balance_info =
4874 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4875 fusion->load_balance_info_pages);
4876 if (!fusion->load_balance_info) {
4877 fusion->load_balance_info =
4878 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4879 sizeof(struct LD_LOAD_BALANCE_INFO)));
4880 if (!fusion->load_balance_info)
4881 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4882 "continuing without Load Balance support\n");
4889 megasas_free_fusion_context(struct megasas_instance *instance)
4891 struct fusion_context *fusion = instance->ctrl_context;
4894 if (fusion->load_balance_info) {
4895 if (is_vmalloc_addr(fusion->load_balance_info))
4896 vfree(fusion->load_balance_info);
4898 free_pages((ulong)fusion->load_balance_info,
4899 fusion->load_balance_info_pages);
4902 if (fusion->log_to_span) {
4903 if (is_vmalloc_addr(fusion->log_to_span))
4904 vfree(fusion->log_to_span);
4906 free_pages((ulong)fusion->log_to_span,
4907 fusion->log_to_span_pages);
4914 struct megasas_instance_template megasas_instance_template_fusion = {
4915 .enable_intr = megasas_enable_intr_fusion,
4916 .disable_intr = megasas_disable_intr_fusion,
4917 .clear_intr = megasas_clear_intr_fusion,
4918 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4919 .adp_reset = megasas_adp_reset_fusion,
4920 .check_reset = megasas_check_reset_fusion,
4921 .service_isr = megasas_isr_fusion,
4922 .tasklet = megasas_complete_cmd_dpc_fusion,
4923 .init_adapter = megasas_init_adapter_fusion,
4924 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
4925 .issue_dcmd = megasas_issue_dcmd_fusion,