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"
52 megasas_complete_cmd(struct megasas_instance *instance,
53 struct megasas_cmd *cmd, u8 alt_status);
55 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
59 megasas_clear_intr_fusion(struct megasas_instance *instance);
61 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
63 extern u32 megasas_dbg_lvl;
64 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
66 extern struct megasas_mgmt_info megasas_mgmt_info;
67 extern unsigned int resetwaittime;
68 extern unsigned int dual_qdepth_disable;
69 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
70 static void megasas_free_reply_fusion(struct megasas_instance *instance);
72 void megasas_configure_queue_sizes(struct megasas_instance *instance);
73 static void megasas_fusion_crash_dump(struct megasas_instance *instance);
76 * megasas_adp_reset_wait_for_ready - initiate chip reset and wait for
77 * controller to come to ready state
78 * @instance: adapter's soft state
79 * @do_adp_reset: If true, do a chip reset
80 * @ocr_context: If called from OCR context this will
83 * This function initates a chip reset followed by a wait for controller to
84 * transition to ready state.
85 * During this, driver will block all access to PCI config space from userspace
88 megasas_adp_reset_wait_for_ready(struct megasas_instance *instance,
95 * Block access to PCI config space from userspace
96 * when diag reset is initiated from driver
98 if (megasas_dbg_lvl & OCR_DEBUG)
99 dev_info(&instance->pdev->dev,
100 "Block access to PCI config space %s %d\n",
103 pci_cfg_access_lock(instance->pdev);
106 if (instance->instancet->adp_reset
107 (instance, instance->reg_set))
111 /* Wait for FW to become ready */
112 if (megasas_transition_to_ready(instance, ocr_context)) {
113 dev_warn(&instance->pdev->dev,
114 "Failed to transition controller to ready for scsi%d.\n",
115 instance->host->host_no);
121 if (megasas_dbg_lvl & OCR_DEBUG)
122 dev_info(&instance->pdev->dev,
123 "Unlock access to PCI config space %s %d\n",
126 pci_cfg_access_unlock(instance->pdev);
132 * megasas_check_same_4gb_region - check if allocation
133 * crosses same 4GB boundary or not
134 * @instance: adapter's soft instance
135 * @start_addr: start address of DMA allocation
136 * @size: size of allocation in bytes
137 * @return: true : allocation does not cross same
139 * false: allocation crosses same
142 static inline bool megasas_check_same_4gb_region
143 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
147 end_addr = start_addr + size;
149 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
150 dev_err(&instance->pdev->dev,
151 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
152 (unsigned long long)start_addr,
153 (unsigned long long)end_addr);
161 * megasas_enable_intr_fusion - Enables interrupts
162 * @instance: adapter's soft instance
165 megasas_enable_intr_fusion(struct megasas_instance *instance)
167 struct megasas_register_set __iomem *regs;
168 regs = instance->reg_set;
170 instance->mask_interrupts = 0;
171 /* For Thunderbolt/Invader also clear intr on enable */
172 writel(~0, ®s->outbound_intr_status);
173 readl(®s->outbound_intr_status);
175 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
177 /* Dummy readl to force pci flush */
178 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
179 __func__, readl(®s->outbound_intr_mask));
183 * megasas_disable_intr_fusion - Disables interrupt
184 * @instance: adapter's soft instance
187 megasas_disable_intr_fusion(struct megasas_instance *instance)
189 u32 mask = 0xFFFFFFFF;
190 struct megasas_register_set __iomem *regs;
191 regs = instance->reg_set;
192 instance->mask_interrupts = 1;
194 writel(mask, ®s->outbound_intr_mask);
195 /* Dummy readl to force pci flush */
196 dev_info(&instance->pdev->dev, "%s is called outbound_intr_mask:0x%08x\n",
197 __func__, readl(®s->outbound_intr_mask));
201 megasas_clear_intr_fusion(struct megasas_instance *instance)
204 struct megasas_register_set __iomem *regs;
205 regs = instance->reg_set;
207 * Check if it is our interrupt
209 status = megasas_readl(instance,
210 ®s->outbound_intr_status);
213 writel(status, ®s->outbound_intr_status);
214 readl(®s->outbound_intr_status);
217 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
224 * megasas_get_cmd_fusion - Get a command from the free pool
225 * @instance: Adapter soft state
226 * @blk_tag: Command tag
228 * Returns a blk_tag indexed mpt frame
230 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
231 *instance, u32 blk_tag)
233 struct fusion_context *fusion;
235 fusion = instance->ctrl_context;
236 return fusion->cmd_list[blk_tag];
240 * megasas_return_cmd_fusion - Return a cmd to free command pool
241 * @instance: Adapter soft state
242 * @cmd: Command packet to be returned to free command pool
244 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
245 struct megasas_cmd_fusion *cmd)
248 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
249 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
250 cmd->cmd_completed = false;
254 * megasas_write_64bit_req_desc - PCI writes 64bit request descriptor
255 * @instance: Adapter soft state
256 * @req_desc: 64bit Request descriptor
259 megasas_write_64bit_req_desc(struct megasas_instance *instance,
260 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
262 #if defined(writeq) && defined(CONFIG_64BIT)
263 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
264 le32_to_cpu(req_desc->u.low));
265 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
268 spin_lock_irqsave(&instance->hba_lock, flags);
269 writel(le32_to_cpu(req_desc->u.low),
270 &instance->reg_set->inbound_low_queue_port);
271 writel(le32_to_cpu(req_desc->u.high),
272 &instance->reg_set->inbound_high_queue_port);
273 spin_unlock_irqrestore(&instance->hba_lock, flags);
278 * megasas_fire_cmd_fusion - Sends command to the FW
279 * @instance: Adapter soft state
280 * @req_desc: 32bit or 64bit Request descriptor
282 * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
283 * Prior to AERO_SERIES support 64 bit Descriptor.
286 megasas_fire_cmd_fusion(struct megasas_instance *instance,
287 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
289 if (instance->atomic_desc_support)
290 writel(le32_to_cpu(req_desc->u.low),
291 &instance->reg_set->inbound_single_queue_port);
293 megasas_write_64bit_req_desc(instance, req_desc);
297 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
298 * @instance: Adapter soft state
299 * @fw_boot_context: Whether this function called during probe or after OCR
301 * This function is only for fusion controllers.
302 * Update host can queue, if firmware downgrade max supported firmware commands.
303 * Firmware upgrade case will be skiped because underlying firmware has
304 * more resource than exposed to the OS.
308 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
310 u16 cur_max_fw_cmds = 0;
311 u16 ldio_threshold = 0;
313 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
314 if (instance->adapter_type < VENTURA_SERIES)
316 megasas_readl(instance,
317 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
319 if (dual_qdepth_disable || !cur_max_fw_cmds)
320 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
323 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
325 dev_info(&instance->pdev->dev,
326 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
327 cur_max_fw_cmds, ldio_threshold);
329 if (fw_boot_context == OCR_CONTEXT) {
330 cur_max_fw_cmds = cur_max_fw_cmds - 1;
331 if (cur_max_fw_cmds < instance->max_fw_cmds) {
332 instance->cur_can_queue =
333 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
334 MEGASAS_FUSION_IOCTL_CMDS);
335 instance->host->can_queue = instance->cur_can_queue;
336 instance->ldio_threshold = ldio_threshold;
339 instance->max_fw_cmds = cur_max_fw_cmds;
340 instance->ldio_threshold = ldio_threshold;
343 instance->max_fw_cmds = min(instance->max_fw_cmds,
344 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
346 * Reduce the max supported cmds by 1. This is to ensure that the
347 * reply_q_sz (1 more than the max cmd that driver may send)
348 * does not exceed max cmds that the FW can support
350 instance->max_fw_cmds = instance->max_fw_cmds-1;
355 megasas_get_msix_index(struct megasas_instance *instance,
356 struct scsi_cmnd *scmd,
357 struct megasas_cmd_fusion *cmd,
362 /* nr_hw_queue = 1 for MegaRAID */
363 struct blk_mq_hw_ctx *hctx =
364 scmd->device->request_queue->queue_hw_ctx[0];
366 sdev_busy = atomic_read(&hctx->nr_active);
368 if (instance->perf_mode == MR_BALANCED_PERF_MODE &&
369 sdev_busy > (data_arms * MR_DEVICE_HIGH_IOPS_DEPTH))
370 cmd->request_desc->SCSIIO.MSIxIndex =
371 mega_mod64((atomic64_add_return(1, &instance->high_iops_outstanding) /
372 MR_HIGH_IOPS_BATCH_COUNT), instance->low_latency_index_start);
373 else if (instance->msix_load_balance)
374 cmd->request_desc->SCSIIO.MSIxIndex =
375 (mega_mod64(atomic64_add_return(1, &instance->total_io_count),
376 instance->msix_vectors));
378 cmd->request_desc->SCSIIO.MSIxIndex =
379 instance->reply_map[raw_smp_processor_id()];
383 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
384 * @instance: Adapter soft state
387 megasas_free_cmds_fusion(struct megasas_instance *instance)
390 struct fusion_context *fusion = instance->ctrl_context;
391 struct megasas_cmd_fusion *cmd;
394 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
395 fusion->sense_phys_addr);
398 if (fusion->cmd_list) {
399 for (i = 0; i < instance->max_mpt_cmds; i++) {
400 cmd = fusion->cmd_list[i];
403 dma_pool_free(fusion->sg_dma_pool,
405 cmd->sg_frame_phys_addr);
409 kfree(fusion->cmd_list);
412 if (fusion->sg_dma_pool) {
413 dma_pool_destroy(fusion->sg_dma_pool);
414 fusion->sg_dma_pool = NULL;
416 if (fusion->sense_dma_pool) {
417 dma_pool_destroy(fusion->sense_dma_pool);
418 fusion->sense_dma_pool = NULL;
422 /* Reply Frame, Desc*/
423 if (instance->is_rdpq)
424 megasas_free_rdpq_fusion(instance);
426 megasas_free_reply_fusion(instance);
428 /* Request Frame, Desc*/
429 if (fusion->req_frames_desc)
430 dma_free_coherent(&instance->pdev->dev,
431 fusion->request_alloc_sz, fusion->req_frames_desc,
432 fusion->req_frames_desc_phys);
433 if (fusion->io_request_frames)
434 dma_pool_free(fusion->io_request_frames_pool,
435 fusion->io_request_frames,
436 fusion->io_request_frames_phys);
437 if (fusion->io_request_frames_pool) {
438 dma_pool_destroy(fusion->io_request_frames_pool);
439 fusion->io_request_frames_pool = NULL;
444 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
445 * @instance: Adapter soft state
448 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
452 struct fusion_context *fusion;
453 struct megasas_cmd_fusion *cmd;
457 fusion = instance->ctrl_context;
458 max_cmd = instance->max_fw_cmds;
459 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
461 fusion->sg_dma_pool =
462 dma_pool_create("mr_sg", &instance->pdev->dev,
463 instance->max_chain_frame_sz,
464 MR_DEFAULT_NVME_PAGE_SIZE, 0);
465 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
466 fusion->sense_dma_pool =
467 dma_pool_create("mr_sense", &instance->pdev->dev,
470 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
471 dev_err(&instance->pdev->dev,
472 "Failed from %s %d\n", __func__, __LINE__);
476 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
477 GFP_KERNEL, &fusion->sense_phys_addr);
478 if (!fusion->sense) {
479 dev_err(&instance->pdev->dev,
480 "failed from %s %d\n", __func__, __LINE__);
484 /* sense buffer, request frame and reply desc pool requires to be in
485 * same 4 gb region. Below function will check this.
486 * In case of failure, new pci pool will be created with updated
488 * Older allocation and pool will be destroyed.
489 * Alignment will be used such a way that next allocation if success,
490 * will always meet same 4gb region requirement.
491 * Actual requirement is not alignment, but we need start and end of
492 * DMA address must have same upper 32 bit address.
495 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
497 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
498 fusion->sense_phys_addr);
499 fusion->sense = NULL;
500 dma_pool_destroy(fusion->sense_dma_pool);
502 fusion->sense_dma_pool =
503 dma_pool_create("mr_sense_align", &instance->pdev->dev,
504 sense_sz, roundup_pow_of_two(sense_sz),
506 if (!fusion->sense_dma_pool) {
507 dev_err(&instance->pdev->dev,
508 "Failed from %s %d\n", __func__, __LINE__);
511 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
513 &fusion->sense_phys_addr);
514 if (!fusion->sense) {
515 dev_err(&instance->pdev->dev,
516 "failed from %s %d\n", __func__, __LINE__);
522 * Allocate and attach a frame to each of the commands in cmd_list
524 for (i = 0; i < max_cmd; i++) {
525 cmd = fusion->cmd_list[i];
526 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
527 GFP_KERNEL, &cmd->sg_frame_phys_addr);
529 offset = SCSI_SENSE_BUFFERSIZE * i;
530 cmd->sense = (u8 *)fusion->sense + offset;
531 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
533 if (!cmd->sg_frame) {
534 dev_err(&instance->pdev->dev,
535 "Failed from %s %d\n", __func__, __LINE__);
540 /* create sense buffer for the raid 1/10 fp */
541 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
542 cmd = fusion->cmd_list[i];
543 offset = SCSI_SENSE_BUFFERSIZE * i;
544 cmd->sense = (u8 *)fusion->sense + offset;
545 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
553 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
555 u32 max_mpt_cmd, i, j;
556 struct fusion_context *fusion;
558 fusion = instance->ctrl_context;
560 max_mpt_cmd = instance->max_mpt_cmds;
563 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
564 * Allocate the dynamic array first and then allocate individual
568 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
570 if (!fusion->cmd_list) {
571 dev_err(&instance->pdev->dev,
572 "Failed from %s %d\n", __func__, __LINE__);
576 for (i = 0; i < max_mpt_cmd; i++) {
577 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
579 if (!fusion->cmd_list[i]) {
580 for (j = 0; j < i; j++)
581 kfree(fusion->cmd_list[j]);
582 kfree(fusion->cmd_list);
583 dev_err(&instance->pdev->dev,
584 "Failed from %s %d\n", __func__, __LINE__);
593 megasas_alloc_request_fusion(struct megasas_instance *instance)
595 struct fusion_context *fusion;
597 fusion = instance->ctrl_context;
600 fusion->io_request_frames_pool =
601 dma_pool_create("mr_ioreq", &instance->pdev->dev,
602 fusion->io_frames_alloc_sz, 16, 0);
604 if (!fusion->io_request_frames_pool) {
605 dev_err(&instance->pdev->dev,
606 "Failed from %s %d\n", __func__, __LINE__);
610 fusion->io_request_frames =
611 dma_pool_alloc(fusion->io_request_frames_pool,
612 GFP_KERNEL | __GFP_NOWARN,
613 &fusion->io_request_frames_phys);
614 if (!fusion->io_request_frames) {
615 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
616 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
617 dma_pool_destroy(fusion->io_request_frames_pool);
618 megasas_configure_queue_sizes(instance);
621 dev_err(&instance->pdev->dev,
622 "Failed from %s %d\n", __func__, __LINE__);
627 if (!megasas_check_same_4gb_region(instance,
628 fusion->io_request_frames_phys,
629 fusion->io_frames_alloc_sz)) {
630 dma_pool_free(fusion->io_request_frames_pool,
631 fusion->io_request_frames,
632 fusion->io_request_frames_phys);
633 fusion->io_request_frames = NULL;
634 dma_pool_destroy(fusion->io_request_frames_pool);
636 fusion->io_request_frames_pool =
637 dma_pool_create("mr_ioreq_align",
638 &instance->pdev->dev,
639 fusion->io_frames_alloc_sz,
640 roundup_pow_of_two(fusion->io_frames_alloc_sz),
643 if (!fusion->io_request_frames_pool) {
644 dev_err(&instance->pdev->dev,
645 "Failed from %s %d\n", __func__, __LINE__);
649 fusion->io_request_frames =
650 dma_pool_alloc(fusion->io_request_frames_pool,
651 GFP_KERNEL | __GFP_NOWARN,
652 &fusion->io_request_frames_phys);
654 if (!fusion->io_request_frames) {
655 dev_err(&instance->pdev->dev,
656 "Failed from %s %d\n", __func__, __LINE__);
661 fusion->req_frames_desc =
662 dma_alloc_coherent(&instance->pdev->dev,
663 fusion->request_alloc_sz,
664 &fusion->req_frames_desc_phys, GFP_KERNEL);
665 if (!fusion->req_frames_desc) {
666 dev_err(&instance->pdev->dev,
667 "Failed from %s %d\n", __func__, __LINE__);
675 megasas_alloc_reply_fusion(struct megasas_instance *instance)
678 struct fusion_context *fusion;
679 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
680 fusion = instance->ctrl_context;
682 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
683 fusion->reply_frames_desc_pool =
684 dma_pool_create("mr_reply", &instance->pdev->dev,
685 fusion->reply_alloc_sz * count, 16, 0);
687 if (!fusion->reply_frames_desc_pool) {
688 dev_err(&instance->pdev->dev,
689 "Failed from %s %d\n", __func__, __LINE__);
693 fusion->reply_frames_desc[0] =
694 dma_pool_alloc(fusion->reply_frames_desc_pool,
695 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
696 if (!fusion->reply_frames_desc[0]) {
697 dev_err(&instance->pdev->dev,
698 "Failed from %s %d\n", __func__, __LINE__);
702 if (!megasas_check_same_4gb_region(instance,
703 fusion->reply_frames_desc_phys[0],
704 (fusion->reply_alloc_sz * count))) {
705 dma_pool_free(fusion->reply_frames_desc_pool,
706 fusion->reply_frames_desc[0],
707 fusion->reply_frames_desc_phys[0]);
708 fusion->reply_frames_desc[0] = NULL;
709 dma_pool_destroy(fusion->reply_frames_desc_pool);
711 fusion->reply_frames_desc_pool =
712 dma_pool_create("mr_reply_align",
713 &instance->pdev->dev,
714 fusion->reply_alloc_sz * count,
715 roundup_pow_of_two(fusion->reply_alloc_sz * count),
718 if (!fusion->reply_frames_desc_pool) {
719 dev_err(&instance->pdev->dev,
720 "Failed from %s %d\n", __func__, __LINE__);
724 fusion->reply_frames_desc[0] =
725 dma_pool_alloc(fusion->reply_frames_desc_pool,
727 &fusion->reply_frames_desc_phys[0]);
729 if (!fusion->reply_frames_desc[0]) {
730 dev_err(&instance->pdev->dev,
731 "Failed from %s %d\n", __func__, __LINE__);
736 reply_desc = fusion->reply_frames_desc[0];
737 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
738 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
740 /* This is not a rdpq mode, but driver still populate
741 * reply_frame_desc array to use same msix index in ISR path.
743 for (i = 0; i < (count - 1); i++)
744 fusion->reply_frames_desc[i + 1] =
745 fusion->reply_frames_desc[i] +
746 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
752 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
754 int i, j, k, msix_count;
755 struct fusion_context *fusion;
756 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
757 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
758 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
759 u8 dma_alloc_count, abs_index;
760 u32 chunk_size, array_size, offset;
762 fusion = instance->ctrl_context;
763 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
764 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
765 MAX_MSIX_QUEUES_FUSION;
767 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev,
768 array_size, &fusion->rdpq_phys,
770 if (!fusion->rdpq_virt) {
771 dev_err(&instance->pdev->dev,
772 "Failed from %s %d\n", __func__, __LINE__);
776 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
778 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
779 &instance->pdev->dev,
781 fusion->reply_frames_desc_pool_align =
782 dma_pool_create("mr_rdpq_align",
783 &instance->pdev->dev,
785 roundup_pow_of_two(chunk_size),
788 if (!fusion->reply_frames_desc_pool ||
789 !fusion->reply_frames_desc_pool_align) {
790 dev_err(&instance->pdev->dev,
791 "Failed from %s %d\n", __func__, __LINE__);
796 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
797 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
798 * within 4GB boundary and also reply queues in a set must have same
799 * upper 32-bits in their memory address. so here driver is allocating the
800 * DMA'able memory for reply queues according. Driver uses limitation of
801 * VENTURA_SERIES to manage INVADER_SERIES as well.
803 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
805 for (i = 0; i < dma_alloc_count; i++) {
807 dma_pool_alloc(fusion->reply_frames_desc_pool,
808 GFP_KERNEL, &rdpq_chunk_phys[i]);
809 if (!rdpq_chunk_virt[i]) {
810 dev_err(&instance->pdev->dev,
811 "Failed from %s %d\n", __func__, __LINE__);
814 /* reply desc pool requires to be in same 4 gb region.
815 * Below function will check this.
816 * In case of failure, new pci pool will be created with updated
818 * For RDPQ buffers, driver always allocate two separate pci pool.
819 * Alignment will be used such a way that next allocation if
820 * success, will always meet same 4gb region requirement.
821 * rdpq_tracker keep track of each buffer's physical,
822 * virtual address and pci pool descriptor. It will help driver
823 * while freeing the resources.
826 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
828 dma_pool_free(fusion->reply_frames_desc_pool,
833 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
834 GFP_KERNEL, &rdpq_chunk_phys[i]);
835 if (!rdpq_chunk_virt[i]) {
836 dev_err(&instance->pdev->dev,
837 "Failed from %s %d\n",
841 fusion->rdpq_tracker[i].dma_pool_ptr =
842 fusion->reply_frames_desc_pool_align;
844 fusion->rdpq_tracker[i].dma_pool_ptr =
845 fusion->reply_frames_desc_pool;
848 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
849 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
852 for (k = 0; k < dma_alloc_count; k++) {
853 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
854 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
856 if (abs_index == msix_count)
858 offset = fusion->reply_alloc_sz * i;
859 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
860 cpu_to_le64(rdpq_chunk_phys[k] + offset);
861 fusion->reply_frames_desc_phys[abs_index] =
862 rdpq_chunk_phys[k] + offset;
863 fusion->reply_frames_desc[abs_index] =
864 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
866 reply_desc = fusion->reply_frames_desc[abs_index];
867 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
868 reply_desc->Words = ULLONG_MAX;
876 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
879 struct fusion_context *fusion;
881 fusion = instance->ctrl_context;
883 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
884 if (fusion->rdpq_tracker[i].pool_entry_virt)
885 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
886 fusion->rdpq_tracker[i].pool_entry_virt,
887 fusion->rdpq_tracker[i].pool_entry_phys);
891 dma_pool_destroy(fusion->reply_frames_desc_pool);
892 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
894 if (fusion->rdpq_virt)
895 dma_free_coherent(&instance->pdev->dev,
896 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
897 fusion->rdpq_virt, fusion->rdpq_phys);
901 megasas_free_reply_fusion(struct megasas_instance *instance) {
903 struct fusion_context *fusion;
905 fusion = instance->ctrl_context;
907 if (fusion->reply_frames_desc[0])
908 dma_pool_free(fusion->reply_frames_desc_pool,
909 fusion->reply_frames_desc[0],
910 fusion->reply_frames_desc_phys[0]);
912 dma_pool_destroy(fusion->reply_frames_desc_pool);
918 * megasas_alloc_cmds_fusion - Allocates the command packets
919 * @instance: Adapter soft state
922 * Each frame has a 32-bit field called context. This context is used to get
923 * back the megasas_cmd_fusion from the frame when a frame gets completed
924 * In this driver, the 32 bit values are the indices into an array cmd_list.
925 * This array is used only to look up the megasas_cmd_fusion given the context.
926 * The free commands themselves are maintained in a linked list called cmd_pool.
928 * cmds are formed in the io_request and sg_frame members of the
929 * megasas_cmd_fusion. The context field is used to get a request descriptor
930 * and is used as SMID of the cmd.
931 * SMID value range is from 1 to max_fw_cmds.
934 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
937 struct fusion_context *fusion;
938 struct megasas_cmd_fusion *cmd;
940 dma_addr_t io_req_base_phys;
944 fusion = instance->ctrl_context;
946 if (megasas_alloc_request_fusion(instance))
949 if (instance->is_rdpq) {
950 if (megasas_alloc_rdpq_fusion(instance))
953 if (megasas_alloc_reply_fusion(instance))
956 if (megasas_alloc_cmdlist_fusion(instance))
959 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
960 instance->max_fw_cmds);
962 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
963 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
964 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
967 * Add all the commands to command pool (fusion->cmd_pool)
970 /* SMID 0 is reserved. Set SMID/index from 1 */
971 for (i = 0; i < instance->max_mpt_cmds; i++) {
972 cmd = fusion->cmd_list[i];
973 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
974 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
978 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
979 (i - instance->max_scsi_cmds) :
980 (u32)ULONG_MAX; /* Set to Invalid */
981 cmd->instance = instance;
983 (struct MPI2_RAID_SCSI_IO_REQUEST *)
984 (io_req_base + offset);
985 memset(cmd->io_request, 0,
986 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
987 cmd->io_request_phys_addr = io_req_base_phys + offset;
988 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
991 if (megasas_create_sg_sense_fusion(instance))
997 megasas_free_cmds_fusion(instance);
1002 * wait_and_poll - Issues a polling command
1003 * @instance: Adapter soft state
1004 * @cmd: Command packet to be issued
1005 * @seconds: Maximum poll time
1007 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
1010 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
1014 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1017 u32 msecs = seconds * 1000;
1020 * Wait for cmd_status to change
1022 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
1026 status_reg = instance->instancet->read_fw_status_reg(instance)
1028 if (status_reg == MFI_STATE_FAULT)
1033 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
1034 return DCMD_TIMEOUT;
1035 else if (frame_hdr->cmd_status == MFI_STAT_OK)
1036 return DCMD_SUCCESS;
1042 * megasas_ioc_init_fusion - Initializes the FW
1043 * @instance: Adapter soft state
1045 * Issues the IOC Init cmd
1048 megasas_ioc_init_fusion(struct megasas_instance *instance)
1050 struct megasas_init_frame *init_frame;
1051 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
1052 dma_addr_t ioc_init_handle;
1053 struct megasas_cmd *cmd;
1054 u8 ret, cur_rdpq_mode;
1055 struct fusion_context *fusion;
1056 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
1058 struct megasas_header *frame_hdr;
1059 const char *sys_info;
1060 MFI_CAPABILITIES *drv_ops;
1063 bool cur_fw_64bit_dma_capable;
1064 bool cur_intr_coalescing;
1066 fusion = instance->ctrl_context;
1068 ioc_init_handle = fusion->ioc_init_request_phys;
1069 IOCInitMessage = fusion->ioc_init_request;
1071 cmd = fusion->ioc_init_cmd;
1073 scratch_pad_1 = megasas_readl
1074 (instance, &instance->reg_set->outbound_scratch_pad_1);
1076 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
1078 if (instance->adapter_type == INVADER_SERIES) {
1079 cur_fw_64bit_dma_capable =
1080 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
1082 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
1083 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
1084 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1085 megaraid_sas_kill_hba(instance);
1091 if (instance->is_rdpq && !cur_rdpq_mode) {
1092 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1093 " from RDPQ mode to non RDPQ mode\n");
1098 cur_intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
1101 if ((instance->low_latency_index_start ==
1102 MR_HIGH_IOPS_QUEUE_COUNT) && cur_intr_coalescing)
1103 instance->perf_mode = MR_BALANCED_PERF_MODE;
1105 dev_info(&instance->pdev->dev, "Performance mode :%s\n",
1106 MEGASAS_PERF_MODE_2STR(instance->perf_mode));
1108 instance->fw_sync_cache_support = (scratch_pad_1 &
1109 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1110 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1111 instance->fw_sync_cache_support ? "Yes" : "No");
1113 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1115 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1116 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
1117 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1118 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1119 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
1121 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
1122 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1123 cpu_to_le64(fusion->rdpq_phys) :
1124 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1125 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1126 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
1127 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
1128 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
1129 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
1130 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
1132 time = ktime_get_real();
1133 /* Convert to milliseconds as per FW requirement */
1134 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
1136 init_frame = (struct megasas_init_frame *)cmd->frame;
1137 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
1139 frame_hdr = &cmd->frame->hdr;
1140 frame_hdr->cmd_status = 0xFF;
1141 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1143 init_frame->cmd = MFI_CMD_INIT;
1144 init_frame->cmd_status = 0xFF;
1146 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1148 /* driver support Extended MSIX */
1149 if (instance->adapter_type >= INVADER_SERIES)
1150 drv_ops->mfi_capabilities.support_additional_msix = 1;
1151 /* driver supports HA / Remote LUN over Fast Path interface */
1152 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1154 drv_ops->mfi_capabilities.support_max_255lds = 1;
1155 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1156 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1158 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1159 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1161 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
1162 if (!dual_qdepth_disable)
1163 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
1165 drv_ops->mfi_capabilities.support_qd_throttling = 1;
1166 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
1167 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
1168 drv_ops->mfi_capabilities.support_fw_exposed_dev_list = 1;
1170 if (instance->consistent_mask_64bit)
1171 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1173 /* Convert capability to LE32 */
1174 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
1176 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1177 if (instance->system_info_buf && sys_info) {
1178 memcpy(instance->system_info_buf->systemId, sys_info,
1179 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1180 instance->system_info_buf->systemIdLength =
1181 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
1182 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1183 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
1186 init_frame->queue_info_new_phys_addr_hi =
1187 cpu_to_le32(upper_32_bits(ioc_init_handle));
1188 init_frame->queue_info_new_phys_addr_lo =
1189 cpu_to_le32(lower_32_bits(ioc_init_handle));
1190 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
1193 * Each bit in replyqueue_mask represents one group of MSI-x vectors
1194 * (each group has 8 vectors)
1196 switch (instance->perf_mode) {
1197 case MR_BALANCED_PERF_MODE:
1198 init_frame->replyqueue_mask =
1199 cpu_to_le16(~(~0 << instance->low_latency_index_start/8));
1201 case MR_IOPS_PERF_MODE:
1202 init_frame->replyqueue_mask =
1203 cpu_to_le16(~(~0 << instance->msix_vectors/8));
1208 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1209 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
1210 req_desc.MFAIo.RequestFlags =
1211 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
1212 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1215 * disable the intr before firing the init frame
1217 instance->instancet->disable_intr(instance);
1219 for (i = 0; i < (10 * 1000); i += 20) {
1220 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1)
1226 /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1227 megasas_write_64bit_req_desc(instance, &req_desc);
1229 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS);
1231 frame_hdr = &cmd->frame->hdr;
1232 if (frame_hdr->cmd_status != 0) {
1237 if (instance->adapter_type >= AERO_SERIES) {
1238 scratch_pad_1 = megasas_readl
1239 (instance, &instance->reg_set->outbound_scratch_pad_1);
1241 instance->atomic_desc_support =
1242 (scratch_pad_1 & MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET) ? 1 : 0;
1244 dev_info(&instance->pdev->dev, "FW supports atomic descriptor\t: %s\n",
1245 instance->atomic_desc_support ? "Yes" : "No");
1251 dev_err(&instance->pdev->dev,
1252 "Init cmd return status FAILED for SCSI host %d\n",
1253 instance->host->host_no);
1259 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1260 * @instance: Adapter soft state
1261 * @pend: set to 1, if it is pended jbod map.
1263 * Issue Jbod map to the firmware. If it is pended command,
1264 * issue command and return. If it is first instance of jbod map
1265 * issue and receive command.
1268 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1270 size_t pd_seq_map_sz;
1271 struct megasas_cmd *cmd;
1272 struct megasas_dcmd_frame *dcmd;
1273 struct fusion_context *fusion = instance->ctrl_context;
1274 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1275 dma_addr_t pd_seq_h;
1277 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1278 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1279 pd_seq_map_sz = struct_size(pd_sync, seq, MAX_PHYSICAL_DEVICES - 1);
1281 cmd = megasas_get_cmd(instance);
1283 dev_err(&instance->pdev->dev,
1284 "Could not get mfi cmd. Fail from %s %d\n",
1285 __func__, __LINE__);
1289 dcmd = &cmd->frame->dcmd;
1291 memset(pd_sync, 0, pd_seq_map_sz);
1292 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1295 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1296 dcmd->flags = MFI_FRAME_DIR_WRITE;
1297 instance->jbod_seq_cmd = cmd;
1299 dcmd->flags = MFI_FRAME_DIR_READ;
1302 dcmd->cmd = MFI_CMD_DCMD;
1303 dcmd->cmd_status = 0xFF;
1304 dcmd->sge_count = 1;
1307 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1308 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
1310 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
1313 instance->instancet->issue_dcmd(instance, cmd);
1317 /* Below code is only for non pended DCMD */
1318 if (!instance->mask_interrupts)
1319 ret = megasas_issue_blocked_cmd(instance, cmd,
1320 MFI_IO_TIMEOUT_SECS);
1322 ret = megasas_issue_polled(instance, cmd);
1324 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1325 dev_warn(&instance->pdev->dev,
1326 "driver supports max %d JBOD, but FW reports %d\n",
1327 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1331 if (ret == DCMD_TIMEOUT)
1332 dev_warn(&instance->pdev->dev,
1333 "%s DCMD timed out, continue without JBOD sequence map\n",
1336 if (ret == DCMD_SUCCESS)
1337 instance->pd_seq_map_id++;
1339 megasas_return_cmd(instance, cmd);
1344 * megasas_get_ld_map_info - Returns FW's ld_map structure
1345 * @instance: Adapter soft state
1346 * @pend: Pend the command or not
1347 * Issues an internal command (DCMD) to get the FW's controller PD
1348 * list structure. This information is mainly used to find out SYSTEM
1349 * supported by the FW.
1350 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1351 * dcmd.mbox.b[0] - number of LDs being sync'd
1352 * dcmd.mbox.b[1] - 0 - complete command immediately.
1353 * - 1 - pend till config change
1354 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1355 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1356 * uses extended struct MR_FW_RAID_MAP_EXT
1359 megasas_get_ld_map_info(struct megasas_instance *instance)
1362 struct megasas_cmd *cmd;
1363 struct megasas_dcmd_frame *dcmd;
1365 dma_addr_t ci_h = 0;
1367 struct fusion_context *fusion;
1369 cmd = megasas_get_cmd(instance);
1372 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1376 fusion = instance->ctrl_context;
1379 megasas_return_cmd(instance, cmd);
1383 dcmd = &cmd->frame->dcmd;
1385 size_map_info = fusion->current_map_sz;
1387 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1388 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1391 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1392 megasas_return_cmd(instance, cmd);
1396 memset(ci, 0, fusion->max_map_sz);
1397 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1398 dcmd->cmd = MFI_CMD_DCMD;
1399 dcmd->cmd_status = 0xFF;
1400 dcmd->sge_count = 1;
1401 dcmd->flags = MFI_FRAME_DIR_READ;
1404 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1405 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1407 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1409 if (!instance->mask_interrupts)
1410 ret = megasas_issue_blocked_cmd(instance, cmd,
1411 MFI_IO_TIMEOUT_SECS);
1413 ret = megasas_issue_polled(instance, cmd);
1415 if (ret == DCMD_TIMEOUT)
1416 dev_warn(&instance->pdev->dev,
1417 "%s DCMD timed out, RAID map is disabled\n",
1420 megasas_return_cmd(instance, cmd);
1426 megasas_get_map_info(struct megasas_instance *instance)
1428 struct fusion_context *fusion = instance->ctrl_context;
1430 fusion->fast_path_io = 0;
1431 if (!megasas_get_ld_map_info(instance)) {
1432 if (MR_ValidateMapInfo(instance, instance->map_id)) {
1433 fusion->fast_path_io = 1;
1441 * megasas_sync_map_info - Returns FW's ld_map structure
1442 * @instance: Adapter soft state
1444 * Issues an internal command (DCMD) to get the FW's controller PD
1445 * list structure. This information is mainly used to find out SYSTEM
1446 * supported by the FW.
1449 megasas_sync_map_info(struct megasas_instance *instance)
1452 struct megasas_cmd *cmd;
1453 struct megasas_dcmd_frame *dcmd;
1455 struct fusion_context *fusion;
1456 struct MR_LD_TARGET_SYNC *ci = NULL;
1457 struct MR_DRV_RAID_MAP_ALL *map;
1458 struct MR_LD_RAID *raid;
1459 struct MR_LD_TARGET_SYNC *ld_sync;
1460 dma_addr_t ci_h = 0;
1463 cmd = megasas_get_cmd(instance);
1466 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1470 fusion = instance->ctrl_context;
1473 megasas_return_cmd(instance, cmd);
1477 map = fusion->ld_drv_map[instance->map_id & 1];
1479 num_lds = le16_to_cpu(map->raidMap.ldCount);
1481 dcmd = &cmd->frame->dcmd;
1483 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1485 ci = (struct MR_LD_TARGET_SYNC *)
1486 fusion->ld_map[(instance->map_id - 1) & 1];
1487 memset(ci, 0, fusion->max_map_sz);
1489 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1491 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1493 for (i = 0; i < num_lds; i++, ld_sync++) {
1494 raid = MR_LdRaidGet(i, map);
1495 ld_sync->targetId = MR_GetLDTgtId(i, map);
1496 ld_sync->seqNum = raid->seqNum;
1499 size_map_info = fusion->current_map_sz;
1501 dcmd->cmd = MFI_CMD_DCMD;
1502 dcmd->cmd_status = 0xFF;
1503 dcmd->sge_count = 1;
1504 dcmd->flags = MFI_FRAME_DIR_WRITE;
1507 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1508 dcmd->mbox.b[0] = num_lds;
1509 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1510 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1512 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
1514 instance->map_update_cmd = cmd;
1516 instance->instancet->issue_dcmd(instance, cmd);
1522 * meagasas_display_intel_branding - Display branding string
1523 * @instance: per adapter object
1528 megasas_display_intel_branding(struct megasas_instance *instance)
1530 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1533 switch (instance->pdev->device) {
1534 case PCI_DEVICE_ID_LSI_INVADER:
1535 switch (instance->pdev->subsystem_device) {
1536 case MEGARAID_INTEL_RS3DC080_SSDID:
1537 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1538 instance->host->host_no,
1539 MEGARAID_INTEL_RS3DC080_BRANDING);
1541 case MEGARAID_INTEL_RS3DC040_SSDID:
1542 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1543 instance->host->host_no,
1544 MEGARAID_INTEL_RS3DC040_BRANDING);
1546 case MEGARAID_INTEL_RS3SC008_SSDID:
1547 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1548 instance->host->host_no,
1549 MEGARAID_INTEL_RS3SC008_BRANDING);
1551 case MEGARAID_INTEL_RS3MC044_SSDID:
1552 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1553 instance->host->host_no,
1554 MEGARAID_INTEL_RS3MC044_BRANDING);
1560 case PCI_DEVICE_ID_LSI_FURY:
1561 switch (instance->pdev->subsystem_device) {
1562 case MEGARAID_INTEL_RS3WC080_SSDID:
1563 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1564 instance->host->host_no,
1565 MEGARAID_INTEL_RS3WC080_BRANDING);
1567 case MEGARAID_INTEL_RS3WC040_SSDID:
1568 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1569 instance->host->host_no,
1570 MEGARAID_INTEL_RS3WC040_BRANDING);
1576 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1577 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1578 switch (instance->pdev->subsystem_device) {
1579 case MEGARAID_INTEL_RMS3BC160_SSDID:
1580 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1581 instance->host->host_no,
1582 MEGARAID_INTEL_RMS3BC160_BRANDING);
1594 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1595 * @instance: Adapter soft state
1597 * return: if success: return 0
1598 * failed: return -ENOMEM
1600 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1602 struct fusion_context *fusion;
1605 fusion = instance->ctrl_context;
1607 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1609 for (i = 0; i < 2; i++) {
1610 fusion->ld_map[i] = NULL;
1612 fusion->ld_drv_map[i] = (void *)
1613 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1614 fusion->drv_map_pages);
1616 if (!fusion->ld_drv_map[i]) {
1617 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1619 if (!fusion->ld_drv_map[i]) {
1620 dev_err(&instance->pdev->dev,
1621 "Could not allocate memory for local map"
1622 " size requested: %d\n",
1623 fusion->drv_map_sz);
1624 goto ld_drv_map_alloc_fail;
1629 for (i = 0; i < 2; i++) {
1630 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1632 &fusion->ld_map_phys[i],
1634 if (!fusion->ld_map[i]) {
1635 dev_err(&instance->pdev->dev,
1636 "Could not allocate memory for map info %s:%d\n",
1637 __func__, __LINE__);
1638 goto ld_map_alloc_fail;
1645 for (i = 0; i < 2; i++) {
1646 if (fusion->ld_map[i])
1647 dma_free_coherent(&instance->pdev->dev,
1650 fusion->ld_map_phys[i]);
1653 ld_drv_map_alloc_fail:
1654 for (i = 0; i < 2; i++) {
1655 if (fusion->ld_drv_map[i]) {
1656 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1657 vfree(fusion->ld_drv_map[i]);
1659 free_pages((ulong)fusion->ld_drv_map[i],
1660 fusion->drv_map_pages);
1668 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1670 * IO request frame queue, set can_queue.
1671 * @instance: Adapter soft state
1675 void megasas_configure_queue_sizes(struct megasas_instance *instance)
1677 struct fusion_context *fusion;
1680 fusion = instance->ctrl_context;
1681 max_cmd = instance->max_fw_cmds;
1683 if (instance->adapter_type >= VENTURA_SERIES)
1684 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1686 instance->max_mpt_cmds = instance->max_fw_cmds;
1688 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
1689 instance->cur_can_queue = instance->max_scsi_cmds;
1690 instance->host->can_queue = instance->cur_can_queue;
1692 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1694 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1695 instance->max_mpt_cmds;
1696 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1697 (fusion->reply_q_depth);
1698 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1699 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1700 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1703 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1705 struct fusion_context *fusion;
1706 struct megasas_cmd *cmd;
1708 fusion = instance->ctrl_context;
1710 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
1713 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1714 __func__, __LINE__);
1718 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1719 IOC_INIT_FRAME_SIZE,
1720 &cmd->frame_phys_addr, GFP_KERNEL);
1723 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1724 __func__, __LINE__);
1729 fusion->ioc_init_cmd = cmd;
1734 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1735 * @instance: Adapter soft state
1737 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1739 struct fusion_context *fusion;
1741 fusion = instance->ctrl_context;
1743 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1744 dma_free_coherent(&instance->pdev->dev,
1745 IOC_INIT_FRAME_SIZE,
1746 fusion->ioc_init_cmd->frame,
1747 fusion->ioc_init_cmd->frame_phys_addr);
1749 kfree(fusion->ioc_init_cmd);
1753 * megasas_init_adapter_fusion - Initializes the FW
1754 * @instance: Adapter soft state
1756 * This is the main function for initializing firmware.
1759 megasas_init_adapter_fusion(struct megasas_instance *instance)
1761 struct fusion_context *fusion;
1766 fusion = instance->ctrl_context;
1768 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1771 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1773 instance->max_mfi_cmds =
1774 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1776 megasas_configure_queue_sizes(instance);
1778 scratch_pad_1 = megasas_readl(instance,
1779 &instance->reg_set->outbound_scratch_pad_1);
1780 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1781 * Firmware support extended IO chain frame which is 4 times more than
1783 * Legacy Firmware - Frame size is (8 * 128) = 1K
1784 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1786 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1787 instance->max_chain_frame_sz =
1788 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1789 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1791 instance->max_chain_frame_sz =
1792 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1793 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1795 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1796 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1797 instance->max_chain_frame_sz,
1798 MEGASAS_CHAIN_FRAME_SZ_MIN);
1799 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1802 fusion->max_sge_in_main_msg =
1803 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1804 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1806 fusion->max_sge_in_chain =
1807 instance->max_chain_frame_sz
1808 / sizeof(union MPI2_SGE_IO_UNION);
1810 instance->max_num_sge =
1811 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1812 + fusion->max_sge_in_chain - 2);
1814 /* Used for pass thru MFI frame (DCMD) */
1815 fusion->chain_offset_mfi_pthru =
1816 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1818 fusion->chain_offset_io_request =
1819 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1820 sizeof(union MPI2_SGE_IO_UNION))/16;
1822 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1823 for (i = 0 ; i < count; i++)
1824 fusion->last_reply_idx[i] = 0;
1827 * For fusion adapters, 3 commands for IOCTL and 8 commands
1828 * for driver's internal DCMDs.
1830 instance->max_scsi_cmds = instance->max_fw_cmds -
1831 (MEGASAS_FUSION_INTERNAL_CMDS +
1832 MEGASAS_FUSION_IOCTL_CMDS);
1833 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1835 if (megasas_alloc_ioc_init_frame(instance))
1839 * Allocate memory for descriptors
1840 * Create a pool of commands
1842 if (megasas_alloc_cmds(instance))
1843 goto fail_alloc_mfi_cmds;
1844 if (megasas_alloc_cmds_fusion(instance))
1845 goto fail_alloc_cmds;
1847 if (megasas_ioc_init_fusion(instance)) {
1848 status_reg = instance->instancet->read_fw_status_reg(instance);
1849 if (((status_reg & MFI_STATE_MASK) == MFI_STATE_FAULT) &&
1850 (status_reg & MFI_RESET_ADAPTER)) {
1851 /* Do a chip reset and then retry IOC INIT once */
1852 if (megasas_adp_reset_wait_for_ready
1853 (instance, true, 0) == FAILED)
1856 if (megasas_ioc_init_fusion(instance))
1863 megasas_display_intel_branding(instance);
1864 if (megasas_get_ctrl_info(instance)) {
1865 dev_err(&instance->pdev->dev,
1866 "Could not get controller info. Fail from %s %d\n",
1867 __func__, __LINE__);
1871 instance->flag_ieee = 1;
1872 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1873 instance->threshold_reply_count = instance->max_fw_cmds / 4;
1874 fusion->fast_path_io = 0;
1876 if (megasas_allocate_raid_maps(instance))
1879 if (!megasas_get_map_info(instance))
1880 megasas_sync_map_info(instance);
1885 megasas_free_cmds_fusion(instance);
1887 megasas_free_cmds(instance);
1888 fail_alloc_mfi_cmds:
1889 megasas_free_ioc_init_cmd(instance);
1894 * megasas_fault_detect_work - Worker function of
1895 * FW fault handling workqueue.
1896 * @work: FW fault work struct
1899 megasas_fault_detect_work(struct work_struct *work)
1901 struct megasas_instance *instance =
1902 container_of(work, struct megasas_instance,
1903 fw_fault_work.work);
1904 u32 fw_state, dma_state, status;
1906 /* Check the fw state */
1907 fw_state = instance->instancet->read_fw_status_reg(instance) &
1910 if (fw_state == MFI_STATE_FAULT) {
1911 dma_state = instance->instancet->read_fw_status_reg(instance) &
1913 /* Start collecting crash, if DMA bit is done */
1914 if (instance->crash_dump_drv_support &&
1915 instance->crash_dump_app_support && dma_state) {
1916 megasas_fusion_crash_dump(instance);
1918 if (instance->unload == 0) {
1919 status = megasas_reset_fusion(instance->host, 0);
1920 if (status != SUCCESS) {
1921 dev_err(&instance->pdev->dev,
1922 "Failed from %s %d, do not re-arm timer\n",
1923 __func__, __LINE__);
1930 if (instance->fw_fault_work_q)
1931 queue_delayed_work(instance->fw_fault_work_q,
1932 &instance->fw_fault_work,
1933 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1937 megasas_fusion_start_watchdog(struct megasas_instance *instance)
1939 /* Check if the Fault WQ is already started */
1940 if (instance->fw_fault_work_q)
1943 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1945 snprintf(instance->fault_handler_work_q_name,
1946 sizeof(instance->fault_handler_work_q_name),
1947 "poll_megasas%d_status", instance->host->host_no);
1949 instance->fw_fault_work_q =
1950 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1951 if (!instance->fw_fault_work_q) {
1952 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1953 __func__, __LINE__);
1957 queue_delayed_work(instance->fw_fault_work_q,
1958 &instance->fw_fault_work,
1959 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1965 megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1967 struct workqueue_struct *wq;
1969 if (instance->fw_fault_work_q) {
1970 wq = instance->fw_fault_work_q;
1971 instance->fw_fault_work_q = NULL;
1972 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1973 flush_workqueue(wq);
1974 destroy_workqueue(wq);
1979 * map_cmd_status - Maps FW cmd status to OS cmd status
1980 * @fusion: fusion context
1981 * @scmd: Pointer to cmd
1982 * @status: status of cmd returned by FW
1983 * @ext_status: ext status of cmd returned by FW
1984 * @data_length: command data length
1985 * @sense: command sense data
1988 map_cmd_status(struct fusion_context *fusion,
1989 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1990 u32 data_length, u8 *sense)
1995 cmd_type = megasas_cmd_type(scmd);
1999 scmd->result = DID_OK << 16;
2002 case MFI_STAT_SCSI_IO_FAILED:
2003 case MFI_STAT_LD_INIT_IN_PROGRESS:
2004 scmd->result = (DID_ERROR << 16) | ext_status;
2007 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2009 scmd->result = (DID_OK << 16) | ext_status;
2010 if (ext_status == SAM_STAT_CHECK_CONDITION) {
2011 memset(scmd->sense_buffer, 0,
2012 SCSI_SENSE_BUFFERSIZE);
2013 memcpy(scmd->sense_buffer, sense,
2014 SCSI_SENSE_BUFFERSIZE);
2015 scmd->result |= DRIVER_SENSE << 24;
2019 * If the IO request is partially completed, then MR FW will
2020 * update "io_request->DataLength" field with actual number of
2021 * bytes transferred.Driver will set residual bytes count in
2022 * SCSI command structure.
2024 resid = (scsi_bufflen(scmd) - data_length);
2025 scsi_set_resid(scmd, resid);
2028 ((cmd_type == READ_WRITE_LDIO) ||
2029 (cmd_type == READ_WRITE_SYSPDIO)))
2030 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
2031 " requested/completed 0x%x/0x%x\n",
2032 status, scsi_bufflen(scmd), data_length);
2035 case MFI_STAT_LD_OFFLINE:
2036 case MFI_STAT_DEVICE_NOT_FOUND:
2037 scmd->result = DID_BAD_TARGET << 16;
2039 case MFI_STAT_CONFIG_SEQ_MISMATCH:
2040 scmd->result = DID_IMM_RETRY << 16;
2043 scmd->result = DID_ERROR << 16;
2049 * megasas_is_prp_possible -
2050 * Checks if native NVMe PRPs can be built for the IO
2052 * @instance: Adapter soft state
2053 * @scmd: SCSI command from the mid-layer
2054 * @sge_count: scatter gather element count.
2056 * Returns: true: PRPs can be built
2057 * false: IEEE SGLs needs to be built
2060 megasas_is_prp_possible(struct megasas_instance *instance,
2061 struct scsi_cmnd *scmd, int sge_count)
2063 u32 data_length = 0;
2064 struct scatterlist *sg_scmd;
2065 bool build_prp = false;
2066 u32 mr_nvme_pg_size;
2068 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2069 MR_DEFAULT_NVME_PAGE_SIZE);
2070 data_length = scsi_bufflen(scmd);
2071 sg_scmd = scsi_sglist(scmd);
2074 * NVMe uses one PRP for each page (or part of a page)
2075 * look at the data length - if 4 pages or less then IEEE is OK
2076 * if > 5 pages then we need to build a native SGL
2077 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2078 * if this first size in the page is >= the residual beyond 4 pages
2079 * then use IEEE, otherwise use native SGL
2082 if (data_length > (mr_nvme_pg_size * 5)) {
2084 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
2085 (data_length <= (mr_nvme_pg_size * 5))) {
2086 /* check if 1st SG entry size is < residual beyond 4 pages */
2087 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
2095 * megasas_make_prp_nvme -
2096 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2098 * @instance: Adapter soft state
2099 * @scmd: SCSI command from the mid-layer
2100 * @sgl_ptr: SGL to be filled in
2101 * @cmd: Fusion command frame
2102 * @sge_count: scatter gather element count.
2104 * Returns: true: PRPs are built
2105 * false: IEEE SGLs needs to be built
2108 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2109 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2110 struct megasas_cmd_fusion *cmd, int sge_count)
2112 int sge_len, offset, num_prp_in_chain = 0;
2113 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
2115 dma_addr_t ptr_sgl_phys;
2117 u32 page_mask, page_mask_result;
2118 struct scatterlist *sg_scmd;
2120 bool build_prp = false;
2121 int data_len = scsi_bufflen(scmd);
2122 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2123 MR_DEFAULT_NVME_PAGE_SIZE);
2125 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2131 * Nvme has a very convoluted prp format. One prp is required
2132 * for each page or partial page. Driver need to split up OS sg_list
2133 * entries if it is longer than one page or cross a page
2134 * boundary. Driver also have to insert a PRP list pointer entry as
2135 * the last entry in each physical page of the PRP list.
2137 * NOTE: The first PRP "entry" is actually placed in the first
2138 * SGL entry in the main message as IEEE 64 format. The 2nd
2139 * entry in the main message is the chain element, and the rest
2140 * of the PRP entries are built in the contiguous pcie buffer.
2142 page_mask = mr_nvme_pg_size - 1;
2143 ptr_sgl = (u64 *)cmd->sg_frame;
2144 ptr_sgl_phys = cmd->sg_frame_phys_addr;
2145 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2147 /* Build chain frame element which holds all prps except first*/
2148 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2149 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2151 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
2152 main_chain_element->NextChainOffset = 0;
2153 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2154 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2155 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2157 /* Build first prp, sge need not to be page aligned*/
2158 ptr_first_sgl = sgl_ptr;
2159 sg_scmd = scsi_sglist(scmd);
2160 sge_addr = sg_dma_address(sg_scmd);
2161 sge_len = sg_dma_len(sg_scmd);
2163 offset = (u32)(sge_addr & page_mask);
2164 first_prp_len = mr_nvme_pg_size - offset;
2166 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2167 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2169 data_len -= first_prp_len;
2171 if (sge_len > first_prp_len) {
2172 sge_addr += first_prp_len;
2173 sge_len -= first_prp_len;
2174 } else if (sge_len == first_prp_len) {
2175 sg_scmd = sg_next(sg_scmd);
2176 sge_addr = sg_dma_address(sg_scmd);
2177 sge_len = sg_dma_len(sg_scmd);
2181 offset = (u32)(sge_addr & page_mask);
2183 /* Put PRP pointer due to page boundary*/
2184 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2185 if (unlikely(!page_mask_result)) {
2186 scmd_printk(KERN_NOTICE,
2187 scmd, "page boundary ptr_sgl: 0x%p\n",
2190 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
2195 *ptr_sgl = cpu_to_le64(sge_addr);
2200 sge_addr += mr_nvme_pg_size;
2201 sge_len -= mr_nvme_pg_size;
2202 data_len -= mr_nvme_pg_size;
2210 sg_scmd = sg_next(sg_scmd);
2211 sge_addr = sg_dma_address(sg_scmd);
2212 sge_len = sg_dma_len(sg_scmd);
2215 main_chain_element->Length =
2216 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2222 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2223 * @instance: Adapter soft state
2224 * @scp: SCSI command from the mid-layer
2225 * @sgl_ptr: SGL to be filled in
2226 * @cmd: cmd we are working on
2227 * @sge_count: sge count
2231 megasas_make_sgl_fusion(struct megasas_instance *instance,
2232 struct scsi_cmnd *scp,
2233 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2234 struct megasas_cmd_fusion *cmd, int sge_count)
2236 int i, sg_processed;
2237 struct scatterlist *os_sgl;
2238 struct fusion_context *fusion;
2240 fusion = instance->ctrl_context;
2242 if (instance->adapter_type >= INVADER_SERIES) {
2243 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2244 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2245 sgl_ptr_end->Flags = 0;
2248 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
2249 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2250 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
2252 if (instance->adapter_type >= INVADER_SERIES)
2253 if (i == sge_count - 1)
2254 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
2256 sg_processed = i + 1;
2258 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2259 (sge_count > fusion->max_sge_in_main_msg)) {
2261 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
2262 if (instance->adapter_type >= INVADER_SERIES) {
2263 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2264 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2265 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
2266 cmd->io_request->ChainOffset =
2268 chain_offset_io_request;
2270 cmd->io_request->ChainOffset = 0;
2272 cmd->io_request->ChainOffset =
2273 fusion->chain_offset_io_request;
2276 /* Prepare chain element */
2277 sg_chain->NextChainOffset = 0;
2278 if (instance->adapter_type >= INVADER_SERIES)
2279 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2282 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2283 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
2284 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2285 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
2288 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
2289 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
2295 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2296 * @scp: SCSI command pointer
2297 * @instance: Soft instance of controller
2298 * @cmd: Fusion command pointer
2300 * This function will build sgls based on device type.
2301 * For nvme drives, there is different way of building sgls in nvme native
2302 * format- PRPs(Physical Region Page).
2304 * Returns the number of sg lists actually used, zero if the sg lists
2305 * is NULL, or -ENOMEM if the mapping failed
2308 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2309 struct megasas_cmd_fusion *cmd)
2312 bool build_prp = false;
2313 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2315 sge_count = scsi_dma_map(scp);
2317 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2320 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2321 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2322 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2323 (cmd->pd_interface == NVME_PD))
2324 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2328 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2335 * megasas_set_pd_lba - Sets PD LBA
2336 * @io_request: IO request
2337 * @cdb_len: cdb length
2338 * @io_info: IO information
2339 * @scp: SCSI command
2340 * @local_map_ptr: Raid map
2341 * @ref_tag: Primary reference tag
2343 * Used to set the PD LBA in CDB for FP IOs
2346 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2347 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
2348 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
2350 struct MR_LD_RAID *raid;
2352 u64 start_blk = io_info->pdBlock;
2353 u8 *cdb = io_request->CDB.CDB32;
2354 u32 num_blocks = io_info->numBlocks;
2355 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
2357 /* Check if T10 PI (DIF) is enabled for this LD */
2358 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2359 raid = MR_LdRaidGet(ld, local_map_ptr);
2360 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2361 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2362 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2363 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2365 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2366 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2368 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2369 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2372 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2373 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2374 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2375 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2376 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2377 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2378 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2379 cdb[19] = (u8)(start_blk & 0xff);
2381 /* Logical block reference tag */
2382 io_request->CDB.EEDP32.PrimaryReferenceTag =
2383 cpu_to_be32(ref_tag);
2384 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
2385 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
2387 /* Transfer length */
2388 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2389 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2390 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2391 cdb[31] = (u8)(num_blocks & 0xff);
2393 /* set SCSI IO EEDPFlags */
2394 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
2395 io_request->EEDPFlags = cpu_to_le16(
2396 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2397 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2398 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2399 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
2400 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
2401 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
2403 io_request->EEDPFlags = cpu_to_le16(
2404 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2405 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
2407 io_request->Control |= cpu_to_le32((0x4 << 26));
2408 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
2410 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2411 if (((cdb_len == 12) || (cdb_len == 16)) &&
2412 (start_blk <= 0xffffffff)) {
2413 if (cdb_len == 16) {
2414 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2419 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2425 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2432 /* Transfer length */
2433 cdb[8] = (u8)(num_blocks & 0xff);
2434 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2436 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
2438 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2439 /* Convert to 16 byte CDB for large LBA's */
2442 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2447 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2454 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2461 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2468 /* Transfer length */
2469 cdb[13] = (u8)(num_blocks & 0xff);
2470 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2471 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2472 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2474 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
2478 /* Normal case, just load LBA here */
2482 u8 val = cdb[1] & 0xE0;
2483 cdb[3] = (u8)(start_blk & 0xff);
2484 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2485 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2489 cdb[5] = (u8)(start_blk & 0xff);
2490 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2491 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2492 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2495 cdb[5] = (u8)(start_blk & 0xff);
2496 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2497 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2498 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2501 cdb[9] = (u8)(start_blk & 0xff);
2502 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2503 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2504 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2505 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2506 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2507 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2508 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2515 * megasas_stream_detect - stream detection on read and and write IOs
2516 * @instance: Adapter soft state
2517 * @cmd: Command to be prepared
2518 * @io_info: IO Request info
2522 /** stream detection on read and and write IOs */
2523 static void megasas_stream_detect(struct megasas_instance *instance,
2524 struct megasas_cmd_fusion *cmd,
2525 struct IO_REQUEST_INFO *io_info)
2527 struct fusion_context *fusion = instance->ctrl_context;
2528 u32 device_id = io_info->ldTgtId;
2529 struct LD_STREAM_DETECT *current_ld_sd
2530 = fusion->stream_detect_by_ld[device_id];
2531 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2532 u32 shifted_values, unshifted_values;
2533 u32 index_value_mask, shifted_values_mask;
2535 bool is_read_ahead = false;
2536 struct STREAM_DETECT *current_sd;
2537 /* find possible stream */
2538 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2539 stream_num = (*track_stream >>
2540 (i * BITS_PER_INDEX_STREAM)) &
2542 current_sd = ¤t_ld_sd->stream_track[stream_num];
2543 /* if we found a stream, update the raid
2544 * context and also update the mruBitMap
2546 /* boundary condition */
2547 if ((current_sd->next_seq_lba) &&
2548 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2549 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2550 (current_sd->is_read == io_info->isRead)) {
2552 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2553 ((!io_info->isRead) || (!is_read_ahead)))
2555 * Once the API availible we need to change this.
2556 * At this point we are not allowing any gap
2560 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2561 current_sd->next_seq_lba =
2562 io_info->ldStartBlock + io_info->numBlocks;
2564 * update the mruBitMap LRU
2566 shifted_values_mask =
2567 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2568 shifted_values = ((*track_stream & shifted_values_mask)
2569 << BITS_PER_INDEX_STREAM);
2571 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2573 *track_stream & ~(shifted_values_mask |
2576 unshifted_values | shifted_values | stream_num;
2581 * if we did not find any stream, create a new one
2582 * from the least recently used
2584 stream_num = (*track_stream >>
2585 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2587 current_sd = ¤t_ld_sd->stream_track[stream_num];
2588 current_sd->is_read = io_info->isRead;
2589 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2590 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2595 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2596 * affinity (cpu of the controller) and raid_flags in the raid context
2599 * @fusion: Fusion context
2600 * @praid_context: IO RAID context
2601 * @raid: LD raid map
2602 * @fp_possible: Is fast path possible?
2603 * @is_read: Is read IO?
2604 * @scsi_buff_len: SCSI command buffer length
2608 megasas_set_raidflag_cpu_affinity(struct fusion_context *fusion,
2609 union RAID_CONTEXT_UNION *praid_context,
2610 struct MR_LD_RAID *raid, bool fp_possible,
2611 u8 is_read, u32 scsi_buff_len)
2613 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2614 struct RAID_CONTEXT_G35 *rctx_g35;
2616 rctx_g35 = &praid_context->raid_context_g35;
2619 if ((raid->cpuAffinity.pdRead.cpu0) &&
2620 (raid->cpuAffinity.pdRead.cpu1))
2621 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2622 else if (raid->cpuAffinity.pdRead.cpu1)
2623 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2625 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2626 (raid->cpuAffinity.pdWrite.cpu1))
2627 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2628 else if (raid->cpuAffinity.pdWrite.cpu1)
2629 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2630 /* Fast path cache by pass capable R0/R1 VD */
2631 if ((raid->level <= 1) &&
2632 (raid->capability.fp_cache_bypass_capable)) {
2633 rctx_g35->routing_flags |=
2634 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2635 rctx_g35->raid_flags =
2636 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2637 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2642 if ((raid->cpuAffinity.ldRead.cpu0) &&
2643 (raid->cpuAffinity.ldRead.cpu1))
2644 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2645 else if (raid->cpuAffinity.ldRead.cpu1)
2646 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2648 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2649 (raid->cpuAffinity.ldWrite.cpu1))
2650 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2651 else if (raid->cpuAffinity.ldWrite.cpu1)
2652 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2654 if (is_stream_detected(rctx_g35) &&
2655 ((raid->level == 5) || (raid->level == 6)) &&
2656 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2657 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2658 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2662 rctx_g35->routing_flags |=
2663 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2665 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2666 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2667 * IO Subtype is not bitmap.
2669 if ((fusion->pcie_bw_limitation) && (raid->level == 1) && (!is_read) &&
2670 (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)) {
2671 praid_context->raid_context_g35.raid_flags =
2672 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2673 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2678 * megasas_build_ldio_fusion - Prepares IOs to devices
2679 * @instance: Adapter soft state
2680 * @scp: SCSI command
2681 * @cmd: Command to be prepared
2683 * Prepares the io_request and chain elements (sg_frame) for IO
2684 * The IO can be for PD (Fast Path) or LD
2687 megasas_build_ldio_fusion(struct megasas_instance *instance,
2688 struct scsi_cmnd *scp,
2689 struct megasas_cmd_fusion *cmd)
2693 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2695 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2696 struct IO_REQUEST_INFO io_info;
2697 struct fusion_context *fusion;
2698 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2700 unsigned long spinlock_flags;
2701 struct MR_LD_RAID *raid = NULL;
2702 struct MR_PRIV_DEVICE *mrdev_priv;
2703 struct RAID_CONTEXT *rctx;
2704 struct RAID_CONTEXT_G35 *rctx_g35;
2706 device_id = MEGASAS_DEV_INDEX(scp);
2708 fusion = instance->ctrl_context;
2710 io_request = cmd->io_request;
2711 rctx = &io_request->RaidContext.raid_context;
2712 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2714 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2716 rctx->ex_status = 0;
2720 fp_possible = false;
2723 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2725 if (scp->cmd_len == 6) {
2726 datalength = (u32) scp->cmnd[4];
2727 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2728 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2730 start_lba_lo &= 0x1FFFFF;
2734 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2736 else if (scp->cmd_len == 10) {
2737 datalength = (u32) scp->cmnd[8] |
2738 ((u32) scp->cmnd[7] << 8);
2739 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2740 ((u32) scp->cmnd[3] << 16) |
2741 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2745 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2747 else if (scp->cmd_len == 12) {
2748 datalength = ((u32) scp->cmnd[6] << 24) |
2749 ((u32) scp->cmnd[7] << 16) |
2750 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2751 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2752 ((u32) scp->cmnd[3] << 16) |
2753 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2757 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2759 else if (scp->cmd_len == 16) {
2760 datalength = ((u32) scp->cmnd[10] << 24) |
2761 ((u32) scp->cmnd[11] << 16) |
2762 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2763 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2764 ((u32) scp->cmnd[7] << 16) |
2765 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2767 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2768 ((u32) scp->cmnd[3] << 16) |
2769 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2772 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2773 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2774 io_info.numBlocks = datalength;
2775 io_info.ldTgtId = device_id;
2776 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2777 scsi_buff_len = scsi_bufflen(scp);
2778 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2779 io_info.data_arms = 1;
2781 if (scp->sc_data_direction == DMA_FROM_DEVICE)
2784 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2785 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2787 if (ld < instance->fw_supported_vd_count)
2788 raid = MR_LdRaidGet(ld, local_map_ptr);
2790 if (!raid || (!fusion->fast_path_io)) {
2791 rctx->reg_lock_flags = 0;
2792 fp_possible = false;
2794 if (MR_BuildRaidContext(instance, &io_info, rctx,
2795 local_map_ptr, &raidLUN))
2796 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2799 megasas_get_msix_index(instance, scp, cmd, io_info.data_arms);
2801 if (instance->adapter_type >= VENTURA_SERIES) {
2802 /* FP for Optimal raid level 1.
2803 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2804 * are built by the driver as LD I/Os.
2805 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2806 * (there is never a reason to process these as buffered writes)
2807 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2808 * with the SLD bit asserted.
2810 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2811 mrdev_priv = scp->device->hostdata;
2813 if (atomic_inc_return(&instance->fw_outstanding) >
2814 (instance->host->can_queue)) {
2815 fp_possible = false;
2816 atomic_dec(&instance->fw_outstanding);
2817 } else if (fusion->pcie_bw_limitation &&
2818 ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2819 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0))) {
2820 fp_possible = false;
2821 atomic_dec(&instance->fw_outstanding);
2822 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2823 atomic_set(&mrdev_priv->r1_ldio_hint,
2824 instance->r1_ldio_hint_default);
2829 (io_info.isRead && io_info.ra_capable)) {
2830 spin_lock_irqsave(&instance->stream_lock,
2832 megasas_stream_detect(instance, cmd, &io_info);
2833 spin_unlock_irqrestore(&instance->stream_lock,
2835 /* In ventura if stream detected for a read and it is
2836 * read ahead capable make this IO as LDIO
2838 if (is_stream_detected(rctx_g35))
2839 fp_possible = false;
2842 /* If raid is NULL, set CPU affinity to default CPU0 */
2844 megasas_set_raidflag_cpu_affinity(fusion, &io_request->RaidContext,
2845 raid, fp_possible, io_info.isRead,
2848 rctx_g35->routing_flags |=
2849 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2853 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2854 local_map_ptr, start_lba_lo);
2855 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2856 cmd->request_desc->SCSIIO.RequestFlags =
2857 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2858 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2859 if (instance->adapter_type == INVADER_SERIES) {
2860 rctx->type = MPI2_TYPE_CUDA;
2862 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2863 rctx->reg_lock_flags |=
2864 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2865 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2866 } else if (instance->adapter_type >= VENTURA_SERIES) {
2867 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2868 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2869 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2870 io_request->IoFlags |=
2871 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2873 if (fusion->load_balance_info &&
2874 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2877 get_updated_dev_handle(instance,
2878 &fusion->load_balance_info[device_id],
2879 &io_info, local_map_ptr);
2880 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2881 cmd->pd_r1_lb = io_info.pd_after_lb;
2882 if (instance->adapter_type >= VENTURA_SERIES)
2883 rctx_g35->span_arm = io_info.span_arm;
2885 rctx->span_arm = io_info.span_arm;
2888 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2890 if (instance->adapter_type >= VENTURA_SERIES)
2891 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2893 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2895 if ((raidLUN[0] == 1) &&
2896 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2897 instance->dev_handle = !(instance->dev_handle);
2899 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2902 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2903 io_request->DevHandle = io_info.devHandle;
2904 cmd->pd_interface = io_info.pd_interface;
2905 /* populate the LUN field */
2906 memcpy(io_request->LUN, raidLUN, 8);
2908 rctx->timeout_value =
2909 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2910 cmd->request_desc->SCSIIO.RequestFlags =
2911 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2912 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2913 if (instance->adapter_type == INVADER_SERIES) {
2914 if (io_info.do_fp_rlbypass ||
2915 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
2916 cmd->request_desc->SCSIIO.RequestFlags =
2917 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2918 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2919 rctx->type = MPI2_TYPE_CUDA;
2920 rctx->reg_lock_flags |=
2921 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2922 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2924 } else if (instance->adapter_type >= VENTURA_SERIES) {
2925 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2926 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2927 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2929 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2930 io_request->DevHandle = cpu_to_le16(device_id);
2936 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2937 * @instance: Adapter soft state
2938 * @scmd: SCSI command
2939 * @cmd: Command to be prepared
2941 * Prepares the io_request frame for non-rw io cmds for vd.
2943 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2944 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2947 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2949 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2950 struct fusion_context *fusion = instance->ctrl_context;
2954 struct MR_LD_RAID *raid;
2955 struct RAID_CONTEXT *pRAID_Context;
2958 io_request = cmd->io_request;
2959 device_id = MEGASAS_DEV_INDEX(scmd);
2960 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2961 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2962 /* get RAID_Context pointer */
2963 pRAID_Context = &io_request->RaidContext.raid_context;
2964 /* Check with FW team */
2965 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2966 pRAID_Context->reg_lock_row_lba = 0;
2967 pRAID_Context->reg_lock_length = 0;
2969 if (fusion->fast_path_io && (
2970 device_id < instance->fw_supported_vd_count)) {
2972 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2973 if (ld >= instance->fw_supported_vd_count - 1)
2976 raid = MR_LdRaidGet(ld, local_map_ptr);
2977 if (!(raid->capability.fpNonRWCapable))
2984 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2985 io_request->DevHandle = cpu_to_le16(device_id);
2986 io_request->LUN[1] = scmd->device->lun;
2987 pRAID_Context->timeout_value =
2988 cpu_to_le16 (scmd->request->timeout / HZ);
2989 cmd->request_desc->SCSIIO.RequestFlags =
2990 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2991 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2994 /* set RAID context values */
2995 pRAID_Context->config_seq_num = raid->seqNum;
2996 if (instance->adapter_type < VENTURA_SERIES)
2997 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2998 pRAID_Context->timeout_value =
2999 cpu_to_le16(raid->fpIoTimeoutForLd);
3001 /* get the DevHandle for the PD (since this is
3002 fpNonRWCapable, this is a single disk RAID0) */
3004 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
3005 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
3006 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
3008 /* build request descriptor */
3009 cmd->request_desc->SCSIIO.RequestFlags =
3010 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3011 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3012 cmd->request_desc->SCSIIO.DevHandle = devHandle;
3014 /* populate the LUN field */
3015 memcpy(io_request->LUN, raid->LUN, 8);
3017 /* build the raidScsiIO structure */
3018 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3019 io_request->DevHandle = devHandle;
3024 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3025 * @instance: Adapter soft state
3026 * @scmd: SCSI command
3027 * @cmd: Command to be prepared
3028 * @fp_possible: parameter to detect fast path or firmware path io.
3030 * Prepares the io_request frame for rw/non-rw io cmds for syspds
3033 megasas_build_syspd_fusion(struct megasas_instance *instance,
3034 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
3038 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
3040 u16 os_timeout_value;
3042 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
3043 struct RAID_CONTEXT *pRAID_Context;
3044 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3045 struct MR_PRIV_DEVICE *mr_device_priv_data;
3046 struct fusion_context *fusion = instance->ctrl_context;
3047 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
3049 device_id = MEGASAS_DEV_INDEX(scmd);
3050 pd_index = MEGASAS_PD_INDEX(scmd);
3051 os_timeout_value = scmd->request->timeout / HZ;
3052 mr_device_priv_data = scmd->device->hostdata;
3053 cmd->pd_interface = mr_device_priv_data->interface_type;
3055 io_request = cmd->io_request;
3056 /* get RAID_Context pointer */
3057 pRAID_Context = &io_request->RaidContext.raid_context;
3058 pRAID_Context->reg_lock_flags = 0;
3059 pRAID_Context->reg_lock_row_lba = 0;
3060 pRAID_Context->reg_lock_length = 0;
3061 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
3062 io_request->LUN[1] = scmd->device->lun;
3063 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
3064 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
3066 /* If FW supports PD sequence number */
3067 if (instance->support_seqnum_jbod_fp) {
3068 if (instance->use_seqnum_jbod_fp &&
3069 instance->pd_list[pd_index].driveType == TYPE_DISK) {
3071 /* More than 256 PD/JBOD support for Ventura */
3072 if (instance->support_morethan256jbod)
3073 pRAID_Context->virtual_disk_tgt_id =
3074 pd_sync->seq[pd_index].pd_target_id;
3076 pRAID_Context->virtual_disk_tgt_id =
3077 cpu_to_le16(device_id +
3078 (MAX_PHYSICAL_DEVICES - 1));
3079 pRAID_Context->config_seq_num =
3080 pd_sync->seq[pd_index].seqNum;
3081 io_request->DevHandle =
3082 pd_sync->seq[pd_index].devHandle;
3083 if (instance->adapter_type >= VENTURA_SERIES) {
3084 io_request->RaidContext.raid_context_g35.routing_flags |=
3085 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3086 io_request->RaidContext.raid_context_g35.nseg_type |=
3087 (1 << RAID_CONTEXT_NSEG_SHIFT);
3088 io_request->RaidContext.raid_context_g35.nseg_type |=
3089 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3091 pRAID_Context->type = MPI2_TYPE_CUDA;
3092 pRAID_Context->nseg = 0x1;
3093 pRAID_Context->reg_lock_flags |=
3094 (MR_RL_FLAGS_SEQ_NUM_ENABLE |
3095 MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3098 pRAID_Context->virtual_disk_tgt_id =
3099 cpu_to_le16(device_id +
3100 (MAX_PHYSICAL_DEVICES - 1));
3101 pRAID_Context->config_seq_num = 0;
3102 io_request->DevHandle = cpu_to_le16(0xFFFF);
3105 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3106 pRAID_Context->config_seq_num = 0;
3108 if (fusion->fast_path_io) {
3110 fusion->ld_drv_map[(instance->map_id & 1)];
3111 io_request->DevHandle =
3112 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3114 io_request->DevHandle = cpu_to_le16(0xFFFF);
3118 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
3120 megasas_get_msix_index(instance, scmd, cmd, 1);
3123 /* system pd firmware path */
3124 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
3125 cmd->request_desc->SCSIIO.RequestFlags =
3126 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3127 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3128 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3129 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3131 if (os_timeout_value)
3134 /* system pd Fast Path */
3135 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
3136 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3138 pRAID_Context->timeout_value =
3139 cpu_to_le16((os_timeout_value > timeout_limit) ?
3140 timeout_limit : os_timeout_value);
3141 if (instance->adapter_type >= INVADER_SERIES)
3142 io_request->IoFlags |=
3143 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
3145 cmd->request_desc->SCSIIO.RequestFlags =
3146 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
3147 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3152 * megasas_build_io_fusion - Prepares IOs to devices
3153 * @instance: Adapter soft state
3154 * @scp: SCSI command
3155 * @cmd: Command to be prepared
3157 * Invokes helper functions to prepare request frames
3158 * and sets flags appropriate for IO/Non-IO cmd
3161 megasas_build_io_fusion(struct megasas_instance *instance,
3162 struct scsi_cmnd *scp,
3163 struct megasas_cmd_fusion *cmd)
3167 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
3168 struct MR_PRIV_DEVICE *mr_device_priv_data;
3169 mr_device_priv_data = scp->device->hostdata;
3171 /* Zero out some fields so they don't get reused */
3172 memset(io_request->LUN, 0x0, 8);
3173 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3174 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3175 io_request->EEDPFlags = 0;
3176 io_request->Control = 0;
3177 io_request->EEDPBlockSize = 0;
3178 io_request->ChainOffset = 0;
3179 io_request->RaidContext.raid_context.raid_flags = 0;
3180 io_request->RaidContext.raid_context.type = 0;
3181 io_request->RaidContext.raid_context.nseg = 0;
3183 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3185 * Just the CDB length,rest of the Flags are zero
3186 * This will be modified for FP in build_ldio_fusion
3188 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
3190 switch (cmd_type = megasas_cmd_type(scp)) {
3191 case READ_WRITE_LDIO:
3192 megasas_build_ldio_fusion(instance, scp, cmd);
3194 case NON_READ_WRITE_LDIO:
3195 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3197 case READ_WRITE_SYSPDIO:
3198 megasas_build_syspd_fusion(instance, scp, cmd, true);
3200 case NON_READ_WRITE_SYSPDIO:
3201 if (instance->secure_jbod_support ||
3202 mr_device_priv_data->is_tm_capable)
3203 megasas_build_syspd_fusion(instance, scp, cmd, false);
3205 megasas_build_syspd_fusion(instance, scp, cmd, true);
3215 sge_count = megasas_make_sgl(instance, scp, cmd);
3217 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3218 dev_err(&instance->pdev->dev,
3219 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3220 __func__, __LINE__, sge_count, instance->max_num_sge);
3224 if (instance->adapter_type >= VENTURA_SERIES) {
3225 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3226 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3227 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3229 /* numSGE store lower 8 bit of sge_count.
3230 * numSGEExt store higher 8 bit of sge_count
3232 io_request->RaidContext.raid_context.num_sge = sge_count;
3233 io_request->RaidContext.raid_context.num_sge_ext =
3234 (u8)(sge_count >> 8);
3237 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
3239 if (scp->sc_data_direction == DMA_TO_DEVICE)
3240 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
3241 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
3242 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
3244 io_request->SGLOffset0 =
3245 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3247 io_request->SenseBufferLowAddress =
3248 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
3249 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3252 scp->SCp.ptr = (char *)cmd;
3257 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3258 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3261 struct fusion_context *fusion;
3263 fusion = instance->ctrl_context;
3264 p = fusion->req_frames_desc +
3265 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
3267 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3271 /* megasas_prepate_secondRaid1_IO
3272 * It prepares the raid 1 second IO
3274 static void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3275 struct megasas_cmd_fusion *cmd,
3276 struct megasas_cmd_fusion *r1_cmd)
3278 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3279 struct fusion_context *fusion;
3280 fusion = instance->ctrl_context;
3281 req_desc = cmd->request_desc;
3282 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3283 memcpy(r1_cmd->io_request, cmd->io_request,
3284 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3285 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3286 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3287 /*sense buffer is different for r1 command*/
3288 r1_cmd->io_request->SenseBufferLowAddress =
3289 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
3290 r1_cmd->scmd = cmd->scmd;
3291 req_desc2 = megasas_get_request_descriptor(instance,
3292 (r1_cmd->index - 1));
3293 req_desc2->Words = 0;
3294 r1_cmd->request_desc = req_desc2;
3295 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3296 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3297 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3298 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3299 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3300 cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3301 cpu_to_le16(r1_cmd->index);
3302 r1_cmd->io_request->RaidContext.raid_context_g35.flow_specific.peer_smid =
3303 cpu_to_le16(cmd->index);
3304 /*MSIxIndex of both commands request descriptors should be same*/
3305 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3306 cmd->request_desc->SCSIIO.MSIxIndex;
3307 /*span arm is different for r1 cmd*/
3308 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
3309 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
3313 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3314 * issuing non IOCTL cmd
3315 * @instance: Adapter soft state
3316 * @scmd: pointer to scsi cmd from OS
3319 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3320 struct scsi_cmnd *scmd)
3322 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
3323 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3326 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3327 instance->ldio_threshold &&
3328 (atomic_inc_return(&instance->ldio_outstanding) >
3329 instance->ldio_threshold)) {
3330 atomic_dec(&instance->ldio_outstanding);
3331 return SCSI_MLQUEUE_DEVICE_BUSY;
3334 if (atomic_inc_return(&instance->fw_outstanding) >
3335 instance->host->can_queue) {
3336 atomic_dec(&instance->fw_outstanding);
3337 return SCSI_MLQUEUE_HOST_BUSY;
3340 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
3343 atomic_dec(&instance->fw_outstanding);
3344 return SCSI_MLQUEUE_HOST_BUSY;
3349 req_desc = megasas_get_request_descriptor(instance, index-1);
3351 req_desc->Words = 0;
3352 cmd->request_desc = req_desc;
3354 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3355 megasas_return_cmd_fusion(instance, cmd);
3356 dev_err(&instance->pdev->dev, "Error building command\n");
3357 cmd->request_desc = NULL;
3358 atomic_dec(&instance->fw_outstanding);
3359 return SCSI_MLQUEUE_HOST_BUSY;
3362 req_desc = cmd->request_desc;
3363 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3365 if (cmd->io_request->ChainOffset != 0 &&
3366 cmd->io_request->ChainOffset != 0xF)
3367 dev_err(&instance->pdev->dev, "The chain offset value is not "
3368 "correct : %x\n", cmd->io_request->ChainOffset);
3370 * if it is raid 1/10 fp write capable.
3371 * try to get second command from pool and construct it.
3372 * From FW, it has confirmed that lba values of two PDs
3373 * corresponds to single R1/10 LD are always same
3376 /* driver side count always should be less than max_fw_cmds
3377 * to get new command
3379 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
3380 r1_cmd = megasas_get_cmd_fusion(instance,
3381 (scmd->request->tag + instance->max_fw_cmds));
3382 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3387 * Issue the command to the FW
3390 megasas_fire_cmd_fusion(instance, req_desc);
3393 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
3400 * megasas_complete_r1_command -
3401 * completes R1 FP write commands which has valid peer smid
3402 * @instance: Adapter soft state
3403 * @cmd: MPT command frame
3407 megasas_complete_r1_command(struct megasas_instance *instance,
3408 struct megasas_cmd_fusion *cmd)
3410 u8 *sense, status, ex_status;
3413 struct fusion_context *fusion;
3414 struct megasas_cmd_fusion *r1_cmd = NULL;
3415 struct scsi_cmnd *scmd_local = NULL;
3416 struct RAID_CONTEXT_G35 *rctx_g35;
3418 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3419 fusion = instance->ctrl_context;
3420 peer_smid = le16_to_cpu(rctx_g35->flow_specific.peer_smid);
3422 r1_cmd = fusion->cmd_list[peer_smid - 1];
3423 scmd_local = cmd->scmd;
3424 status = rctx_g35->status;
3425 ex_status = rctx_g35->ex_status;
3426 data_length = cmd->io_request->DataLength;
3429 cmd->cmd_completed = true;
3431 /* Check if peer command is completed or not*/
3432 if (r1_cmd->cmd_completed) {
3433 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3434 if (rctx_g35->status != MFI_STAT_OK) {
3435 status = rctx_g35->status;
3436 ex_status = rctx_g35->ex_status;
3437 data_length = r1_cmd->io_request->DataLength;
3438 sense = r1_cmd->sense;
3441 megasas_return_cmd_fusion(instance, r1_cmd);
3442 map_cmd_status(fusion, scmd_local, status, ex_status,
3443 le32_to_cpu(data_length), sense);
3444 if (instance->ldio_threshold &&
3445 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3446 atomic_dec(&instance->ldio_outstanding);
3447 scmd_local->SCp.ptr = NULL;
3448 megasas_return_cmd_fusion(instance, cmd);
3449 scsi_dma_unmap(scmd_local);
3450 scmd_local->scsi_done(scmd_local);
3455 * complete_cmd_fusion - Completes command
3456 * @instance: Adapter soft state
3457 * @MSIxIndex: MSI number
3458 * @irq_context: IRQ context
3460 * Completes all commands that is in reply descriptor queue
3463 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex,
3464 struct megasas_irq_context *irq_context)
3466 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3467 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3468 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3469 struct fusion_context *fusion;
3470 struct megasas_cmd *cmd_mfi;
3471 struct megasas_cmd_fusion *cmd_fusion;
3472 u16 smid, num_completed;
3473 u8 reply_descript_type, *sense, status, extStatus;
3474 u32 device_id, data_length;
3475 union desc_value d_val;
3476 struct LD_LOAD_BALANCE_INFO *lbinfo;
3477 int threshold_reply_count = 0;
3478 struct scsi_cmnd *scmd_local = NULL;
3479 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3480 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
3482 fusion = instance->ctrl_context;
3484 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3487 desc = fusion->reply_frames_desc[MSIxIndex] +
3488 fusion->last_reply_idx[MSIxIndex];
3490 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3492 d_val.word = desc->Words;
3494 reply_descript_type = reply_desc->ReplyFlags &
3495 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3497 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3502 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3503 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3505 smid = le16_to_cpu(reply_desc->SMID);
3506 cmd_fusion = fusion->cmd_list[smid - 1];
3507 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3508 cmd_fusion->io_request;
3510 scmd_local = cmd_fusion->scmd;
3511 status = scsi_io_req->RaidContext.raid_context.status;
3512 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3513 sense = cmd_fusion->sense;
3514 data_length = scsi_io_req->DataLength;
3516 switch (scsi_io_req->Function) {
3517 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3518 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3519 cmd_fusion->io_request;
3520 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3521 &mr_tm_req->TmRequest;
3522 dev_dbg(&instance->pdev->dev, "TM completion:"
3523 "type: 0x%x TaskMID: 0x%x\n",
3524 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3525 complete(&cmd_fusion->done);
3527 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3528 /* Update load balancing info */
3529 if (fusion->load_balance_info &&
3530 (cmd_fusion->scmd->SCp.Status &
3531 MEGASAS_LOAD_BALANCE_FLAG)) {
3532 device_id = MEGASAS_DEV_INDEX(scmd_local);
3533 lbinfo = &fusion->load_balance_info[device_id];
3534 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3535 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3537 fallthrough; /* and complete IO */
3538 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3539 atomic_dec(&instance->fw_outstanding);
3540 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3541 map_cmd_status(fusion, scmd_local, status,
3542 extStatus, le32_to_cpu(data_length),
3544 if (instance->ldio_threshold &&
3545 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3546 atomic_dec(&instance->ldio_outstanding);
3547 scmd_local->SCp.ptr = NULL;
3548 megasas_return_cmd_fusion(instance, cmd_fusion);
3549 scsi_dma_unmap(scmd_local);
3550 scmd_local->scsi_done(scmd_local);
3551 } else /* Optimal VD - R1 FP command completion. */
3552 megasas_complete_r1_command(instance, cmd_fusion);
3554 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3555 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3556 /* Poll mode. Dummy free.
3557 * In case of Interrupt mode, caller has reverse check.
3559 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3560 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3561 megasas_return_cmd(instance, cmd_mfi);
3563 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3567 fusion->last_reply_idx[MSIxIndex]++;
3568 if (fusion->last_reply_idx[MSIxIndex] >=
3569 fusion->reply_q_depth)
3570 fusion->last_reply_idx[MSIxIndex] = 0;
3572 desc->Words = cpu_to_le64(ULLONG_MAX);
3574 threshold_reply_count++;
3576 /* Get the next reply descriptor */
3577 if (!fusion->last_reply_idx[MSIxIndex])
3578 desc = fusion->reply_frames_desc[MSIxIndex];
3583 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3585 d_val.word = desc->Words;
3587 reply_descript_type = reply_desc->ReplyFlags &
3588 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3590 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3593 * Write to reply post host index register after completing threshold
3594 * number of reply counts and still there are more replies in reply queue
3595 * pending to be completed
3597 if (threshold_reply_count >= instance->threshold_reply_count) {
3598 if (instance->msix_combined)
3599 writel(((MSIxIndex & 0x7) << 24) |
3600 fusion->last_reply_idx[MSIxIndex],
3601 instance->reply_post_host_index_addr[MSIxIndex/8]);
3603 writel((MSIxIndex << 24) |
3604 fusion->last_reply_idx[MSIxIndex],
3605 instance->reply_post_host_index_addr[0]);
3606 threshold_reply_count = 0;
3608 if (!irq_context->irq_poll_scheduled) {
3609 irq_context->irq_poll_scheduled = true;
3610 irq_context->irq_line_enable = true;
3611 irq_poll_sched(&irq_context->irqpoll);
3613 return num_completed;
3618 if (num_completed) {
3620 if (instance->msix_combined)
3621 writel(((MSIxIndex & 0x7) << 24) |
3622 fusion->last_reply_idx[MSIxIndex],
3623 instance->reply_post_host_index_addr[MSIxIndex/8]);
3625 writel((MSIxIndex << 24) |
3626 fusion->last_reply_idx[MSIxIndex],
3627 instance->reply_post_host_index_addr[0]);
3628 megasas_check_and_restore_queue_depth(instance);
3630 return num_completed;
3634 * megasas_enable_irq_poll() - enable irqpoll
3635 * @instance: Adapter soft state
3637 static void megasas_enable_irq_poll(struct megasas_instance *instance)
3640 struct megasas_irq_context *irq_ctx;
3642 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3644 for (i = 0; i < count; i++) {
3645 irq_ctx = &instance->irq_context[i];
3646 irq_poll_enable(&irq_ctx->irqpoll);
3651 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3652 * @instance_addr: Adapter soft state address
3654 static void megasas_sync_irqs(unsigned long instance_addr)
3657 struct megasas_instance *instance =
3658 (struct megasas_instance *)instance_addr;
3659 struct megasas_irq_context *irq_ctx;
3661 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3663 for (i = 0; i < count; i++) {
3664 synchronize_irq(pci_irq_vector(instance->pdev, i));
3665 irq_ctx = &instance->irq_context[i];
3666 irq_poll_disable(&irq_ctx->irqpoll);
3667 if (irq_ctx->irq_poll_scheduled) {
3668 irq_ctx->irq_poll_scheduled = false;
3669 enable_irq(irq_ctx->os_irq);
3670 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3676 * megasas_irqpoll() - process a queue for completed reply descriptors
3677 * @irqpoll: IRQ poll structure associated with queue to poll.
3678 * @budget: Threshold of reply descriptors to process per poll.
3680 * Return: The number of entries processed.
3683 int megasas_irqpoll(struct irq_poll *irqpoll, int budget)
3685 struct megasas_irq_context *irq_ctx;
3686 struct megasas_instance *instance;
3689 irq_ctx = container_of(irqpoll, struct megasas_irq_context, irqpoll);
3690 instance = irq_ctx->instance;
3692 if (irq_ctx->irq_line_enable) {
3693 disable_irq_nosync(irq_ctx->os_irq);
3694 irq_ctx->irq_line_enable = false;
3697 num_entries = complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3698 if (num_entries < budget) {
3699 irq_poll_complete(irqpoll);
3700 irq_ctx->irq_poll_scheduled = false;
3701 enable_irq(irq_ctx->os_irq);
3702 complete_cmd_fusion(instance, irq_ctx->MSIxIndex, irq_ctx);
3709 * megasas_complete_cmd_dpc_fusion - Completes command
3710 * @instance_addr: Adapter soft state address
3712 * Tasklet to complete cmds
3715 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3717 struct megasas_instance *instance =
3718 (struct megasas_instance *)instance_addr;
3719 struct megasas_irq_context *irq_ctx = NULL;
3720 u32 count, MSIxIndex;
3722 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3724 /* If we have already declared adapter dead, donot complete cmds */
3725 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3728 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) {
3729 irq_ctx = &instance->irq_context[MSIxIndex];
3730 complete_cmd_fusion(instance, MSIxIndex, irq_ctx);
3735 * megasas_isr_fusion - isr entry point
3737 * @devp: IRQ context
3739 static irqreturn_t megasas_isr_fusion(int irq, void *devp)
3741 struct megasas_irq_context *irq_context = devp;
3742 struct megasas_instance *instance = irq_context->instance;
3745 if (instance->mask_interrupts)
3748 if (irq_context->irq_poll_scheduled)
3751 if (!instance->msix_vectors) {
3752 mfiStatus = instance->instancet->clear_intr(instance);
3757 /* If we are resetting, bail */
3758 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3759 instance->instancet->clear_intr(instance);
3763 return complete_cmd_fusion(instance, irq_context->MSIxIndex, irq_context)
3764 ? IRQ_HANDLED : IRQ_NONE;
3768 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3769 * @instance: Adapter soft state
3770 * @mfi_cmd: megasas_cmd pointer
3774 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3775 struct megasas_cmd *mfi_cmd)
3777 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3778 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3779 struct megasas_cmd_fusion *cmd;
3780 struct fusion_context *fusion;
3781 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3783 fusion = instance->ctrl_context;
3785 cmd = megasas_get_cmd_fusion(instance,
3786 instance->max_scsi_cmds + mfi_cmd->index);
3788 /* Save the smid. To be used for returning the cmd */
3789 mfi_cmd->context.smid = cmd->index;
3792 * For cmds where the flag is set, store the flag and check
3793 * on completion. For cmds with this flag, don't call
3794 * megasas_complete_cmd
3797 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3798 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3800 io_req = cmd->io_request;
3802 if (instance->adapter_type >= INVADER_SERIES) {
3803 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3804 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3805 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3806 sgl_ptr_end->Flags = 0;
3810 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3812 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3813 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3815 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3817 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3819 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3820 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3822 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
3826 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3827 * @instance: Adapter soft state
3828 * @cmd: mfi cmd to build
3831 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3832 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3834 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3837 build_mpt_mfi_pass_thru(instance, cmd);
3838 index = cmd->context.smid;
3840 req_desc = megasas_get_request_descriptor(instance, index - 1);
3842 req_desc->Words = 0;
3843 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3844 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3846 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3852 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3853 * @instance: Adapter soft state
3854 * @cmd: mfi cmd pointer
3858 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3859 struct megasas_cmd *cmd)
3861 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3863 req_desc = build_mpt_cmd(instance, cmd);
3865 megasas_fire_cmd_fusion(instance, req_desc);
3870 * megasas_release_fusion - Reverses the FW initialization
3871 * @instance: Adapter soft state
3874 megasas_release_fusion(struct megasas_instance *instance)
3876 megasas_free_ioc_init_cmd(instance);
3877 megasas_free_cmds(instance);
3878 megasas_free_cmds_fusion(instance);
3880 iounmap(instance->reg_set);
3882 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3886 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3887 * @instance: Adapter soft state
3890 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance)
3892 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0);
3896 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3897 * @instance: Controller's soft instance
3898 * @return: Number of allocated host crash buffers
3901 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3905 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3906 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
3907 if (!instance->crash_buf[i]) {
3908 dev_info(&instance->pdev->dev, "Firmware crash dump "
3909 "memory allocation failed at index %d\n", i);
3913 instance->drv_buf_alloc = i;
3917 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3918 * @instance: Controller's soft instance
3921 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3924 for (i = 0; i < instance->drv_buf_alloc; i++) {
3925 if (instance->crash_buf[i])
3926 vfree(instance->crash_buf[i]);
3928 instance->drv_buf_index = 0;
3929 instance->drv_buf_alloc = 0;
3930 instance->fw_crash_state = UNAVAILABLE;
3931 instance->fw_crash_buffer_size = 0;
3935 * megasas_adp_reset_fusion - For controller reset
3936 * @instance: Controller's soft instance
3937 * @regs: MFI register set
3940 megasas_adp_reset_fusion(struct megasas_instance *instance,
3941 struct megasas_register_set __iomem *regs)
3943 u32 host_diag, abs_state, retry;
3945 /* Now try to reset the chip */
3946 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3947 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3948 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3949 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3950 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3951 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3952 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3954 /* Check that the diag write enable (DRWE) bit is on */
3955 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3957 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3959 host_diag = megasas_readl(instance,
3960 &instance->reg_set->fusion_host_diag);
3961 if (retry++ == 100) {
3962 dev_warn(&instance->pdev->dev,
3963 "Host diag unlock failed from %s %d\n",
3964 __func__, __LINE__);
3968 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3971 /* Send chip reset command */
3972 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3973 &instance->reg_set->fusion_host_diag);
3976 /* Make sure reset adapter bit is cleared */
3977 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag);
3979 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3981 host_diag = megasas_readl(instance,
3982 &instance->reg_set->fusion_host_diag);
3983 if (retry++ == 1000) {
3984 dev_warn(&instance->pdev->dev,
3985 "Diag reset adapter never cleared %s %d\n",
3986 __func__, __LINE__);
3990 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3993 abs_state = instance->instancet->read_fw_status_reg(instance)
3997 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3999 abs_state = instance->instancet->
4000 read_fw_status_reg(instance) & MFI_STATE_MASK;
4002 if (abs_state <= MFI_STATE_FW_INIT) {
4003 dev_warn(&instance->pdev->dev,
4004 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4005 abs_state, __func__, __LINE__);
4013 * megasas_check_reset_fusion - For controller reset check
4014 * @instance: Controller's soft instance
4015 * @regs: MFI register set
4018 megasas_check_reset_fusion(struct megasas_instance *instance,
4019 struct megasas_register_set __iomem *regs)
4025 * megasas_trigger_snap_dump - Trigger snap dump in FW
4026 * @instance: Soft instance of adapter
4028 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
4031 u32 fw_state, abs_state;
4033 if (!instance->disableOnlineCtrlReset) {
4034 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
4035 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
4036 &instance->reg_set->doorbell);
4037 readl(&instance->reg_set->doorbell);
4040 for (j = 0; j < instance->snapdump_wait_time; j++) {
4041 abs_state = instance->instancet->read_fw_status_reg(instance);
4042 fw_state = abs_state & MFI_STATE_MASK;
4043 if (fw_state == MFI_STATE_FAULT) {
4044 dev_printk(KERN_ERR, &instance->pdev->dev,
4045 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4046 abs_state & MFI_STATE_FAULT_CODE,
4047 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4054 /* This function waits for outstanding commands on fusion to complete */
4056 megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
4057 int reason, int *convert)
4059 int i, outstanding, retval = 0, hb_seconds_missed = 0;
4060 u32 fw_state, abs_state;
4061 u32 waittime_for_io_completion;
4063 waittime_for_io_completion =
4064 min_t(u32, resetwaittime,
4065 (resetwaittime - instance->snapdump_wait_time));
4067 if (reason == MFI_IO_TIMEOUT_OCR) {
4068 dev_info(&instance->pdev->dev,
4069 "MFI command is timed out\n");
4070 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4071 if (instance->snapdump_wait_time)
4072 megasas_trigger_snap_dump(instance);
4077 for (i = 0; i < waittime_for_io_completion; i++) {
4078 /* Check if firmware is in fault state */
4079 abs_state = instance->instancet->read_fw_status_reg(instance);
4080 fw_state = abs_state & MFI_STATE_MASK;
4081 if (fw_state == MFI_STATE_FAULT) {
4082 dev_printk(KERN_ERR, &instance->pdev->dev,
4083 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4084 abs_state & MFI_STATE_FAULT_CODE,
4085 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4086 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4087 if (instance->requestorId && reason) {
4088 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
4089 " state while polling during"
4090 " I/O timeout handling for %d\n",
4091 instance->host->host_no);
4100 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4101 if (instance->requestorId && !reason) {
4106 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4107 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
4108 if (instance->hb_host_mem->HB.fwCounter !=
4109 instance->hb_host_mem->HB.driverCounter) {
4110 instance->hb_host_mem->HB.driverCounter =
4111 instance->hb_host_mem->HB.fwCounter;
4112 hb_seconds_missed = 0;
4114 hb_seconds_missed++;
4115 if (hb_seconds_missed ==
4116 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
4117 dev_warn(&instance->pdev->dev, "SR-IOV:"
4118 " Heartbeat never completed "
4119 " while polling during I/O "
4120 " timeout handling for "
4122 instance->host->host_no);
4130 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
4131 outstanding = atomic_read(&instance->fw_outstanding);
4135 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4136 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
4137 "commands to complete for scsi%d\n", i,
4138 outstanding, instance->host->host_no);
4143 if (instance->snapdump_wait_time) {
4144 megasas_trigger_snap_dump(instance);
4149 if (atomic_read(&instance->fw_outstanding)) {
4150 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
4151 "will reset adapter scsi%d.\n",
4152 instance->host->host_no);
4161 void megasas_reset_reply_desc(struct megasas_instance *instance)
4164 struct fusion_context *fusion;
4165 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4167 fusion = instance->ctrl_context;
4168 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
4169 for (i = 0 ; i < count ; i++) {
4170 fusion->last_reply_idx[i] = 0;
4171 reply_desc = fusion->reply_frames_desc[i];
4172 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4173 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4178 * megasas_refire_mgmt_cmd : Re-fire management commands
4179 * @instance: Controller's soft instance
4181 static void megasas_refire_mgmt_cmd(struct megasas_instance *instance,
4185 struct megasas_cmd_fusion *cmd_fusion;
4186 struct fusion_context *fusion;
4187 struct megasas_cmd *cmd_mfi;
4188 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4189 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
4191 bool refire_cmd = false;
4195 fusion = instance->ctrl_context;
4197 /* Re-fire management commands.
4198 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4200 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4201 cmd_fusion = fusion->cmd_list[j];
4202 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4203 smid = le16_to_cpu(cmd_mfi->context.smid);
4204 result = REFIRE_CMD;
4209 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4211 switch (cmd_mfi->frame->hdr.cmd) {
4213 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4214 /* Do not refire shutdown command */
4215 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4216 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4217 result = COMPLETE_CMD;
4221 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4222 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4223 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4226 result = RETURN_CMD;
4230 if (!instance->support_nvme_passthru) {
4231 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4232 result = COMPLETE_CMD;
4236 case MFI_CMD_TOOLBOX:
4237 if (!instance->support_pci_lane_margining) {
4238 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4239 result = COMPLETE_CMD;
4247 if (return_ioctl && cmd_mfi->sync_cmd &&
4248 cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT) {
4249 dev_err(&instance->pdev->dev,
4250 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x\n",
4251 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4252 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4253 cmd_mfi->cmd_status_drv = DCMD_BUSY;
4254 result = COMPLETE_CMD;
4257 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
4258 cmd_fusion->io_request;
4259 if (scsi_io_req->Function == MPI2_FUNCTION_SCSI_TASK_MGMT)
4260 result = RETURN_CMD;
4264 megasas_fire_cmd_fusion(instance, req_desc);
4267 megasas_return_cmd(instance, cmd_mfi);
4270 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4277 * megasas_return_polled_cmds: Return polled mode commands back to the pool
4278 * before initiating an OCR.
4279 * @instance: Controller's soft instance
4282 megasas_return_polled_cmds(struct megasas_instance *instance)
4285 struct megasas_cmd_fusion *cmd_fusion;
4286 struct fusion_context *fusion;
4287 struct megasas_cmd *cmd_mfi;
4289 fusion = instance->ctrl_context;
4291 for (i = instance->max_scsi_cmds; i < instance->max_fw_cmds; i++) {
4292 cmd_fusion = fusion->cmd_list[i];
4293 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4295 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
4296 if (megasas_dbg_lvl & OCR_DEBUG)
4297 dev_info(&instance->pdev->dev,
4298 "%s %d return cmd 0x%x opcode 0x%x\n",
4299 __func__, __LINE__, cmd_mfi->frame->hdr.cmd,
4300 le32_to_cpu(cmd_mfi->frame->dcmd.opcode));
4301 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
4302 megasas_return_cmd(instance, cmd_mfi);
4308 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4309 * @instance: per adapter struct
4310 * @channel: the channel assigned by the OS
4311 * @id: the id assigned by the OS
4313 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4316 static int megasas_track_scsiio(struct megasas_instance *instance,
4317 int id, int channel)
4320 struct megasas_cmd_fusion *cmd_fusion;
4321 struct fusion_context *fusion;
4322 fusion = instance->ctrl_context;
4324 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4325 cmd_fusion = fusion->cmd_list[i];
4326 if (cmd_fusion->scmd &&
4327 (cmd_fusion->scmd->device->id == id &&
4328 cmd_fusion->scmd->device->channel == channel)) {
4329 dev_info(&instance->pdev->dev,
4330 "SCSI commands pending to target"
4331 "channel %d id %d \tSMID: 0x%x\n",
4332 channel, id, cmd_fusion->index);
4333 scsi_print_command(cmd_fusion->scmd);
4339 return found ? FAILED : SUCCESS;
4343 * megasas_tm_response_code - translation of device response code
4344 * @instance: Controller's soft instance
4345 * @mpi_reply: MPI reply returned by firmware
4350 megasas_tm_response_code(struct megasas_instance *instance,
4351 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4355 switch (mpi_reply->ResponseCode) {
4356 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4357 desc = "task management request completed";
4359 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4360 desc = "invalid frame";
4362 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4363 desc = "task management request not supported";
4365 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4366 desc = "task management request failed";
4368 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4369 desc = "task management request succeeded";
4371 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4372 desc = "invalid lun";
4375 desc = "overlapped tag attempted";
4377 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4378 desc = "task queued, however not sent to target";
4384 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4385 mpi_reply->ResponseCode, desc);
4386 dev_dbg(&instance->pdev->dev,
4387 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4388 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4389 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4390 mpi_reply->Function, mpi_reply->TaskType,
4391 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4395 * megasas_issue_tm - main routine for sending tm requests
4396 * @instance: per adapter struct
4397 * @device_handle: device handle
4398 * @channel: the channel assigned by the OS
4399 * @id: the id assigned by the OS
4400 * @smid_task: smid assigned to the task
4401 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4402 * @mr_device_priv_data: private data
4405 * MegaRaid use MPT interface for Task Magement request.
4406 * A generic API for sending task management requests to firmware.
4408 * Return SUCCESS or FAILED.
4411 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
4412 uint channel, uint id, u16 smid_task, u8 type,
4413 struct MR_PRIV_DEVICE *mr_device_priv_data)
4415 struct MR_TASK_MANAGE_REQUEST *mr_request;
4416 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4417 unsigned long timeleft;
4418 struct megasas_cmd_fusion *cmd_fusion;
4419 struct megasas_cmd *cmd_mfi;
4420 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4421 struct fusion_context *fusion = NULL;
4422 struct megasas_cmd_fusion *scsi_lookup;
4424 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
4425 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4427 fusion = instance->ctrl_context;
4429 cmd_mfi = megasas_get_cmd(instance);
4432 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4433 __func__, __LINE__);
4437 cmd_fusion = megasas_get_cmd_fusion(instance,
4438 instance->max_scsi_cmds + cmd_mfi->index);
4440 /* Save the smid. To be used for returning the cmd */
4441 cmd_mfi->context.smid = cmd_fusion->index;
4443 req_desc = megasas_get_request_descriptor(instance,
4444 (cmd_fusion->index - 1));
4446 cmd_fusion->request_desc = req_desc;
4447 req_desc->Words = 0;
4449 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4450 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4451 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4452 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4453 mpi_request->DevHandle = cpu_to_le16(device_handle);
4454 mpi_request->TaskType = type;
4455 mpi_request->TaskMID = cpu_to_le16(smid_task);
4456 mpi_request->LUN[1] = 0;
4459 req_desc = cmd_fusion->request_desc;
4460 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4461 req_desc->HighPriority.RequestFlags =
4462 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4463 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4464 req_desc->HighPriority.MSIxIndex = 0;
4465 req_desc->HighPriority.LMID = 0;
4466 req_desc->HighPriority.Reserved1 = 0;
4468 if (channel < MEGASAS_MAX_PD_CHANNELS)
4469 mr_request->tmReqFlags.isTMForPD = 1;
4471 mr_request->tmReqFlags.isTMForLD = 1;
4473 init_completion(&cmd_fusion->done);
4474 megasas_fire_cmd_fusion(instance, req_desc);
4477 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4478 timeout = mr_device_priv_data->task_abort_tmo;
4480 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4481 timeout = mr_device_priv_data->target_reset_tmo;
4485 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
4488 dev_err(&instance->pdev->dev,
4489 "task mgmt type 0x%x timed out\n", type);
4490 mutex_unlock(&instance->reset_mutex);
4491 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4492 mutex_lock(&instance->reset_mutex);
4496 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4497 megasas_tm_response_code(instance, mpi_reply);
4499 megasas_return_cmd(instance, cmd_mfi);
4502 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4503 scsi_lookup = fusion->cmd_list[smid_task - 1];
4505 if (scsi_lookup->scmd == NULL)
4508 instance->instancet->disable_intr(instance);
4509 megasas_sync_irqs((unsigned long)instance);
4510 instance->instancet->enable_intr(instance);
4511 megasas_enable_irq_poll(instance);
4512 if (scsi_lookup->scmd == NULL)
4518 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4519 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4521 instance->instancet->disable_intr(instance);
4522 megasas_sync_irqs((unsigned long)instance);
4523 rc = megasas_track_scsiio(instance, id, channel);
4524 instance->instancet->enable_intr(instance);
4525 megasas_enable_irq_poll(instance);
4528 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4529 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4541 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4542 * @instance: per adapter struct
4544 * Return Non Zero index, if SMID found in outstanding commands
4546 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4549 struct megasas_instance *instance;
4550 struct megasas_cmd_fusion *cmd_fusion;
4551 struct fusion_context *fusion;
4553 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4555 fusion = instance->ctrl_context;
4557 for (i = 0; i < instance->max_scsi_cmds; i++) {
4558 cmd_fusion = fusion->cmd_list[i];
4559 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4560 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4561 " SMID: %d\n", cmd_fusion->index);
4562 ret = cmd_fusion->index;
4571 * megasas_get_tm_devhandle - Get devhandle for TM request
4572 * @sdev- OS provided scsi device
4574 * Returns- devhandle/targetID of SCSI device
4576 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4580 struct megasas_instance *instance;
4581 struct fusion_context *fusion;
4582 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4583 u16 devhandle = (u16)ULONG_MAX;
4585 instance = (struct megasas_instance *)sdev->host->hostdata;
4586 fusion = instance->ctrl_context;
4588 if (!MEGASAS_IS_LOGICAL(sdev)) {
4589 if (instance->use_seqnum_jbod_fp) {
4590 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4592 pd_sync = (void *)fusion->pd_seq_sync
4593 [(instance->pd_seq_map_id - 1) & 1];
4594 devhandle = pd_sync->seq[pd_index].devHandle;
4596 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4597 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4599 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4601 devhandle = device_id;
4608 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4609 * @scmd : pointer to scsi command object
4611 * Return SUCCESS, if command aborted else FAILED
4614 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4616 struct megasas_instance *instance;
4617 u16 smid, devhandle;
4619 struct MR_PRIV_DEVICE *mr_device_priv_data;
4620 mr_device_priv_data = scmd->device->hostdata;
4622 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4624 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4625 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4626 "SCSI host:%d\n", instance->host->host_no);
4631 if (!mr_device_priv_data) {
4632 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4633 "scmd(%p)\n", scmd);
4634 scmd->result = DID_NO_CONNECT << 16;
4639 if (!mr_device_priv_data->is_tm_capable) {
4644 mutex_lock(&instance->reset_mutex);
4646 smid = megasas_fusion_smid_lookup(scmd);
4650 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4651 " issued is not found in outstanding commands\n");
4652 mutex_unlock(&instance->reset_mutex);
4656 devhandle = megasas_get_tm_devhandle(scmd->device);
4658 if (devhandle == (u16)ULONG_MAX) {
4660 sdev_printk(KERN_INFO, scmd->device,
4661 "task abort issued for invalid devhandle\n");
4662 mutex_unlock(&instance->reset_mutex);
4665 sdev_printk(KERN_INFO, scmd->device,
4666 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4669 mr_device_priv_data->tm_busy = true;
4670 ret = megasas_issue_tm(instance, devhandle,
4671 scmd->device->channel, scmd->device->id, smid,
4672 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4673 mr_device_priv_data);
4674 mr_device_priv_data->tm_busy = false;
4676 mutex_unlock(&instance->reset_mutex);
4677 scmd_printk(KERN_INFO, scmd, "task abort %s!! scmd(0x%p)\n",
4678 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4680 scsi_print_command(scmd);
4681 if (megasas_dbg_lvl & TM_DEBUG)
4682 megasas_dump_fusion_io(scmd);
4688 * megasas_reset_target_fusion : target reset function for fusion adapters
4689 * scmd: SCSI command pointer
4691 * Returns SUCCESS if all commands associated with target aborted else FAILED
4694 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4697 struct megasas_instance *instance;
4700 struct MR_PRIV_DEVICE *mr_device_priv_data;
4701 mr_device_priv_data = scmd->device->hostdata;
4703 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4705 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4706 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4707 "SCSI host:%d\n", instance->host->host_no);
4712 if (!mr_device_priv_data) {
4713 sdev_printk(KERN_INFO, scmd->device,
4714 "device been deleted! scmd: (0x%p)\n", scmd);
4715 scmd->result = DID_NO_CONNECT << 16;
4720 if (!mr_device_priv_data->is_tm_capable) {
4725 mutex_lock(&instance->reset_mutex);
4726 devhandle = megasas_get_tm_devhandle(scmd->device);
4728 if (devhandle == (u16)ULONG_MAX) {
4730 sdev_printk(KERN_INFO, scmd->device,
4731 "target reset issued for invalid devhandle\n");
4732 mutex_unlock(&instance->reset_mutex);
4736 sdev_printk(KERN_INFO, scmd->device,
4737 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4739 mr_device_priv_data->tm_busy = true;
4740 ret = megasas_issue_tm(instance, devhandle,
4741 scmd->device->channel, scmd->device->id, 0,
4742 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4743 mr_device_priv_data);
4744 mr_device_priv_data->tm_busy = false;
4745 mutex_unlock(&instance->reset_mutex);
4746 scmd_printk(KERN_NOTICE, scmd, "target reset %s!!\n",
4747 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4753 /*SRIOV get other instance in cluster if any*/
4755 megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4759 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4760 if (megasas_mgmt_info.instance[i] &&
4761 (megasas_mgmt_info.instance[i] != instance) &&
4762 megasas_mgmt_info.instance[i]->requestorId &&
4763 megasas_mgmt_info.instance[i]->peerIsPresent &&
4764 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4765 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4766 return megasas_mgmt_info.instance[i];
4771 /* Check for a second path that is currently UP */
4772 int megasas_check_mpio_paths(struct megasas_instance *instance,
4773 struct scsi_cmnd *scmd)
4775 struct megasas_instance *peer_instance = NULL;
4776 int retval = (DID_REQUEUE << 16);
4778 if (instance->peerIsPresent) {
4779 peer_instance = megasas_get_peer_instance(instance);
4780 if ((peer_instance) &&
4781 (atomic_read(&peer_instance->adprecovery) ==
4782 MEGASAS_HBA_OPERATIONAL))
4783 retval = (DID_NO_CONNECT << 16);
4788 /* Core fusion reset function */
4789 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4791 int retval = SUCCESS, i, j, convert = 0;
4792 struct megasas_instance *instance;
4793 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4794 struct fusion_context *fusion;
4795 u32 abs_state, status_reg, reset_adapter, fpio_count = 0;
4796 u32 io_timeout_in_crash_mode = 0;
4797 struct scsi_cmnd *scmd_local = NULL;
4798 struct scsi_device *sdev;
4799 int ret_target_prop = DCMD_FAILED;
4800 bool is_target_prop = false;
4801 bool do_adp_reset = true;
4802 int max_reset_tries = MEGASAS_FUSION_MAX_RESET_TRIES;
4804 instance = (struct megasas_instance *)shost->hostdata;
4805 fusion = instance->ctrl_context;
4807 mutex_lock(&instance->reset_mutex);
4809 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4810 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4811 "returning FAILED for scsi%d.\n",
4812 instance->host->host_no);
4813 mutex_unlock(&instance->reset_mutex);
4816 status_reg = instance->instancet->read_fw_status_reg(instance);
4817 abs_state = status_reg & MFI_STATE_MASK;
4819 /* IO timeout detected, forcibly put FW in FAULT state */
4820 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4821 instance->crash_dump_app_support && reason) {
4822 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4823 "forcibly FAULT Firmware\n");
4824 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4825 status_reg = megasas_readl(instance, &instance->reg_set->doorbell);
4826 writel(status_reg | MFI_STATE_FORCE_OCR,
4827 &instance->reg_set->doorbell);
4828 readl(&instance->reg_set->doorbell);
4829 mutex_unlock(&instance->reset_mutex);
4832 io_timeout_in_crash_mode++;
4833 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4834 "seconds for crash dump collection and OCR "
4835 "to be done\n", (io_timeout_in_crash_mode * 3));
4836 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4837 (io_timeout_in_crash_mode < 80));
4839 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4840 dev_info(&instance->pdev->dev, "OCR done for IO "
4844 dev_info(&instance->pdev->dev, "Controller is not "
4845 "operational after 240 seconds wait for IO "
4846 "timeout case in FW crash dump mode\n do "
4847 "OCR/kill adapter\n");
4848 retval = megasas_reset_fusion(shost, 0);
4853 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4854 del_timer_sync(&instance->sriov_heartbeat_timer);
4855 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4856 set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
4857 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4858 instance->instancet->disable_intr(instance);
4859 megasas_sync_irqs((unsigned long)instance);
4861 /* First try waiting for commands to complete */
4862 if (megasas_wait_for_outstanding_fusion(instance, reason,
4864 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4865 dev_warn(&instance->pdev->dev, "resetting fusion "
4866 "adapter scsi%d.\n", instance->host->host_no);
4870 if (megasas_dbg_lvl & OCR_DEBUG)
4871 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4873 /* Now return commands back to the OS */
4874 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4875 cmd_fusion = fusion->cmd_list[i];
4876 /*check for extra commands issued by driver*/
4877 if (instance->adapter_type >= VENTURA_SERIES) {
4878 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4879 megasas_return_cmd_fusion(instance, r1_cmd);
4881 scmd_local = cmd_fusion->scmd;
4882 if (cmd_fusion->scmd) {
4883 if (megasas_dbg_lvl & OCR_DEBUG) {
4884 sdev_printk(KERN_INFO,
4885 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4887 megasas_dump_fusion_io(cmd_fusion->scmd);
4890 if (cmd_fusion->io_request->Function ==
4891 MPI2_FUNCTION_SCSI_IO_REQUEST)
4894 scmd_local->result =
4895 megasas_check_mpio_paths(instance,
4897 if (instance->ldio_threshold &&
4898 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4899 atomic_dec(&instance->ldio_outstanding);
4900 megasas_return_cmd_fusion(instance, cmd_fusion);
4901 scsi_dma_unmap(scmd_local);
4902 scmd_local->scsi_done(scmd_local);
4906 dev_info(&instance->pdev->dev, "Outstanding fastpath IOs: %d\n",
4909 atomic_set(&instance->fw_outstanding, 0);
4911 status_reg = instance->instancet->read_fw_status_reg(instance);
4912 abs_state = status_reg & MFI_STATE_MASK;
4913 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4914 if (instance->disableOnlineCtrlReset ||
4915 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4916 /* Reset not supported, kill adapter */
4917 dev_warn(&instance->pdev->dev, "Reset not supported"
4918 ", killing adapter scsi%d.\n",
4919 instance->host->host_no);
4923 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4924 if (instance->requestorId && !reason) {
4925 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4926 do_adp_reset = false;
4927 max_reset_tries = MEGASAS_SRIOV_MAX_RESET_TRIES_VF;
4930 /* Now try to reset the chip */
4931 for (i = 0; i < max_reset_tries; i++) {
4933 * Do adp reset and wait for
4934 * controller to transition to ready
4936 if (megasas_adp_reset_wait_for_ready(instance,
4937 do_adp_reset, 1) == FAILED)
4940 /* Wait for FW to become ready */
4941 if (megasas_transition_to_ready(instance, 1)) {
4942 dev_warn(&instance->pdev->dev,
4943 "Failed to transition controller to ready for "
4944 "scsi%d.\n", instance->host->host_no);
4947 megasas_reset_reply_desc(instance);
4948 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4950 if (megasas_ioc_init_fusion(instance)) {
4954 if (megasas_get_ctrl_info(instance)) {
4955 dev_info(&instance->pdev->dev,
4956 "Failed from %s %d\n",
4957 __func__, __LINE__);
4961 megasas_refire_mgmt_cmd(instance,
4962 (i == (MEGASAS_FUSION_MAX_RESET_TRIES - 1)
4965 /* Reset load balance info */
4966 if (fusion->load_balance_info)
4967 memset(fusion->load_balance_info, 0,
4968 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4969 MAX_LOGICAL_DRIVES_EXT));
4971 if (!megasas_get_map_info(instance)) {
4972 megasas_sync_map_info(instance);
4975 * Return pending polled mode cmds before
4978 megasas_return_polled_cmds(instance);
4982 megasas_setup_jbod_map(instance);
4984 /* reset stream detection array */
4985 if (instance->adapter_type >= VENTURA_SERIES) {
4986 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4987 memset(fusion->stream_detect_by_ld[j],
4988 0, sizeof(struct LD_STREAM_DETECT));
4989 fusion->stream_detect_by_ld[j]->mru_bit_map
4994 clear_bit(MEGASAS_FUSION_IN_RESET,
4995 &instance->reset_flags);
4996 instance->instancet->enable_intr(instance);
4997 megasas_enable_irq_poll(instance);
4998 shost_for_each_device(sdev, shost) {
4999 if ((instance->tgt_prop) &&
5000 (instance->nvme_page_size))
5001 ret_target_prop = megasas_get_target_prop(instance, sdev);
5003 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
5004 megasas_set_dynamic_target_properties(sdev, is_target_prop);
5007 status_reg = instance->instancet->read_fw_status_reg
5009 abs_state = status_reg & MFI_STATE_MASK;
5010 if (abs_state != MFI_STATE_OPERATIONAL) {
5011 dev_info(&instance->pdev->dev,
5012 "Adapter is not OPERATIONAL, state 0x%x for scsi:%d\n",
5013 abs_state, instance->host->host_no);
5016 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5018 dev_info(&instance->pdev->dev,
5019 "Adapter is OPERATIONAL for scsi:%d\n",
5020 instance->host->host_no);
5022 /* Restart SR-IOV heartbeat */
5023 if (instance->requestorId) {
5024 if (!megasas_sriov_start_heartbeat(instance, 0))
5025 megasas_start_timer(instance);
5027 instance->skip_heartbeat_timer_del = 1;
5030 if (instance->crash_dump_drv_support &&
5031 instance->crash_dump_app_support)
5032 megasas_set_crash_dump_params(instance,
5033 MR_CRASH_BUF_TURN_ON);
5035 megasas_set_crash_dump_params(instance,
5036 MR_CRASH_BUF_TURN_OFF);
5038 if (instance->snapdump_wait_time) {
5039 megasas_get_snapdump_properties(instance);
5040 dev_info(&instance->pdev->dev,
5041 "Snap dump wait time\t: %d\n",
5042 instance->snapdump_wait_time);
5047 /* Adapter reset completed successfully */
5048 dev_warn(&instance->pdev->dev,
5049 "Reset successful for scsi%d.\n",
5050 instance->host->host_no);
5054 /* Reset failed, kill the adapter */
5055 dev_warn(&instance->pdev->dev, "Reset failed, killing "
5056 "adapter scsi%d.\n", instance->host->host_no);
5059 /* For VF: Restart HB timer if we didn't OCR */
5060 if (instance->requestorId) {
5061 megasas_start_timer(instance);
5063 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
5064 instance->instancet->enable_intr(instance);
5065 megasas_enable_irq_poll(instance);
5066 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
5070 megaraid_sas_kill_hba(instance);
5071 megasas_enable_irq_poll(instance);
5072 instance->skip_heartbeat_timer_del = 1;
5075 clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE, &instance->reset_flags);
5076 mutex_unlock(&instance->reset_mutex);
5080 /* Fusion Crash dump collection */
5081 static void megasas_fusion_crash_dump(struct megasas_instance *instance)
5084 u8 partial_copy = 0;
5088 status_reg = instance->instancet->read_fw_status_reg(instance);
5091 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5092 * to host crash buffers
5094 if (instance->drv_buf_index == 0) {
5095 /* Buffer is already allocated for old Crash dump.
5096 * Do OCR and do not wait for crash dump collection
5098 if (instance->drv_buf_alloc) {
5099 dev_info(&instance->pdev->dev, "earlier crash dump is "
5100 "not yet copied by application, ignoring this "
5101 "crash dump and initiating OCR\n");
5102 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5104 &instance->reg_set->outbound_scratch_pad_0);
5105 readl(&instance->reg_set->outbound_scratch_pad_0);
5108 megasas_alloc_host_crash_buffer(instance);
5109 dev_info(&instance->pdev->dev, "Number of host crash buffers "
5110 "allocated: %d\n", instance->drv_buf_alloc);
5113 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
5114 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
5115 if (!(status_reg & MFI_STATE_DMADONE)) {
5117 * Next crash dump buffer is not yet DMA'd by FW
5118 * Check after 10ms. Wait for 1 second for FW to
5119 * post the next buffer. If not bail out.
5122 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5123 status_reg = instance->instancet->read_fw_status_reg(
5129 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
5130 dev_info(&instance->pdev->dev,
5131 "Driver is done copying the buffer: %d\n",
5132 instance->drv_buf_alloc);
5133 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
5137 memcpy(instance->crash_buf[instance->drv_buf_index],
5138 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
5139 instance->drv_buf_index++;
5140 status_reg &= ~MFI_STATE_DMADONE;
5143 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5144 readl(&instance->reg_set->outbound_scratch_pad_0);
5146 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
5147 status_reg = instance->instancet->read_fw_status_reg(instance);
5150 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
5151 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
5152 "of copied buffers: %d\n", instance->drv_buf_index);
5153 instance->fw_crash_buffer_size = instance->drv_buf_index;
5154 instance->fw_crash_state = AVAILABLE;
5155 instance->drv_buf_index = 0;
5156 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
5157 readl(&instance->reg_set->outbound_scratch_pad_0);
5159 megasas_reset_fusion(instance->host, 0);
5164 /* Fusion OCR work queue */
5165 void megasas_fusion_ocr_wq(struct work_struct *work)
5167 struct megasas_instance *instance =
5168 container_of(work, struct megasas_instance, work_init);
5170 megasas_reset_fusion(instance->host, 0);
5173 /* Allocate fusion context */
5175 megasas_alloc_fusion_context(struct megasas_instance *instance)
5177 struct fusion_context *fusion;
5179 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
5181 if (!instance->ctrl_context) {
5182 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5183 __func__, __LINE__);
5187 fusion = instance->ctrl_context;
5189 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5190 sizeof(LD_SPAN_INFO));
5191 fusion->log_to_span =
5192 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5193 fusion->log_to_span_pages);
5194 if (!fusion->log_to_span) {
5195 fusion->log_to_span =
5196 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5197 sizeof(LD_SPAN_INFO)));
5198 if (!fusion->log_to_span) {
5199 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5200 __func__, __LINE__);
5205 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
5206 sizeof(struct LD_LOAD_BALANCE_INFO));
5207 fusion->load_balance_info =
5208 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
5209 fusion->load_balance_info_pages);
5210 if (!fusion->load_balance_info) {
5211 fusion->load_balance_info =
5212 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
5213 sizeof(struct LD_LOAD_BALANCE_INFO)));
5214 if (!fusion->load_balance_info)
5215 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
5216 "continuing without Load Balance support\n");
5223 megasas_free_fusion_context(struct megasas_instance *instance)
5225 struct fusion_context *fusion = instance->ctrl_context;
5228 if (fusion->load_balance_info) {
5229 if (is_vmalloc_addr(fusion->load_balance_info))
5230 vfree(fusion->load_balance_info);
5232 free_pages((ulong)fusion->load_balance_info,
5233 fusion->load_balance_info_pages);
5236 if (fusion->log_to_span) {
5237 if (is_vmalloc_addr(fusion->log_to_span))
5238 vfree(fusion->log_to_span);
5240 free_pages((ulong)fusion->log_to_span,
5241 fusion->log_to_span_pages);
5248 struct megasas_instance_template megasas_instance_template_fusion = {
5249 .enable_intr = megasas_enable_intr_fusion,
5250 .disable_intr = megasas_disable_intr_fusion,
5251 .clear_intr = megasas_clear_intr_fusion,
5252 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5253 .adp_reset = megasas_adp_reset_fusion,
5254 .check_reset = megasas_check_reset_fusion,
5255 .service_isr = megasas_isr_fusion,
5256 .tasklet = megasas_complete_cmd_dpc_fusion,
5257 .init_adapter = megasas_init_adapter_fusion,
5258 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5259 .issue_dcmd = megasas_issue_dcmd_fusion,