1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2009-2013 LSI Corporation
6 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
9 * FILE: megaraid_sas_fusion.c
11 * Authors: Broadcom Inc.
14 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
17 * Send feedback to: megaraidlinux.pdl@broadcom.com
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/uaccess.h>
32 #include <linux/compat.h>
33 #include <linux/blkdev.h>
34 #include <linux/mutex.h>
35 #include <linux/poll.h>
36 #include <linux/vmalloc.h>
37 #include <linux/workqueue.h>
38 #include <linux/irq_poll.h>
40 #include <scsi/scsi.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_host.h>
44 #include <scsi/scsi_dbg.h>
45 #include <linux/dmi.h>
47 #include "megaraid_sas_fusion.h"
48 #include "megaraid_sas.h"
51 extern void megasas_free_cmds(struct megasas_instance *instance);
52 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
55 megasas_complete_cmd(struct megasas_instance *instance,
56 struct megasas_cmd *cmd, u8 alt_status);
58 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
62 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
63 int megasas_alloc_cmds(struct megasas_instance *instance);
65 megasas_clear_intr_fusion(struct megasas_instance *instance);
67 megasas_issue_polled(struct megasas_instance *instance,
68 struct megasas_cmd *cmd);
70 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
72 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
73 void megaraid_sas_kill_hba(struct megasas_instance *instance);
75 extern u32 megasas_dbg_lvl;
76 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
78 void megasas_start_timer(struct megasas_instance *instance);
79 extern struct megasas_mgmt_info megasas_mgmt_info;
80 extern unsigned int resetwaittime;
81 extern unsigned int dual_qdepth_disable;
82 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
83 static void megasas_free_reply_fusion(struct megasas_instance *instance);
85 void megasas_configure_queue_sizes(struct megasas_instance *instance);
86 static void megasas_fusion_crash_dump(struct megasas_instance *instance);
87 extern u32 megasas_readl(struct megasas_instance *instance,
88 const volatile void __iomem *addr);
91 * megasas_adp_reset_wait_for_ready - initiate chip reset and wait for
92 * controller to come to ready state
93 * @instance - adapter's soft state
94 * @do_adp_reset - If true, do a chip reset
95 * @ocr_context - If called from OCR context this will
98 * This function initates a chip reset followed by a wait for controller to
99 * transition to ready state.
100 * During this, driver will block all access to PCI config space from userspace
103 megasas_adp_reset_wait_for_ready(struct megasas_instance *instance,
110 * Block access to PCI config space from userspace
111 * when diag reset is initiated from driver
113 if (megasas_dbg_lvl & OCR_DEBUG)
114 dev_info(&instance->pdev->dev,
115 "Block access to PCI config space %s %d\n",
118 pci_cfg_access_lock(instance->pdev);
121 if (instance->instancet->adp_reset
122 (instance, instance->reg_set))
126 /* Wait for FW to become ready */
127 if (megasas_transition_to_ready(instance, ocr_context)) {
128 dev_warn(&instance->pdev->dev,
129 "Failed to transition controller to ready for scsi%d.\n",
130 instance->host->host_no);
136 if (megasas_dbg_lvl & OCR_DEBUG)
137 dev_info(&instance->pdev->dev,
138 "Unlock access to PCI config space %s %d\n",
141 pci_cfg_access_unlock(instance->pdev);
147 * megasas_check_same_4gb_region - check if allocation
148 * crosses same 4GB boundary or not
149 * @instance - adapter's soft instance
150 * start_addr - start address of DMA allocation
151 * size - size of allocation in bytes
152 * return - true : allocation does not cross same
154 * false: allocation crosses same
157 static inline bool megasas_check_same_4gb_region
158 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
162 end_addr = start_addr + size;
164 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
165 dev_err(&instance->pdev->dev,
166 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
167 (unsigned long long)start_addr,
168 (unsigned long long)end_addr);
176 * megasas_enable_intr_fusion - Enables interrupts
177 * @regs: MFI register set
180 megasas_enable_intr_fusion(struct megasas_instance *instance)
182 struct megasas_register_set __iomem *regs;
183 regs = instance->reg_set;
185 instance->mask_interrupts = 0;
186 /* For Thunderbolt/Invader also clear intr on enable */
187 writel(~0, ®s->outbound_intr_status);
188 readl(®s->outbound_intr_status);
190 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
192 /* Dummy readl to force pci flush */
193 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
194 __func__, readl(®s->outbound_intr_mask));
198 * megasas_disable_intr_fusion - Disables interrupt
199 * @regs: MFI register set
202 megasas_disable_intr_fusion(struct megasas_instance *instance)
204 u32 mask = 0xFFFFFFFF;
205 struct megasas_register_set __iomem *regs;
206 regs = instance->reg_set;
207 instance->mask_interrupts = 1;
209 writel(mask, ®s->outbound_intr_mask);
210 /* Dummy readl to force pci flush */
211 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
212 __func__, readl(®s->outbound_intr_mask));
216 megasas_clear_intr_fusion(struct megasas_instance *instance)
219 struct megasas_register_set __iomem *regs;
220 regs = instance->reg_set;
222 * Check if it is our interrupt
224 status = megasas_readl(instance,
225 ®s->outbound_intr_status);
228 writel(status, ®s->outbound_intr_status);
229 readl(®s->outbound_intr_status);
232 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
239 * megasas_get_cmd_fusion - Get a command from the free pool
240 * @instance: Adapter soft state
242 * Returns a blk_tag indexed mpt frame
244 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
245 *instance, u32 blk_tag)
247 struct fusion_context *fusion;
249 fusion = instance->ctrl_context;
250 return fusion->cmd_list[blk_tag];
254 * megasas_return_cmd_fusion - Return a cmd to free command pool
255 * @instance: Adapter soft state
256 * @cmd: Command packet to be returned to free command pool
258 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
259 struct megasas_cmd_fusion *cmd)
262 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
263 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
264 cmd->cmd_completed = false;
268 * megasas_write_64bit_req_desc - PCI writes 64bit request descriptor
269 * @instance: Adapter soft state
270 * @req_desc: 64bit Request descriptor
273 megasas_write_64bit_req_desc(struct megasas_instance *instance,
274 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
276 #if defined(writeq) && defined(CONFIG_64BIT)
277 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
278 le32_to_cpu(req_desc->u.low));
279 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
282 spin_lock_irqsave(&instance->hba_lock, flags);
283 writel(le32_to_cpu(req_desc->u.low),
284 &instance->reg_set->inbound_low_queue_port);
285 writel(le32_to_cpu(req_desc->u.high),
286 &instance->reg_set->inbound_high_queue_port);
287 spin_unlock_irqrestore(&instance->hba_lock, flags);
292 * megasas_fire_cmd_fusion - Sends command to the FW
293 * @instance: Adapter soft state
294 * @req_desc: 32bit or 64bit Request descriptor
296 * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
297 * Prior to AERO_SERIES support 64 bit Descriptor.
300 megasas_fire_cmd_fusion(struct megasas_instance *instance,
301 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
303 if (instance->atomic_desc_support)
304 writel(le32_to_cpu(req_desc->u.low),
305 &instance->reg_set->inbound_single_queue_port);
307 megasas_write_64bit_req_desc(instance, req_desc);
311 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
312 * @instance: Adapter soft state
313 * fw_boot_context: Whether this function called during probe or after OCR
315 * This function is only for fusion controllers.
316 * Update host can queue, if firmware downgrade max supported firmware commands.
317 * Firmware upgrade case will be skiped because underlying firmware has
318 * more resource than exposed to the OS.
322 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
324 u16 cur_max_fw_cmds = 0;
325 u16 ldio_threshold = 0;
326 struct megasas_register_set __iomem *reg_set;
328 reg_set = instance->reg_set;
330 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
331 if (instance->adapter_type < VENTURA_SERIES)
333 megasas_readl(instance,
334 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
336 if (dual_qdepth_disable || !cur_max_fw_cmds)
337 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
340 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
342 dev_info(&instance->pdev->dev,
343 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
344 cur_max_fw_cmds, ldio_threshold);
346 if (fw_boot_context == OCR_CONTEXT) {
347 cur_max_fw_cmds = cur_max_fw_cmds - 1;
348 if (cur_max_fw_cmds < instance->max_fw_cmds) {
349 instance->cur_can_queue =
350 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
351 MEGASAS_FUSION_IOCTL_CMDS);
352 instance->host->can_queue = instance->cur_can_queue;
353 instance->ldio_threshold = ldio_threshold;
356 instance->max_fw_cmds = cur_max_fw_cmds;
357 instance->ldio_threshold = ldio_threshold;
360 instance->max_fw_cmds = min(instance->max_fw_cmds,
361 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
363 * Reduce the max supported cmds by 1. This is to ensure that the
364 * reply_q_sz (1 more than the max cmd that driver may send)
365 * does not exceed max cmds that the FW can support
367 instance->max_fw_cmds = instance->max_fw_cmds-1;
371 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
372 * @instance: Adapter soft state
375 megasas_free_cmds_fusion(struct megasas_instance *instance)
378 struct fusion_context *fusion = instance->ctrl_context;
379 struct megasas_cmd_fusion *cmd;
382 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
383 fusion->sense_phys_addr);
386 if (fusion->cmd_list) {
387 for (i = 0; i < instance->max_mpt_cmds; i++) {
388 cmd = fusion->cmd_list[i];
391 dma_pool_free(fusion->sg_dma_pool,
393 cmd->sg_frame_phys_addr);
397 kfree(fusion->cmd_list);
400 if (fusion->sg_dma_pool) {
401 dma_pool_destroy(fusion->sg_dma_pool);
402 fusion->sg_dma_pool = NULL;
404 if (fusion->sense_dma_pool) {
405 dma_pool_destroy(fusion->sense_dma_pool);
406 fusion->sense_dma_pool = NULL;
410 /* Reply Frame, Desc*/
411 if (instance->is_rdpq)
412 megasas_free_rdpq_fusion(instance);
414 megasas_free_reply_fusion(instance);
416 /* Request Frame, Desc*/
417 if (fusion->req_frames_desc)
418 dma_free_coherent(&instance->pdev->dev,
419 fusion->request_alloc_sz, fusion->req_frames_desc,
420 fusion->req_frames_desc_phys);
421 if (fusion->io_request_frames)
422 dma_pool_free(fusion->io_request_frames_pool,
423 fusion->io_request_frames,
424 fusion->io_request_frames_phys);
425 if (fusion->io_request_frames_pool) {
426 dma_pool_destroy(fusion->io_request_frames_pool);
427 fusion->io_request_frames_pool = NULL;
432 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
433 * @instance: Adapter soft state
436 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
440 struct fusion_context *fusion;
441 struct megasas_cmd_fusion *cmd;
445 fusion = instance->ctrl_context;
446 max_cmd = instance->max_fw_cmds;
447 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
449 fusion->sg_dma_pool =
450 dma_pool_create("mr_sg", &instance->pdev->dev,
451 instance->max_chain_frame_sz,
452 MR_DEFAULT_NVME_PAGE_SIZE, 0);
453 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
454 fusion->sense_dma_pool =
455 dma_pool_create("mr_sense", &instance->pdev->dev,
458 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
459 dev_err(&instance->pdev->dev,
460 "Failed from %s %d\n", __func__, __LINE__);
464 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
465 GFP_KERNEL, &fusion->sense_phys_addr);
466 if (!fusion->sense) {
467 dev_err(&instance->pdev->dev,
468 "failed from %s %d\n", __func__, __LINE__);
472 /* sense buffer, request frame and reply desc pool requires to be in
473 * same 4 gb region. Below function will check this.
474 * In case of failure, new pci pool will be created with updated
476 * Older allocation and pool will be destroyed.
477 * Alignment will be used such a way that next allocation if success,
478 * will always meet same 4gb region requirement.
479 * Actual requirement is not alignment, but we need start and end of
480 * DMA address must have same upper 32 bit address.
483 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
485 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
486 fusion->sense_phys_addr);
487 fusion->sense = NULL;
488 dma_pool_destroy(fusion->sense_dma_pool);
490 fusion->sense_dma_pool =
491 dma_pool_create("mr_sense_align", &instance->pdev->dev,
492 sense_sz, roundup_pow_of_two(sense_sz),
494 if (!fusion->sense_dma_pool) {
495 dev_err(&instance->pdev->dev,
496 "Failed from %s %d\n", __func__, __LINE__);
499 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
501 &fusion->sense_phys_addr);
502 if (!fusion->sense) {
503 dev_err(&instance->pdev->dev,
504 "failed from %s %d\n", __func__, __LINE__);
510 * Allocate and attach a frame to each of the commands in cmd_list
512 for (i = 0; i < max_cmd; i++) {
513 cmd = fusion->cmd_list[i];
514 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
515 GFP_KERNEL, &cmd->sg_frame_phys_addr);
517 offset = SCSI_SENSE_BUFFERSIZE * i;
518 cmd->sense = (u8 *)fusion->sense + offset;
519 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
521 if (!cmd->sg_frame) {
522 dev_err(&instance->pdev->dev,
523 "Failed from %s %d\n", __func__, __LINE__);
528 /* create sense buffer for the raid 1/10 fp */
529 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
530 cmd = fusion->cmd_list[i];
531 offset = SCSI_SENSE_BUFFERSIZE * i;
532 cmd->sense = (u8 *)fusion->sense + offset;
533 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
541 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
543 u32 max_mpt_cmd, i, j;
544 struct fusion_context *fusion;
546 fusion = instance->ctrl_context;
548 max_mpt_cmd = instance->max_mpt_cmds;
551 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
552 * Allocate the dynamic array first and then allocate individual
556 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
558 if (!fusion->cmd_list) {
559 dev_err(&instance->pdev->dev,
560 "Failed from %s %d\n", __func__, __LINE__);
564 for (i = 0; i < max_mpt_cmd; i++) {
565 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
567 if (!fusion->cmd_list[i]) {
568 for (j = 0; j < i; j++)
569 kfree(fusion->cmd_list[j]);
570 kfree(fusion->cmd_list);
571 dev_err(&instance->pdev->dev,
572 "Failed from %s %d\n", __func__, __LINE__);
581 megasas_alloc_request_fusion(struct megasas_instance *instance)
583 struct fusion_context *fusion;
585 fusion = instance->ctrl_context;
588 fusion->io_request_frames_pool =
589 dma_pool_create("mr_ioreq", &instance->pdev->dev,
590 fusion->io_frames_alloc_sz, 16, 0);
592 if (!fusion->io_request_frames_pool) {
593 dev_err(&instance->pdev->dev,
594 "Failed from %s %d\n", __func__, __LINE__);
598 fusion->io_request_frames =
599 dma_pool_alloc(fusion->io_request_frames_pool,
600 GFP_KERNEL, &fusion->io_request_frames_phys);
601 if (!fusion->io_request_frames) {
602 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
603 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
604 dma_pool_destroy(fusion->io_request_frames_pool);
605 megasas_configure_queue_sizes(instance);
608 dev_err(&instance->pdev->dev,
609 "Failed from %s %d\n", __func__, __LINE__);
614 if (!megasas_check_same_4gb_region(instance,
615 fusion->io_request_frames_phys,
616 fusion->io_frames_alloc_sz)) {
617 dma_pool_free(fusion->io_request_frames_pool,
618 fusion->io_request_frames,
619 fusion->io_request_frames_phys);
620 fusion->io_request_frames = NULL;
621 dma_pool_destroy(fusion->io_request_frames_pool);
623 fusion->io_request_frames_pool =
624 dma_pool_create("mr_ioreq_align",
625 &instance->pdev->dev,
626 fusion->io_frames_alloc_sz,
627 roundup_pow_of_two(fusion->io_frames_alloc_sz),
630 if (!fusion->io_request_frames_pool) {
631 dev_err(&instance->pdev->dev,
632 "Failed from %s %d\n", __func__, __LINE__);
636 fusion->io_request_frames =
637 dma_pool_alloc(fusion->io_request_frames_pool,
639 &fusion->io_request_frames_phys);
641 if (!fusion->io_request_frames) {
642 dev_err(&instance->pdev->dev,
643 "Failed from %s %d\n", __func__, __LINE__);
648 fusion->req_frames_desc =
649 dma_alloc_coherent(&instance->pdev->dev,
650 fusion->request_alloc_sz,
651 &fusion->req_frames_desc_phys, GFP_KERNEL);
652 if (!fusion->req_frames_desc) {
653 dev_err(&instance->pdev->dev,
654 "Failed from %s %d\n", __func__, __LINE__);
662 megasas_alloc_reply_fusion(struct megasas_instance *instance)
665 struct fusion_context *fusion;
666 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
667 fusion = instance->ctrl_context;
669 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
670 fusion->reply_frames_desc_pool =
671 dma_pool_create("mr_reply", &instance->pdev->dev,
672 fusion->reply_alloc_sz * count, 16, 0);
674 if (!fusion->reply_frames_desc_pool) {
675 dev_err(&instance->pdev->dev,
676 "Failed from %s %d\n", __func__, __LINE__);
680 fusion->reply_frames_desc[0] =
681 dma_pool_alloc(fusion->reply_frames_desc_pool,
682 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
683 if (!fusion->reply_frames_desc[0]) {
684 dev_err(&instance->pdev->dev,
685 "Failed from %s %d\n", __func__, __LINE__);
689 if (!megasas_check_same_4gb_region(instance,
690 fusion->reply_frames_desc_phys[0],
691 (fusion->reply_alloc_sz * count))) {
692 dma_pool_free(fusion->reply_frames_desc_pool,
693 fusion->reply_frames_desc[0],
694 fusion->reply_frames_desc_phys[0]);
695 fusion->reply_frames_desc[0] = NULL;
696 dma_pool_destroy(fusion->reply_frames_desc_pool);
698 fusion->reply_frames_desc_pool =
699 dma_pool_create("mr_reply_align",
700 &instance->pdev->dev,
701 fusion->reply_alloc_sz * count,
702 roundup_pow_of_two(fusion->reply_alloc_sz * count),
705 if (!fusion->reply_frames_desc_pool) {
706 dev_err(&instance->pdev->dev,
707 "Failed from %s %d\n", __func__, __LINE__);
711 fusion->reply_frames_desc[0] =
712 dma_pool_alloc(fusion->reply_frames_desc_pool,
714 &fusion->reply_frames_desc_phys[0]);
716 if (!fusion->reply_frames_desc[0]) {
717 dev_err(&instance->pdev->dev,
718 "Failed from %s %d\n", __func__, __LINE__);
723 reply_desc = fusion->reply_frames_desc[0];
724 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
725 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
727 /* This is not a rdpq mode, but driver still populate
728 * reply_frame_desc array to use same msix index in ISR path.
730 for (i = 0; i < (count - 1); i++)
731 fusion->reply_frames_desc[i + 1] =
732 fusion->reply_frames_desc[i] +
733 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
739 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
741 int i, j, k, msix_count;
742 struct fusion_context *fusion;
743 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
744 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
745 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
746 u8 dma_alloc_count, abs_index;
747 u32 chunk_size, array_size, offset;
749 fusion = instance->ctrl_context;
750 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
751 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
752 MAX_MSIX_QUEUES_FUSION;
754 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
755 array_size, &fusion->rdpq_phys,
757 if (!fusion->rdpq_virt) {
758 dev_err(&instance->pdev->dev,
759 "Failed from %s %d\n", __func__, __LINE__);
763 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
765 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
766 &instance->pdev->dev,
768 fusion->reply_frames_desc_pool_align =
769 dma_pool_create("mr_rdpq_align",
770 &instance->pdev->dev,
772 roundup_pow_of_two(chunk_size),
775 if (!fusion->reply_frames_desc_pool ||
776 !fusion->reply_frames_desc_pool_align) {
777 dev_err(&instance->pdev->dev,
778 "Failed from %s %d\n", __func__, __LINE__);
783 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
784 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
785 * within 4GB boundary and also reply queues in a set must have same
786 * upper 32-bits in their memory address. so here driver is allocating the
787 * DMA'able memory for reply queues according. Driver uses limitation of
788 * VENTURA_SERIES to manage INVADER_SERIES as well.
790 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
792 for (i = 0; i < dma_alloc_count; i++) {
794 dma_pool_alloc(fusion->reply_frames_desc_pool,
795 GFP_KERNEL, &rdpq_chunk_phys[i]);
796 if (!rdpq_chunk_virt[i]) {
797 dev_err(&instance->pdev->dev,
798 "Failed from %s %d\n", __func__, __LINE__);
801 /* reply desc pool requires to be in same 4 gb region.
802 * Below function will check this.
803 * In case of failure, new pci pool will be created with updated
805 * For RDPQ buffers, driver always allocate two separate pci pool.
806 * Alignment will be used such a way that next allocation if
807 * success, will always meet same 4gb region requirement.
808 * rdpq_tracker keep track of each buffer's physical,
809 * virtual address and pci pool descriptor. It will help driver
810 * while freeing the resources.
813 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
815 dma_pool_free(fusion->reply_frames_desc_pool,
820 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
821 GFP_KERNEL, &rdpq_chunk_phys[i]);
822 if (!rdpq_chunk_virt[i]) {
823 dev_err(&instance->pdev->dev,
824 "Failed from %s %d\n",
828 fusion->rdpq_tracker[i].dma_pool_ptr =
829 fusion->reply_frames_desc_pool_align;
831 fusion->rdpq_tracker[i].dma_pool_ptr =
832 fusion->reply_frames_desc_pool;
835 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
836 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
839 for (k = 0; k < dma_alloc_count; k++) {
840 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
841 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
843 if (abs_index == msix_count)
845 offset = fusion->reply_alloc_sz * i;
846 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
847 cpu_to_le64(rdpq_chunk_phys[k] + offset);
848 fusion->reply_frames_desc_phys[abs_index] =
849 rdpq_chunk_phys[k] + offset;
850 fusion->reply_frames_desc[abs_index] =
851 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
853 reply_desc = fusion->reply_frames_desc[abs_index];
854 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
855 reply_desc->Words = ULLONG_MAX;
863 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
866 struct fusion_context *fusion;
868 fusion = instance->ctrl_context;
870 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
871 if (fusion->rdpq_tracker[i].pool_entry_virt)
872 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
873 fusion->rdpq_tracker[i].pool_entry_virt,
874 fusion->rdpq_tracker[i].pool_entry_phys);
878 dma_pool_destroy(fusion->reply_frames_desc_pool);
879 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
881 if (fusion->rdpq_virt)
882 dma_free_coherent(&instance->pdev->dev,
883 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
884 fusion->rdpq_virt, fusion->rdpq_phys);
888 megasas_free_reply_fusion(struct megasas_instance *instance) {
890 struct fusion_context *fusion;
892 fusion = instance->ctrl_context;
894 if (fusion->reply_frames_desc[0])
895 dma_pool_free(fusion->reply_frames_desc_pool,
896 fusion->reply_frames_desc[0],
897 fusion->reply_frames_desc_phys[0]);
899 dma_pool_destroy(fusion->reply_frames_desc_pool);
905 * megasas_alloc_cmds_fusion - Allocates the command packets
906 * @instance: Adapter soft state
909 * Each frame has a 32-bit field called context. This context is used to get
910 * back the megasas_cmd_fusion from the frame when a frame gets completed
911 * In this driver, the 32 bit values are the indices into an array cmd_list.
912 * This array is used only to look up the megasas_cmd_fusion given the context.
913 * The free commands themselves are maintained in a linked list called cmd_pool.
915 * cmds are formed in the io_request and sg_frame members of the
916 * megasas_cmd_fusion. The context field is used to get a request descriptor
917 * and is used as SMID of the cmd.
918 * SMID value range is from 1 to max_fw_cmds.
921 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
924 struct fusion_context *fusion;
925 struct megasas_cmd_fusion *cmd;
927 dma_addr_t io_req_base_phys;
931 fusion = instance->ctrl_context;
933 if (megasas_alloc_request_fusion(instance))
936 if (instance->is_rdpq) {
937 if (megasas_alloc_rdpq_fusion(instance))
940 if (megasas_alloc_reply_fusion(instance))
943 if (megasas_alloc_cmdlist_fusion(instance))
946 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
947 instance->max_fw_cmds);
949 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
950 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
951 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
954 * Add all the commands to command pool (fusion->cmd_pool)
957 /* SMID 0 is reserved. Set SMID/index from 1 */
958 for (i = 0; i < instance->max_mpt_cmds; i++) {
959 cmd = fusion->cmd_list[i];
960 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
961 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
965 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
966 (i - instance->max_scsi_cmds) :
967 (u32)ULONG_MAX; /* Set to Invalid */
968 cmd->instance = instance;
970 (struct MPI2_RAID_SCSI_IO_REQUEST *)
971 (io_req_base + offset);
972 memset(cmd->io_request, 0,
973 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
974 cmd->io_request_phys_addr = io_req_base_phys + offset;
975 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
978 if (megasas_create_sg_sense_fusion(instance))
984 megasas_free_cmds_fusion(instance);
989 * wait_and_poll - Issues a polling command
990 * @instance: Adapter soft state
991 * @cmd: Command packet to be issued
993 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
996 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
1000 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1003 u32 msecs = seconds * 1000;
1006 * Wait for cmd_status to change
1008 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
1012 status_reg = instance->instancet->read_fw_status_reg(instance)
1014 if (status_reg == MFI_STATE_FAULT)
1019 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
1020 return DCMD_TIMEOUT;
1021 else if (frame_hdr->cmd_status == MFI_STAT_OK)
1022 return DCMD_SUCCESS;
1028 * megasas_ioc_init_fusion - Initializes the FW
1029 * @instance: Adapter soft state
1031 * Issues the IOC Init cmd
1034 megasas_ioc_init_fusion(struct megasas_instance *instance)
1036 struct megasas_init_frame *init_frame;
1037 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
1038 dma_addr_t ioc_init_handle;
1039 struct megasas_cmd *cmd;
1040 u8 ret, cur_rdpq_mode;
1041 struct fusion_context *fusion;
1042 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
1044 struct megasas_header *frame_hdr;
1045 const char *sys_info;
1046 MFI_CAPABILITIES *drv_ops;
1049 bool cur_fw_64bit_dma_capable;
1050 bool cur_intr_coalescing;
1052 fusion = instance->ctrl_context;
1054 ioc_init_handle = fusion->ioc_init_request_phys;
1055 IOCInitMessage = fusion->ioc_init_request;
1057 cmd = fusion->ioc_init_cmd;
1059 scratch_pad_1 = megasas_readl
1060 (instance, &instance->reg_set->outbound_scratch_pad_1);
1062 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
1064 if (instance->adapter_type == INVADER_SERIES) {
1065 cur_fw_64bit_dma_capable =
1066 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
1068 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1069 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1070 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1071 megaraid_sas_kill_hba(instance);
1077 if (instance->is_rdpq && !cur_rdpq_mode) {
1078 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1079 " from RDPQ mode to non RDPQ mode\n");
1084 cur_intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
1087 if ((instance->low_latency_index_start ==
1088 MR_HIGH_IOPS_QUEUE_COUNT) && cur_intr_coalescing)
1089 instance->perf_mode = MR_BALANCED_PERF_MODE;
1091 dev_info(&instance->pdev->dev, "Performance mode :%s\n",
1092 MEGASAS_PERF_MODE_2STR(instance->perf_mode));
1094 instance->fw_sync_cache_support = (scratch_pad_1 &
1095 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1096 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1097 instance->fw_sync_cache_support ? "Yes" : "No");
1099 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1101 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1102 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1103 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1104 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1105 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1107 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1108 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1109 cpu_to_le64(fusion->rdpq_phys) :
1110 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1111 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1112 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1113 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1114 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1115 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1116 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1118 time = ktime_get_real();
1119 /* Convert to milliseconds as per FW requirement */
1120 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1122 init_frame = (struct megasas_init_frame *)cmd->frame;
1123 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1125 frame_hdr = &cmd->frame->hdr;
1126 frame_hdr->cmd_status = 0xFF;
1127 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1129 init_frame->cmd = MFI_CMD_INIT;
1130 init_frame->cmd_status = 0xFF;
1132 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1134 /* driver support Extended MSIX */
1135 if (instance->adapter_type >= INVADER_SERIES)
1136 drv_ops->mfi_capabilities.support_additional_msix = 1;
1137 /* driver supports HA / Remote LUN over Fast Path interface */
1138 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1140 drv_ops->mfi_capabilities.support_max_255lds = 1;
1141 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1142 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1144 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1145 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1147 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1148 if (!dual_qdepth_disable)
1149 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1151 drv_ops->mfi_capabilities.support_qd_throttling = 1;
1152 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1153 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1154 drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
1156 if (instance->consistent_mask_64bit)
1157 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1159 /* Convert capability to LE32 */
1160 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1162 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1163 if (instance->system_info_buf && sys_info) {
1164 memcpy(instance->system_info_buf->systemId, sys_info,
1165 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1166 instance->system_info_buf->systemIdLength =
1167 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1168 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1169 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1172 init_frame->queue_info_new_phys_addr_hi =
1173 cpu_to_le32(upper_32_bits(ioc_init_handle));
1174 init_frame->queue_info_new_phys_addr_lo =
1175 cpu_to_le32(lower_32_bits(ioc_init_handle));
1176 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1179 * Each bit in replyqueue_mask represents one group of MSI-x vectors
1180 * (each group has 8 vectors)
1182 switch (instance->perf_mode) {
1183 case MR_BALANCED_PERF_MODE:
1184 init_frame->replyqueue_mask =
1185 cpu_to_le16(~(~0 << instance->low_latency_index_start/8));
1187 case MR_IOPS_PERF_MODE:
1188 init_frame->replyqueue_mask =
1189 cpu_to_le16(~(~0 << instance->msix_vectors/8));
1194 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1195 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1196 req_desc.MFAIo.RequestFlags =
1197 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1198 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1201 * disable the intr before firing the init frame
1203 instance->instancet->disable_intr(instance);
1205 for (i = 0; i < (10 * 1000); i += 20) {
1206 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1212 /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1213 megasas_write_64bit_req_desc(instance, &req_desc);
1215 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1217 frame_hdr = &cmd->frame->hdr;
1218 if (frame_hdr->cmd_status != 0) {
1223 if (instance->adapter_type >= AERO_SERIES) {
1224 scratch_pad_1 = megasas_readl
1225 (instance, &instance->reg_set->outbound_scratch_pad_1);
1227 instance->atomic_desc_support =
1228 (scratch_pad_1 & MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
1230 dev_info(&instance->pdev->dev, "FW supports atomic descriptor\t: %s\n",
1231 instance->atomic_desc_support ? "Yes" : "No");
1237 dev_err(&instance->pdev->dev,
1238 "Init cmd return status FAILED for SCSI host %d\n",
1239 instance->host->host_no);
1245 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1246 * @instance: Adapter soft state
1247 * @pend: set to 1, if it is pended jbod map.
1249 * Issue Jbod map to the firmware. If it is pended command,
1250 * issue command and return. If it is first instance of jbod map
1251 * issue and receive command.
1254 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1256 size_t pd_seq_map_sz;
1257 struct megasas_cmd *cmd;
1258 struct megasas_dcmd_frame *dcmd;
1259 struct fusion_context *fusion = instance->ctrl_context;
1260 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1261 dma_addr_t pd_seq_h;
1263 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1264 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1265 pd_seq_map_sz = struct_size(pd_sync, seq, MAX_PHYSICAL_DEVICES - 1);
1267 cmd = megasas_get_cmd(instance);
1269 dev_err(&instance->pdev->dev,
1270 "Could not get mfi cmd. Fail from %s %d\n",
1271 __func__, __LINE__);
1275 dcmd = &cmd->frame->dcmd;
1277 memset(pd_sync, 0, pd_seq_map_sz);
1278 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1281 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1282 dcmd->flags = MFI_FRAME_DIR_WRITE;
1283 instance->jbod_seq_cmd = cmd;
1285 dcmd->flags = MFI_FRAME_DIR_READ;
1288 dcmd->cmd = MFI_CMD_DCMD;
1289 dcmd->cmd_status = 0xFF;
1290 dcmd->sge_count = 1;
1293 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1294 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1296 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1299 instance->instancet->issue_dcmd(instance, cmd);
1303 /* Below code is only for non pended DCMD */
1304 if (!instance->mask_interrupts)
1305 ret = megasas_issue_blocked_cmd(instance, cmd,
1306 MFI_IO_TIMEOUT_SECS);
1308 ret = megasas_issue_polled(instance, cmd);
1310 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1311 dev_warn(&instance->pdev->dev,
1312 "driver supports max %d JBOD, but FW reports %d\n",
1313 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1317 if (ret == DCMD_TIMEOUT)
1318 megaraid_sas_kill_hba(instance);
1320 if (ret == DCMD_SUCCESS)
1321 instance->pd_seq_map_id++;
1323 megasas_return_cmd(instance, cmd);
1328 * megasas_get_ld_map_info - Returns FW's ld_map structure
1329 * @instance: Adapter soft state
1330 * @pend: Pend the command or not
1331 * Issues an internal command (DCMD) to get the FW's controller PD
1332 * list structure. This information is mainly used to find out SYSTEM
1333 * supported by the FW.
1334 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1335 * dcmd.mbox.b[0] - number of LDs being sync'd
1336 * dcmd.mbox.b[1] - 0 - complete command immediately.
1337 * - 1 - pend till config change
1338 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1339 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1340 * uses extended struct MR_FW_RAID_MAP_EXT
1343 megasas_get_ld_map_info(struct megasas_instance *instance)
1346 struct megasas_cmd *cmd;
1347 struct megasas_dcmd_frame *dcmd;
1349 dma_addr_t ci_h = 0;
1351 struct fusion_context *fusion;
1353 cmd = megasas_get_cmd(instance);
1356 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1360 fusion = instance->ctrl_context;
1363 megasas_return_cmd(instance, cmd);
1367 dcmd = &cmd->frame->dcmd;
1369 size_map_info = fusion->current_map_sz;
1371 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1372 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1375 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1376 megasas_return_cmd(instance, cmd);
1380 memset(ci, 0, fusion->max_map_sz);
1381 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1382 dcmd->cmd = MFI_CMD_DCMD;
1383 dcmd->cmd_status = 0xFF;
1384 dcmd->sge_count = 1;
1385 dcmd->flags = MFI_FRAME_DIR_READ;
1388 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1389 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1391 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1393 if (!instance->mask_interrupts)
1394 ret = megasas_issue_blocked_cmd(instance, cmd,
1395 MFI_IO_TIMEOUT_SECS);
1397 ret = megasas_issue_polled(instance, cmd);
1399 if (ret == DCMD_TIMEOUT)
1400 megaraid_sas_kill_hba(instance);
1402 megasas_return_cmd(instance, cmd);
1408 megasas_get_map_info(struct megasas_instance *instance)
1410 struct fusion_context *fusion = instance->ctrl_context;
1412 fusion->fast_path_io = 0;
1413 if (!megasas_get_ld_map_info(instance)) {
1414 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1415 fusion->fast_path_io = 1;
1423 * megasas_sync_map_info - Returns FW's ld_map structure
1424 * @instance: Adapter soft state
1426 * Issues an internal command (DCMD) to get the FW's controller PD
1427 * list structure. This information is mainly used to find out SYSTEM
1428 * supported by the FW.
1431 megasas_sync_map_info(struct megasas_instance *instance)
1434 struct megasas_cmd *cmd;
1435 struct megasas_dcmd_frame *dcmd;
1437 struct fusion_context *fusion;
1438 struct MR_LD_TARGET_SYNC *ci = NULL;
1439 struct MR_DRV_RAID_MAP_ALL *map;
1440 struct MR_LD_RAID *raid;
1441 struct MR_LD_TARGET_SYNC *ld_sync;
1442 dma_addr_t ci_h = 0;
1445 cmd = megasas_get_cmd(instance);
1448 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1452 fusion = instance->ctrl_context;
1455 megasas_return_cmd(instance, cmd);
1459 map = fusion->ld_drv_map[instance->map_id & 1];
1461 num_lds = le16_to_cpu(map->raidMap.ldCount);
1463 dcmd = &cmd->frame->dcmd;
1465 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1467 ci = (struct MR_LD_TARGET_SYNC *)
1468 fusion->ld_map[(instance->map_id - 1) & 1];
1469 memset(ci, 0, fusion->max_map_sz);
1471 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1473 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1475 for (i = 0; i < num_lds; i++, ld_sync++) {
1476 raid = MR_LdRaidGet(i, map);
1477 ld_sync->targetId = MR_GetLDTgtId(i, map);
1478 ld_sync->seqNum = raid->seqNum;
1481 size_map_info = fusion->current_map_sz;
1483 dcmd->cmd = MFI_CMD_DCMD;
1484 dcmd->cmd_status = 0xFF;
1485 dcmd->sge_count = 1;
1486 dcmd->flags = MFI_FRAME_DIR_WRITE;
1489 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1490 dcmd->mbox.b[0] = num_lds;
1491 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1492 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1494 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1496 instance->map_update_cmd = cmd;
1498 instance->instancet->issue_dcmd(instance, cmd);
1504 * meagasas_display_intel_branding - Display branding string
1505 * @instance: per adapter object
1510 megasas_display_intel_branding(struct megasas_instance *instance)
1512 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1515 switch (instance->pdev->device) {
1516 case PCI_DEVICE_ID_LSI_INVADER:
1517 switch (instance->pdev->subsystem_device) {
1518 case MEGARAID_INTEL_RS3DC080_SSDID:
1519 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1520 instance->host->host_no,
1521 MEGARAID_INTEL_RS3DC080_BRANDING);
1523 case MEGARAID_INTEL_RS3DC040_SSDID:
1524 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1525 instance->host->host_no,
1526 MEGARAID_INTEL_RS3DC040_BRANDING);
1528 case MEGARAID_INTEL_RS3SC008_SSDID:
1529 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1530 instance->host->host_no,
1531 MEGARAID_INTEL_RS3SC008_BRANDING);
1533 case MEGARAID_INTEL_RS3MC044_SSDID:
1534 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1535 instance->host->host_no,
1536 MEGARAID_INTEL_RS3MC044_BRANDING);
1542 case PCI_DEVICE_ID_LSI_FURY:
1543 switch (instance->pdev->subsystem_device) {
1544 case MEGARAID_INTEL_RS3WC080_SSDID:
1545 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1546 instance->host->host_no,
1547 MEGARAID_INTEL_RS3WC080_BRANDING);
1549 case MEGARAID_INTEL_RS3WC040_SSDID:
1550 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1551 instance->host->host_no,
1552 MEGARAID_INTEL_RS3WC040_BRANDING);
1558 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1559 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1560 switch (instance->pdev->subsystem_device) {
1561 case MEGARAID_INTEL_RMS3BC160_SSDID:
1562 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1563 instance->host->host_no,
1564 MEGARAID_INTEL_RMS3BC160_BRANDING);
1576 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1577 * @instance: Adapter soft state
1579 * return: if success: return 0
1580 * failed: return -ENOMEM
1582 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1584 struct fusion_context *fusion;
1587 fusion = instance->ctrl_context;
1589 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1591 for (i = 0; i < 2; i++) {
1592 fusion->ld_map[i] = NULL;
1594 fusion->ld_drv_map[i] = (void *)
1595 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1596 fusion->drv_map_pages);
1598 if (!fusion->ld_drv_map[i]) {
1599 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1601 if (!fusion->ld_drv_map[i]) {
1602 dev_err(&instance->pdev->dev,
1603 "Could not allocate memory for local map"
1604 " size requested: %d\n",
1605 fusion->drv_map_sz);
1606 goto ld_drv_map_alloc_fail;
1611 for (i = 0; i < 2; i++) {
1612 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1614 &fusion->ld_map_phys[i],
1616 if (!fusion->ld_map[i]) {
1617 dev_err(&instance->pdev->dev,
1618 "Could not allocate memory for map info %s:%d\n",
1619 __func__, __LINE__);
1620 goto ld_map_alloc_fail;
1627 for (i = 0; i < 2; i++) {
1628 if (fusion->ld_map[i])
1629 dma_free_coherent(&instance->pdev->dev,
1632 fusion->ld_map_phys[i]);
1635 ld_drv_map_alloc_fail:
1636 for (i = 0; i < 2; i++) {
1637 if (fusion->ld_drv_map[i]) {
1638 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1639 vfree(fusion->ld_drv_map[i]);
1641 free_pages((ulong)fusion->ld_drv_map[i],
1642 fusion->drv_map_pages);
1650 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1652 * IO request frame queue, set can_queue.
1653 * @instance: Adapter soft state
1657 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1659 struct fusion_context *fusion;
1662 fusion = instance->ctrl_context;
1663 max_cmd = instance->max_fw_cmds;
1665 if (instance->adapter_type >= VENTURA_SERIES)
1666 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1668 instance->max_mpt_cmds = instance->max_fw_cmds;
1670 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1671 instance->cur_can_queue = instance->max_scsi_cmds;
1672 instance->host->can_queue = instance->cur_can_queue;
1674 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1676 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1677 instance->max_mpt_cmds;
1678 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1679 (fusion->reply_q_depth);
1680 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1681 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1682 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1685 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1687 struct fusion_context *fusion;
1688 struct megasas_cmd *cmd;
1690 fusion = instance->ctrl_context;
1692 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1695 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1696 __func__, __LINE__);
1700 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1701 IOC_INIT_FRAME_SIZE,
1702 &cmd->frame_phys_addr, GFP_KERNEL);
1705 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1706 __func__, __LINE__);
1711 fusion->ioc_init_cmd = cmd;
1716 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1717 * @instance: Adapter soft state
1719 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1721 struct fusion_context *fusion;
1723 fusion = instance->ctrl_context;
1725 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1726 dma_free_coherent(&instance->pdev->dev,
1727 IOC_INIT_FRAME_SIZE,
1728 fusion->ioc_init_cmd->frame,
1729 fusion->ioc_init_cmd->frame_phys_addr);
1731 kfree(fusion->ioc_init_cmd);
1735 * megasas_init_adapter_fusion - Initializes the FW
1736 * @instance: Adapter soft state
1738 * This is the main function for initializing firmware.
1741 megasas_init_adapter_fusion(struct megasas_instance *instance)
1743 struct fusion_context *fusion;
1748 fusion = instance->ctrl_context;
1750 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1753 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1755 instance->max_mfi_cmds =
1756 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1758 megasas_configure_queue_sizes(instance);
1760 scratch_pad_1 = megasas_readl(instance,
1761 &instance->reg_set->outbound_scratch_pad_1);
1762 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1763 * Firmware support extended IO chain frame which is 4 times more than
1765 * Legacy Firmware - Frame size is (8 * 128) = 1K
1766 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1768 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1769 instance->max_chain_frame_sz =
1770 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1771 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1773 instance->max_chain_frame_sz =
1774 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1775 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1777 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1778 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1779 instance->max_chain_frame_sz,
1780 MEGASAS_CHAIN_FRAME_SZ_MIN);
1781 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1784 fusion->max_sge_in_main_msg =
1785 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1786 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1788 fusion->max_sge_in_chain =
1789 instance->max_chain_frame_sz
1790 / sizeof(union MPI2_SGE_IO_UNION);
1792 instance->max_num_sge =
1793 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1794 + fusion->max_sge_in_chain - 2);
1796 /* Used for pass thru MFI frame (DCMD) */
1797 fusion->chain_offset_mfi_pthru =
1798 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1800 fusion->chain_offset_io_request =
1801 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1802 sizeof(union MPI2_SGE_IO_UNION))/16;
1804 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1805 for (i = 0 ; i < count; i++)
1806 fusion->last_reply_idx[i] = 0;
1809 * For fusion adapters, 3 commands for IOCTL and 8 commands
1810 * for driver's internal DCMDs.
1812 instance->max_scsi_cmds = instance->max_fw_cmds -
1813 (MEGASAS_FUSION_INTERNAL_CMDS +
1814 MEGASAS_FUSION_IOCTL_CMDS);
1815 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1817 if (megasas_alloc_ioc_init_frame(instance))
1821 * Allocate memory for descriptors
1822 * Create a pool of commands
1824 if (megasas_alloc_cmds(instance))
1825 goto fail_alloc_mfi_cmds;
1826 if (megasas_alloc_cmds_fusion(instance))
1827 goto fail_alloc_cmds;
1829 if (megasas_ioc_init_fusion(instance)) {
1830 status_reg = instance->instancet->read_fw_status_reg(instance);
1831 if (((status_reg & MFI_STATE_MASK) == MFI_STATE_FAULT) &&
1832 (status_reg & MFI_RESET_ADAPTER)) {
1833 /* Do a chip reset and then retry IOC INIT once */
1834 if (megasas_adp_reset_wait_for_ready
1835 (instance, true, 0) == FAILED)
1838 if (megasas_ioc_init_fusion(instance))
1845 megasas_display_intel_branding(instance);
1846 if (megasas_get_ctrl_info(instance)) {
1847 dev_err(&instance->pdev->dev,
1848 "Could not get controller info. Fail from %s %d\n",
1849 __func__, __LINE__);
1853 instance->flag_ieee = 1;
1854 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1855 instance->threshold_reply_count = instance->max_fw_cmds / 4;
1856 fusion->fast_path_io = 0;
1858 if (megasas_allocate_raid_maps(instance))
1861 if (!megasas_get_map_info(instance))
1862 megasas_sync_map_info(instance);
1867 megasas_free_cmds_fusion(instance);
1869 megasas_free_cmds(instance);
1870 fail_alloc_mfi_cmds:
1871 megasas_free_ioc_init_cmd(instance);
1876 * megasas_fault_detect_work - Worker function of
1877 * FW fault handling workqueue.
1880 megasas_fault_detect_work(struct work_struct *work)
1882 struct megasas_instance *instance =
1883 container_of(work, struct megasas_instance,
1884 fw_fault_work.work);
1885 u32 fw_state, dma_state, status;
1887 /* Check the fw state */
1888 fw_state = instance->instancet->read_fw_status_reg(instance) &
1891 if (fw_state == MFI_STATE_FAULT) {
1892 dma_state = instance->instancet->read_fw_status_reg(instance) &
1894 /* Start collecting crash, if DMA bit is done */
1895 if (instance->crash_dump_drv_support &&
1896 instance->crash_dump_app_support && dma_state) {
1897 megasas_fusion_crash_dump(instance);
1899 if (instance->unload == 0) {
1900 status = megasas_reset_fusion(instance->host, 0);
1901 if (status != SUCCESS) {
1902 dev_err(&instance->pdev->dev,
1903 "Failed from %s %d, do not re-arm timer\n",
1904 __func__, __LINE__);
1911 if (instance->fw_fault_work_q)
1912 queue_delayed_work(instance->fw_fault_work_q,
1913 &instance->fw_fault_work,
1914 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1918 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1920 /* Check if the Fault WQ is already started */
1921 if (instance->fw_fault_work_q)
1924 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1926 snprintf(instance->fault_handler_work_q_name,
1927 sizeof(instance->fault_handler_work_q_name),
1928 "poll_megasas%d_status", instance->host->host_no);
1930 instance->fw_fault_work_q =
1931 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1932 if (!instance->fw_fault_work_q) {
1933 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1934 __func__, __LINE__);
1938 queue_delayed_work(instance->fw_fault_work_q,
1939 &instance->fw_fault_work,
1940 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1946 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1948 struct workqueue_struct *wq;
1950 if (instance->fw_fault_work_q) {
1951 wq = instance->fw_fault_work_q;
1952 instance->fw_fault_work_q = NULL;
1953 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1954 flush_workqueue(wq);
1955 destroy_workqueue(wq);
1960 * map_cmd_status - Maps FW cmd status to OS cmd status
1961 * @cmd : Pointer to cmd
1962 * @status : status of cmd returned by FW
1963 * @ext_status : ext status of cmd returned by FW
1967 map_cmd_status(struct fusion_context *fusion,
1968 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1969 u32 data_length, u8 *sense)
1974 cmd_type = megasas_cmd_type(scmd);
1978 scmd->result = DID_OK << 16;
1981 case MFI_STAT_SCSI_IO_FAILED:
1982 case MFI_STAT_LD_INIT_IN_PROGRESS:
1983 scmd->result = (DID_ERROR << 16) | ext_status;
1986 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1988 scmd->result = (DID_OK << 16) | ext_status;
1989 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1990 memset(scmd->sense_buffer, 0,
1991 SCSI_SENSE_BUFFERSIZE);
1992 memcpy(scmd->sense_buffer, sense,
1993 SCSI_SENSE_BUFFERSIZE);
1994 scmd->result |= DRIVER_SENSE << 24;
1998 * If the IO request is partially completed, then MR FW will
1999 * update "io_request->DataLength" field with actual number of
2000 * bytes transferred.Driver will set residual bytes count in
2001 * SCSI command structure.
2003 resid = (scsi_bufflen(scmd) - data_length);
2004 scsi_set_resid(scmd, resid);
2007 ((cmd_type == READ_WRITE_LDIO) ||
2008 (cmd_type == READ_WRITE_SYSPDIO)))
2009 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
2010 " requested/completed 0x%x/0x%x\n",
2011 status, scsi_bufflen(scmd), data_length);
2014 case MFI_STAT_LD_OFFLINE:
2015 case MFI_STAT_DEVICE_NOT_FOUND:
2016 scmd->result = DID_BAD_TARGET << 16;
2018 case MFI_STAT_CONFIG_SEQ_MISMATCH:
2019 scmd->result = DID_IMM_RETRY << 16;
2022 scmd->result = DID_ERROR << 16;
2028 * megasas_is_prp_possible -
2029 * Checks if native NVMe PRPs can be built for the IO
2031 * @instance: Adapter soft state
2032 * @scmd: SCSI command from the mid-layer
2033 * @sge_count: scatter gather element count.
2035 * Returns: true: PRPs can be built
2036 * false: IEEE SGLs needs to be built
2039 megasas_is_prp_possible(struct megasas_instance *instance,
2040 struct scsi_cmnd *scmd, int sge_count)
2043 u32 data_length = 0;
2044 struct scatterlist *sg_scmd;
2045 bool build_prp = false;
2046 u32 mr_nvme_pg_size;
2048 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2049 MR_DEFAULT_NVME_PAGE_SIZE);
2050 data_length = scsi_bufflen(scmd);
2051 sg_scmd = scsi_sglist(scmd);
2054 * NVMe uses one PRP for each page (or part of a page)
2055 * look at the data length - if 4 pages or less then IEEE is OK
2056 * if > 5 pages then we need to build a native SGL
2057 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2058 * if this first size in the page is >= the residual beyond 4 pages
2059 * then use IEEE, otherwise use native SGL
2062 if (data_length > (mr_nvme_pg_size * 5)) {
2064 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
2065 (data_length <= (mr_nvme_pg_size * 5))) {
2066 /* check if 1st SG entry size is < residual beyond 4 pages */
2067 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
2072 * Below code detects gaps/holes in IO data buffers.
2073 * What does holes/gaps mean?
2074 * Any SGE except first one in a SGL starts at non NVME page size
2075 * aligned address OR Any SGE except last one in a SGL ends at
2076 * non NVME page size boundary.
2078 * Driver has already informed block layer by setting boundary rules for
2079 * bio merging done at NVME page size boundary calling kernel API
2080 * blk_queue_virt_boundary inside slave_config.
2081 * Still there is possibility of IO coming with holes to driver because of
2082 * IO merging done by IO scheduler.
2084 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
2085 * IO scheduling so no IO merging.
2087 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
2088 * then sending IOs with holes.
2090 * Though driver can request block layer to disable IO merging by calling-
2091 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
2092 * user may tune sysfs parameter- nomerges again to 0 or 1.
2094 * If in future IO scheduling is enabled with SCSI BLK MQ,
2095 * this algorithm to detect holes will be required in driver
2096 * for SCSI BLK MQ enabled case as well.
2100 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
2101 if ((i != 0) && (i != (sge_count - 1))) {
2102 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
2103 mega_mod64(sg_dma_address(sg_scmd),
2110 if ((sge_count > 1) && (i == 0)) {
2111 if ((mega_mod64((sg_dma_address(sg_scmd) +
2112 sg_dma_len(sg_scmd)),
2113 mr_nvme_pg_size))) {
2119 if ((sge_count > 1) && (i == (sge_count - 1))) {
2120 if (mega_mod64(sg_dma_address(sg_scmd),
2132 * megasas_make_prp_nvme -
2133 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2135 * @instance: Adapter soft state
2136 * @scmd: SCSI command from the mid-layer
2137 * @sgl_ptr: SGL to be filled in
2138 * @cmd: Fusion command frame
2139 * @sge_count: scatter gather element count.
2141 * Returns: true: PRPs are built
2142 * false: IEEE SGLs needs to be built
2145 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2146 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2147 struct megasas_cmd_fusion *cmd, int sge_count)
2149 int sge_len, offset, num_prp_in_chain = 0;
2150 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2152 dma_addr_t ptr_sgl_phys;
2154 u32 page_mask, page_mask_result;
2155 struct scatterlist *sg_scmd;
2157 bool build_prp = false;
2158 int data_len = scsi_bufflen(scmd);
2159 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2160 MR_DEFAULT_NVME_PAGE_SIZE);
2162 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2168 * Nvme has a very convoluted prp format. One prp is required
2169 * for each page or partial page. Driver need to split up OS sg_list
2170 * entries if it is longer than one page or cross a page
2171 * boundary. Driver also have to insert a PRP list pointer entry as
2172 * the last entry in each physical page of the PRP list.
2174 * NOTE: The first PRP "entry" is actually placed in the first
2175 * SGL entry in the main message as IEEE 64 format. The 2nd
2176 * entry in the main message is the chain element, and the rest
2177 * of the PRP entries are built in the contiguous pcie buffer.
2179 page_mask = mr_nvme_pg_size - 1;
2180 ptr_sgl = (u64 *)cmd->sg_frame;
2181 ptr_sgl_phys = cmd->sg_frame_phys_addr;
2182 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2184 /* Build chain frame element which holds all prps except first*/
2185 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2186 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2188 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2189 main_chain_element->NextChainOffset = 0;
2190 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2191 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2192 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2194 /* Build first prp, sge need not to be page aligned*/
2195 ptr_first_sgl = sgl_ptr;
2196 sg_scmd = scsi_sglist(scmd);
2197 sge_addr = sg_dma_address(sg_scmd);
2198 sge_len = sg_dma_len(sg_scmd);
2200 offset = (u32)(sge_addr & page_mask);
2201 first_prp_len = mr_nvme_pg_size - offset;
2203 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2204 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2206 data_len -= first_prp_len;
2208 if (sge_len > first_prp_len) {
2209 sge_addr += first_prp_len;
2210 sge_len -= first_prp_len;
2211 } else if (sge_len == first_prp_len) {
2212 sg_scmd = sg_next(sg_scmd);
2213 sge_addr = sg_dma_address(sg_scmd);
2214 sge_len = sg_dma_len(sg_scmd);
2218 offset = (u32)(sge_addr & page_mask);
2220 /* Put PRP pointer due to page boundary*/
2221 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2222 if (unlikely(!page_mask_result)) {
2223 scmd_printk(KERN_NOTICE,
2224 scmd, "page boundary ptr_sgl: 0x%p\n",
2227 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2232 *ptr_sgl = cpu_to_le64(sge_addr);
2237 sge_addr += mr_nvme_pg_size;
2238 sge_len -= mr_nvme_pg_size;
2239 data_len -= mr_nvme_pg_size;
2247 sg_scmd = sg_next(sg_scmd);
2248 sge_addr = sg_dma_address(sg_scmd);
2249 sge_len = sg_dma_len(sg_scmd);
2252 main_chain_element->Length =
2253 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2259 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2260 * @instance: Adapter soft state
2261 * @scp: SCSI command from the mid-layer
2262 * @sgl_ptr: SGL to be filled in
2263 * @cmd: cmd we are working on
2264 * @sge_count sge count
2268 megasas_make_sgl_fusion(struct megasas_instance *instance,
2269 struct scsi_cmnd *scp,
2270 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2271 struct megasas_cmd_fusion *cmd, int sge_count)
2273 int i, sg_processed;
2274 struct scatterlist *os_sgl;
2275 struct fusion_context *fusion;
2277 fusion = instance->ctrl_context;
2279 if (instance->adapter_type >= INVADER_SERIES) {
2280 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2281 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2282 sgl_ptr_end->Flags = 0;
2285 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2286 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2287 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2289 if (instance->adapter_type >= INVADER_SERIES)
2290 if (i == sge_count - 1)
2291 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2293 sg_processed = i + 1;
2295 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2296 (sge_count > fusion->max_sge_in_main_msg)) {
2298 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2299 if (instance->adapter_type >= INVADER_SERIES) {
2300 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2301 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2302 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2303 cmd->io_request->ChainOffset =
2305 chain_offset_io_request;
2307 cmd->io_request->ChainOffset = 0;
2309 cmd->io_request->ChainOffset =
2310 fusion->chain_offset_io_request;
2313 /* Prepare chain element */
2314 sg_chain->NextChainOffset = 0;
2315 if (instance->adapter_type >= INVADER_SERIES)
2316 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2319 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2320 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2321 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2322 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2325 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2326 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2332 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2333 * @scp: SCSI command pointer
2334 * @instance: Soft instance of controller
2335 * @cmd: Fusion command pointer
2337 * This function will build sgls based on device type.
2338 * For nvme drives, there is different way of building sgls in nvme native
2339 * format- PRPs(Physical Region Page).
2341 * Returns the number of sg lists actually used, zero if the sg lists
2342 * is NULL, or -ENOMEM if the mapping failed
2345 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2346 struct megasas_cmd_fusion *cmd)
2349 bool build_prp = false;
2350 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2352 sge_count = scsi_dma_map(scp);
2354 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2357 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2358 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2359 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2360 (cmd->pd_interface == NVME_PD))
2361 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2365 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2372 * megasas_set_pd_lba - Sets PD LBA
2374 * @cdb_len: cdb length
2375 * @start_blk: Start block of IO
2377 * Used to set the PD LBA in CDB for FP IOs
2380 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2381 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2382 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2384 struct MR_LD_RAID *raid;
2386 u64 start_blk = io_info->pdBlock;
2387 u8 *cdb = io_request->CDB.CDB32;
2388 u32 num_blocks = io_info->numBlocks;
2389 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2391 /* Check if T10 PI (DIF) is enabled for this LD */
2392 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2393 raid = MR_LdRaidGet(ld, local_map_ptr);
2394 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2395 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2396 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2397 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2399 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2400 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2402 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2403 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2406 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2407 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2408 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2409 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2410 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2411 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2412 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2413 cdb[19] = (u8)(start_blk & 0xff);
2415 /* Logical block reference tag */
2416 io_request->CDB.EEDP32.PrimaryReferenceTag =
2417 cpu_to_be32(ref_tag);
2418 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2419 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2421 /* Transfer length */
2422 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2423 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2424 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2425 cdb[31] = (u8)(num_blocks & 0xff);
2427 /* set SCSI IO EEDPFlags */
2428 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2429 io_request->EEDPFlags = cpu_to_le16(
2430 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2431 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2432 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2433 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2434 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2435 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2437 io_request->EEDPFlags = cpu_to_le16(
2438 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2439 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2441 io_request->Control |= cpu_to_le32((0x4 << 26));
2442 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2444 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2445 if (((cdb_len == 12) || (cdb_len == 16)) &&
2446 (start_blk <= 0xffffffff)) {
2447 if (cdb_len == 16) {
2448 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2453 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2459 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2466 /* Transfer length */
2467 cdb[8] = (u8)(num_blocks & 0xff);
2468 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2470 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2472 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2473 /* Convert to 16 byte CDB for large LBA's */
2476 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2481 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2488 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2495 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2502 /* Transfer length */
2503 cdb[13] = (u8)(num_blocks & 0xff);
2504 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2505 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2506 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2508 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2512 /* Normal case, just load LBA here */
2516 u8 val = cdb[1] & 0xE0;
2517 cdb[3] = (u8)(start_blk & 0xff);
2518 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2519 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2523 cdb[5] = (u8)(start_blk & 0xff);
2524 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2525 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2526 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2529 cdb[5] = (u8)(start_blk & 0xff);
2530 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2531 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2532 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2535 cdb[9] = (u8)(start_blk & 0xff);
2536 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2537 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2538 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2539 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2540 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2541 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2542 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2549 * megasas_stream_detect - stream detection on read and and write IOs
2550 * @instance: Adapter soft state
2551 * @cmd: Command to be prepared
2552 * @io_info: IO Request info
2556 /** stream detection on read and and write IOs */
2557 static void megasas_stream_detect(struct megasas_instance *instance,
2558 struct megasas_cmd_fusion *cmd,
2559 struct IO_REQUEST_INFO *io_info)
2561 struct fusion_context *fusion = instance->ctrl_context;
2562 u32 device_id = io_info->ldTgtId;
2563 struct LD_STREAM_DETECT *current_ld_sd
2564 = fusion->stream_detect_by_ld[device_id];
2565 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2566 u32 shifted_values, unshifted_values;
2567 u32 index_value_mask, shifted_values_mask;
2569 bool is_read_ahead = false;
2570 struct STREAM_DETECT *current_sd;
2571 /* find possible stream */
2572 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2573 stream_num = (*track_stream >>
2574 (i * BITS_PER_INDEX_STREAM)) &
2576 current_sd = ¤t_ld_sd->stream_track[stream_num];
2577 /* if we found a stream, update the raid
2578 * context and also update the mruBitMap
2580 /* boundary condition */
2581 if ((current_sd->next_seq_lba) &&
2582 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2583 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2584 (current_sd->is_read == io_info->isRead)) {
2586 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2587 ((!io_info->isRead) || (!is_read_ahead)))
2589 * Once the API availible we need to change this.
2590 * At this point we are not allowing any gap
2594 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2595 current_sd->next_seq_lba =
2596 io_info->ldStartBlock + io_info->numBlocks;
2598 * update the mruBitMap LRU
2600 shifted_values_mask =
2601 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2602 shifted_values = ((*track_stream & shifted_values_mask)
2603 << BITS_PER_INDEX_STREAM);
2605 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2607 *track_stream & ~(shifted_values_mask |
2610 unshifted_values | shifted_values | stream_num;
2615 * if we did not find any stream, create a new one
2616 * from the least recently used
2618 stream_num = (*track_stream >>
2619 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2621 current_sd = ¤t_ld_sd->stream_track[stream_num];
2622 current_sd->is_read = io_info->isRead;
2623 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2624 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2629 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2630 * affinity (cpu of the controller) and raid_flags in the raid context
2633 * @praid_context: IO RAID context
2634 * @raid: LD raid map
2635 * @fp_possible: Is fast path possible?
2636 * @is_read: Is read IO?
2640 megasas_set_raidflag_cpu_affinity(struct fusion_context *fusion,
2641 union RAID_CONTEXT_UNION *praid_context,
2642 struct MR_LD_RAID *raid, bool fp_possible,
2643 u8 is_read, u32 scsi_buff_len)
2645 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2646 struct RAID_CONTEXT_G35 *rctx_g35;
2648 rctx_g35 = &praid_context->raid_context_g35;
2651 if ((raid->cpuAffinity.pdRead.cpu0) &&
2652 (raid->cpuAffinity.pdRead.cpu1))
2653 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2654 else if (raid->cpuAffinity.pdRead.cpu1)
2655 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2657 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2658 (raid->cpuAffinity.pdWrite.cpu1))
2659 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2660 else if (raid->cpuAffinity.pdWrite.cpu1)
2661 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2662 /* Fast path cache by pass capable R0/R1 VD */
2663 if ((raid->level <= 1) &&
2664 (raid->capability.fp_cache_bypass_capable)) {
2665 rctx_g35->routing_flags |=
2666 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2667 rctx_g35->raid_flags =
2668 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2669 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2674 if ((raid->cpuAffinity.ldRead.cpu0) &&
2675 (raid->cpuAffinity.ldRead.cpu1))
2676 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2677 else if (raid->cpuAffinity.ldRead.cpu1)
2678 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2680 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2681 (raid->cpuAffinity.ldWrite.cpu1))
2682 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2683 else if (raid->cpuAffinity.ldWrite.cpu1)
2684 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2686 if (is_stream_detected(rctx_g35) &&
2687 ((raid->level == 5) || (raid->level == 6)) &&
2688 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2689 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2690 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2694 rctx_g35->routing_flags |=
2695 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2697 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2698 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2699 * IO Subtype is not bitmap.
2701 if ((fusion->pcie_bw_limitation) && (raid->level == 1) && (!is_read) &&
2702 (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)) {
2703 praid_context->raid_context_g35.raid_flags =
2704 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2705 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2710 * megasas_build_ldio_fusion - Prepares IOs to devices
2711 * @instance: Adapter soft state
2712 * @scp: SCSI command
2713 * @cmd: Command to be prepared
2715 * Prepares the io_request and chain elements (sg_frame) for IO
2716 * The IO can be for PD (Fast Path) or LD
2719 megasas_build_ldio_fusion(struct megasas_instance *instance,
2720 struct scsi_cmnd *scp,
2721 struct megasas_cmd_fusion *cmd)
2725 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2727 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2728 struct IO_REQUEST_INFO io_info;
2729 struct fusion_context *fusion;
2730 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2732 unsigned long spinlock_flags;
2733 struct MR_LD_RAID *raid = NULL;
2734 struct MR_PRIV_DEVICE *mrdev_priv;
2735 struct RAID_CONTEXT *rctx;
2736 struct RAID_CONTEXT_G35 *rctx_g35;
2738 device_id = MEGASAS_DEV_INDEX(scp);
2740 fusion = instance->ctrl_context;
2742 io_request = cmd->io_request;
2743 rctx = &io_request->RaidContext.raid_context;
2744 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2746 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2748 rctx->ex_status = 0;
2752 fp_possible = false;
2755 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2757 if (scp->cmd_len == 6) {
2758 datalength = (u32) scp->cmnd[4];
2759 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2760 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2762 start_lba_lo &= 0x1FFFFF;
2766 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2768 else if (scp->cmd_len == 10) {
2769 datalength = (u32) scp->cmnd[8] |
2770 ((u32) scp->cmnd[7] << 8);
2771 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2772 ((u32) scp->cmnd[3] << 16) |
2773 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2777 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2779 else if (scp->cmd_len == 12) {
2780 datalength = ((u32) scp->cmnd[6] << 24) |
2781 ((u32) scp->cmnd[7] << 16) |
2782 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2783 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2784 ((u32) scp->cmnd[3] << 16) |
2785 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2789 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2791 else if (scp->cmd_len == 16) {
2792 datalength = ((u32) scp->cmnd[10] << 24) |
2793 ((u32) scp->cmnd[11] << 16) |
2794 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2795 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2796 ((u32) scp->cmnd[7] << 16) |
2797 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2799 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2800 ((u32) scp->cmnd[3] << 16) |
2801 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2804 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2805 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2806 io_info.numBlocks = datalength;
2807 io_info.ldTgtId = device_id;
2808 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2809 scsi_buff_len = scsi_bufflen(scp);
2810 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2811 io_info.data_arms = 1;
2813 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2816 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2817 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2819 if (ld < instance->fw_supported_vd_count)
2820 raid = MR_LdRaidGet(ld, local_map_ptr);
2822 if (!raid || (!fusion->fast_path_io)) {
2823 rctx->reg_lock_flags = 0;
2824 fp_possible = false;
2826 if (MR_BuildRaidContext(instance, &io_info, rctx,
2827 local_map_ptr, &raidLUN))
2828 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2831 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
2832 atomic_read(&scp->device->device_busy) >
2833 (io_info.data_arms * MR_DEVICE_HIGH_IOPS_DEPTH))
2834 cmd->request_desc->SCSIIO.MSIxIndex =
2835 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
2836 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
2837 else if (instance->msix_load_balance)
2838 cmd->request_desc->SCSIIO.MSIxIndex =
2839 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
2840 instance->msix_vectors));
2842 cmd->request_desc->SCSIIO.MSIxIndex =
2843 instance->reply_map[raw_smp_processor_id()];
2845 if (instance->adapter_type >= VENTURA_SERIES) {
2846 /* FP for Optimal raid level 1.
2847 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2848 * are built by the driver as LD I/Os.
2849 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2850 * (there is never a reason to process these as buffered writes)
2851 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2852 * with the SLD bit asserted.
2854 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2855 mrdev_priv = scp->device->hostdata;
2857 if (atomic_inc_return(&instance->fw_outstanding) >
2858 (instance->host->can_queue)) {
2859 fp_possible = false;
2860 atomic_dec(&instance->fw_outstanding);
2861 } else if (fusion->pcie_bw_limitation &&
2862 ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2863 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0))) {
2864 fp_possible = false;
2865 atomic_dec(&instance->fw_outstanding);
2866 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2867 atomic_set(&mrdev_priv->r1_ldio_hint,
2868 instance->r1_ldio_hint_default);
2873 (io_info.isRead && io_info.ra_capable)) {
2874 spin_lock_irqsave(&instance->stream_lock,
2876 megasas_stream_detect(instance, cmd, &io_info);
2877 spin_unlock_irqrestore(&instance->stream_lock,
2879 /* In ventura if stream detected for a read and it is
2880 * read ahead capable make this IO as LDIO
2882 if (is_stream_detected(rctx_g35))
2883 fp_possible = false;
2886 /* If raid is NULL, set CPU affinity to default CPU0 */
2888 megasas_set_raidflag_cpu_affinity(fusion, &io_request->RaidContext,
2889 raid, fp_possible, io_info.isRead,
2892 rctx_g35->routing_flags |=
2893 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2897 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2898 local_map_ptr, start_lba_lo);
2899 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2900 cmd->request_desc->SCSIIO.RequestFlags =
2901 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2902 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2903 if (instance->adapter_type == INVADER_SERIES) {
2904 rctx->type = MPI2_TYPE_CUDA;
2906 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2907 rctx->reg_lock_flags |=
2908 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2909 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2910 } else if (instance->adapter_type >= VENTURA_SERIES) {
2911 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2912 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2913 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2914 io_request->IoFlags |=
2915 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2917 if (fusion->load_balance_info &&
2918 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2921 get_updated_dev_handle(instance,
2922 &fusion->load_balance_info[device_id],
2923 &io_info, local_map_ptr);
2924 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2925 cmd->pd_r1_lb = io_info.pd_after_lb;
2926 if (instance->adapter_type >= VENTURA_SERIES)
2927 rctx_g35->span_arm = io_info.span_arm;
2929 rctx->span_arm = io_info.span_arm;
2932 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2934 if (instance->adapter_type >= VENTURA_SERIES)
2935 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2937 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2939 if ((raidLUN[0] == 1) &&
2940 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2941 instance->dev_handle = !(instance->dev_handle);
2943 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2946 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2947 io_request->DevHandle = io_info.devHandle;
2948 cmd->pd_interface = io_info.pd_interface;
2949 /* populate the LUN field */
2950 memcpy(io_request->LUN, raidLUN, 8);
2952 rctx->timeout_value =
2953 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2954 cmd->request_desc->SCSIIO.RequestFlags =
2955 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2956 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2957 if (instance->adapter_type == INVADER_SERIES) {
2958 if (io_info.do_fp_rlbypass ||
2959 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2960 cmd->request_desc->SCSIIO.RequestFlags =
2961 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2962 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2963 rctx->type = MPI2_TYPE_CUDA;
2964 rctx->reg_lock_flags |=
2965 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2966 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2968 } else if (instance->adapter_type >= VENTURA_SERIES) {
2969 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2970 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2971 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2973 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2974 io_request->DevHandle = cpu_to_le16(device_id);
2980 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2981 * @instance: Adapter soft state
2982 * @scp: SCSI command
2983 * @cmd: Command to be prepared
2985 * Prepares the io_request frame for non-rw io cmds for vd.
2987 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2988 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2991 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2993 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2994 struct fusion_context *fusion = instance->ctrl_context;
2998 struct MR_LD_RAID *raid;
2999 struct RAID_CONTEXT *pRAID_Context;
3002 io_request = cmd->io_request;
3003 device_id = MEGASAS_DEV_INDEX(scmd);
3004 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
3005 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3006 /* get RAID_Context pointer */
3007 pRAID_Context = &io_request->RaidContext.raid_context;
3008 /* Check with FW team */
3009 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3010 pRAID_Context->reg_lock_row_lba = 0;
3011 pRAID_Context->reg_lock_length = 0;
3013 if (fusion->fast_path_io && (
3014 device_id < instance->fw_supported_vd_count)) {
3016 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
3017 if (ld >= instance->fw_supported_vd_count - 1)
3020 raid = MR_LdRaidGet(ld, local_map_ptr);
3021 if (!(raid->capability.fpNonRWCapable))
3028 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3029 io_request->DevHandle = cpu_to_le16(device_id);
3030 io_request->LUN[1] = scmd->device->lun;
3031 pRAID_Context->timeout_value =
3032 cpu_to_le16 (scmd->request->timeout / HZ);
3033 cmd->request_desc->SCSIIO.RequestFlags =
3034 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3035 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3038 /* set RAID context values */
3039 pRAID_Context->config_seq_num = raid->seqNum;
3040 if (instance->adapter_type < VENTURA_SERIES)
3041 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
3042 pRAID_Context->timeout_value =
3043 cpu_to_le16(raid->fpIoTimeoutForLd);
3045 /* get the DevHandle for the PD (since this is
3046 fpNonRWCapable, this is a single disk RAID0) */
3048 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
3049 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
3050 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
3052 /* build request descriptor */
3053 cmd->request_desc->SCSIIO.RequestFlags =
3054 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3055 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3056 cmd->request_desc->SCSIIO.DevHandle = devHandle;
3058 /* populate the LUN field */
3059 memcpy(io_request->LUN, raid->LUN, 8);
3061 /* build the raidScsiIO structure */
3062 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3063 io_request->DevHandle = devHandle;
3068 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3069 * @instance: Adapter soft state
3070 * @scp: SCSI command
3071 * @cmd: Command to be prepared
3072 * @fp_possible: parameter to detect fast path or firmware path io.
3074 * Prepares the io_request frame for rw/non-rw io cmds for syspds
3077 megasas_build_syspd_fusion(struct megasas_instance *instance,
3078 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
3082 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
3084 u16 os_timeout_value;
3086 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
3087 struct RAID_CONTEXT *pRAID_Context;
3088 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3089 struct MR_PRIV_DEVICE *mr_device_priv_data;
3090 struct fusion_context *fusion = instance->ctrl_context;
3091 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
3093 device_id = MEGASAS_DEV_INDEX(scmd);
3094 pd_index = MEGASAS_PD_INDEX(scmd);
3095 os_timeout_value = scmd->request->timeout / HZ;
3096 mr_device_priv_data = scmd->device->hostdata;
3097 cmd->pd_interface = mr_device_priv_data->interface_type;
3099 io_request = cmd->io_request;
3100 /* get RAID_Context pointer */
3101 pRAID_Context = &io_request->RaidContext.raid_context;
3102 pRAID_Context->reg_lock_flags = 0;
3103 pRAID_Context->reg_lock_row_lba = 0;
3104 pRAID_Context->reg_lock_length = 0;
3105 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3106 io_request->LUN[1] = scmd->device->lun;
3107 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
3108 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
3110 /* If FW supports PD sequence number */
3111 if (instance->support_seqnum_jbod_fp) {
3112 if (instance->use_seqnum_jbod_fp &&
3113 instance->pd_list[pd_index].driveType == TYPE_DISK) {
3115 /* More than 256 PD/JBOD support for Ventura */
3116 if (instance->support_morethan256jbod)
3117 pRAID_Context->virtual_disk_tgt_id =
3118 pd_sync->seq[pd_index].pd_target_id;
3120 pRAID_Context->virtual_disk_tgt_id =
3121 cpu_to_le16(device_id +
3122 (MAX_PHYSICAL_DEVICES - 1));
3123 pRAID_Context->config_seq_num =
3124 pd_sync->seq[pd_index].seqNum;
3125 io_request->DevHandle =
3126 pd_sync->seq[pd_index].devHandle;
3127 if (instance->adapter_type >= VENTURA_SERIES) {
3128 io_request->RaidContext.raid_context_g35.routing_flags |=
3129 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3130 io_request->RaidContext.raid_context_g35.nseg_type |=
3131 (1 << RAID_CONTEXT_NSEG_SHIFT);
3132 io_request->RaidContext.raid_context_g35.nseg_type |=
3133 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3135 pRAID_Context->type = MPI2_TYPE_CUDA;
3136 pRAID_Context->nseg = 0x1;
3137 pRAID_Context->reg_lock_flags |=
3138 (MR_RL_FLAGS_SEQ_NUM_ENABLE |
3139 MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3142 pRAID_Context->virtual_disk_tgt_id =
3143 cpu_to_le16(device_id +
3144 (MAX_PHYSICAL_DEVICES - 1));
3145 pRAID_Context->config_seq_num = 0;
3146 io_request->DevHandle = cpu_to_le16(0xFFFF);
3149 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3150 pRAID_Context->config_seq_num = 0;
3152 if (fusion->fast_path_io) {
3154 fusion->ld_drv_map[(instance->map_id & 1)];
3155 io_request->DevHandle =
3156 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3158 io_request->DevHandle = cpu_to_le16(0xFFFF);
3162 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3164 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
3165 atomic_read(&scmd->device->device_busy) > MR_DEVICE_HIGH_IOPS_DEPTH)
3166 cmd->request_desc->SCSIIO.MSIxIndex =
3167 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
3168 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
3169 else if (instance->msix_load_balance)
3170 cmd->request_desc->SCSIIO.MSIxIndex =
3171 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
3172 instance->msix_vectors));
3174 cmd->request_desc->SCSIIO.MSIxIndex =
3175 instance->reply_map[raw_smp_processor_id()];
3178 /* system pd firmware path */
3179 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3180 cmd->request_desc->SCSIIO.RequestFlags =
3181 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3182 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3183 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3184 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3186 if (os_timeout_value)
3189 /* system pd Fast Path */
3190 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3191 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3193 pRAID_Context->timeout_value =
3194 cpu_to_le16((os_timeout_value > timeout_limit) ?
3195 timeout_limit : os_timeout_value);
3196 if (instance->adapter_type >= INVADER_SERIES)
3197 io_request->IoFlags |=
3198 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3200 cmd->request_desc->SCSIIO.RequestFlags =
3201 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3202 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3207 * megasas_build_io_fusion - Prepares IOs to devices
3208 * @instance: Adapter soft state
3209 * @scp: SCSI command
3210 * @cmd: Command to be prepared
3212 * Invokes helper functions to prepare request frames
3213 * and sets flags appropriate for IO/Non-IO cmd
3216 megasas_build_io_fusion(struct megasas_instance *instance,
3217 struct scsi_cmnd *scp,
3218 struct megasas_cmd_fusion *cmd)
3222 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3223 struct MR_PRIV_DEVICE *mr_device_priv_data;
3224 mr_device_priv_data = scp->device->hostdata;
3226 /* Zero out some fields so they don't get reused */
3227 memset(io_request->LUN, 0x0, 8);
3228 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3229 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3230 io_request->EEDPFlags = 0;
3231 io_request->Control = 0;
3232 io_request->EEDPBlockSize = 0;
3233 io_request->ChainOffset = 0;
3234 io_request->RaidContext.raid_context.raid_flags = 0;
3235 io_request->RaidContext.raid_context.type = 0;
3236 io_request->RaidContext.raid_context.nseg = 0;
3238 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3240 * Just the CDB length,rest of the Flags are zero
3241 * This will be modified for FP in build_ldio_fusion
3243 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3245 switch (cmd_type = megasas_cmd_type(scp)) {
3246 case READ_WRITE_LDIO:
3247 megasas_build_ldio_fusion(instance, scp, cmd);
3249 case NON_READ_WRITE_LDIO:
3250 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3252 case READ_WRITE_SYSPDIO:
3253 megasas_build_syspd_fusion(instance, scp, cmd, true);
3255 case NON_READ_WRITE_SYSPDIO:
3256 if (instance->secure_jbod_support ||
3257 mr_device_priv_data->is_tm_capable)
3258 megasas_build_syspd_fusion(instance, scp, cmd, false);
3260 megasas_build_syspd_fusion(instance, scp, cmd, true);
3270 sge_count = megasas_make_sgl(instance, scp, cmd);
3272 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3273 dev_err(&instance->pdev->dev,
3274 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3275 __func__, __LINE__, sge_count, instance->max_num_sge);
3279 if (instance->adapter_type >= VENTURA_SERIES) {
3280 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3281 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3282 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3284 /* numSGE store lower 8 bit of sge_count.
3285 * numSGEExt store higher 8 bit of sge_count
3287 io_request->RaidContext.raid_context.num_sge = sge_count;
3288 io_request->RaidContext.raid_context.num_sge_ext =
3289 (u8)(sge_count >> 8);
3292 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3294 if (scp->sc_data_direction == DMA_TO_DEVICE)
3295 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3296 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3297 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3299 io_request->SGLOffset0 =
3300 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3302 io_request->SenseBufferLowAddress =
3303 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3304 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3307 scp->SCp.ptr = (char *)cmd;
3312 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3313 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3316 struct fusion_context *fusion;
3318 fusion = instance->ctrl_context;
3319 p = fusion->req_frames_desc +
3320 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3322 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3326 /* megasas_prepate_secondRaid1_IO
3327 * It prepares the raid 1 second IO
3329 static void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3330 struct megasas_cmd_fusion *cmd,
3331 struct megasas_cmd_fusion *r1_cmd)
3333 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3334 struct fusion_context *fusion;
3335 fusion = instance->ctrl_context;
3336 req_desc = cmd->request_desc;
3337 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3338 memcpy(r1_cmd->io_request, cmd->io_request,
3339 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3340 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3341 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3342 /*sense buffer is different for r1 command*/
3343 r1_cmd->io_request->SenseBufferLowAddress =
3344 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3345 r1_cmd->scmd = cmd->scmd;
3346 req_desc2 = megasas_get_request_descriptor(instance,
3347 (r1_cmd->index - 1));
3348 req_desc2->Words = 0;
3349 r1_cmd->request_desc = req_desc2;
3350 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3351 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3352 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3353 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3354 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3355 cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3356 cpu_to_le16(r1_cmd->index);
3357 r1_cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3358 cpu_to_le16(cmd->index);
3359 /*MSIxIndex of both commands request descriptors should be same*/
3360 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3361 cmd->request_desc->SCSIIO.MSIxIndex;
3362 /*span arm is different for r1 cmd*/
3363 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3364 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3368 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3369 * issuing non IOCTL cmd
3370 * @instance: Adapter soft state
3371 * @scmd: pointer to scsi cmd from OS
3374 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3375 struct scsi_cmnd *scmd)
3377 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3378 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3381 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3382 instance->ldio_threshold &&
3383 (atomic_inc_return(&instance->ldio_outstanding) >
3384 instance->ldio_threshold)) {
3385 atomic_dec(&instance->ldio_outstanding);
3386 return SCSI_MLQUEUE_DEVICE_BUSY;
3389 if (atomic_inc_return(&instance->fw_outstanding) >
3390 instance->host->can_queue) {
3391 atomic_dec(&instance->fw_outstanding);
3392 return SCSI_MLQUEUE_HOST_BUSY;
3395 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3398 atomic_dec(&instance->fw_outstanding);
3399 return SCSI_MLQUEUE_HOST_BUSY;
3404 req_desc = megasas_get_request_descriptor(instance, index-1);
3406 req_desc->Words = 0;
3407 cmd->request_desc = req_desc;
3409 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3410 megasas_return_cmd_fusion(instance, cmd);
3411 dev_err(&instance->pdev->dev, "Error building command\n");
3412 cmd->request_desc = NULL;
3413 atomic_dec(&instance->fw_outstanding);
3414 return SCSI_MLQUEUE_HOST_BUSY;
3417 req_desc = cmd->request_desc;
3418 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3420 if (cmd->io_request->ChainOffset != 0 &&
3421 cmd->io_request->ChainOffset != 0xF)
3422 dev_err(&instance->pdev->dev, "The chain offset value is not "
3423 "correct : %x\n", cmd->io_request->ChainOffset);
3425 * if it is raid 1/10 fp write capable.
3426 * try to get second command from pool and construct it.
3427 * From FW, it has confirmed that lba values of two PDs
3428 * corresponds to single R1/10 LD are always same
3431 /* driver side count always should be less than max_fw_cmds
3432 * to get new command
3434 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3435 r1_cmd = megasas_get_cmd_fusion(instance,
3436 (scmd->request->tag + instance->max_fw_cmds));
3437 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3442 * Issue the command to the FW
3445 megasas_fire_cmd_fusion(instance, req_desc);
3448 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3455 * megasas_complete_r1_command -
3456 * completes R1 FP write commands which has valid peer smid
3457 * @instance: Adapter soft state
3458 * @cmd_fusion: MPT command frame
3462 megasas_complete_r1_command(struct megasas_instance *instance,
3463 struct megasas_cmd_fusion *cmd)
3465 u8 *sense, status, ex_status;
3468 struct fusion_context *fusion;
3469 struct megasas_cmd_fusion *r1_cmd = NULL;
3470 struct scsi_cmnd *scmd_local = NULL;
3471 struct RAID_CONTEXT_G35 *rctx_g35;
3473 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3474 fusion = instance->ctrl_context;
3475 peer_smid = le16_to_cpu(rctx_g35->flow_specific.peer_smid);
3477 r1_cmd = fusion->cmd_list[peer_smid - 1];
3478 scmd_local = cmd->scmd;
3479 status = rctx_g35->status;
3480 ex_status = rctx_g35->ex_status;
3481 data_length = cmd->io_request->DataLength;
3484 cmd->cmd_completed = true;
3486 /* Check if peer command is completed or not*/
3487 if (r1_cmd->cmd_completed) {
3488 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3489 if (rctx_g35->status != MFI_STAT_OK) {
3490 status = rctx_g35->status;
3491 ex_status = rctx_g35->ex_status;
3492 data_length = r1_cmd->io_request->DataLength;
3493 sense = r1_cmd->sense;
3496 megasas_return_cmd_fusion(instance, r1_cmd);
3497 map_cmd_status(fusion, scmd_local, status, ex_status,
3498 le32_to_cpu(data_length), sense);
3499 if (instance->ldio_threshold &&
3500 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3501 atomic_dec(&instance->ldio_outstanding);
3502 scmd_local->SCp.ptr = NULL;
3503 megasas_return_cmd_fusion(instance, cmd);
3504 scsi_dma_unmap(scmd_local);
3505 scmd_local->scsi_done(scmd_local);
3510 * complete_cmd_fusion - Completes command
3511 * @instance: Adapter soft state
3512 * Completes all commands that is in reply descriptor queue
3515 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex,
3516 struct megasas_irq_context *irq_context)
3518 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3519 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3520 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3521 struct fusion_context *fusion;
3522 struct megasas_cmd *cmd_mfi;
3523 struct megasas_cmd_fusion *cmd_fusion;
3524 u16 smid, num_completed;
3525 u8 reply_descript_type, *sense, status, extStatus;
3526 u32 device_id, data_length;
3527 union desc_value d_val;
3528 struct LD_LOAD_BALANCE_INFO *lbinfo;
3529 int threshold_reply_count = 0;
3530 struct scsi_cmnd *scmd_local = NULL;
3531 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3532 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3534 fusion = instance->ctrl_context;
3536 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3539 desc = fusion->reply_frames_desc[MSIxIndex] +
3540 fusion->last_reply_idx[MSIxIndex];
3542 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3544 d_val.word = desc->Words;
3546 reply_descript_type = reply_desc->ReplyFlags &
3547 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3549 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3554 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3555 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3557 smid = le16_to_cpu(reply_desc->SMID);
3558 cmd_fusion = fusion->cmd_list[smid - 1];
3559 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3560 cmd_fusion->io_request;
3562 scmd_local = cmd_fusion->scmd;
3563 status = scsi_io_req->RaidContext.raid_context.status;
3564 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3565 sense = cmd_fusion->sense;
3566 data_length = scsi_io_req->DataLength;
3568 switch (scsi_io_req->Function) {
3569 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3570 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3571 cmd_fusion->io_request;
3572 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3573 &mr_tm_req->TmRequest;
3574 dev_dbg(&instance->pdev->dev, "TM completion:"
3575 "type: 0x%x TaskMID: 0x%x\n",
3576 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3577 complete(&cmd_fusion->done);
3579 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3580 /* Update load balancing info */
3581 if (fusion->load_balance_info &&
3582 (cmd_fusion->scmd->SCp.Status &
3583 MEGASAS_LOAD_BALANCE_FLAG)) {
3584 device_id = MEGASAS_DEV_INDEX(scmd_local);
3585 lbinfo = &fusion->load_balance_info[device_id];
3586 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3587 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3589 /* Fall through - and complete IO */
3590 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3591 atomic_dec(&instance->fw_outstanding);
3592 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3593 map_cmd_status(fusion, scmd_local, status,
3594 extStatus, le32_to_cpu(data_length),
3596 if (instance->ldio_threshold &&
3597 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3598 atomic_dec(&instance->ldio_outstanding);
3599 scmd_local->SCp.ptr = NULL;
3600 megasas_return_cmd_fusion(instance, cmd_fusion);
3601 scsi_dma_unmap(scmd_local);
3602 scmd_local->scsi_done(scmd_local);
3603 } else /* Optimal VD - R1 FP command completion. */
3604 megasas_complete_r1_command(instance, cmd_fusion);
3606 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3607 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3608 /* Poll mode. Dummy free.
3609 * In case of Interrupt mode, caller has reverse check.
3611 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3612 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3613 megasas_return_cmd(instance, cmd_mfi);
3615 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3619 fusion->last_reply_idx[MSIxIndex]++;
3620 if (fusion->last_reply_idx[MSIxIndex] >=
3621 fusion->reply_q_depth)
3622 fusion->last_reply_idx[MSIxIndex] = 0;
3624 desc->Words = cpu_to_le64(ULLONG_MAX);
3626 threshold_reply_count++;
3628 /* Get the next reply descriptor */
3629 if (!fusion->last_reply_idx[MSIxIndex])
3630 desc = fusion->reply_frames_desc[MSIxIndex];
3635 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3637 d_val.word = desc->Words;
3639 reply_descript_type = reply_desc->ReplyFlags &
3640 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3642 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3645 * Write to reply post host index register after completing threshold
3646 * number of reply counts and still there are more replies in reply queue
3647 * pending to be completed
3649 if (threshold_reply_count >= instance->threshold_reply_count) {
3650 if (instance->msix_combined)
3651 writel(((MSIxIndex & 0x7) << 24) |
3652 fusion->last_reply_idx[MSIxIndex],
3653 instance->reply_post_host_index_addr[MSIxIndex/8]);
3655 writel((MSIxIndex << 24) |
3656 fusion->last_reply_idx[MSIxIndex],
3657 instance->reply_post_host_index_addr[0]);
3658 threshold_reply_count = 0;
3660 if (!irq_context->irq_poll_scheduled) {
3661 irq_context->irq_poll_scheduled = true;
3662 irq_context->irq_line_enable = true;
3663 irq_poll_sched(&irq_context->irqpoll);
3665 return num_completed;
3670 if (num_completed) {
3672 if (instance->msix_combined)
3673 writel(((MSIxIndex & 0x7) << 24) |
3674 fusion->last_reply_idx[MSIxIndex],
3675 instance->reply_post_host_index_addr[MSIxIndex/8]);
3677 writel((MSIxIndex << 24) |
3678 fusion->last_reply_idx[MSIxIndex],
3679 instance->reply_post_host_index_addr[0]);
3680 megasas_check_and_restore_queue_depth(instance);
3682 return num_completed;
3686 * megasas_enable_irq_poll() - enable irqpoll
3688 static void megasas_enable_irq_poll(struct megasas_instance *instance)
3691 struct megasas_irq_context *irq_ctx;
3693 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3695 for (i = 0; i < count; i++) {
3696 irq_ctx = &instance->irq_context[i];
3697 irq_poll_enable(&irq_ctx->irqpoll);
3702 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3703 * @instance: Adapter soft state
3705 void megasas_sync_irqs(unsigned long instance_addr)
3708 struct megasas_instance *instance =
3709 (struct megasas_instance *)instance_addr;
3710 struct megasas_irq_context *irq_ctx;
3712 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3714 for (i = 0; i < count; i++) {
3715 synchronize_irq(pci_irq_vector(instance->pdev, i));
3716 irq_ctx = &instance->irq_context[i];
3717 irq_poll_disable(&irq_ctx->irqpoll);
3718 if (irq_ctx->irq_poll_scheduled) {
3719 irq_ctx->irq_poll_scheduled = false;
3720 enable_irq(irq_ctx->os_irq);
3726 * megasas_irqpoll() - process a queue for completed reply descriptors
3727 * @irqpoll: IRQ poll structure associated with queue to poll.
3728 * @budget: Threshold of reply descriptors to process per poll.
3730 * Return: The number of entries processed.
3733 int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3735 struct megasas_irq_context *irq_ctx;
3736 struct megasas_instance *instance;
3739 irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3740 instance = irq_ctx->instance;
3742 if (irq_ctx->irq_line_enable) {
3743 disable_irq(irq_ctx->os_irq);
3744 irq_ctx->irq_line_enable = false;
3747 num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3748 if (num_entries < budget) {
3749 irq_poll_complete(irqpoll);
3750 irq_ctx->irq_poll_scheduled = false;
3751 enable_irq(irq_ctx->os_irq);
3758 * megasas_complete_cmd_dpc_fusion - Completes command
3759 * @instance: Adapter soft state
3761 * Tasklet to complete cmds
3764 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3766 struct megasas_instance *instance =
3767 (struct megasas_instance *)instance_addr;
3768 u32 count, MSIxIndex;
3770 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3772 /* If we have already declared adapter dead, donot complete cmds */
3773 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3776 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3777 complete_cmd_fusion(instance, MSIxIndex, NULL);
3781 * megasas_isr_fusion - isr entry point
3783 irqreturn_t megasas_isr_fusion(int irq, void *devp)
3785 struct megasas_irq_context *irq_context = devp;
3786 struct megasas_instance *instance = irq_context->instance;
3789 if (instance->mask_interrupts)
3792 #if defined(ENABLE_IRQ_POLL)
3793 if (irq_context->irq_poll_scheduled)
3797 if (!instance->msix_vectors) {
3798 mfiStatus = instance->instancet->clear_intr(instance);
3803 /* If we are resetting, bail */
3804 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3805 instance->instancet->clear_intr(instance);
3809 return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3810 ? IRQ_HANDLED : IRQ_NONE;
3814 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3815 * @instance: Adapter soft state
3816 * mfi_cmd: megasas_cmd pointer
3820 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3821 struct megasas_cmd *mfi_cmd)
3823 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3824 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3825 struct megasas_cmd_fusion *cmd;
3826 struct fusion_context *fusion;
3827 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3829 fusion = instance->ctrl_context;
3831 cmd = megasas_get_cmd_fusion(instance,
3832 instance->max_scsi_cmds + mfi_cmd->index);
3834 /* Save the smid. To be used for returning the cmd */
3835 mfi_cmd->context.smid = cmd->index;
3838 * For cmds where the flag is set, store the flag and check
3839 * on completion. For cmds with this flag, don't call
3840 * megasas_complete_cmd
3843 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3844 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3846 io_req = cmd->io_request;
3848 if (instance->adapter_type >= INVADER_SERIES) {
3849 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3850 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3851 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3852 sgl_ptr_end->Flags = 0;
3856 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3858 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3859 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3861 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3863 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3865 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3866 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3868 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3872 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3873 * @instance: Adapter soft state
3874 * @cmd: mfi cmd to build
3877 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3878 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3880 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3883 build_mpt_mfi_pass_thru(instance, cmd);
3884 index = cmd->context.smid;
3886 req_desc = megasas_get_request_descriptor(instance, index - 1);
3888 req_desc->Words = 0;
3889 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3890 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3892 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3898 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3899 * @instance: Adapter soft state
3900 * @cmd: mfi cmd pointer
3904 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3905 struct megasas_cmd *cmd)
3907 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3909 req_desc = build_mpt_cmd(instance, cmd);
3911 megasas_fire_cmd_fusion(instance, req_desc);
3916 * megasas_release_fusion - Reverses the FW initialization
3917 * @instance: Adapter soft state
3920 megasas_release_fusion(struct megasas_instance *instance)
3922 megasas_free_ioc_init_cmd(instance);
3923 megasas_free_cmds(instance);
3924 megasas_free_cmds_fusion(instance);
3926 iounmap(instance->reg_set);
3928 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3932 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3933 * @regs: MFI register set
3936 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
3938 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
3942 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3943 * @instance: Controller's soft instance
3944 * return: Number of allocated host crash buffers
3947 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3951 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3952 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3953 if (!instance->crash_buf[i]) {
3954 dev_info(&instance->pdev->dev, "Firmware crash dump "
3955 "memory allocation failed at index %d\n", i);
3959 instance->drv_buf_alloc = i;
3963 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3964 * @instance: Controller's soft instance
3967 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3970 for (i = 0; i < instance->drv_buf_alloc; i++) {
3971 if (instance->crash_buf[i])
3972 vfree(instance->crash_buf[i]);
3974 instance->drv_buf_index = 0;
3975 instance->drv_buf_alloc = 0;
3976 instance->fw_crash_state = UNAVAILABLE;
3977 instance->fw_crash_buffer_size = 0;
3981 * megasas_adp_reset_fusion - For controller reset
3982 * @regs: MFI register set
3985 megasas_adp_reset_fusion(struct megasas_instance *instance,
3986 struct megasas_register_set __iomem *regs)
3988 u32 host_diag, abs_state, retry;
3990 /* Now try to reset the chip */
3991 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3992 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3993 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3994 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3995 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3996 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3997 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3999 /* Check that the diag write enable (DRWE) bit is on */
4000 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4002 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
4004 host_diag = megasas_readl(instance,
4005 &instance->reg_set->fusion_host_diag);
4006 if (retry++ == 100) {
4007 dev_warn(&instance->pdev->dev,
4008 "Host diag unlock failed from %s %d\n",
4009 __func__, __LINE__);
4013 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
4016 /* Send chip reset command */
4017 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
4018 &instance->reg_set->fusion_host_diag);
4021 /* Make sure reset adapter bit is cleared */
4022 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
4024 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
4026 host_diag = megasas_readl(instance,
4027 &instance->reg_set->fusion_host_diag);
4028 if (retry++ == 1000) {
4029 dev_warn(&instance->pdev->dev,
4030 "Diag reset adapter never cleared %s %d\n",
4031 __func__, __LINE__);
4035 if (host_diag & HOST_DIAG_RESET_ADAPTER)
4038 abs_state = instance->instancet->read_fw_status_reg(instance)
4042 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
4044 abs_state = instance->instancet->
4045 read_fw_status_reg(instance) & MFI_STATE_MASK;
4047 if (abs_state <= MFI_STATE_FW_INIT) {
4048 dev_warn(&instance->pdev->dev,
4049 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4050 abs_state, __func__, __LINE__);
4058 * megasas_check_reset_fusion - For controller reset check
4059 * @regs: MFI register set
4062 megasas_check_reset_fusion(struct megasas_instance *instance,
4063 struct megasas_register_set __iomem *regs)
4069 * megasas_trigger_snap_dump - Trigger snap dump in FW
4070 * @instance: Soft instance of adapter
4072 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4075 u32 fw_state, abs_state;
4077 if (!instance->disableOnlineCtrlReset) {
4078 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4079 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4080 &instance->reg_set->doorbell);
4081 readl(&instance->reg_set->doorbell);
4084 for (j = 0; j < instance->snapdump_wait_time; j++) {
4085 abs_state = instance->instancet->read_fw_status_reg(instance);
4086 fw_state = abs_state & MFI_STATE_MASK;
4087 if (fw_state == MFI_STATE_FAULT) {
4088 dev_printk(KERN_ERR, &instance->pdev->dev,
4089 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4090 abs_state & MFI_STATE_FAULT_CODE,
4091 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4098 /* This function waits for outstanding commands on fusion to complete */
4099 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
4100 int reason, int *convert)
4102 int i, outstanding, retval = 0, hb_seconds_missed = 0;
4103 u32 fw_state, abs_state;
4104 u32 waittime_for_io_completion;
4106 waittime_for_io_completion =
4107 min_t(u32, resetwaittime,
4108 (resetwaittime - instance->snapdump_wait_time));
4110 if (reason == MFI_IO_TIMEOUT_OCR) {
4111 dev_info(&instance->pdev->dev,
4112 "MFI command is timed out\n");
4113 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4114 if (instance->snapdump_wait_time)
4115 megasas_trigger_snap_dump(instance);
4120 for (i = 0; i < waittime_for_io_completion; i++) {
4121 /* Check if firmware is in fault state */
4122 abs_state = instance->instancet->read_fw_status_reg(instance);
4123 fw_state = abs_state & MFI_STATE_MASK;
4124 if (fw_state == MFI_STATE_FAULT) {
4125 dev_printk(KERN_ERR, &instance->pdev->dev,
4126 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4127 abs_state & MFI_STATE_FAULT_CODE,
4128 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4129 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4130 if (instance->requestorId && reason) {
4131 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4132 " state while polling during"
4133 " I/O timeout handling for %d\n",
4134 instance->host->host_no);
4143 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4144 if (instance->requestorId && !reason) {
4149 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4150 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
4151 if (instance->hb_host_mem->HB.fwCounter !=
4152 instance->hb_host_mem->HB.driverCounter) {
4153 instance->hb_host_mem->HB.driverCounter =
4154 instance->hb_host_mem->HB.fwCounter;
4155 hb_seconds_missed = 0;
4157 hb_seconds_missed++;
4158 if (hb_seconds_missed ==
4159 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
4160 dev_warn(&instance->pdev->dev, "SR-IOV:"
4161 " Heartbeat never completed "
4162 " while polling during I/O "
4163 " timeout handling for "
4165 instance->host->host_no);
4173 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4174 outstanding = atomic_read(&instance->fw_outstanding);
4178 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4179 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
4180 "commands to complete for scsi%d\n", i,
4181 outstanding, instance->host->host_no);
4186 if (instance->snapdump_wait_time) {
4187 megasas_trigger_snap_dump(instance);
4192 if (atomic_read(&instance->fw_outstanding)) {
4193 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
4194 "will reset adapter scsi%d.\n",
4195 instance->host->host_no);
4204 void megasas_reset_reply_desc(struct megasas_instance *instance)
4207 struct fusion_context *fusion;
4208 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4210 fusion = instance->ctrl_context;
4211 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4212 for (i = 0 ; i < count ; i++) {
4213 fusion->last_reply_idx[i] = 0;
4214 reply_desc = fusion->reply_frames_desc[i];
4215 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4216 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4221 * megasas_refire_mgmt_cmd : Re-fire management commands
4222 * @instance: Controller's soft instance
4224 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
4227 struct megasas_cmd_fusion *cmd_fusion;
4228 struct fusion_context *fusion;
4229 struct megasas_cmd *cmd_mfi;
4230 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4232 bool refire_cmd = 0;
4236 fusion = instance->ctrl_context;
4238 /* Re-fire management commands.
4239 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4241 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4242 cmd_fusion = fusion->cmd_list[j];
4243 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4244 smid = le16_to_cpu(cmd_mfi->context.smid);
4245 result = REFIRE_CMD;
4250 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4252 switch (cmd_mfi->frame->hdr.cmd) {
4254 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4255 /* Do not refire shutdown command */
4256 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4257 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4258 result = COMPLETE_CMD;
4262 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4263 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4264 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4267 result = RETURN_CMD;
4271 if (!instance->support_nvme_passthru) {
4272 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4273 result = COMPLETE_CMD;
4277 case MFI_CMD_TOOLBOX:
4278 if (!instance->support_pci_lane_margining) {
4279 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4280 result = COMPLETE_CMD;
4290 megasas_fire_cmd_fusion(instance, req_desc);
4293 megasas_return_cmd(instance, cmd_mfi);
4296 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4303 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4304 * @instance: per adapter struct
4305 * @channel: the channel assigned by the OS
4306 * @id: the id assigned by the OS
4308 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4311 static int megasas_track_scsiio(struct megasas_instance *instance,
4312 int id, int channel)
4315 struct megasas_cmd_fusion *cmd_fusion;
4316 struct fusion_context *fusion;
4317 fusion = instance->ctrl_context;
4319 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4320 cmd_fusion = fusion->cmd_list[i];
4321 if (cmd_fusion->scmd &&
4322 (cmd_fusion->scmd->device->id == id &&
4323 cmd_fusion->scmd->device->channel == channel)) {
4324 dev_info(&instance->pdev->dev,
4325 "SCSI commands pending to target"
4326 "channel %d id %d \tSMID: 0x%x\n",
4327 channel, id, cmd_fusion->index);
4328 scsi_print_command(cmd_fusion->scmd);
4334 return found ? FAILED : SUCCESS;
4338 * megasas_tm_response_code - translation of device response code
4339 * @ioc: per adapter object
4340 * @mpi_reply: MPI reply returned by firmware
4345 megasas_tm_response_code(struct megasas_instance *instance,
4346 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4350 switch (mpi_reply->ResponseCode) {
4351 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4352 desc = "task management request completed";
4354 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4355 desc = "invalid frame";
4357 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4358 desc = "task management request not supported";
4360 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4361 desc = "task management request failed";
4363 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4364 desc = "task management request succeeded";
4366 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4367 desc = "invalid lun";
4370 desc = "overlapped tag attempted";
4372 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4373 desc = "task queued, however not sent to target";
4379 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4380 mpi_reply->ResponseCode, desc);
4381 dev_dbg(&instance->pdev->dev,
4382 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4383 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4384 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4385 mpi_reply->Function, mpi_reply->TaskType,
4386 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4390 * megasas_issue_tm - main routine for sending tm requests
4391 * @instance: per adapter struct
4392 * @device_handle: device handle
4393 * @channel: the channel assigned by the OS
4394 * @id: the id assigned by the OS
4395 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4396 * @smid_task: smid assigned to the task
4397 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4400 * MegaRaid use MPT interface for Task Magement request.
4401 * A generic API for sending task management requests to firmware.
4403 * Return SUCCESS or FAILED.
4406 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4407 uint channel, uint id, u16 smid_task, u8 type,
4408 struct MR_PRIV_DEVICE *mr_device_priv_data)
4410 struct MR_TASK_MANAGE_REQUEST *mr_request;
4411 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4412 unsigned long timeleft;
4413 struct megasas_cmd_fusion *cmd_fusion;
4414 struct megasas_cmd *cmd_mfi;
4415 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4416 struct fusion_context *fusion = NULL;
4417 struct megasas_cmd_fusion *scsi_lookup;
4419 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4420 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4422 fusion = instance->ctrl_context;
4424 cmd_mfi = megasas_get_cmd(instance);
4427 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4428 __func__, __LINE__);
4432 cmd_fusion = megasas_get_cmd_fusion(instance,
4433 instance->max_scsi_cmds + cmd_mfi->index);
4435 /* Save the smid. To be used for returning the cmd */
4436 cmd_mfi->context.smid = cmd_fusion->index;
4438 req_desc = megasas_get_request_descriptor(instance,
4439 (cmd_fusion->index - 1));
4441 cmd_fusion->request_desc = req_desc;
4442 req_desc->Words = 0;
4444 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4445 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4446 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4447 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4448 mpi_request->DevHandle = cpu_to_le16(device_handle);
4449 mpi_request->TaskType = type;
4450 mpi_request->TaskMID = cpu_to_le16(smid_task);
4451 mpi_request->LUN[1] = 0;
4454 req_desc = cmd_fusion->request_desc;
4455 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4456 req_desc->HighPriority.RequestFlags =
4457 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4458 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4459 req_desc->HighPriority.MSIxIndex = 0;
4460 req_desc->HighPriority.LMID = 0;
4461 req_desc->HighPriority.Reserved1 = 0;
4463 if (channel < MEGASAS_MAX_PD_CHANNELS)
4464 mr_request->tmReqFlags.isTMForPD = 1;
4466 mr_request->tmReqFlags.isTMForLD = 1;
4468 init_completion(&cmd_fusion->done);
4469 megasas_fire_cmd_fusion(instance, req_desc);
4472 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4473 timeout = mr_device_priv_data->task_abort_tmo;
4475 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4476 timeout = mr_device_priv_data->target_reset_tmo;
4480 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4483 dev_err(&instance->pdev->dev,
4484 "task mgmt type 0x%x timed out\n", type);
4485 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
4486 mutex_unlock(&instance->reset_mutex);
4487 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4488 mutex_lock(&instance->reset_mutex);
4492 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4493 megasas_tm_response_code(instance, mpi_reply);
4495 megasas_return_cmd(instance, cmd_mfi);
4498 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4499 scsi_lookup = fusion->cmd_list[smid_task - 1];
4501 if (scsi_lookup->scmd == NULL)
4504 instance->instancet->disable_intr(instance);
4505 megasas_sync_irqs((unsigned long)instance);
4506 instance->instancet->enable_intr(instance);
4507 megasas_enable_irq_poll(instance);
4508 if (scsi_lookup->scmd == NULL)
4514 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4515 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4517 instance->instancet->disable_intr(instance);
4518 megasas_sync_irqs((unsigned long)instance);
4519 rc = megasas_track_scsiio(instance, id, channel);
4520 instance->instancet->enable_intr(instance);
4521 megasas_enable_irq_poll(instance);
4524 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4525 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4537 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4538 * @instance: per adapter struct
4540 * Return Non Zero index, if SMID found in outstanding commands
4542 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4545 struct megasas_instance *instance;
4546 struct megasas_cmd_fusion *cmd_fusion;
4547 struct fusion_context *fusion;
4549 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4551 fusion = instance->ctrl_context;
4553 for (i = 0; i < instance->max_scsi_cmds; i++) {
4554 cmd_fusion = fusion->cmd_list[i];
4555 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4556 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4557 " SMID: %d\n", cmd_fusion->index);
4558 ret = cmd_fusion->index;
4567 * megasas_get_tm_devhandle - Get devhandle for TM request
4568 * @sdev- OS provided scsi device
4570 * Returns- devhandle/targetID of SCSI device
4572 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4576 struct megasas_instance *instance;
4577 struct fusion_context *fusion;
4578 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4579 u16 devhandle = (u16)ULONG_MAX;
4581 instance = (struct megasas_instance *)sdev->host->hostdata;
4582 fusion = instance->ctrl_context;
4584 if (!MEGASAS_IS_LOGICAL(sdev)) {
4585 if (instance->use_seqnum_jbod_fp) {
4586 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4588 pd_sync = (void *)fusion->pd_seq_sync
4589 [(instance->pd_seq_map_id - 1) & 1];
4590 devhandle = pd_sync->seq[pd_index].devHandle;
4592 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4593 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4595 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4597 devhandle = device_id;
4604 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4605 * @scmd : pointer to scsi command object
4607 * Return SUCCESS, if command aborted else FAILED
4610 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4612 struct megasas_instance *instance;
4613 u16 smid, devhandle;
4615 struct MR_PRIV_DEVICE *mr_device_priv_data;
4616 mr_device_priv_data = scmd->device->hostdata;
4618 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4620 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4621 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4622 "SCSI host:%d\n", instance->host->host_no);
4627 if (!mr_device_priv_data) {
4628 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4629 "scmd(%p)\n", scmd);
4630 scmd->result = DID_NO_CONNECT << 16;
4635 if (!mr_device_priv_data->is_tm_capable) {
4640 mutex_lock(&instance->reset_mutex);
4642 smid = megasas_fusion_smid_lookup(scmd);
4646 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4647 " issued is not found in outstanding commands\n");
4648 mutex_unlock(&instance->reset_mutex);
4652 devhandle = megasas_get_tm_devhandle(scmd->device);
4654 if (devhandle == (u16)ULONG_MAX) {
4656 sdev_printk(KERN_INFO, scmd->device,
4657 "task abort issued for invalid devhandle\n");
4658 mutex_unlock(&instance->reset_mutex);
4661 sdev_printk(KERN_INFO, scmd->device,
4662 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4665 mr_device_priv_data->tm_busy = 1;
4666 ret = megasas_issue_tm(instance, devhandle,
4667 scmd->device->channel, scmd->device->id, smid,
4668 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4669 mr_device_priv_data);
4670 mr_device_priv_data->tm_busy = 0;
4672 mutex_unlock(&instance->reset_mutex);
4673 scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
4674 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4676 scsi_print_command(scmd);
4677 if (megasas_dbg_lvl & TM_DEBUG)
4678 megasas_dump_fusion_io(scmd);
4684 * megasas_reset_target_fusion : target reset function for fusion adapters
4685 * scmd: SCSI command pointer
4687 * Returns SUCCESS if all commands associated with target aborted else FAILED
4690 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4693 struct megasas_instance *instance;
4696 struct MR_PRIV_DEVICE *mr_device_priv_data;
4697 mr_device_priv_data = scmd->device->hostdata;
4699 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4701 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4702 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4703 "SCSI host:%d\n", instance->host->host_no);
4708 if (!mr_device_priv_data) {
4709 sdev_printk(KERN_INFO, scmd->device,
4710 "device been deleted! scmd: (0x%p)\n", scmd);
4711 scmd->result = DID_NO_CONNECT << 16;
4716 if (!mr_device_priv_data->is_tm_capable) {
4721 mutex_lock(&instance->reset_mutex);
4722 devhandle = megasas_get_tm_devhandle(scmd->device);
4724 if (devhandle == (u16)ULONG_MAX) {
4726 sdev_printk(KERN_INFO, scmd->device,
4727 "target reset issued for invalid devhandle\n");
4728 mutex_unlock(&instance->reset_mutex);
4732 sdev_printk(KERN_INFO, scmd->device,
4733 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4735 mr_device_priv_data->tm_busy = 1;
4736 ret = megasas_issue_tm(instance, devhandle,
4737 scmd->device->channel, scmd->device->id, 0,
4738 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4739 mr_device_priv_data);
4740 mr_device_priv_data->tm_busy = 0;
4741 mutex_unlock(&instance->reset_mutex);
4742 scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
4743 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4749 /*SRIOV get other instance in cluster if any*/
4750 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4754 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4755 if (megasas_mgmt_info.instance[i] &&
4756 (megasas_mgmt_info.instance[i] != instance) &&
4757 megasas_mgmt_info.instance[i]->requestorId &&
4758 megasas_mgmt_info.instance[i]->peerIsPresent &&
4759 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4760 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4761 return megasas_mgmt_info.instance[i];
4766 /* Check for a second path that is currently UP */
4767 int megasas_check_mpio_paths(struct megasas_instance *instance,
4768 struct scsi_cmnd *scmd)
4770 struct megasas_instance *peer_instance = NULL;
4771 int retval = (DID_REQUEUE << 16);
4773 if (instance->peerIsPresent) {
4774 peer_instance = megasas_get_peer_instance(instance);
4775 if ((peer_instance) &&
4776 (atomic_read(&peer_instance->adprecovery) ==
4777 MEGASAS_HBA_OPERATIONAL))
4778 retval = (DID_NO_CONNECT << 16);
4783 /* Core fusion reset function */
4784 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4786 int retval = SUCCESS, i, j, convert = 0;
4787 struct megasas_instance *instance;
4788 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4789 struct fusion_context *fusion;
4790 u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
4791 u32 io_timeout_in_crash_mode = 0;
4792 struct scsi_cmnd *scmd_local = NULL;
4793 struct scsi_device *sdev;
4794 int ret_target_prop = DCMD_FAILED;
4795 bool is_target_prop = false;
4796 bool do_adp_reset = true;
4797 int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
4799 instance = (struct megasas_instance *)shost->hostdata;
4800 fusion = instance->ctrl_context;
4802 mutex_lock(&instance->reset_mutex);
4804 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4805 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4806 "returning FAILED for scsi%d.\n",
4807 instance->host->host_no);
4808 mutex_unlock(&instance->reset_mutex);
4811 status_reg = instance->instancet->read_fw_status_reg(instance);
4812 abs_state = status_reg & MFI_STATE_MASK;
4814 /* IO timeout detected, forcibly put FW in FAULT state */
4815 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4816 instance->crash_dump_app_support && reason) {
4817 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4818 "forcibly FAULT Firmware\n");
4819 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4820 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4821 writel(status_reg | MFI_STATE_FORCE_OCR,
4822 &instance->reg_set->doorbell);
4823 readl(&instance->reg_set->doorbell);
4824 mutex_unlock(&instance->reset_mutex);
4827 io_timeout_in_crash_mode++;
4828 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4829 "seconds for crash dump collection and OCR "
4830 "to be done\n", (io_timeout_in_crash_mode * 3));
4831 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4832 (io_timeout_in_crash_mode < 80));
4834 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4835 dev_info(&instance->pdev->dev, "OCR done for IO "
4839 dev_info(&instance->pdev->dev, "Controller is not "
4840 "operational after 240 seconds wait for IO "
4841 "timeout case in FW crash dump mode\n do "
4842 "OCR/kill adapter\n");
4843 retval = megasas_reset_fusion(shost, 0);
4848 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4849 del_timer_sync(&instance->sriov_heartbeat_timer);
4850 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4851 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4852 instance->instancet->disable_intr(instance);
4853 megasas_sync_irqs((unsigned long)instance);
4855 /* First try waiting for commands to complete */
4856 if (megasas_wait_for_outstanding_fusion(instance, reason,
4858 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4859 dev_warn(&instance->pdev->dev, "resetting fusion "
4860 "adapter scsi%d.\n", instance->host->host_no);
4864 if (megasas_dbg_lvl & OCR_DEBUG)
4865 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4867 /* Now return commands back to the OS */
4868 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4869 cmd_fusion = fusion->cmd_list[i];
4870 /*check for extra commands issued by driver*/
4871 if (instance->adapter_type >= VENTURA_SERIES) {
4872 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4873 megasas_return_cmd_fusion(instance, r1_cmd);
4875 scmd_local = cmd_fusion->scmd;
4876 if (cmd_fusion->scmd) {
4877 if (megasas_dbg_lvl & OCR_DEBUG) {
4878 sdev_printk(KERN_INFO,
4879 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4881 megasas_dump_fusion_io(cmd_fusion->scmd);
4884 if (cmd_fusion->io_request->Function ==
4885 MPI2_FUNCTION_SCSI_IO_REQUEST)
4888 scmd_local->result =
4889 megasas_check_mpio_paths(instance,
4891 if (instance->ldio_threshold &&
4892 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4893 atomic_dec(&instance->ldio_outstanding);
4894 megasas_return_cmd_fusion(instance, cmd_fusion);
4895 scsi_dma_unmap(scmd_local);
4896 scmd_local->scsi_done(scmd_local);
4900 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
4903 atomic_set(&instance->fw_outstanding, 0);
4905 status_reg = instance->instancet->read_fw_status_reg(instance);
4906 abs_state = status_reg & MFI_STATE_MASK;
4907 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4908 if (instance->disableOnlineCtrlReset ||
4909 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4910 /* Reset not supported, kill adapter */
4911 dev_warn(&instance->pdev->dev, "Reset not supported"
4912 ", killing adapter scsi%d.\n",
4913 instance->host->host_no);
4917 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4918 if (instance->requestorId && !reason) {
4919 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4920 do_adp_reset = false;
4921 max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
4924 /* Now try to reset the chip */
4925 for (i = 0; i < max_reset_tries; i++) {
4927 * Do adp reset and wait for
4928 * controller to transition to ready
4930 if (megasas_adp_reset_wait_for_ready(instance,
4931 do_adp_reset, 1) == FAILED)
4934 /* Wait for FW to become ready */
4935 if (megasas_transition_to_ready(instance, 1)) {
4936 dev_warn(&instance->pdev->dev,
4937 "Failed to transition controller to ready for "
4938 "scsi%d.\n", instance->host->host_no);
4941 megasas_reset_reply_desc(instance);
4942 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4944 if (megasas_ioc_init_fusion(instance)) {
4948 if (megasas_get_ctrl_info(instance)) {
4949 dev_info(&instance->pdev->dev,
4950 "Failed from %s %d\n",
4951 __func__, __LINE__);
4955 megasas_refire_mgmt_cmd(instance);
4957 /* Reset load balance info */
4958 if (fusion->load_balance_info)
4959 memset(fusion->load_balance_info, 0,
4960 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4961 MAX_LOGICAL_DRIVES_EXT));
4963 if (!megasas_get_map_info(instance))
4964 megasas_sync_map_info(instance);
4966 megasas_setup_jbod_map(instance);
4968 /* reset stream detection array */
4969 if (instance->adapter_type >= VENTURA_SERIES) {
4970 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4971 memset(fusion->stream_detect_by_ld[j],
4972 0, sizeof(struct LD_STREAM_DETECT));
4973 fusion->stream_detect_by_ld[j]->mru_bit_map
4978 clear_bit(MEGASAS_FUSION_IN_RESET,
4979 &instance->reset_flags);
4980 instance->instancet->enable_intr(instance);
4981 megasas_enable_irq_poll(instance);
4982 shost_for_each_device(sdev, shost) {
4983 if ((instance->tgt_prop) &&
4984 (instance->nvme_page_size))
4985 ret_target_prop = megasas_get_target_prop(instance, sdev);
4987 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4988 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4991 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4993 dev_info(&instance->pdev->dev,
4994 "Adapter is OPERATIONAL for scsi:%d\n",
4995 instance->host->host_no);
4997 /* Restart SR-IOV heartbeat */
4998 if (instance->requestorId) {
4999 if (!megasas_sriov_start_heartbeat(instance, 0))
5000 megasas_start_timer(instance);
5002 instance->skip_heartbeat_timer_del = 1;
5005 if (instance->crash_dump_drv_support &&
5006 instance->crash_dump_app_support)
5007 megasas_set_crash_dump_params(instance,
5008 MR_CRASH_BUF_TURN_ON);
5010 megasas_set_crash_dump_params(instance,
5011 MR_CRASH_BUF_TURN_OFF);
5013 if (instance->snapdump_wait_time) {
5014 megasas_get_snapdump_properties(instance);
5015 dev_info(&instance->pdev->dev,
5016 "Snap dump wait time\t: %d\n",
5017 instance->snapdump_wait_time);
5022 /* Adapter reset completed successfully */
5023 dev_warn(&instance->pdev->dev,
5024 "Reset successful for scsi%d.\n",
5025 instance->host->host_no);
5029 /* Reset failed, kill the adapter */
5030 dev_warn(&instance->pdev->dev, "Reset failed, killing "
5031 "adapter scsi%d.\n", instance->host->host_no);
5034 /* For VF: Restart HB timer if we didn't OCR */
5035 if (instance->requestorId) {
5036 megasas_start_timer(instance);
5038 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5039 instance->instancet->enable_intr(instance);
5040 megasas_enable_irq_poll(instance);
5041 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5045 megaraid_sas_kill_hba(instance);
5046 megasas_enable_irq_poll(instance);
5047 instance->skip_heartbeat_timer_del = 1;
5050 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5051 mutex_unlock(&instance->reset_mutex);
5055 /* Fusion Crash dump collection */
5056 void megasas_fusion_crash_dump(struct megasas_instance *instance)
5059 u8 partial_copy = 0;
5063 status_reg = instance->instancet->read_fw_status_reg(instance);
5066 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5067 * to host crash buffers
5069 if (instance->drv_buf_index == 0) {
5070 /* Buffer is already allocated for old Crash dump.
5071 * Do OCR and do not wait for crash dump collection
5073 if (instance->drv_buf_alloc) {
5074 dev_info(&instance->pdev->dev, "earlier crash dump is "
5075 "not yet copied by application, ignoring this "
5076 "crash dump and initiating OCR\n");
5077 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5079 &instance->reg_set->outbound_scratch_pad_0);
5080 readl(&instance->reg_set->outbound_scratch_pad_0);
5083 megasas_alloc_host_crash_buffer(instance);
5084 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5085 "allocated: %d\n", instance->drv_buf_alloc);
5088 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5089 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5090 if (!(status_reg & MFI_STATE_DMADONE)) {
5092 * Next crash dump buffer is not yet DMA'd by FW
5093 * Check after 10ms. Wait for 1 second for FW to
5094 * post the next buffer. If not bail out.
5097 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5098 status_reg = instance->instancet->read_fw_status_reg(
5104 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5105 dev_info(&instance->pdev->dev,
5106 "Driver is done copying the buffer: %d\n",
5107 instance->drv_buf_alloc);
5108 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5112 memcpy(instance->crash_buf[instance->drv_buf_index],
5113 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5114 instance->drv_buf_index++;
5115 status_reg &= ~MFI_STATE_DMADONE;
5118 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5119 readl(&instance->reg_set->outbound_scratch_pad_0);
5121 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5122 status_reg = instance->instancet->read_fw_status_reg(instance);
5125 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5126 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5127 "of copied buffers: %d\n", instance->drv_buf_index);
5128 instance->fw_crash_buffer_size = instance->drv_buf_index;
5129 instance->fw_crash_state = AVAILABLE;
5130 instance->drv_buf_index = 0;
5131 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5132 readl(&instance->reg_set->outbound_scratch_pad_0);
5134 megasas_reset_fusion(instance->host, 0);
5139 /* Fusion OCR work queue */
5140 void megasas_fusion_ocr_wq(struct work_struct *work)
5142 struct megasas_instance *instance =
5143 container_of(work, struct megasas_instance, work_init);
5145 megasas_reset_fusion(instance->host, 0);
5148 /* Allocate fusion context */
5150 megasas_alloc_fusion_context(struct megasas_instance *instance)
5152 struct fusion_context *fusion;
5154 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
5156 if (!instance->ctrl_context) {
5157 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5158 __func__, __LINE__);
5162 fusion = instance->ctrl_context;
5164 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5165 sizeof(LD_SPAN_INFO));
5166 fusion->log_to_span =
5167 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5168 fusion->log_to_span_pages);
5169 if (!fusion->log_to_span) {
5170 fusion->log_to_span =
5171 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5172 sizeof(LD_SPAN_INFO)));
5173 if (!fusion->log_to_span) {
5174 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5175 __func__, __LINE__);
5180 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5181 sizeof(struct LD_LOAD_BALANCE_INFO));
5182 fusion->load_balance_info =
5183 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5184 fusion->load_balance_info_pages);
5185 if (!fusion->load_balance_info) {
5186 fusion->load_balance_info =
5187 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5188 sizeof(struct LD_LOAD_BALANCE_INFO)));
5189 if (!fusion->load_balance_info)
5190 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5191 "continuing without Load Balance support\n");
5198 megasas_free_fusion_context(struct megasas_instance *instance)
5200 struct fusion_context *fusion = instance->ctrl_context;
5203 if (fusion->load_balance_info) {
5204 if (is_vmalloc_addr(fusion->load_balance_info))
5205 vfree(fusion->load_balance_info);
5207 free_pages((ulong)fusion->load_balance_info,
5208 fusion->load_balance_info_pages);
5211 if (fusion->log_to_span) {
5212 if (is_vmalloc_addr(fusion->log_to_span))
5213 vfree(fusion->log_to_span);
5215 free_pages((ulong)fusion->log_to_span,
5216 fusion->log_to_span_pages);
5223 struct megasas_instance_template megasas_instance_template_fusion = {
5224 .enable_intr = megasas_enable_intr_fusion,
5225 .disable_intr = megasas_disable_intr_fusion,
5226 .clear_intr = megasas_clear_intr_fusion,
5227 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5228 .adp_reset = megasas_adp_reset_fusion,
5229 .check_reset = megasas_check_reset_fusion,
5230 .service_isr = megasas_isr_fusion,
5231 .tasklet = megasas_complete_cmd_dpc_fusion,
5232 .init_adapter = megasas_init_adapter_fusion,
5233 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5234 .issue_dcmd = megasas_issue_dcmd_fusion,