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 initiates 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_sdev_busy_inc(struct megasas_instance
*instance
,
225 struct scsi_cmnd
*scmd
)
227 if (instance
->perf_mode
== MR_BALANCED_PERF_MODE
) {
228 struct MR_PRIV_DEVICE
*mr_device_priv_data
=
229 scmd
->device
->hostdata
;
230 atomic_inc(&mr_device_priv_data
->sdev_priv_busy
);
235 megasas_sdev_busy_dec(struct megasas_instance
*instance
,
236 struct scsi_cmnd
*scmd
)
238 if (instance
->perf_mode
== MR_BALANCED_PERF_MODE
) {
239 struct MR_PRIV_DEVICE
*mr_device_priv_data
=
240 scmd
->device
->hostdata
;
241 atomic_dec(&mr_device_priv_data
->sdev_priv_busy
);
246 megasas_sdev_busy_read(struct megasas_instance
*instance
,
247 struct scsi_cmnd
*scmd
)
249 if (instance
->perf_mode
== MR_BALANCED_PERF_MODE
) {
250 struct MR_PRIV_DEVICE
*mr_device_priv_data
=
251 scmd
->device
->hostdata
;
252 return atomic_read(&mr_device_priv_data
->sdev_priv_busy
);
258 * megasas_get_cmd_fusion - Get a command from the free pool
259 * @instance: Adapter soft state
260 * @blk_tag: Command tag
262 * Returns a blk_tag indexed mpt frame
264 inline struct megasas_cmd_fusion
*megasas_get_cmd_fusion(struct megasas_instance
265 *instance
, u32 blk_tag
)
267 struct fusion_context
*fusion
;
269 fusion
= instance
->ctrl_context
;
270 return fusion
->cmd_list
[blk_tag
];
274 * megasas_return_cmd_fusion - Return a cmd to free command pool
275 * @instance: Adapter soft state
276 * @cmd: Command packet to be returned to free command pool
278 inline void megasas_return_cmd_fusion(struct megasas_instance
*instance
,
279 struct megasas_cmd_fusion
*cmd
)
282 memset(cmd
->io_request
, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
);
283 cmd
->r1_alt_dev_handle
= MR_DEVHANDLE_INVALID
;
284 cmd
->cmd_completed
= false;
288 * megasas_write_64bit_req_desc - PCI writes 64bit request descriptor
289 * @instance: Adapter soft state
290 * @req_desc: 64bit Request descriptor
293 megasas_write_64bit_req_desc(struct megasas_instance
*instance
,
294 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
)
296 #if defined(writeq) && defined(CONFIG_64BIT)
297 u64 req_data
= (((u64
)le32_to_cpu(req_desc
->u
.high
) << 32) |
298 le32_to_cpu(req_desc
->u
.low
));
299 writeq(req_data
, &instance
->reg_set
->inbound_low_queue_port
);
302 spin_lock_irqsave(&instance
->hba_lock
, flags
);
303 writel(le32_to_cpu(req_desc
->u
.low
),
304 &instance
->reg_set
->inbound_low_queue_port
);
305 writel(le32_to_cpu(req_desc
->u
.high
),
306 &instance
->reg_set
->inbound_high_queue_port
);
307 spin_unlock_irqrestore(&instance
->hba_lock
, flags
);
312 * megasas_fire_cmd_fusion - Sends command to the FW
313 * @instance: Adapter soft state
314 * @req_desc: 32bit or 64bit Request descriptor
316 * Perform PCI Write. AERO SERIES supports 32 bit Descriptor.
317 * Prior to AERO_SERIES support 64 bit Descriptor.
320 megasas_fire_cmd_fusion(struct megasas_instance
*instance
,
321 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
)
323 if (instance
->atomic_desc_support
)
324 writel(le32_to_cpu(req_desc
->u
.low
),
325 &instance
->reg_set
->inbound_single_queue_port
);
327 megasas_write_64bit_req_desc(instance
, req_desc
);
331 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
332 * @instance: Adapter soft state
333 * @fw_boot_context: Whether this function called during probe or after OCR
335 * This function is only for fusion controllers.
336 * Update host can queue, if firmware downgrade max supported firmware commands.
337 * Firmware upgrade case will be skipped because underlying firmware has
338 * more resource than exposed to the OS.
342 megasas_fusion_update_can_queue(struct megasas_instance
*instance
, int fw_boot_context
)
344 u16 cur_max_fw_cmds
= 0;
345 u16 ldio_threshold
= 0;
347 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
348 if (instance
->adapter_type
< VENTURA_SERIES
)
350 megasas_readl(instance
,
351 &instance
->reg_set
->outbound_scratch_pad_2
) & 0x00FFFF;
353 if (dual_qdepth_disable
|| !cur_max_fw_cmds
)
354 cur_max_fw_cmds
= instance
->instancet
->read_fw_status_reg(instance
) & 0x00FFFF;
357 (instance
->instancet
->read_fw_status_reg(instance
) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS
;
359 dev_info(&instance
->pdev
->dev
,
360 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
361 cur_max_fw_cmds
, ldio_threshold
);
363 if (fw_boot_context
== OCR_CONTEXT
) {
364 cur_max_fw_cmds
= cur_max_fw_cmds
- 1;
365 if (cur_max_fw_cmds
< instance
->max_fw_cmds
) {
366 instance
->cur_can_queue
=
367 cur_max_fw_cmds
- (MEGASAS_FUSION_INTERNAL_CMDS
+
368 MEGASAS_FUSION_IOCTL_CMDS
);
369 instance
->host
->can_queue
= instance
->cur_can_queue
;
370 instance
->ldio_threshold
= ldio_threshold
;
373 instance
->max_fw_cmds
= cur_max_fw_cmds
;
374 instance
->ldio_threshold
= ldio_threshold
;
377 instance
->max_fw_cmds
= min(instance
->max_fw_cmds
,
378 (u16
)MEGASAS_KDUMP_QUEUE_DEPTH
);
380 * Reduce the max supported cmds by 1. This is to ensure that the
381 * reply_q_sz (1 more than the max cmd that driver may send)
382 * does not exceed max cmds that the FW can support
384 instance
->max_fw_cmds
= instance
->max_fw_cmds
-1;
389 megasas_get_msix_index(struct megasas_instance
*instance
,
390 struct scsi_cmnd
*scmd
,
391 struct megasas_cmd_fusion
*cmd
,
394 if (instance
->perf_mode
== MR_BALANCED_PERF_MODE
&&
395 (megasas_sdev_busy_read(instance
, scmd
) >
396 (data_arms
* MR_DEVICE_HIGH_IOPS_DEPTH
))) {
397 cmd
->request_desc
->SCSIIO
.MSIxIndex
=
398 mega_mod64((atomic64_add_return(1, &instance
->high_iops_outstanding
) /
399 MR_HIGH_IOPS_BATCH_COUNT
), instance
->low_latency_index_start
);
400 } else if (instance
->msix_load_balance
) {
401 cmd
->request_desc
->SCSIIO
.MSIxIndex
=
402 (mega_mod64(atomic64_add_return(1, &instance
->total_io_count
),
403 instance
->msix_vectors
));
404 } else if (instance
->host
->nr_hw_queues
> 1) {
405 u32 tag
= blk_mq_unique_tag(scsi_cmd_to_rq(scmd
));
407 cmd
->request_desc
->SCSIIO
.MSIxIndex
= blk_mq_unique_tag_to_hwq(tag
) +
408 instance
->low_latency_index_start
;
410 cmd
->request_desc
->SCSIIO
.MSIxIndex
=
411 instance
->reply_map
[raw_smp_processor_id()];
416 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
417 * @instance: Adapter soft state
420 megasas_free_cmds_fusion(struct megasas_instance
*instance
)
423 struct fusion_context
*fusion
= instance
->ctrl_context
;
424 struct megasas_cmd_fusion
*cmd
;
427 dma_pool_free(fusion
->sense_dma_pool
, fusion
->sense
,
428 fusion
->sense_phys_addr
);
431 if (fusion
->cmd_list
) {
432 for (i
= 0; i
< instance
->max_mpt_cmds
; i
++) {
433 cmd
= fusion
->cmd_list
[i
];
436 dma_pool_free(fusion
->sg_dma_pool
,
438 cmd
->sg_frame_phys_addr
);
442 kfree(fusion
->cmd_list
);
445 if (fusion
->sg_dma_pool
) {
446 dma_pool_destroy(fusion
->sg_dma_pool
);
447 fusion
->sg_dma_pool
= NULL
;
449 if (fusion
->sense_dma_pool
) {
450 dma_pool_destroy(fusion
->sense_dma_pool
);
451 fusion
->sense_dma_pool
= NULL
;
455 /* Reply Frame, Desc*/
456 if (instance
->is_rdpq
)
457 megasas_free_rdpq_fusion(instance
);
459 megasas_free_reply_fusion(instance
);
461 /* Request Frame, Desc*/
462 if (fusion
->req_frames_desc
)
463 dma_free_coherent(&instance
->pdev
->dev
,
464 fusion
->request_alloc_sz
, fusion
->req_frames_desc
,
465 fusion
->req_frames_desc_phys
);
466 if (fusion
->io_request_frames
)
467 dma_pool_free(fusion
->io_request_frames_pool
,
468 fusion
->io_request_frames
,
469 fusion
->io_request_frames_phys
);
470 if (fusion
->io_request_frames_pool
) {
471 dma_pool_destroy(fusion
->io_request_frames_pool
);
472 fusion
->io_request_frames_pool
= NULL
;
477 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
478 * @instance: Adapter soft state
481 static int megasas_create_sg_sense_fusion(struct megasas_instance
*instance
)
485 struct fusion_context
*fusion
;
486 struct megasas_cmd_fusion
*cmd
;
490 fusion
= instance
->ctrl_context
;
491 max_cmd
= instance
->max_fw_cmds
;
492 sense_sz
= instance
->max_mpt_cmds
* SCSI_SENSE_BUFFERSIZE
;
494 fusion
->sg_dma_pool
=
495 dma_pool_create("mr_sg", &instance
->pdev
->dev
,
496 instance
->max_chain_frame_sz
,
497 MR_DEFAULT_NVME_PAGE_SIZE
, 0);
498 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
499 fusion
->sense_dma_pool
=
500 dma_pool_create("mr_sense", &instance
->pdev
->dev
,
503 if (!fusion
->sense_dma_pool
|| !fusion
->sg_dma_pool
) {
504 dev_err(&instance
->pdev
->dev
,
505 "Failed from %s %d\n", __func__
, __LINE__
);
509 fusion
->sense
= dma_pool_alloc(fusion
->sense_dma_pool
,
510 GFP_KERNEL
, &fusion
->sense_phys_addr
);
511 if (!fusion
->sense
) {
512 dev_err(&instance
->pdev
->dev
,
513 "failed from %s %d\n", __func__
, __LINE__
);
517 /* sense buffer, request frame and reply desc pool requires to be in
518 * same 4 gb region. Below function will check this.
519 * In case of failure, new pci pool will be created with updated
521 * Older allocation and pool will be destroyed.
522 * Alignment will be used such a way that next allocation if success,
523 * will always meet same 4gb region requirement.
524 * Actual requirement is not alignment, but we need start and end of
525 * DMA address must have same upper 32 bit address.
528 if (!megasas_check_same_4gb_region(instance
, fusion
->sense_phys_addr
,
530 dma_pool_free(fusion
->sense_dma_pool
, fusion
->sense
,
531 fusion
->sense_phys_addr
);
532 fusion
->sense
= NULL
;
533 dma_pool_destroy(fusion
->sense_dma_pool
);
535 fusion
->sense_dma_pool
=
536 dma_pool_create("mr_sense_align", &instance
->pdev
->dev
,
537 sense_sz
, roundup_pow_of_two(sense_sz
),
539 if (!fusion
->sense_dma_pool
) {
540 dev_err(&instance
->pdev
->dev
,
541 "Failed from %s %d\n", __func__
, __LINE__
);
544 fusion
->sense
= dma_pool_alloc(fusion
->sense_dma_pool
,
546 &fusion
->sense_phys_addr
);
547 if (!fusion
->sense
) {
548 dev_err(&instance
->pdev
->dev
,
549 "failed from %s %d\n", __func__
, __LINE__
);
555 * Allocate and attach a frame to each of the commands in cmd_list
557 for (i
= 0; i
< max_cmd
; i
++) {
558 cmd
= fusion
->cmd_list
[i
];
559 cmd
->sg_frame
= dma_pool_alloc(fusion
->sg_dma_pool
,
560 GFP_KERNEL
, &cmd
->sg_frame_phys_addr
);
562 offset
= SCSI_SENSE_BUFFERSIZE
* i
;
563 cmd
->sense
= (u8
*)fusion
->sense
+ offset
;
564 cmd
->sense_phys_addr
= fusion
->sense_phys_addr
+ offset
;
566 if (!cmd
->sg_frame
) {
567 dev_err(&instance
->pdev
->dev
,
568 "Failed from %s %d\n", __func__
, __LINE__
);
573 /* create sense buffer for the raid 1/10 fp */
574 for (i
= max_cmd
; i
< instance
->max_mpt_cmds
; i
++) {
575 cmd
= fusion
->cmd_list
[i
];
576 offset
= SCSI_SENSE_BUFFERSIZE
* i
;
577 cmd
->sense
= (u8
*)fusion
->sense
+ offset
;
578 cmd
->sense_phys_addr
= fusion
->sense_phys_addr
+ offset
;
586 megasas_alloc_cmdlist_fusion(struct megasas_instance
*instance
)
588 u32 max_mpt_cmd
, i
, j
;
589 struct fusion_context
*fusion
;
591 fusion
= instance
->ctrl_context
;
593 max_mpt_cmd
= instance
->max_mpt_cmds
;
596 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
597 * Allocate the dynamic array first and then allocate individual
601 kcalloc(max_mpt_cmd
, sizeof(struct megasas_cmd_fusion
*),
603 if (!fusion
->cmd_list
) {
604 dev_err(&instance
->pdev
->dev
,
605 "Failed from %s %d\n", __func__
, __LINE__
);
609 for (i
= 0; i
< max_mpt_cmd
; i
++) {
610 fusion
->cmd_list
[i
] = kzalloc(sizeof(struct megasas_cmd_fusion
),
612 if (!fusion
->cmd_list
[i
]) {
613 for (j
= 0; j
< i
; j
++)
614 kfree(fusion
->cmd_list
[j
]);
615 kfree(fusion
->cmd_list
);
616 dev_err(&instance
->pdev
->dev
,
617 "Failed from %s %d\n", __func__
, __LINE__
);
626 megasas_alloc_request_fusion(struct megasas_instance
*instance
)
628 struct fusion_context
*fusion
;
630 fusion
= instance
->ctrl_context
;
633 fusion
->io_request_frames_pool
=
634 dma_pool_create("mr_ioreq", &instance
->pdev
->dev
,
635 fusion
->io_frames_alloc_sz
, 16, 0);
637 if (!fusion
->io_request_frames_pool
) {
638 dev_err(&instance
->pdev
->dev
,
639 "Failed from %s %d\n", __func__
, __LINE__
);
643 fusion
->io_request_frames
=
644 dma_pool_alloc(fusion
->io_request_frames_pool
,
645 GFP_KERNEL
| __GFP_NOWARN
,
646 &fusion
->io_request_frames_phys
);
647 if (!fusion
->io_request_frames
) {
648 if (instance
->max_fw_cmds
>= (MEGASAS_REDUCE_QD_COUNT
* 2)) {
649 instance
->max_fw_cmds
-= MEGASAS_REDUCE_QD_COUNT
;
650 dma_pool_destroy(fusion
->io_request_frames_pool
);
651 megasas_configure_queue_sizes(instance
);
654 dev_err(&instance
->pdev
->dev
,
655 "Failed from %s %d\n", __func__
, __LINE__
);
660 if (!megasas_check_same_4gb_region(instance
,
661 fusion
->io_request_frames_phys
,
662 fusion
->io_frames_alloc_sz
)) {
663 dma_pool_free(fusion
->io_request_frames_pool
,
664 fusion
->io_request_frames
,
665 fusion
->io_request_frames_phys
);
666 fusion
->io_request_frames
= NULL
;
667 dma_pool_destroy(fusion
->io_request_frames_pool
);
669 fusion
->io_request_frames_pool
=
670 dma_pool_create("mr_ioreq_align",
671 &instance
->pdev
->dev
,
672 fusion
->io_frames_alloc_sz
,
673 roundup_pow_of_two(fusion
->io_frames_alloc_sz
),
676 if (!fusion
->io_request_frames_pool
) {
677 dev_err(&instance
->pdev
->dev
,
678 "Failed from %s %d\n", __func__
, __LINE__
);
682 fusion
->io_request_frames
=
683 dma_pool_alloc(fusion
->io_request_frames_pool
,
684 GFP_KERNEL
| __GFP_NOWARN
,
685 &fusion
->io_request_frames_phys
);
687 if (!fusion
->io_request_frames
) {
688 dev_err(&instance
->pdev
->dev
,
689 "Failed from %s %d\n", __func__
, __LINE__
);
694 fusion
->req_frames_desc
=
695 dma_alloc_coherent(&instance
->pdev
->dev
,
696 fusion
->request_alloc_sz
,
697 &fusion
->req_frames_desc_phys
, GFP_KERNEL
);
698 if (!fusion
->req_frames_desc
) {
699 dev_err(&instance
->pdev
->dev
,
700 "Failed from %s %d\n", __func__
, __LINE__
);
708 megasas_alloc_reply_fusion(struct megasas_instance
*instance
)
711 struct fusion_context
*fusion
;
712 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
713 fusion
= instance
->ctrl_context
;
715 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
716 count
+= instance
->iopoll_q_count
;
718 fusion
->reply_frames_desc_pool
=
719 dma_pool_create("mr_reply", &instance
->pdev
->dev
,
720 fusion
->reply_alloc_sz
* count
, 16, 0);
722 if (!fusion
->reply_frames_desc_pool
) {
723 dev_err(&instance
->pdev
->dev
,
724 "Failed from %s %d\n", __func__
, __LINE__
);
728 fusion
->reply_frames_desc
[0] =
729 dma_pool_alloc(fusion
->reply_frames_desc_pool
,
730 GFP_KERNEL
, &fusion
->reply_frames_desc_phys
[0]);
731 if (!fusion
->reply_frames_desc
[0]) {
732 dev_err(&instance
->pdev
->dev
,
733 "Failed from %s %d\n", __func__
, __LINE__
);
737 if (!megasas_check_same_4gb_region(instance
,
738 fusion
->reply_frames_desc_phys
[0],
739 (fusion
->reply_alloc_sz
* count
))) {
740 dma_pool_free(fusion
->reply_frames_desc_pool
,
741 fusion
->reply_frames_desc
[0],
742 fusion
->reply_frames_desc_phys
[0]);
743 fusion
->reply_frames_desc
[0] = NULL
;
744 dma_pool_destroy(fusion
->reply_frames_desc_pool
);
746 fusion
->reply_frames_desc_pool
=
747 dma_pool_create("mr_reply_align",
748 &instance
->pdev
->dev
,
749 fusion
->reply_alloc_sz
* count
,
750 roundup_pow_of_two(fusion
->reply_alloc_sz
* count
),
753 if (!fusion
->reply_frames_desc_pool
) {
754 dev_err(&instance
->pdev
->dev
,
755 "Failed from %s %d\n", __func__
, __LINE__
);
759 fusion
->reply_frames_desc
[0] =
760 dma_pool_alloc(fusion
->reply_frames_desc_pool
,
762 &fusion
->reply_frames_desc_phys
[0]);
764 if (!fusion
->reply_frames_desc
[0]) {
765 dev_err(&instance
->pdev
->dev
,
766 "Failed from %s %d\n", __func__
, __LINE__
);
771 reply_desc
= fusion
->reply_frames_desc
[0];
772 for (i
= 0; i
< fusion
->reply_q_depth
* count
; i
++, reply_desc
++)
773 reply_desc
->Words
= cpu_to_le64(ULLONG_MAX
);
775 /* This is not a rdpq mode, but driver still populate
776 * reply_frame_desc array to use same msix index in ISR path.
778 for (i
= 0; i
< (count
- 1); i
++)
779 fusion
->reply_frames_desc
[i
+ 1] =
780 fusion
->reply_frames_desc
[i
] +
781 (fusion
->reply_alloc_sz
)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
);
787 megasas_alloc_rdpq_fusion(struct megasas_instance
*instance
)
789 int i
, j
, k
, msix_count
;
790 struct fusion_context
*fusion
;
791 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
792 union MPI2_REPLY_DESCRIPTORS_UNION
*rdpq_chunk_virt
[RDPQ_MAX_CHUNK_COUNT
];
793 dma_addr_t rdpq_chunk_phys
[RDPQ_MAX_CHUNK_COUNT
];
794 u8 dma_alloc_count
, abs_index
;
795 u32 chunk_size
, array_size
, offset
;
797 fusion
= instance
->ctrl_context
;
798 chunk_size
= fusion
->reply_alloc_sz
* RDPQ_MAX_INDEX_IN_ONE_CHUNK
;
799 array_size
= sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY
) *
800 MAX_MSIX_QUEUES_FUSION
;
802 fusion
->rdpq_virt
= dma_alloc_coherent(&instance
->pdev
->dev
,
803 array_size
, &fusion
->rdpq_phys
,
805 if (!fusion
->rdpq_virt
) {
806 dev_err(&instance
->pdev
->dev
,
807 "Failed from %s %d\n", __func__
, __LINE__
);
811 msix_count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
812 msix_count
+= instance
->iopoll_q_count
;
814 fusion
->reply_frames_desc_pool
= dma_pool_create("mr_rdpq",
815 &instance
->pdev
->dev
,
817 fusion
->reply_frames_desc_pool_align
=
818 dma_pool_create("mr_rdpq_align",
819 &instance
->pdev
->dev
,
821 roundup_pow_of_two(chunk_size
),
824 if (!fusion
->reply_frames_desc_pool
||
825 !fusion
->reply_frames_desc_pool_align
) {
826 dev_err(&instance
->pdev
->dev
,
827 "Failed from %s %d\n", __func__
, __LINE__
);
832 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
833 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
834 * within 4GB boundary and also reply queues in a set must have same
835 * upper 32-bits in their memory address. so here driver is allocating the
836 * DMA'able memory for reply queues according. Driver uses limitation of
837 * VENTURA_SERIES to manage INVADER_SERIES as well.
839 dma_alloc_count
= DIV_ROUND_UP(msix_count
, RDPQ_MAX_INDEX_IN_ONE_CHUNK
);
841 for (i
= 0; i
< dma_alloc_count
; i
++) {
843 dma_pool_alloc(fusion
->reply_frames_desc_pool
,
844 GFP_KERNEL
, &rdpq_chunk_phys
[i
]);
845 if (!rdpq_chunk_virt
[i
]) {
846 dev_err(&instance
->pdev
->dev
,
847 "Failed from %s %d\n", __func__
, __LINE__
);
850 /* reply desc pool requires to be in same 4 gb region.
851 * Below function will check this.
852 * In case of failure, new pci pool will be created with updated
854 * For RDPQ buffers, driver always allocate two separate pci pool.
855 * Alignment will be used such a way that next allocation if
856 * success, will always meet same 4gb region requirement.
857 * rdpq_tracker keep track of each buffer's physical,
858 * virtual address and pci pool descriptor. It will help driver
859 * while freeing the resources.
862 if (!megasas_check_same_4gb_region(instance
, rdpq_chunk_phys
[i
],
864 dma_pool_free(fusion
->reply_frames_desc_pool
,
869 dma_pool_alloc(fusion
->reply_frames_desc_pool_align
,
870 GFP_KERNEL
, &rdpq_chunk_phys
[i
]);
871 if (!rdpq_chunk_virt
[i
]) {
872 dev_err(&instance
->pdev
->dev
,
873 "Failed from %s %d\n",
877 fusion
->rdpq_tracker
[i
].dma_pool_ptr
=
878 fusion
->reply_frames_desc_pool_align
;
880 fusion
->rdpq_tracker
[i
].dma_pool_ptr
=
881 fusion
->reply_frames_desc_pool
;
884 fusion
->rdpq_tracker
[i
].pool_entry_phys
= rdpq_chunk_phys
[i
];
885 fusion
->rdpq_tracker
[i
].pool_entry_virt
= rdpq_chunk_virt
[i
];
888 for (k
= 0; k
< dma_alloc_count
; k
++) {
889 for (i
= 0; i
< RDPQ_MAX_INDEX_IN_ONE_CHUNK
; i
++) {
890 abs_index
= (k
* RDPQ_MAX_INDEX_IN_ONE_CHUNK
) + i
;
892 if (abs_index
== msix_count
)
894 offset
= fusion
->reply_alloc_sz
* i
;
895 fusion
->rdpq_virt
[abs_index
].RDPQBaseAddress
=
896 cpu_to_le64(rdpq_chunk_phys
[k
] + offset
);
897 fusion
->reply_frames_desc_phys
[abs_index
] =
898 rdpq_chunk_phys
[k
] + offset
;
899 fusion
->reply_frames_desc
[abs_index
] =
900 (union MPI2_REPLY_DESCRIPTORS_UNION
*)((u8
*)rdpq_chunk_virt
[k
] + offset
);
902 reply_desc
= fusion
->reply_frames_desc
[abs_index
];
903 for (j
= 0; j
< fusion
->reply_q_depth
; j
++, reply_desc
++)
904 reply_desc
->Words
= ULLONG_MAX
;
912 megasas_free_rdpq_fusion(struct megasas_instance
*instance
) {
915 struct fusion_context
*fusion
;
917 fusion
= instance
->ctrl_context
;
919 for (i
= 0; i
< RDPQ_MAX_CHUNK_COUNT
; i
++) {
920 if (fusion
->rdpq_tracker
[i
].pool_entry_virt
)
921 dma_pool_free(fusion
->rdpq_tracker
[i
].dma_pool_ptr
,
922 fusion
->rdpq_tracker
[i
].pool_entry_virt
,
923 fusion
->rdpq_tracker
[i
].pool_entry_phys
);
927 dma_pool_destroy(fusion
->reply_frames_desc_pool
);
928 dma_pool_destroy(fusion
->reply_frames_desc_pool_align
);
930 if (fusion
->rdpq_virt
)
931 dma_free_coherent(&instance
->pdev
->dev
,
932 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY
) * MAX_MSIX_QUEUES_FUSION
,
933 fusion
->rdpq_virt
, fusion
->rdpq_phys
);
937 megasas_free_reply_fusion(struct megasas_instance
*instance
) {
939 struct fusion_context
*fusion
;
941 fusion
= instance
->ctrl_context
;
943 if (fusion
->reply_frames_desc
[0])
944 dma_pool_free(fusion
->reply_frames_desc_pool
,
945 fusion
->reply_frames_desc
[0],
946 fusion
->reply_frames_desc_phys
[0]);
948 dma_pool_destroy(fusion
->reply_frames_desc_pool
);
954 * megasas_alloc_cmds_fusion - Allocates the command packets
955 * @instance: Adapter soft state
958 * Each frame has a 32-bit field called context. This context is used to get
959 * back the megasas_cmd_fusion from the frame when a frame gets completed
960 * In this driver, the 32 bit values are the indices into an array cmd_list.
961 * This array is used only to look up the megasas_cmd_fusion given the context.
962 * The free commands themselves are maintained in a linked list called cmd_pool.
964 * cmds are formed in the io_request and sg_frame members of the
965 * megasas_cmd_fusion. The context field is used to get a request descriptor
966 * and is used as SMID of the cmd.
967 * SMID value range is from 1 to max_fw_cmds.
970 megasas_alloc_cmds_fusion(struct megasas_instance
*instance
)
973 struct fusion_context
*fusion
;
974 struct megasas_cmd_fusion
*cmd
;
976 dma_addr_t io_req_base_phys
;
980 fusion
= instance
->ctrl_context
;
982 if (megasas_alloc_request_fusion(instance
))
985 if (instance
->is_rdpq
) {
986 if (megasas_alloc_rdpq_fusion(instance
))
989 if (megasas_alloc_reply_fusion(instance
))
992 if (megasas_alloc_cmdlist_fusion(instance
))
995 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
996 io_req_base
= fusion
->io_request_frames
+ MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
997 io_req_base_phys
= fusion
->io_request_frames_phys
+ MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
;
1000 * Add all the commands to command pool (fusion->cmd_pool)
1003 /* SMID 0 is reserved. Set SMID/index from 1 */
1004 for (i
= 0; i
< instance
->max_mpt_cmds
; i
++) {
1005 cmd
= fusion
->cmd_list
[i
];
1006 offset
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
* i
;
1007 memset(cmd
, 0, sizeof(struct megasas_cmd_fusion
));
1011 (i
>= instance
->max_scsi_cmds
&& i
< instance
->max_fw_cmds
) ?
1012 (i
- instance
->max_scsi_cmds
) :
1013 (u32
)ULONG_MAX
; /* Set to Invalid */
1014 cmd
->instance
= instance
;
1016 (struct MPI2_RAID_SCSI_IO_REQUEST
*)
1017 (io_req_base
+ offset
);
1018 memset(cmd
->io_request
, 0,
1019 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
));
1020 cmd
->io_request_phys_addr
= io_req_base_phys
+ offset
;
1021 cmd
->r1_alt_dev_handle
= MR_DEVHANDLE_INVALID
;
1024 if (megasas_create_sg_sense_fusion(instance
))
1030 megasas_free_cmds_fusion(instance
);
1035 * wait_and_poll - Issues a polling command
1036 * @instance: Adapter soft state
1037 * @cmd: Command packet to be issued
1038 * @seconds: Maximum poll time
1040 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
1043 wait_and_poll(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
,
1047 struct megasas_header
*frame_hdr
= &cmd
->frame
->hdr
;
1050 u32 msecs
= seconds
* 1000;
1053 * Wait for cmd_status to change
1055 for (i
= 0; (i
< msecs
) && (frame_hdr
->cmd_status
== 0xff); i
+= 20) {
1059 status_reg
= instance
->instancet
->read_fw_status_reg(instance
)
1061 if (status_reg
== MFI_STATE_FAULT
)
1066 if (frame_hdr
->cmd_status
== MFI_STAT_INVALID_STATUS
)
1067 return DCMD_TIMEOUT
;
1068 else if (frame_hdr
->cmd_status
== MFI_STAT_OK
)
1069 return DCMD_SUCCESS
;
1075 * megasas_ioc_init_fusion - Initializes the FW
1076 * @instance: Adapter soft state
1078 * Issues the IOC Init cmd
1081 megasas_ioc_init_fusion(struct megasas_instance
*instance
)
1083 struct megasas_init_frame
*init_frame
;
1084 struct MPI2_IOC_INIT_REQUEST
*IOCInitMessage
= NULL
;
1085 dma_addr_t ioc_init_handle
;
1086 struct megasas_cmd
*cmd
;
1087 u8 ret
, cur_rdpq_mode
;
1088 struct fusion_context
*fusion
;
1089 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc
;
1091 struct megasas_header
*frame_hdr
;
1092 const char *sys_info
;
1093 MFI_CAPABILITIES
*drv_ops
;
1096 bool cur_fw_64bit_dma_capable
;
1097 bool cur_intr_coalescing
;
1099 fusion
= instance
->ctrl_context
;
1101 ioc_init_handle
= fusion
->ioc_init_request_phys
;
1102 IOCInitMessage
= fusion
->ioc_init_request
;
1104 cmd
= fusion
->ioc_init_cmd
;
1106 scratch_pad_1
= megasas_readl
1107 (instance
, &instance
->reg_set
->outbound_scratch_pad_1
);
1109 cur_rdpq_mode
= (scratch_pad_1
& MR_RDPQ_MODE_OFFSET
) ? 1 : 0;
1111 if (instance
->adapter_type
== INVADER_SERIES
) {
1112 cur_fw_64bit_dma_capable
=
1113 (scratch_pad_1
& MR_CAN_HANDLE_64_BIT_DMA_OFFSET
) ? true : false;
1115 if (instance
->consistent_mask_64bit
&& !cur_fw_64bit_dma_capable
) {
1116 dev_err(&instance
->pdev
->dev
, "Driver was operating on 64bit "
1117 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1118 megaraid_sas_kill_hba(instance
);
1124 if (instance
->is_rdpq
&& !cur_rdpq_mode
) {
1125 dev_err(&instance
->pdev
->dev
, "Firmware downgrade *NOT SUPPORTED*"
1126 " from RDPQ mode to non RDPQ mode\n");
1131 cur_intr_coalescing
= (scratch_pad_1
& MR_INTR_COALESCING_SUPPORT_OFFSET
) ?
1134 if ((instance
->low_latency_index_start
==
1135 MR_HIGH_IOPS_QUEUE_COUNT
) && cur_intr_coalescing
)
1136 instance
->perf_mode
= MR_BALANCED_PERF_MODE
;
1138 dev_info(&instance
->pdev
->dev
, "Performance mode :%s (latency index = %d)\n",
1139 MEGASAS_PERF_MODE_2STR(instance
->perf_mode
),
1140 instance
->low_latency_index_start
);
1142 instance
->fw_sync_cache_support
= (scratch_pad_1
&
1143 MR_CAN_HANDLE_SYNC_CACHE_OFFSET
) ? 1 : 0;
1144 dev_info(&instance
->pdev
->dev
, "FW supports sync cache\t: %s\n",
1145 instance
->fw_sync_cache_support
? "Yes" : "No");
1147 memset(IOCInitMessage
, 0, sizeof(struct MPI2_IOC_INIT_REQUEST
));
1149 IOCInitMessage
->Function
= MPI2_FUNCTION_IOC_INIT
;
1150 IOCInitMessage
->WhoInit
= MPI2_WHOINIT_HOST_DRIVER
;
1151 IOCInitMessage
->MsgVersion
= cpu_to_le16(MPI2_VERSION
);
1152 IOCInitMessage
->HeaderVersion
= cpu_to_le16(MPI2_HEADER_VERSION
);
1153 IOCInitMessage
->SystemRequestFrameSize
= cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
/ 4);
1155 IOCInitMessage
->ReplyDescriptorPostQueueDepth
= cpu_to_le16(fusion
->reply_q_depth
);
1156 IOCInitMessage
->ReplyDescriptorPostQueueAddress
= instance
->is_rdpq
?
1157 cpu_to_le64(fusion
->rdpq_phys
) :
1158 cpu_to_le64(fusion
->reply_frames_desc_phys
[0]);
1159 IOCInitMessage
->MsgFlags
= instance
->is_rdpq
?
1160 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE
: 0;
1161 IOCInitMessage
->SystemRequestFrameBaseAddress
= cpu_to_le64(fusion
->io_request_frames_phys
);
1162 IOCInitMessage
->SenseBufferAddressHigh
= cpu_to_le32(upper_32_bits(fusion
->sense_phys_addr
));
1163 IOCInitMessage
->HostMSIxVectors
= instance
->msix_vectors
+ instance
->iopoll_q_count
;
1164 IOCInitMessage
->HostPageSize
= MR_DEFAULT_NVME_PAGE_SHIFT
;
1166 time
= ktime_get_real();
1167 /* Convert to milliseconds as per FW requirement */
1168 IOCInitMessage
->TimeStamp
= cpu_to_le64(ktime_to_ms(time
));
1170 init_frame
= (struct megasas_init_frame
*)cmd
->frame
;
1171 memset(init_frame
, 0, IOC_INIT_FRAME_SIZE
);
1173 frame_hdr
= &cmd
->frame
->hdr
;
1174 frame_hdr
->cmd_status
= 0xFF;
1175 frame_hdr
->flags
|= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
);
1177 init_frame
->cmd
= MFI_CMD_INIT
;
1178 init_frame
->cmd_status
= 0xFF;
1180 drv_ops
= (MFI_CAPABILITIES
*) &(init_frame
->driver_operations
);
1182 /* driver support Extended MSIX */
1183 if (instance
->adapter_type
>= INVADER_SERIES
)
1184 drv_ops
->mfi_capabilities
.support_additional_msix
= 1;
1185 /* driver supports HA / Remote LUN over Fast Path interface */
1186 drv_ops
->mfi_capabilities
.support_fp_remote_lun
= 1;
1188 drv_ops
->mfi_capabilities
.support_max_255lds
= 1;
1189 drv_ops
->mfi_capabilities
.support_ndrive_r1_lb
= 1;
1190 drv_ops
->mfi_capabilities
.security_protocol_cmds_fw
= 1;
1192 if (instance
->max_chain_frame_sz
> MEGASAS_CHAIN_FRAME_SZ_MIN
)
1193 drv_ops
->mfi_capabilities
.support_ext_io_size
= 1;
1195 drv_ops
->mfi_capabilities
.support_fp_rlbypass
= 1;
1196 if (!dual_qdepth_disable
)
1197 drv_ops
->mfi_capabilities
.support_ext_queue_depth
= 1;
1199 drv_ops
->mfi_capabilities
.support_qd_throttling
= 1;
1200 drv_ops
->mfi_capabilities
.support_pd_map_target_id
= 1;
1201 drv_ops
->mfi_capabilities
.support_nvme_passthru
= 1;
1202 drv_ops
->mfi_capabilities
.support_fw_exposed_dev_list
= 1;
1205 drv_ops
->mfi_capabilities
.support_memdump
= 1;
1207 if (instance
->consistent_mask_64bit
)
1208 drv_ops
->mfi_capabilities
.support_64bit_mode
= 1;
1210 /* Convert capability to LE32 */
1211 cpu_to_le32s((u32
*)&init_frame
->driver_operations
.mfi_capabilities
);
1213 sys_info
= dmi_get_system_info(DMI_PRODUCT_UUID
);
1214 if (instance
->system_info_buf
&& sys_info
) {
1215 memcpy(instance
->system_info_buf
->systemId
, sys_info
,
1216 strlen(sys_info
) > 64 ? 64 : strlen(sys_info
));
1217 instance
->system_info_buf
->systemIdLength
=
1218 strlen(sys_info
) > 64 ? 64 : strlen(sys_info
);
1219 init_frame
->system_info_lo
= cpu_to_le32(lower_32_bits(instance
->system_info_h
));
1220 init_frame
->system_info_hi
= cpu_to_le32(upper_32_bits(instance
->system_info_h
));
1223 init_frame
->queue_info_new_phys_addr_hi
=
1224 cpu_to_le32(upper_32_bits(ioc_init_handle
));
1225 init_frame
->queue_info_new_phys_addr_lo
=
1226 cpu_to_le32(lower_32_bits(ioc_init_handle
));
1227 init_frame
->data_xfer_len
= cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST
));
1230 * Each bit in replyqueue_mask represents one group of MSI-x vectors
1231 * (each group has 8 vectors)
1233 switch (instance
->perf_mode
) {
1234 case MR_BALANCED_PERF_MODE
:
1235 init_frame
->replyqueue_mask
=
1236 cpu_to_le16(~(~0 << instance
->low_latency_index_start
/8));
1238 case MR_IOPS_PERF_MODE
:
1239 init_frame
->replyqueue_mask
=
1240 cpu_to_le16(~(~0 << instance
->msix_vectors
/8));
1245 req_desc
.u
.low
= cpu_to_le32(lower_32_bits(cmd
->frame_phys_addr
));
1246 req_desc
.u
.high
= cpu_to_le32(upper_32_bits(cmd
->frame_phys_addr
));
1247 req_desc
.MFAIo
.RequestFlags
=
1248 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA
<<
1249 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
1252 * disable the intr before firing the init frame
1254 instance
->instancet
->disable_intr(instance
);
1256 for (i
= 0; i
< (10 * 1000); i
+= 20) {
1257 if (megasas_readl(instance
, &instance
->reg_set
->doorbell
) & 1)
1263 /* For AERO also, IOC_INIT requires 64 bit descriptor write */
1264 megasas_write_64bit_req_desc(instance
, &req_desc
);
1266 wait_and_poll(instance
, cmd
, MFI_IO_TIMEOUT_SECS
);
1268 frame_hdr
= &cmd
->frame
->hdr
;
1269 if (frame_hdr
->cmd_status
!= 0) {
1274 if (instance
->adapter_type
>= AERO_SERIES
) {
1275 scratch_pad_1
= megasas_readl
1276 (instance
, &instance
->reg_set
->outbound_scratch_pad_1
);
1278 instance
->atomic_desc_support
=
1279 (scratch_pad_1
& MR_ATOMIC_DESCRIPTOR_SUPPORT_OFFSET
) ? 1 : 0;
1281 dev_info(&instance
->pdev
->dev
, "FW supports atomic descriptor\t: %s\n",
1282 instance
->atomic_desc_support
? "Yes" : "No");
1288 dev_err(&instance
->pdev
->dev
,
1289 "Init cmd return status FAILED for SCSI host %d\n",
1290 instance
->host
->host_no
);
1296 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1297 * @instance: Adapter soft state
1298 * @pend: set to 1, if it is pended jbod map.
1300 * Issue Jbod map to the firmware. If it is pended command,
1301 * issue command and return. If it is first instance of jbod map
1302 * issue and receive command.
1305 megasas_sync_pd_seq_num(struct megasas_instance
*instance
, bool pend
) {
1307 size_t pd_seq_map_sz
;
1308 struct megasas_cmd
*cmd
;
1309 struct megasas_dcmd_frame
*dcmd
;
1310 struct fusion_context
*fusion
= instance
->ctrl_context
;
1311 struct MR_PD_CFG_SEQ_NUM_SYNC
*pd_sync
;
1312 dma_addr_t pd_seq_h
;
1314 pd_sync
= (void *)fusion
->pd_seq_sync
[(instance
->pd_seq_map_id
& 1)];
1315 pd_seq_h
= fusion
->pd_seq_phys
[(instance
->pd_seq_map_id
& 1)];
1316 pd_seq_map_sz
= struct_size(pd_sync
, seq
, MAX_PHYSICAL_DEVICES
);
1318 cmd
= megasas_get_cmd(instance
);
1320 dev_err(&instance
->pdev
->dev
,
1321 "Could not get mfi cmd. Fail from %s %d\n",
1322 __func__
, __LINE__
);
1326 dcmd
= &cmd
->frame
->dcmd
;
1328 memset(pd_sync
, 0, pd_seq_map_sz
);
1329 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1332 dcmd
->mbox
.b
[0] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
1333 dcmd
->flags
= MFI_FRAME_DIR_WRITE
;
1334 instance
->jbod_seq_cmd
= cmd
;
1336 dcmd
->flags
= MFI_FRAME_DIR_READ
;
1339 dcmd
->cmd
= MFI_CMD_DCMD
;
1340 dcmd
->cmd_status
= 0xFF;
1341 dcmd
->sge_count
= 1;
1344 dcmd
->data_xfer_len
= cpu_to_le32(pd_seq_map_sz
);
1345 dcmd
->opcode
= cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO
);
1347 megasas_set_dma_settings(instance
, dcmd
, pd_seq_h
, pd_seq_map_sz
);
1350 instance
->instancet
->issue_dcmd(instance
, cmd
);
1354 /* Below code is only for non pended DCMD */
1355 if (!instance
->mask_interrupts
)
1356 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
1357 MFI_IO_TIMEOUT_SECS
);
1359 ret
= megasas_issue_polled(instance
, cmd
);
1361 if (le32_to_cpu(pd_sync
->count
) > MAX_PHYSICAL_DEVICES
) {
1362 dev_warn(&instance
->pdev
->dev
,
1363 "driver supports max %d JBOD, but FW reports %d\n",
1364 MAX_PHYSICAL_DEVICES
, le32_to_cpu(pd_sync
->count
));
1368 if (ret
== DCMD_TIMEOUT
)
1369 dev_warn(&instance
->pdev
->dev
,
1370 "%s DCMD timed out, continue without JBOD sequence map\n",
1373 if (ret
== DCMD_SUCCESS
)
1374 instance
->pd_seq_map_id
++;
1376 megasas_return_cmd(instance
, cmd
);
1381 * megasas_get_ld_map_info - Returns FW's ld_map structure
1382 * @instance: Adapter soft state
1383 * @pend: Pend the command or not
1384 * Issues an internal command (DCMD) to get the FW's controller PD
1385 * list structure. This information is mainly used to find out SYSTEM
1386 * supported by the FW.
1387 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1388 * dcmd.mbox.b[0] - number of LDs being sync'd
1389 * dcmd.mbox.b[1] - 0 - complete command immediately.
1390 * - 1 - pend till config change
1391 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1392 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1393 * uses extended struct MR_FW_RAID_MAP_EXT
1396 megasas_get_ld_map_info(struct megasas_instance
*instance
)
1399 struct megasas_cmd
*cmd
;
1400 struct megasas_dcmd_frame
*dcmd
;
1402 dma_addr_t ci_h
= 0;
1404 struct fusion_context
*fusion
;
1406 cmd
= megasas_get_cmd(instance
);
1409 dev_printk(KERN_DEBUG
, &instance
->pdev
->dev
, "Failed to get cmd for map info\n");
1413 fusion
= instance
->ctrl_context
;
1416 megasas_return_cmd(instance
, cmd
);
1420 dcmd
= &cmd
->frame
->dcmd
;
1422 size_map_info
= fusion
->current_map_sz
;
1424 ci
= (void *) fusion
->ld_map
[(instance
->map_id
& 1)];
1425 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
& 1)];
1428 dev_printk(KERN_DEBUG
, &instance
->pdev
->dev
, "Failed to alloc mem for ld_map_info\n");
1429 megasas_return_cmd(instance
, cmd
);
1433 memset(ci
, 0, fusion
->max_map_sz
);
1434 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1435 dcmd
->cmd
= MFI_CMD_DCMD
;
1436 dcmd
->cmd_status
= 0xFF;
1437 dcmd
->sge_count
= 1;
1438 dcmd
->flags
= MFI_FRAME_DIR_READ
;
1441 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
1442 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
1444 megasas_set_dma_settings(instance
, dcmd
, ci_h
, size_map_info
);
1446 if (!instance
->mask_interrupts
)
1447 ret
= megasas_issue_blocked_cmd(instance
, cmd
,
1448 MFI_IO_TIMEOUT_SECS
);
1450 ret
= megasas_issue_polled(instance
, cmd
);
1452 if (ret
== DCMD_TIMEOUT
)
1453 dev_warn(&instance
->pdev
->dev
,
1454 "%s DCMD timed out, RAID map is disabled\n",
1457 megasas_return_cmd(instance
, cmd
);
1463 megasas_get_map_info(struct megasas_instance
*instance
)
1465 struct fusion_context
*fusion
= instance
->ctrl_context
;
1467 fusion
->fast_path_io
= 0;
1468 if (!megasas_get_ld_map_info(instance
)) {
1469 if (MR_ValidateMapInfo(instance
, instance
->map_id
)) {
1470 fusion
->fast_path_io
= 1;
1478 * megasas_sync_map_info - Returns FW's ld_map structure
1479 * @instance: Adapter soft state
1481 * Issues an internal command (DCMD) to get the FW's controller PD
1482 * list structure. This information is mainly used to find out SYSTEM
1483 * supported by the FW.
1486 megasas_sync_map_info(struct megasas_instance
*instance
)
1489 struct megasas_cmd
*cmd
;
1490 struct megasas_dcmd_frame
*dcmd
;
1492 struct fusion_context
*fusion
;
1493 struct MR_LD_TARGET_SYNC
*ci
= NULL
;
1494 struct MR_DRV_RAID_MAP_ALL
*map
;
1495 struct MR_LD_RAID
*raid
;
1496 struct MR_LD_TARGET_SYNC
*ld_sync
;
1497 dma_addr_t ci_h
= 0;
1500 cmd
= megasas_get_cmd(instance
);
1503 dev_printk(KERN_DEBUG
, &instance
->pdev
->dev
, "Failed to get cmd for sync info\n");
1507 fusion
= instance
->ctrl_context
;
1510 megasas_return_cmd(instance
, cmd
);
1514 map
= fusion
->ld_drv_map
[instance
->map_id
& 1];
1516 num_lds
= le16_to_cpu(map
->raidMap
.ldCount
);
1518 dcmd
= &cmd
->frame
->dcmd
;
1520 memset(dcmd
->mbox
.b
, 0, MFI_MBOX_SIZE
);
1522 ci
= (struct MR_LD_TARGET_SYNC
*)
1523 fusion
->ld_map
[(instance
->map_id
- 1) & 1];
1524 memset(ci
, 0, fusion
->max_map_sz
);
1526 ci_h
= fusion
->ld_map_phys
[(instance
->map_id
- 1) & 1];
1528 ld_sync
= (struct MR_LD_TARGET_SYNC
*)ci
;
1530 for (i
= 0; i
< num_lds
; i
++, ld_sync
++) {
1531 raid
= MR_LdRaidGet(i
, map
);
1532 ld_sync
->targetId
= MR_GetLDTgtId(i
, map
);
1533 ld_sync
->seqNum
= raid
->seqNum
;
1536 size_map_info
= fusion
->current_map_sz
;
1538 dcmd
->cmd
= MFI_CMD_DCMD
;
1539 dcmd
->cmd_status
= 0xFF;
1540 dcmd
->sge_count
= 1;
1541 dcmd
->flags
= MFI_FRAME_DIR_WRITE
;
1544 dcmd
->data_xfer_len
= cpu_to_le32(size_map_info
);
1545 dcmd
->mbox
.b
[0] = num_lds
;
1546 dcmd
->mbox
.b
[1] = MEGASAS_DCMD_MBOX_PEND_FLAG
;
1547 dcmd
->opcode
= cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO
);
1549 megasas_set_dma_settings(instance
, dcmd
, ci_h
, size_map_info
);
1551 instance
->map_update_cmd
= cmd
;
1553 instance
->instancet
->issue_dcmd(instance
, cmd
);
1559 * meagasas_display_intel_branding - Display branding string
1560 * @instance: per adapter object
1565 megasas_display_intel_branding(struct megasas_instance
*instance
)
1567 if (instance
->pdev
->subsystem_vendor
!= PCI_VENDOR_ID_INTEL
)
1570 switch (instance
->pdev
->device
) {
1571 case PCI_DEVICE_ID_LSI_INVADER
:
1572 switch (instance
->pdev
->subsystem_device
) {
1573 case MEGARAID_INTEL_RS3DC080_SSDID
:
1574 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1575 instance
->host
->host_no
,
1576 MEGARAID_INTEL_RS3DC080_BRANDING
);
1578 case MEGARAID_INTEL_RS3DC040_SSDID
:
1579 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1580 instance
->host
->host_no
,
1581 MEGARAID_INTEL_RS3DC040_BRANDING
);
1583 case MEGARAID_INTEL_RS3SC008_SSDID
:
1584 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1585 instance
->host
->host_no
,
1586 MEGARAID_INTEL_RS3SC008_BRANDING
);
1588 case MEGARAID_INTEL_RS3MC044_SSDID
:
1589 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1590 instance
->host
->host_no
,
1591 MEGARAID_INTEL_RS3MC044_BRANDING
);
1597 case PCI_DEVICE_ID_LSI_FURY
:
1598 switch (instance
->pdev
->subsystem_device
) {
1599 case MEGARAID_INTEL_RS3WC080_SSDID
:
1600 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1601 instance
->host
->host_no
,
1602 MEGARAID_INTEL_RS3WC080_BRANDING
);
1604 case MEGARAID_INTEL_RS3WC040_SSDID
:
1605 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1606 instance
->host
->host_no
,
1607 MEGARAID_INTEL_RS3WC040_BRANDING
);
1613 case PCI_DEVICE_ID_LSI_CUTLASS_52
:
1614 case PCI_DEVICE_ID_LSI_CUTLASS_53
:
1615 switch (instance
->pdev
->subsystem_device
) {
1616 case MEGARAID_INTEL_RMS3BC160_SSDID
:
1617 dev_info(&instance
->pdev
->dev
, "scsi host %d: %s\n",
1618 instance
->host
->host_no
,
1619 MEGARAID_INTEL_RMS3BC160_BRANDING
);
1631 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1632 * @instance: Adapter soft state
1634 * return: if success: return 0
1635 * failed: return -ENOMEM
1637 static inline int megasas_allocate_raid_maps(struct megasas_instance
*instance
)
1639 struct fusion_context
*fusion
;
1642 fusion
= instance
->ctrl_context
;
1644 fusion
->drv_map_pages
= get_order(fusion
->drv_map_sz
);
1646 for (i
= 0; i
< 2; i
++) {
1647 fusion
->ld_map
[i
] = NULL
;
1649 fusion
->ld_drv_map
[i
] = (void *)
1650 __get_free_pages(__GFP_ZERO
| GFP_KERNEL
,
1651 fusion
->drv_map_pages
);
1653 if (!fusion
->ld_drv_map
[i
]) {
1654 fusion
->ld_drv_map
[i
] = vzalloc(fusion
->drv_map_sz
);
1656 if (!fusion
->ld_drv_map
[i
]) {
1657 dev_err(&instance
->pdev
->dev
,
1658 "Could not allocate memory for local map"
1659 " size requested: %d\n",
1660 fusion
->drv_map_sz
);
1661 goto ld_drv_map_alloc_fail
;
1666 for (i
= 0; i
< 2; i
++) {
1667 fusion
->ld_map
[i
] = dma_alloc_coherent(&instance
->pdev
->dev
,
1669 &fusion
->ld_map_phys
[i
],
1671 if (!fusion
->ld_map
[i
]) {
1672 dev_err(&instance
->pdev
->dev
,
1673 "Could not allocate memory for map info %s:%d\n",
1674 __func__
, __LINE__
);
1675 goto ld_map_alloc_fail
;
1682 for (i
= 0; i
< 2; i
++) {
1683 if (fusion
->ld_map
[i
])
1684 dma_free_coherent(&instance
->pdev
->dev
,
1687 fusion
->ld_map_phys
[i
]);
1690 ld_drv_map_alloc_fail
:
1691 for (i
= 0; i
< 2; i
++) {
1692 if (fusion
->ld_drv_map
[i
]) {
1693 if (is_vmalloc_addr(fusion
->ld_drv_map
[i
]))
1694 vfree(fusion
->ld_drv_map
[i
]);
1696 free_pages((ulong
)fusion
->ld_drv_map
[i
],
1697 fusion
->drv_map_pages
);
1705 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1707 * IO request frame queue, set can_queue.
1708 * @instance: Adapter soft state
1712 void megasas_configure_queue_sizes(struct megasas_instance
*instance
)
1714 struct fusion_context
*fusion
;
1717 fusion
= instance
->ctrl_context
;
1718 max_cmd
= instance
->max_fw_cmds
;
1720 if (instance
->adapter_type
>= VENTURA_SERIES
)
1721 instance
->max_mpt_cmds
= instance
->max_fw_cmds
* RAID_1_PEER_CMDS
;
1723 instance
->max_mpt_cmds
= instance
->max_fw_cmds
;
1725 instance
->max_scsi_cmds
= instance
->max_fw_cmds
- instance
->max_mfi_cmds
;
1726 instance
->cur_can_queue
= instance
->max_scsi_cmds
;
1727 instance
->host
->can_queue
= instance
->cur_can_queue
;
1729 fusion
->reply_q_depth
= 2 * ((max_cmd
+ 1 + 15) / 16) * 16;
1731 fusion
->request_alloc_sz
= sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) *
1732 instance
->max_mpt_cmds
;
1733 fusion
->reply_alloc_sz
= sizeof(union MPI2_REPLY_DESCRIPTORS_UNION
) *
1734 (fusion
->reply_q_depth
);
1735 fusion
->io_frames_alloc_sz
= MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
+
1736 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1737 * (instance
->max_mpt_cmds
+ 1)); /* Extra 1 for SMID 0 */
1740 static int megasas_alloc_ioc_init_frame(struct megasas_instance
*instance
)
1742 struct fusion_context
*fusion
;
1743 struct megasas_cmd
*cmd
;
1745 fusion
= instance
->ctrl_context
;
1747 cmd
= kzalloc(sizeof(struct megasas_cmd
), GFP_KERNEL
);
1750 dev_err(&instance
->pdev
->dev
, "Failed from func: %s line: %d\n",
1751 __func__
, __LINE__
);
1755 cmd
->frame
= dma_alloc_coherent(&instance
->pdev
->dev
,
1756 IOC_INIT_FRAME_SIZE
,
1757 &cmd
->frame_phys_addr
, GFP_KERNEL
);
1760 dev_err(&instance
->pdev
->dev
, "Failed from func: %s line: %d\n",
1761 __func__
, __LINE__
);
1766 fusion
->ioc_init_cmd
= cmd
;
1771 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1772 * @instance: Adapter soft state
1774 static inline void megasas_free_ioc_init_cmd(struct megasas_instance
*instance
)
1776 struct fusion_context
*fusion
;
1778 fusion
= instance
->ctrl_context
;
1780 if (fusion
->ioc_init_cmd
&& fusion
->ioc_init_cmd
->frame
)
1781 dma_free_coherent(&instance
->pdev
->dev
,
1782 IOC_INIT_FRAME_SIZE
,
1783 fusion
->ioc_init_cmd
->frame
,
1784 fusion
->ioc_init_cmd
->frame_phys_addr
);
1786 kfree(fusion
->ioc_init_cmd
);
1790 * megasas_init_adapter_fusion - Initializes the FW
1791 * @instance: Adapter soft state
1793 * This is the main function for initializing firmware.
1796 megasas_init_adapter_fusion(struct megasas_instance
*instance
)
1798 struct fusion_context
*fusion
;
1803 fusion
= instance
->ctrl_context
;
1805 megasas_fusion_update_can_queue(instance
, PROBE_CONTEXT
);
1808 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1810 instance
->max_mfi_cmds
=
1811 MEGASAS_FUSION_INTERNAL_CMDS
+ MEGASAS_FUSION_IOCTL_CMDS
;
1813 megasas_configure_queue_sizes(instance
);
1815 scratch_pad_1
= megasas_readl(instance
,
1816 &instance
->reg_set
->outbound_scratch_pad_1
);
1817 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1818 * Firmware support extended IO chain frame which is 4 times more than
1820 * Legacy Firmware - Frame size is (8 * 128) = 1K
1821 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1823 if (scratch_pad_1
& MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK
)
1824 instance
->max_chain_frame_sz
=
1825 ((scratch_pad_1
& MEGASAS_MAX_CHAIN_SIZE_MASK
) >>
1826 MEGASAS_MAX_CHAIN_SHIFT
) * MEGASAS_1MB_IO
;
1828 instance
->max_chain_frame_sz
=
1829 ((scratch_pad_1
& MEGASAS_MAX_CHAIN_SIZE_MASK
) >>
1830 MEGASAS_MAX_CHAIN_SHIFT
) * MEGASAS_256K_IO
;
1832 if (instance
->max_chain_frame_sz
< MEGASAS_CHAIN_FRAME_SZ_MIN
) {
1833 dev_warn(&instance
->pdev
->dev
, "frame size %d invalid, fall back to legacy max frame size %d\n",
1834 instance
->max_chain_frame_sz
,
1835 MEGASAS_CHAIN_FRAME_SZ_MIN
);
1836 instance
->max_chain_frame_sz
= MEGASAS_CHAIN_FRAME_SZ_MIN
;
1839 fusion
->max_sge_in_main_msg
=
1840 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1841 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
))/16;
1843 fusion
->max_sge_in_chain
=
1844 instance
->max_chain_frame_sz
1845 / sizeof(union MPI2_SGE_IO_UNION
);
1847 instance
->max_num_sge
=
1848 rounddown_pow_of_two(fusion
->max_sge_in_main_msg
1849 + fusion
->max_sge_in_chain
- 2);
1851 /* Used for pass thru MFI frame (DCMD) */
1852 fusion
->chain_offset_mfi_pthru
=
1853 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
)/16;
1855 fusion
->chain_offset_io_request
=
1856 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
-
1857 sizeof(union MPI2_SGE_IO_UNION
))/16;
1859 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
1860 count
+= instance
->iopoll_q_count
;
1862 for (i
= 0 ; i
< count
; i
++)
1863 fusion
->last_reply_idx
[i
] = 0;
1866 * For fusion adapters, 3 commands for IOCTL and 8 commands
1867 * for driver's internal DCMDs.
1869 instance
->max_scsi_cmds
= instance
->max_fw_cmds
-
1870 (MEGASAS_FUSION_INTERNAL_CMDS
+
1871 MEGASAS_FUSION_IOCTL_CMDS
);
1872 sema_init(&instance
->ioctl_sem
, MEGASAS_FUSION_IOCTL_CMDS
);
1874 for (i
= 0; i
< MAX_MSIX_QUEUES_FUSION
; i
++)
1875 atomic_set(&fusion
->busy_mq_poll
[i
], 0);
1877 if (megasas_alloc_ioc_init_frame(instance
))
1881 * Allocate memory for descriptors
1882 * Create a pool of commands
1884 if (megasas_alloc_cmds(instance
))
1885 goto fail_alloc_mfi_cmds
;
1886 if (megasas_alloc_cmds_fusion(instance
))
1887 goto fail_alloc_cmds
;
1889 if (megasas_ioc_init_fusion(instance
)) {
1890 status_reg
= instance
->instancet
->read_fw_status_reg(instance
);
1891 if (((status_reg
& MFI_STATE_MASK
) == MFI_STATE_FAULT
) &&
1892 (status_reg
& MFI_RESET_ADAPTER
)) {
1893 /* Do a chip reset and then retry IOC INIT once */
1894 if (megasas_adp_reset_wait_for_ready
1895 (instance
, true, 0) == FAILED
)
1898 if (megasas_ioc_init_fusion(instance
))
1905 megasas_display_intel_branding(instance
);
1906 if (megasas_get_ctrl_info(instance
)) {
1907 dev_err(&instance
->pdev
->dev
,
1908 "Could not get controller info. Fail from %s %d\n",
1909 __func__
, __LINE__
);
1913 instance
->flag_ieee
= 1;
1914 instance
->r1_ldio_hint_default
= MR_R1_LDIO_PIGGYBACK_DEFAULT
;
1915 instance
->threshold_reply_count
= instance
->max_fw_cmds
/ 4;
1916 fusion
->fast_path_io
= 0;
1918 if (megasas_allocate_raid_maps(instance
))
1921 if (!megasas_get_map_info(instance
))
1922 megasas_sync_map_info(instance
);
1927 megasas_free_cmds_fusion(instance
);
1929 megasas_free_cmds(instance
);
1930 fail_alloc_mfi_cmds
:
1931 megasas_free_ioc_init_cmd(instance
);
1936 * megasas_fault_detect_work - Worker function of
1937 * FW fault handling workqueue.
1938 * @work: FW fault work struct
1941 megasas_fault_detect_work(struct work_struct
*work
)
1943 struct megasas_instance
*instance
=
1944 container_of(work
, struct megasas_instance
,
1945 fw_fault_work
.work
);
1946 u32 fw_state
, dma_state
, status
;
1948 /* Check the fw state */
1949 fw_state
= instance
->instancet
->read_fw_status_reg(instance
) &
1952 if (fw_state
== MFI_STATE_FAULT
) {
1953 dma_state
= instance
->instancet
->read_fw_status_reg(instance
) &
1955 /* Start collecting crash, if DMA bit is done */
1956 if (instance
->crash_dump_drv_support
&&
1957 instance
->crash_dump_app_support
&& dma_state
) {
1958 megasas_fusion_crash_dump(instance
);
1960 if (instance
->unload
== 0) {
1961 status
= megasas_reset_fusion(instance
->host
, 0);
1962 if (status
!= SUCCESS
) {
1963 dev_err(&instance
->pdev
->dev
,
1964 "Failed from %s %d, do not re-arm timer\n",
1965 __func__
, __LINE__
);
1972 if (instance
->fw_fault_work_q
)
1973 queue_delayed_work(instance
->fw_fault_work_q
,
1974 &instance
->fw_fault_work
,
1975 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL
));
1979 megasas_fusion_start_watchdog(struct megasas_instance
*instance
)
1981 /* Check if the Fault WQ is already started */
1982 if (instance
->fw_fault_work_q
)
1985 INIT_DELAYED_WORK(&instance
->fw_fault_work
, megasas_fault_detect_work
);
1987 snprintf(instance
->fault_handler_work_q_name
,
1988 sizeof(instance
->fault_handler_work_q_name
),
1989 "poll_megasas%d_status", instance
->host
->host_no
);
1991 instance
->fw_fault_work_q
= alloc_ordered_workqueue(
1992 "%s", WQ_MEM_RECLAIM
, instance
->fault_handler_work_q_name
);
1993 if (!instance
->fw_fault_work_q
) {
1994 dev_err(&instance
->pdev
->dev
, "Failed from %s %d\n",
1995 __func__
, __LINE__
);
1999 queue_delayed_work(instance
->fw_fault_work_q
,
2000 &instance
->fw_fault_work
,
2001 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL
));
2007 megasas_fusion_stop_watchdog(struct megasas_instance
*instance
)
2009 struct workqueue_struct
*wq
;
2011 if (instance
->fw_fault_work_q
) {
2012 wq
= instance
->fw_fault_work_q
;
2013 instance
->fw_fault_work_q
= NULL
;
2014 if (!cancel_delayed_work_sync(&instance
->fw_fault_work
))
2015 flush_workqueue(wq
);
2016 destroy_workqueue(wq
);
2021 * map_cmd_status - Maps FW cmd status to OS cmd status
2022 * @fusion: fusion context
2023 * @scmd: Pointer to cmd
2024 * @status: status of cmd returned by FW
2025 * @ext_status: ext status of cmd returned by FW
2026 * @data_length: command data length
2027 * @sense: command sense data
2030 map_cmd_status(struct fusion_context
*fusion
,
2031 struct scsi_cmnd
*scmd
, u8 status
, u8 ext_status
,
2032 u32 data_length
, u8
*sense
)
2037 cmd_type
= megasas_cmd_type(scmd
);
2041 scmd
->result
= DID_OK
<< 16;
2044 case MFI_STAT_SCSI_IO_FAILED
:
2045 case MFI_STAT_LD_INIT_IN_PROGRESS
:
2046 scmd
->result
= (DID_ERROR
<< 16) | ext_status
;
2049 case MFI_STAT_SCSI_DONE_WITH_ERROR
:
2051 scmd
->result
= (DID_OK
<< 16) | ext_status
;
2052 if (ext_status
== SAM_STAT_CHECK_CONDITION
) {
2053 memcpy(scmd
->sense_buffer
, sense
,
2054 SCSI_SENSE_BUFFERSIZE
);
2058 * If the IO request is partially completed, then MR FW will
2059 * update "io_request->DataLength" field with actual number of
2060 * bytes transferred.Driver will set residual bytes count in
2061 * SCSI command structure.
2063 resid
= (scsi_bufflen(scmd
) - data_length
);
2064 scsi_set_resid(scmd
, resid
);
2067 ((cmd_type
== READ_WRITE_LDIO
) ||
2068 (cmd_type
== READ_WRITE_SYSPDIO
)))
2069 scmd_printk(KERN_INFO
, scmd
, "BRCM Debug mfi stat 0x%x, data len"
2070 " requested/completed 0x%x/0x%x\n",
2071 status
, scsi_bufflen(scmd
), data_length
);
2074 case MFI_STAT_LD_OFFLINE
:
2075 case MFI_STAT_DEVICE_NOT_FOUND
:
2076 scmd
->result
= DID_BAD_TARGET
<< 16;
2078 case MFI_STAT_CONFIG_SEQ_MISMATCH
:
2079 scmd
->result
= DID_IMM_RETRY
<< 16;
2082 scmd
->result
= DID_ERROR
<< 16;
2088 * megasas_is_prp_possible -
2089 * Checks if native NVMe PRPs can be built for the IO
2091 * @instance: Adapter soft state
2092 * @scmd: SCSI command from the mid-layer
2093 * @sge_count: scatter gather element count.
2095 * Returns: true: PRPs can be built
2096 * false: IEEE SGLs needs to be built
2099 megasas_is_prp_possible(struct megasas_instance
*instance
,
2100 struct scsi_cmnd
*scmd
, int sge_count
)
2102 u32 data_length
= 0;
2103 struct scatterlist
*sg_scmd
;
2104 bool build_prp
= false;
2105 u32 mr_nvme_pg_size
;
2107 mr_nvme_pg_size
= max_t(u32
, instance
->nvme_page_size
,
2108 MR_DEFAULT_NVME_PAGE_SIZE
);
2109 data_length
= scsi_bufflen(scmd
);
2110 sg_scmd
= scsi_sglist(scmd
);
2113 * NVMe uses one PRP for each page (or part of a page)
2114 * look at the data length - if 4 pages or less then IEEE is OK
2115 * if > 5 pages then we need to build a native SGL
2116 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
2117 * if this first size in the page is >= the residual beyond 4 pages
2118 * then use IEEE, otherwise use native SGL
2121 if (data_length
> (mr_nvme_pg_size
* 5)) {
2123 } else if ((data_length
> (mr_nvme_pg_size
* 4)) &&
2124 (data_length
<= (mr_nvme_pg_size
* 5))) {
2125 /* check if 1st SG entry size is < residual beyond 4 pages */
2126 if (sg_dma_len(sg_scmd
) < (data_length
- (mr_nvme_pg_size
* 4)))
2134 * megasas_make_prp_nvme -
2135 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2137 * @instance: Adapter soft state
2138 * @scmd: SCSI command from the mid-layer
2139 * @sgl_ptr: SGL to be filled in
2140 * @cmd: Fusion command frame
2141 * @sge_count: scatter gather element count.
2143 * Returns: true: PRPs are built
2144 * false: IEEE SGLs needs to be built
2147 megasas_make_prp_nvme(struct megasas_instance
*instance
, struct scsi_cmnd
*scmd
,
2148 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
2149 struct megasas_cmd_fusion
*cmd
, int sge_count
)
2151 int sge_len
, offset
, num_prp_in_chain
= 0;
2152 struct MPI25_IEEE_SGE_CHAIN64
*main_chain_element
, *ptr_first_sgl
;
2154 dma_addr_t ptr_sgl_phys
;
2156 u32 page_mask
, page_mask_result
;
2157 struct scatterlist
*sg_scmd
;
2159 bool build_prp
= false;
2160 int data_len
= scsi_bufflen(scmd
);
2161 u32 mr_nvme_pg_size
= max_t(u32
, instance
->nvme_page_size
,
2162 MR_DEFAULT_NVME_PAGE_SIZE
);
2164 build_prp
= megasas_is_prp_possible(instance
, scmd
, sge_count
);
2170 * Nvme has a very convoluted prp format. One prp is required
2171 * for each page or partial page. Driver need to split up OS sg_list
2172 * entries if it is longer than one page or cross a page
2173 * boundary. Driver also have to insert a PRP list pointer entry as
2174 * the last entry in each physical page of the PRP list.
2176 * NOTE: The first PRP "entry" is actually placed in the first
2177 * SGL entry in the main message as IEEE 64 format. The 2nd
2178 * entry in the main message is the chain element, and the rest
2179 * of the PRP entries are built in the contiguous pcie buffer.
2181 page_mask
= mr_nvme_pg_size
- 1;
2182 ptr_sgl
= (u64
*)cmd
->sg_frame
;
2183 ptr_sgl_phys
= cmd
->sg_frame_phys_addr
;
2184 memset(ptr_sgl
, 0, instance
->max_chain_frame_sz
);
2186 /* Build chain frame element which holds all prps except first*/
2187 main_chain_element
= (struct MPI25_IEEE_SGE_CHAIN64
*)
2188 ((u8
*)sgl_ptr
+ sizeof(struct MPI25_IEEE_SGE_CHAIN64
));
2190 main_chain_element
->Address
= cpu_to_le64(ptr_sgl_phys
);
2191 main_chain_element
->NextChainOffset
= 0;
2192 main_chain_element
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
2193 IEEE_SGE_FLAGS_SYSTEM_ADDR
|
2194 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP
;
2196 /* Build first prp, sge need not to be page aligned*/
2197 ptr_first_sgl
= sgl_ptr
;
2198 sg_scmd
= scsi_sglist(scmd
);
2199 sge_addr
= sg_dma_address(sg_scmd
);
2200 sge_len
= sg_dma_len(sg_scmd
);
2202 offset
= (u32
)(sge_addr
& page_mask
);
2203 first_prp_len
= mr_nvme_pg_size
- offset
;
2205 ptr_first_sgl
->Address
= cpu_to_le64(sge_addr
);
2206 ptr_first_sgl
->Length
= cpu_to_le32(first_prp_len
);
2208 data_len
-= first_prp_len
;
2210 if (sge_len
> first_prp_len
) {
2211 sge_addr
+= first_prp_len
;
2212 sge_len
-= first_prp_len
;
2213 } else if (sge_len
== first_prp_len
) {
2214 sg_scmd
= sg_next(sg_scmd
);
2215 sge_addr
= sg_dma_address(sg_scmd
);
2216 sge_len
= sg_dma_len(sg_scmd
);
2220 offset
= (u32
)(sge_addr
& page_mask
);
2222 /* Put PRP pointer due to page boundary*/
2223 page_mask_result
= (uintptr_t)(ptr_sgl
+ 1) & page_mask
;
2224 if (unlikely(!page_mask_result
)) {
2225 scmd_printk(KERN_NOTICE
,
2226 scmd
, "page boundary ptr_sgl: 0x%p\n",
2229 *ptr_sgl
= cpu_to_le64(ptr_sgl_phys
);
2234 *ptr_sgl
= cpu_to_le64(sge_addr
);
2239 sge_addr
+= mr_nvme_pg_size
;
2240 sge_len
-= mr_nvme_pg_size
;
2241 data_len
-= mr_nvme_pg_size
;
2249 sg_scmd
= sg_next(sg_scmd
);
2250 sge_addr
= sg_dma_address(sg_scmd
);
2251 sge_len
= sg_dma_len(sg_scmd
);
2254 main_chain_element
->Length
=
2255 cpu_to_le32(num_prp_in_chain
* sizeof(u64
));
2261 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2262 * @instance: Adapter soft state
2263 * @scp: SCSI command from the mid-layer
2264 * @sgl_ptr: SGL to be filled in
2265 * @cmd: cmd we are working on
2266 * @sge_count: sge count
2270 megasas_make_sgl_fusion(struct megasas_instance
*instance
,
2271 struct scsi_cmnd
*scp
,
2272 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr
,
2273 struct megasas_cmd_fusion
*cmd
, int sge_count
)
2275 int i
, sg_processed
;
2276 struct scatterlist
*os_sgl
;
2277 struct fusion_context
*fusion
;
2279 fusion
= instance
->ctrl_context
;
2281 if (instance
->adapter_type
>= INVADER_SERIES
) {
2282 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
= sgl_ptr
;
2283 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
2284 sgl_ptr_end
->Flags
= 0;
2287 scsi_for_each_sg(scp
, os_sgl
, sge_count
, i
) {
2288 sgl_ptr
->Length
= cpu_to_le32(sg_dma_len(os_sgl
));
2289 sgl_ptr
->Address
= cpu_to_le64(sg_dma_address(os_sgl
));
2291 if (instance
->adapter_type
>= INVADER_SERIES
)
2292 if (i
== sge_count
- 1)
2293 sgl_ptr
->Flags
= IEEE_SGE_FLAGS_END_OF_LIST
;
2295 sg_processed
= i
+ 1;
2297 if ((sg_processed
== (fusion
->max_sge_in_main_msg
- 1)) &&
2298 (sge_count
> fusion
->max_sge_in_main_msg
)) {
2300 struct MPI25_IEEE_SGE_CHAIN64
*sg_chain
;
2301 if (instance
->adapter_type
>= INVADER_SERIES
) {
2302 if ((le16_to_cpu(cmd
->io_request
->IoFlags
) &
2303 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
) !=
2304 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
)
2305 cmd
->io_request
->ChainOffset
=
2307 chain_offset_io_request
;
2309 cmd
->io_request
->ChainOffset
= 0;
2311 cmd
->io_request
->ChainOffset
=
2312 fusion
->chain_offset_io_request
;
2315 /* Prepare chain element */
2316 sg_chain
->NextChainOffset
= 0;
2317 if (instance
->adapter_type
>= INVADER_SERIES
)
2318 sg_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
;
2321 (IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
2322 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
);
2323 sg_chain
->Length
= cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION
) * (sge_count
- sg_processed
)));
2324 sg_chain
->Address
= cpu_to_le64(cmd
->sg_frame_phys_addr
);
2327 (struct MPI25_IEEE_SGE_CHAIN64
*)cmd
->sg_frame
;
2328 memset(sgl_ptr
, 0, instance
->max_chain_frame_sz
);
2334 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2335 * @scp: SCSI command pointer
2336 * @instance: Soft instance of controller
2337 * @cmd: Fusion command pointer
2339 * This function will build sgls based on device type.
2340 * For nvme drives, there is different way of building sgls in nvme native
2341 * format- PRPs(Physical Region Page).
2343 * Returns the number of sg lists actually used, zero if the sg lists
2344 * is NULL, or -ENOMEM if the mapping failed
2347 int megasas_make_sgl(struct megasas_instance
*instance
, struct scsi_cmnd
*scp
,
2348 struct megasas_cmd_fusion
*cmd
)
2351 bool build_prp
= false;
2352 struct MPI25_IEEE_SGE_CHAIN64
*sgl_chain64
;
2354 sge_count
= scsi_dma_map(scp
);
2356 if ((sge_count
> instance
->max_num_sge
) || (sge_count
<= 0))
2359 sgl_chain64
= (struct MPI25_IEEE_SGE_CHAIN64
*)&cmd
->io_request
->SGL
;
2360 if ((le16_to_cpu(cmd
->io_request
->IoFlags
) &
2361 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
) &&
2362 (cmd
->pd_interface
== NVME_PD
))
2363 build_prp
= megasas_make_prp_nvme(instance
, scp
, sgl_chain64
,
2367 megasas_make_sgl_fusion(instance
, scp
, sgl_chain64
,
2374 * megasas_set_pd_lba - Sets PD LBA
2375 * @io_request: IO request
2376 * @cdb_len: cdb length
2377 * @io_info: IO information
2378 * @scp: SCSI command
2379 * @local_map_ptr: Raid map
2380 * @ref_tag: Primary reference tag
2382 * Used to set the PD LBA in CDB for FP IOs
2385 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
, u8 cdb_len
,
2386 struct IO_REQUEST_INFO
*io_info
, struct scsi_cmnd
*scp
,
2387 struct MR_DRV_RAID_MAP_ALL
*local_map_ptr
, u32 ref_tag
)
2389 struct MR_LD_RAID
*raid
;
2391 u64 start_blk
= io_info
->pdBlock
;
2392 u8
*cdb
= io_request
->CDB
.CDB32
;
2393 u32 num_blocks
= io_info
->numBlocks
;
2394 u8 opcode
= 0, flagvals
= 0, groupnum
= 0, control
= 0;
2396 /* Check if T10 PI (DIF) is enabled for this LD */
2397 ld
= MR_TargetIdToLdGet(io_info
->ldTgtId
, local_map_ptr
);
2398 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
2399 if (raid
->capability
.ldPiMode
== MR_PROT_INFO_TYPE_CONTROLLER
) {
2400 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
2401 cdb
[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD
;
2402 cdb
[7] = MEGASAS_SCSI_ADDL_CDB_LEN
;
2404 if (scp
->sc_data_direction
== DMA_FROM_DEVICE
)
2405 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32
;
2407 cdb
[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32
;
2408 cdb
[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL
;
2411 cdb
[12] = (u8
)((start_blk
>> 56) & 0xff);
2412 cdb
[13] = (u8
)((start_blk
>> 48) & 0xff);
2413 cdb
[14] = (u8
)((start_blk
>> 40) & 0xff);
2414 cdb
[15] = (u8
)((start_blk
>> 32) & 0xff);
2415 cdb
[16] = (u8
)((start_blk
>> 24) & 0xff);
2416 cdb
[17] = (u8
)((start_blk
>> 16) & 0xff);
2417 cdb
[18] = (u8
)((start_blk
>> 8) & 0xff);
2418 cdb
[19] = (u8
)(start_blk
& 0xff);
2420 /* Logical block reference tag */
2421 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
=
2422 cpu_to_be32(ref_tag
);
2423 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= cpu_to_be16(0xffff);
2424 io_request
->IoFlags
= cpu_to_le16(32); /* Specify 32-byte cdb */
2426 /* Transfer length */
2427 cdb
[28] = (u8
)((num_blocks
>> 24) & 0xff);
2428 cdb
[29] = (u8
)((num_blocks
>> 16) & 0xff);
2429 cdb
[30] = (u8
)((num_blocks
>> 8) & 0xff);
2430 cdb
[31] = (u8
)(num_blocks
& 0xff);
2432 /* set SCSI IO EEDPFlags */
2433 if (scp
->sc_data_direction
== DMA_FROM_DEVICE
) {
2434 io_request
->EEDPFlags
= cpu_to_le16(
2435 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
2436 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG
|
2437 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
|
2438 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG
|
2439 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE
|
2440 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD
);
2442 io_request
->EEDPFlags
= cpu_to_le16(
2443 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG
|
2444 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
);
2446 io_request
->Control
|= cpu_to_le32((0x4 << 26));
2447 io_request
->EEDPBlockSize
= cpu_to_le32(scp
->device
->sector_size
);
2449 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2450 if (((cdb_len
== 12) || (cdb_len
== 16)) &&
2451 (start_blk
<= 0xffffffff)) {
2452 if (cdb_len
== 16) {
2453 opcode
= cdb
[0] == READ_16
? READ_10
: WRITE_10
;
2458 opcode
= cdb
[0] == READ_12
? READ_10
: WRITE_10
;
2464 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
2471 /* Transfer length */
2472 cdb
[8] = (u8
)(num_blocks
& 0xff);
2473 cdb
[7] = (u8
)((num_blocks
>> 8) & 0xff);
2475 io_request
->IoFlags
= cpu_to_le16(10); /* Specify 10-byte cdb */
2477 } else if ((cdb_len
< 16) && (start_blk
> 0xffffffff)) {
2478 /* Convert to 16 byte CDB for large LBA's */
2481 opcode
= cdb
[0] == READ_6
? READ_16
: WRITE_16
;
2486 cdb
[0] == READ_10
? READ_16
: WRITE_16
;
2493 cdb
[0] == READ_12
? READ_16
: WRITE_16
;
2500 memset(cdb
, 0, sizeof(io_request
->CDB
.CDB32
));
2507 /* Transfer length */
2508 cdb
[13] = (u8
)(num_blocks
& 0xff);
2509 cdb
[12] = (u8
)((num_blocks
>> 8) & 0xff);
2510 cdb
[11] = (u8
)((num_blocks
>> 16) & 0xff);
2511 cdb
[10] = (u8
)((num_blocks
>> 24) & 0xff);
2513 io_request
->IoFlags
= cpu_to_le16(16); /* Specify 16-byte cdb */
2517 /* Normal case, just load LBA here */
2521 u8 val
= cdb
[1] & 0xE0;
2522 cdb
[3] = (u8
)(start_blk
& 0xff);
2523 cdb
[2] = (u8
)((start_blk
>> 8) & 0xff);
2524 cdb
[1] = val
| ((u8
)(start_blk
>> 16) & 0x1f);
2528 cdb
[5] = (u8
)(start_blk
& 0xff);
2529 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
2530 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
2531 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
2534 cdb
[5] = (u8
)(start_blk
& 0xff);
2535 cdb
[4] = (u8
)((start_blk
>> 8) & 0xff);
2536 cdb
[3] = (u8
)((start_blk
>> 16) & 0xff);
2537 cdb
[2] = (u8
)((start_blk
>> 24) & 0xff);
2540 cdb
[9] = (u8
)(start_blk
& 0xff);
2541 cdb
[8] = (u8
)((start_blk
>> 8) & 0xff);
2542 cdb
[7] = (u8
)((start_blk
>> 16) & 0xff);
2543 cdb
[6] = (u8
)((start_blk
>> 24) & 0xff);
2544 cdb
[5] = (u8
)((start_blk
>> 32) & 0xff);
2545 cdb
[4] = (u8
)((start_blk
>> 40) & 0xff);
2546 cdb
[3] = (u8
)((start_blk
>> 48) & 0xff);
2547 cdb
[2] = (u8
)((start_blk
>> 56) & 0xff);
2554 * megasas_stream_detect - stream detection on read and and write IOs
2555 * @instance: Adapter soft state
2556 * @cmd: Command to be prepared
2557 * @io_info: IO Request info
2561 /** stream detection on read and and write IOs */
2562 static void megasas_stream_detect(struct megasas_instance
*instance
,
2563 struct megasas_cmd_fusion
*cmd
,
2564 struct IO_REQUEST_INFO
*io_info
)
2566 struct fusion_context
*fusion
= instance
->ctrl_context
;
2567 u32 device_id
= io_info
->ldTgtId
;
2568 struct LD_STREAM_DETECT
*current_ld_sd
2569 = fusion
->stream_detect_by_ld
[device_id
];
2570 u32
*track_stream
= ¤t_ld_sd
->mru_bit_map
, stream_num
;
2571 u32 shifted_values
, unshifted_values
;
2572 u32 index_value_mask
, shifted_values_mask
;
2574 bool is_read_ahead
= false;
2575 struct STREAM_DETECT
*current_sd
;
2576 /* find possible stream */
2577 for (i
= 0; i
< MAX_STREAMS_TRACKED
; ++i
) {
2578 stream_num
= (*track_stream
>>
2579 (i
* BITS_PER_INDEX_STREAM
)) &
2581 current_sd
= ¤t_ld_sd
->stream_track
[stream_num
];
2582 /* if we found a stream, update the raid
2583 * context and also update the mruBitMap
2585 /* boundary condition */
2586 if ((current_sd
->next_seq_lba
) &&
2587 (io_info
->ldStartBlock
>= current_sd
->next_seq_lba
) &&
2588 (io_info
->ldStartBlock
<= (current_sd
->next_seq_lba
+ 32)) &&
2589 (current_sd
->is_read
== io_info
->isRead
)) {
2591 if ((io_info
->ldStartBlock
!= current_sd
->next_seq_lba
) &&
2592 ((!io_info
->isRead
) || (!is_read_ahead
)))
2594 * Once the API is available we need to change this.
2595 * At this point we are not allowing any gap
2599 SET_STREAM_DETECTED(cmd
->io_request
->RaidContext
.raid_context_g35
);
2600 current_sd
->next_seq_lba
=
2601 io_info
->ldStartBlock
+ io_info
->numBlocks
;
2603 * update the mruBitMap LRU
2605 shifted_values_mask
=
2606 (1 << i
* BITS_PER_INDEX_STREAM
) - 1;
2607 shifted_values
= ((*track_stream
& shifted_values_mask
)
2608 << BITS_PER_INDEX_STREAM
);
2610 STREAM_MASK
<< i
* BITS_PER_INDEX_STREAM
;
2612 *track_stream
& ~(shifted_values_mask
|
2615 unshifted_values
| shifted_values
| stream_num
;
2620 * if we did not find any stream, create a new one
2621 * from the least recently used
2623 stream_num
= (*track_stream
>>
2624 ((MAX_STREAMS_TRACKED
- 1) * BITS_PER_INDEX_STREAM
)) &
2626 current_sd
= ¤t_ld_sd
->stream_track
[stream_num
];
2627 current_sd
->is_read
= io_info
->isRead
;
2628 current_sd
->next_seq_lba
= io_info
->ldStartBlock
+ io_info
->numBlocks
;
2629 *track_stream
= (((*track_stream
& ZERO_LAST_STREAM
) << 4) | stream_num
);
2634 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2635 * affinity (cpu of the controller) and raid_flags in the raid context
2638 * @fusion: Fusion context
2639 * @praid_context: IO RAID context
2640 * @raid: LD raid map
2641 * @fp_possible: Is fast path possible?
2642 * @is_read: Is read IO?
2643 * @scsi_buff_len: SCSI command buffer length
2647 megasas_set_raidflag_cpu_affinity(struct fusion_context
*fusion
,
2648 union RAID_CONTEXT_UNION
*praid_context
,
2649 struct MR_LD_RAID
*raid
, bool fp_possible
,
2650 u8 is_read
, u32 scsi_buff_len
)
2652 u8 cpu_sel
= MR_RAID_CTX_CPUSEL_0
;
2653 struct RAID_CONTEXT_G35
*rctx_g35
;
2655 rctx_g35
= &praid_context
->raid_context_g35
;
2658 if ((raid
->cpuAffinity
.pdRead
.cpu0
) &&
2659 (raid
->cpuAffinity
.pdRead
.cpu1
))
2660 cpu_sel
= MR_RAID_CTX_CPUSEL_FCFS
;
2661 else if (raid
->cpuAffinity
.pdRead
.cpu1
)
2662 cpu_sel
= MR_RAID_CTX_CPUSEL_1
;
2664 if ((raid
->cpuAffinity
.pdWrite
.cpu0
) &&
2665 (raid
->cpuAffinity
.pdWrite
.cpu1
))
2666 cpu_sel
= MR_RAID_CTX_CPUSEL_FCFS
;
2667 else if (raid
->cpuAffinity
.pdWrite
.cpu1
)
2668 cpu_sel
= MR_RAID_CTX_CPUSEL_1
;
2669 /* Fast path cache by pass capable R0/R1 VD */
2670 if ((raid
->level
<= 1) &&
2671 (raid
->capability
.fp_cache_bypass_capable
)) {
2672 rctx_g35
->routing_flags
|=
2673 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT
);
2674 rctx_g35
->raid_flags
=
2675 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2676 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
);
2681 if ((raid
->cpuAffinity
.ldRead
.cpu0
) &&
2682 (raid
->cpuAffinity
.ldRead
.cpu1
))
2683 cpu_sel
= MR_RAID_CTX_CPUSEL_FCFS
;
2684 else if (raid
->cpuAffinity
.ldRead
.cpu1
)
2685 cpu_sel
= MR_RAID_CTX_CPUSEL_1
;
2687 if ((raid
->cpuAffinity
.ldWrite
.cpu0
) &&
2688 (raid
->cpuAffinity
.ldWrite
.cpu1
))
2689 cpu_sel
= MR_RAID_CTX_CPUSEL_FCFS
;
2690 else if (raid
->cpuAffinity
.ldWrite
.cpu1
)
2691 cpu_sel
= MR_RAID_CTX_CPUSEL_1
;
2693 if (is_stream_detected(rctx_g35
) &&
2694 ((raid
->level
== 5) || (raid
->level
== 6)) &&
2695 (raid
->writeMode
== MR_RL_WRITE_THROUGH_MODE
) &&
2696 (cpu_sel
== MR_RAID_CTX_CPUSEL_FCFS
))
2697 cpu_sel
= MR_RAID_CTX_CPUSEL_0
;
2701 rctx_g35
->routing_flags
|=
2702 (cpu_sel
<< MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT
);
2704 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2705 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2706 * IO Subtype is not bitmap.
2708 if ((fusion
->pcie_bw_limitation
) && (raid
->level
== 1) && (!is_read
) &&
2709 (scsi_buff_len
> MR_LARGE_IO_MIN_SIZE
)) {
2710 praid_context
->raid_context_g35
.raid_flags
=
2711 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2712 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
);
2717 * megasas_build_ldio_fusion - Prepares IOs to devices
2718 * @instance: Adapter soft state
2719 * @scp: SCSI command
2720 * @cmd: Command to be prepared
2722 * Prepares the io_request and chain elements (sg_frame) for IO
2723 * The IO can be for PD (Fast Path) or LD
2726 megasas_build_ldio_fusion(struct megasas_instance
*instance
,
2727 struct scsi_cmnd
*scp
,
2728 struct megasas_cmd_fusion
*cmd
)
2732 u32 start_lba_lo
, start_lba_hi
, device_id
, datalength
= 0;
2734 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
2735 struct IO_REQUEST_INFO io_info
;
2736 struct fusion_context
*fusion
;
2737 struct MR_DRV_RAID_MAP_ALL
*local_map_ptr
;
2739 unsigned long spinlock_flags
;
2740 struct MR_LD_RAID
*raid
= NULL
;
2741 struct MR_PRIV_DEVICE
*mrdev_priv
;
2742 struct RAID_CONTEXT
*rctx
;
2743 struct RAID_CONTEXT_G35
*rctx_g35
;
2745 device_id
= MEGASAS_DEV_INDEX(scp
);
2747 fusion
= instance
->ctrl_context
;
2749 io_request
= cmd
->io_request
;
2750 rctx
= &io_request
->RaidContext
.raid_context
;
2751 rctx_g35
= &io_request
->RaidContext
.raid_context_g35
;
2753 rctx
->virtual_disk_tgt_id
= cpu_to_le16(device_id
);
2755 rctx
->ex_status
= 0;
2759 fp_possible
= false;
2762 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2764 if (scp
->cmd_len
== 6) {
2765 datalength
= (u32
) scp
->cmnd
[4];
2766 start_lba_lo
= ((u32
) scp
->cmnd
[1] << 16) |
2767 ((u32
) scp
->cmnd
[2] << 8) | (u32
) scp
->cmnd
[3];
2769 start_lba_lo
&= 0x1FFFFF;
2773 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2775 else if (scp
->cmd_len
== 10) {
2776 datalength
= (u32
) scp
->cmnd
[8] |
2777 ((u32
) scp
->cmnd
[7] << 8);
2778 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
2779 ((u32
) scp
->cmnd
[3] << 16) |
2780 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
2784 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2786 else if (scp
->cmd_len
== 12) {
2787 datalength
= ((u32
) scp
->cmnd
[6] << 24) |
2788 ((u32
) scp
->cmnd
[7] << 16) |
2789 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
2790 start_lba_lo
= ((u32
) scp
->cmnd
[2] << 24) |
2791 ((u32
) scp
->cmnd
[3] << 16) |
2792 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
2796 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2798 else if (scp
->cmd_len
== 16) {
2799 datalength
= ((u32
) scp
->cmnd
[10] << 24) |
2800 ((u32
) scp
->cmnd
[11] << 16) |
2801 ((u32
) scp
->cmnd
[12] << 8) | (u32
) scp
->cmnd
[13];
2802 start_lba_lo
= ((u32
) scp
->cmnd
[6] << 24) |
2803 ((u32
) scp
->cmnd
[7] << 16) |
2804 ((u32
) scp
->cmnd
[8] << 8) | (u32
) scp
->cmnd
[9];
2806 start_lba_hi
= ((u32
) scp
->cmnd
[2] << 24) |
2807 ((u32
) scp
->cmnd
[3] << 16) |
2808 ((u32
) scp
->cmnd
[4] << 8) | (u32
) scp
->cmnd
[5];
2811 memset(&io_info
, 0, sizeof(struct IO_REQUEST_INFO
));
2812 io_info
.ldStartBlock
= ((u64
)start_lba_hi
<< 32) | start_lba_lo
;
2813 io_info
.numBlocks
= datalength
;
2814 io_info
.ldTgtId
= device_id
;
2815 io_info
.r1_alt_dev_handle
= MR_DEVHANDLE_INVALID
;
2816 scsi_buff_len
= scsi_bufflen(scp
);
2817 io_request
->DataLength
= cpu_to_le32(scsi_buff_len
);
2818 io_info
.data_arms
= 1;
2820 if (scp
->sc_data_direction
== DMA_FROM_DEVICE
)
2823 local_map_ptr
= fusion
->ld_drv_map
[(instance
->map_id
& 1)];
2824 ld
= MR_TargetIdToLdGet(device_id
, local_map_ptr
);
2826 if (ld
< instance
->fw_supported_vd_count
)
2827 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
2829 if (!raid
|| (!fusion
->fast_path_io
)) {
2830 rctx
->reg_lock_flags
= 0;
2831 fp_possible
= false;
2833 if (MR_BuildRaidContext(instance
, &io_info
, rctx
,
2834 local_map_ptr
, &raidLUN
))
2835 fp_possible
= (io_info
.fpOkForIo
> 0) ? true : false;
2838 megasas_get_msix_index(instance
, scp
, cmd
, io_info
.data_arms
);
2840 if (instance
->adapter_type
>= VENTURA_SERIES
) {
2841 /* FP for Optimal raid level 1.
2842 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2843 * are built by the driver as LD I/Os.
2844 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2845 * (there is never a reason to process these as buffered writes)
2846 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2847 * with the SLD bit asserted.
2849 if (io_info
.r1_alt_dev_handle
!= MR_DEVHANDLE_INVALID
) {
2850 mrdev_priv
= scp
->device
->hostdata
;
2852 if (atomic_inc_return(&instance
->fw_outstanding
) >
2853 (instance
->host
->can_queue
)) {
2854 fp_possible
= false;
2855 atomic_dec(&instance
->fw_outstanding
);
2856 } else if (fusion
->pcie_bw_limitation
&&
2857 ((scsi_buff_len
> MR_LARGE_IO_MIN_SIZE
) ||
2858 (atomic_dec_if_positive(&mrdev_priv
->r1_ldio_hint
) > 0))) {
2859 fp_possible
= false;
2860 atomic_dec(&instance
->fw_outstanding
);
2861 if (scsi_buff_len
> MR_LARGE_IO_MIN_SIZE
)
2862 atomic_set(&mrdev_priv
->r1_ldio_hint
,
2863 instance
->r1_ldio_hint_default
);
2868 (io_info
.isRead
&& io_info
.ra_capable
)) {
2869 spin_lock_irqsave(&instance
->stream_lock
,
2871 megasas_stream_detect(instance
, cmd
, &io_info
);
2872 spin_unlock_irqrestore(&instance
->stream_lock
,
2874 /* In ventura if stream detected for a read and it is
2875 * read ahead capable make this IO as LDIO
2877 if (is_stream_detected(rctx_g35
))
2878 fp_possible
= false;
2881 /* If raid is NULL, set CPU affinity to default CPU0 */
2883 megasas_set_raidflag_cpu_affinity(fusion
, &io_request
->RaidContext
,
2884 raid
, fp_possible
, io_info
.isRead
,
2887 rctx_g35
->routing_flags
|=
2888 (MR_RAID_CTX_CPUSEL_0
<< MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT
);
2892 megasas_set_pd_lba(io_request
, scp
->cmd_len
, &io_info
, scp
,
2893 local_map_ptr
, start_lba_lo
);
2894 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
2895 cmd
->request_desc
->SCSIIO
.RequestFlags
=
2896 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2897 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
2898 if (instance
->adapter_type
== INVADER_SERIES
) {
2899 rctx
->type
= MPI2_TYPE_CUDA
;
2901 io_request
->IoFlags
|= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
);
2902 rctx
->reg_lock_flags
|=
2903 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA
|
2904 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
2905 } else if (instance
->adapter_type
>= VENTURA_SERIES
) {
2906 rctx_g35
->nseg_type
|= (1 << RAID_CONTEXT_NSEG_SHIFT
);
2907 rctx_g35
->nseg_type
|= (MPI2_TYPE_CUDA
<< RAID_CONTEXT_TYPE_SHIFT
);
2908 rctx_g35
->routing_flags
|= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT
);
2909 io_request
->IoFlags
|=
2910 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
);
2912 if (fusion
->load_balance_info
&&
2913 (fusion
->load_balance_info
[device_id
].loadBalanceFlag
) &&
2916 get_updated_dev_handle(instance
,
2917 &fusion
->load_balance_info
[device_id
],
2918 &io_info
, local_map_ptr
);
2919 megasas_priv(scp
)->status
|= MEGASAS_LOAD_BALANCE_FLAG
;
2920 cmd
->pd_r1_lb
= io_info
.pd_after_lb
;
2921 if (instance
->adapter_type
>= VENTURA_SERIES
)
2922 rctx_g35
->span_arm
= io_info
.span_arm
;
2924 rctx
->span_arm
= io_info
.span_arm
;
2927 megasas_priv(scp
)->status
&= ~MEGASAS_LOAD_BALANCE_FLAG
;
2929 if (instance
->adapter_type
>= VENTURA_SERIES
)
2930 cmd
->r1_alt_dev_handle
= io_info
.r1_alt_dev_handle
;
2932 cmd
->r1_alt_dev_handle
= MR_DEVHANDLE_INVALID
;
2934 if ((raidLUN
[0] == 1) &&
2935 (local_map_ptr
->raidMap
.devHndlInfo
[io_info
.pd_after_lb
].validHandles
> 1)) {
2936 instance
->dev_handle
= !(instance
->dev_handle
);
2938 local_map_ptr
->raidMap
.devHndlInfo
[io_info
.pd_after_lb
].devHandle
[instance
->dev_handle
];
2941 cmd
->request_desc
->SCSIIO
.DevHandle
= io_info
.devHandle
;
2942 io_request
->DevHandle
= io_info
.devHandle
;
2943 cmd
->pd_interface
= io_info
.pd_interface
;
2944 /* populate the LUN field */
2945 memcpy(io_request
->LUN
, raidLUN
, 8);
2947 rctx
->timeout_value
=
2948 cpu_to_le16(local_map_ptr
->raidMap
.fpPdIoTimeoutSec
);
2949 cmd
->request_desc
->SCSIIO
.RequestFlags
=
2950 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2951 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
2952 if (instance
->adapter_type
== INVADER_SERIES
) {
2953 if (io_info
.do_fp_rlbypass
||
2954 (rctx
->reg_lock_flags
== REGION_TYPE_UNUSED
))
2955 cmd
->request_desc
->SCSIIO
.RequestFlags
=
2956 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK
<<
2957 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
2958 rctx
->type
= MPI2_TYPE_CUDA
;
2959 rctx
->reg_lock_flags
|=
2960 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0
|
2961 MR_RL_FLAGS_SEQ_NUM_ENABLE
);
2963 } else if (instance
->adapter_type
>= VENTURA_SERIES
) {
2964 rctx_g35
->routing_flags
|= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT
);
2965 rctx_g35
->nseg_type
|= (1 << RAID_CONTEXT_NSEG_SHIFT
);
2966 rctx_g35
->nseg_type
|= (MPI2_TYPE_CUDA
<< RAID_CONTEXT_TYPE_SHIFT
);
2968 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
2969 io_request
->DevHandle
= cpu_to_le16(device_id
);
2975 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2976 * @instance: Adapter soft state
2977 * @scmd: SCSI command
2978 * @cmd: Command to be prepared
2980 * Prepares the io_request frame for non-rw io cmds for vd.
2982 static void megasas_build_ld_nonrw_fusion(struct megasas_instance
*instance
,
2983 struct scsi_cmnd
*scmd
, struct megasas_cmd_fusion
*cmd
)
2986 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
2988 struct MR_DRV_RAID_MAP_ALL
*local_map_ptr
;
2989 struct fusion_context
*fusion
= instance
->ctrl_context
;
2993 struct MR_LD_RAID
*raid
;
2994 struct RAID_CONTEXT
*pRAID_Context
;
2997 io_request
= cmd
->io_request
;
2998 device_id
= MEGASAS_DEV_INDEX(scmd
);
2999 local_map_ptr
= fusion
->ld_drv_map
[(instance
->map_id
& 1)];
3000 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scmd
));
3001 /* get RAID_Context pointer */
3002 pRAID_Context
= &io_request
->RaidContext
.raid_context
;
3003 /* Check with FW team */
3004 pRAID_Context
->virtual_disk_tgt_id
= cpu_to_le16(device_id
);
3005 pRAID_Context
->reg_lock_row_lba
= 0;
3006 pRAID_Context
->reg_lock_length
= 0;
3008 if (fusion
->fast_path_io
&& (
3009 device_id
< instance
->fw_supported_vd_count
)) {
3011 ld
= MR_TargetIdToLdGet(device_id
, local_map_ptr
);
3012 if (ld
>= instance
->fw_supported_vd_count
- 1)
3015 raid
= MR_LdRaidGet(ld
, local_map_ptr
);
3016 if (!(raid
->capability
.fpNonRWCapable
))
3023 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
3024 io_request
->DevHandle
= cpu_to_le16(device_id
);
3025 io_request
->LUN
[1] = scmd
->device
->lun
;
3026 pRAID_Context
->timeout_value
=
3027 cpu_to_le16(scsi_cmd_to_rq(scmd
)->timeout
/ HZ
);
3028 cmd
->request_desc
->SCSIIO
.RequestFlags
=
3029 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
3030 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
3033 /* set RAID context values */
3034 pRAID_Context
->config_seq_num
= raid
->seqNum
;
3035 if (instance
->adapter_type
< VENTURA_SERIES
)
3036 pRAID_Context
->reg_lock_flags
= REGION_TYPE_SHARED_READ
;
3037 pRAID_Context
->timeout_value
=
3038 cpu_to_le16(raid
->fpIoTimeoutForLd
);
3040 /* get the DevHandle for the PD (since this is
3041 fpNonRWCapable, this is a single disk RAID0) */
3043 arRef
= MR_LdSpanArrayGet(ld
, span
, local_map_ptr
);
3044 pd
= MR_ArPdGet(arRef
, physArm
, local_map_ptr
);
3045 devHandle
= MR_PdDevHandleGet(pd
, local_map_ptr
);
3047 /* build request descriptor */
3048 cmd
->request_desc
->SCSIIO
.RequestFlags
=
3049 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
<<
3050 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
3051 cmd
->request_desc
->SCSIIO
.DevHandle
= devHandle
;
3053 /* populate the LUN field */
3054 memcpy(io_request
->LUN
, raid
->LUN
, 8);
3056 /* build the raidScsiIO structure */
3057 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
3058 io_request
->DevHandle
= devHandle
;
3063 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
3064 * @instance: Adapter soft state
3065 * @scmd: SCSI command
3066 * @cmd: Command to be prepared
3067 * @fp_possible: parameter to detect fast path or firmware path io.
3069 * Prepares the io_request frame for rw/non-rw io cmds for syspds
3072 megasas_build_syspd_fusion(struct megasas_instance
*instance
,
3073 struct scsi_cmnd
*scmd
, struct megasas_cmd_fusion
*cmd
,
3077 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
;
3079 u16 os_timeout_value
;
3081 struct MR_DRV_RAID_MAP_ALL
*local_map_ptr
;
3082 struct RAID_CONTEXT
*pRAID_Context
;
3083 struct MR_PD_CFG_SEQ_NUM_SYNC
*pd_sync
;
3084 struct MR_PRIV_DEVICE
*mr_device_priv_data
;
3085 struct fusion_context
*fusion
= instance
->ctrl_context
;
3086 pd_sync
= (void *)fusion
->pd_seq_sync
[(instance
->pd_seq_map_id
- 1) & 1];
3088 device_id
= MEGASAS_DEV_INDEX(scmd
);
3089 pd_index
= MEGASAS_PD_INDEX(scmd
);
3090 os_timeout_value
= scsi_cmd_to_rq(scmd
)->timeout
/ HZ
;
3091 mr_device_priv_data
= scmd
->device
->hostdata
;
3092 cmd
->pd_interface
= mr_device_priv_data
->interface_type
;
3094 io_request
= cmd
->io_request
;
3095 /* get RAID_Context pointer */
3096 pRAID_Context
= &io_request
->RaidContext
.raid_context
;
3097 pRAID_Context
->reg_lock_flags
= 0;
3098 pRAID_Context
->reg_lock_row_lba
= 0;
3099 pRAID_Context
->reg_lock_length
= 0;
3100 io_request
->DataLength
= cpu_to_le32(scsi_bufflen(scmd
));
3101 io_request
->LUN
[1] = scmd
->device
->lun
;
3102 pRAID_Context
->raid_flags
= MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
3103 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT
;
3105 /* If FW supports PD sequence number */
3106 if (instance
->support_seqnum_jbod_fp
) {
3107 if (instance
->use_seqnum_jbod_fp
&&
3108 instance
->pd_list
[pd_index
].driveType
== TYPE_DISK
) {
3110 /* More than 256 PD/JBOD support for Ventura */
3111 if (instance
->support_morethan256jbod
)
3112 pRAID_Context
->virtual_disk_tgt_id
=
3113 pd_sync
->seq
[pd_index
].pd_target_id
;
3115 pRAID_Context
->virtual_disk_tgt_id
=
3116 cpu_to_le16(device_id
+
3117 (MAX_PHYSICAL_DEVICES
- 1));
3118 pRAID_Context
->config_seq_num
=
3119 pd_sync
->seq
[pd_index
].seqNum
;
3120 io_request
->DevHandle
=
3121 pd_sync
->seq
[pd_index
].devHandle
;
3122 if (instance
->adapter_type
>= VENTURA_SERIES
) {
3123 io_request
->RaidContext
.raid_context_g35
.routing_flags
|=
3124 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT
);
3125 io_request
->RaidContext
.raid_context_g35
.nseg_type
|=
3126 (1 << RAID_CONTEXT_NSEG_SHIFT
);
3127 io_request
->RaidContext
.raid_context_g35
.nseg_type
|=
3128 (MPI2_TYPE_CUDA
<< RAID_CONTEXT_TYPE_SHIFT
);
3130 pRAID_Context
->type
= MPI2_TYPE_CUDA
;
3131 pRAID_Context
->nseg
= 0x1;
3132 pRAID_Context
->reg_lock_flags
|=
3133 (MR_RL_FLAGS_SEQ_NUM_ENABLE
|
3134 MR_RL_FLAGS_GRANT_DESTINATION_CUDA
);
3137 pRAID_Context
->virtual_disk_tgt_id
=
3138 cpu_to_le16(device_id
+
3139 (MAX_PHYSICAL_DEVICES
- 1));
3140 pRAID_Context
->config_seq_num
= 0;
3141 io_request
->DevHandle
= cpu_to_le16(0xFFFF);
3144 pRAID_Context
->virtual_disk_tgt_id
= cpu_to_le16(device_id
);
3145 pRAID_Context
->config_seq_num
= 0;
3147 if (fusion
->fast_path_io
) {
3149 fusion
->ld_drv_map
[(instance
->map_id
& 1)];
3150 io_request
->DevHandle
=
3151 local_map_ptr
->raidMap
.devHndlInfo
[device_id
].curDevHdl
;
3153 io_request
->DevHandle
= cpu_to_le16(0xFFFF);
3157 cmd
->request_desc
->SCSIIO
.DevHandle
= io_request
->DevHandle
;
3159 megasas_get_msix_index(instance
, scmd
, cmd
, 1);
3162 /* system pd firmware path */
3163 io_request
->Function
= MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
;
3164 cmd
->request_desc
->SCSIIO
.RequestFlags
=
3165 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
3166 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
3167 pRAID_Context
->timeout_value
= cpu_to_le16(os_timeout_value
);
3168 pRAID_Context
->virtual_disk_tgt_id
= cpu_to_le16(device_id
);
3170 if (os_timeout_value
)
3173 /* system pd Fast Path */
3174 io_request
->Function
= MPI2_FUNCTION_SCSI_IO_REQUEST
;
3175 timeout_limit
= (scmd
->device
->type
== TYPE_DISK
) ?
3177 pRAID_Context
->timeout_value
=
3178 cpu_to_le16((os_timeout_value
> timeout_limit
) ?
3179 timeout_limit
: os_timeout_value
);
3180 if (instance
->adapter_type
>= INVADER_SERIES
)
3181 io_request
->IoFlags
|=
3182 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH
);
3184 cmd
->request_desc
->SCSIIO
.RequestFlags
=
3185 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
<<
3186 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
3191 * megasas_build_io_fusion - Prepares IOs to devices
3192 * @instance: Adapter soft state
3193 * @scp: SCSI command
3194 * @cmd: Command to be prepared
3196 * Invokes helper functions to prepare request frames
3197 * and sets flags appropriate for IO/Non-IO cmd
3200 megasas_build_io_fusion(struct megasas_instance
*instance
,
3201 struct scsi_cmnd
*scp
,
3202 struct megasas_cmd_fusion
*cmd
)
3207 struct MPI2_RAID_SCSI_IO_REQUEST
*io_request
= cmd
->io_request
;
3208 struct MR_PRIV_DEVICE
*mr_device_priv_data
;
3209 mr_device_priv_data
= scp
->device
->hostdata
;
3211 /* Zero out some fields so they don't get reused */
3212 memset(io_request
->LUN
, 0x0, 8);
3213 io_request
->CDB
.EEDP32
.PrimaryReferenceTag
= 0;
3214 io_request
->CDB
.EEDP32
.PrimaryApplicationTagMask
= 0;
3215 io_request
->EEDPFlags
= 0;
3216 io_request
->Control
= 0;
3217 io_request
->EEDPBlockSize
= 0;
3218 io_request
->ChainOffset
= 0;
3219 io_request
->RaidContext
.raid_context
.raid_flags
= 0;
3220 io_request
->RaidContext
.raid_context
.type
= 0;
3221 io_request
->RaidContext
.raid_context
.nseg
= 0;
3223 memcpy(io_request
->CDB
.CDB32
, scp
->cmnd
, scp
->cmd_len
);
3225 * Just the CDB length,rest of the Flags are zero
3226 * This will be modified for FP in build_ldio_fusion
3228 io_request
->IoFlags
= cpu_to_le16(scp
->cmd_len
);
3230 switch (megasas_cmd_type(scp
)) {
3231 case READ_WRITE_LDIO
:
3232 megasas_build_ldio_fusion(instance
, scp
, cmd
);
3234 case NON_READ_WRITE_LDIO
:
3235 megasas_build_ld_nonrw_fusion(instance
, scp
, cmd
);
3237 case READ_WRITE_SYSPDIO
:
3238 megasas_build_syspd_fusion(instance
, scp
, cmd
, true);
3240 case NON_READ_WRITE_SYSPDIO
:
3241 pd_index
= MEGASAS_PD_INDEX(scp
);
3242 drive_type
= instance
->pd_list
[pd_index
].driveType
;
3243 if ((instance
->secure_jbod_support
||
3244 mr_device_priv_data
->is_tm_capable
) ||
3245 (instance
->adapter_type
>= VENTURA_SERIES
&&
3246 drive_type
== TYPE_ENCLOSURE
))
3247 megasas_build_syspd_fusion(instance
, scp
, cmd
, false);
3249 megasas_build_syspd_fusion(instance
, scp
, cmd
, true);
3259 sge_count
= megasas_make_sgl(instance
, scp
, cmd
);
3261 if (sge_count
> instance
->max_num_sge
|| (sge_count
< 0)) {
3262 dev_err(&instance
->pdev
->dev
,
3263 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3264 __func__
, __LINE__
, sge_count
, instance
->max_num_sge
);
3268 if (instance
->adapter_type
>= VENTURA_SERIES
) {
3269 set_num_sge(&io_request
->RaidContext
.raid_context_g35
, sge_count
);
3270 cpu_to_le16s(&io_request
->RaidContext
.raid_context_g35
.routing_flags
);
3271 cpu_to_le16s(&io_request
->RaidContext
.raid_context_g35
.nseg_type
);
3273 /* numSGE store lower 8 bit of sge_count.
3274 * numSGEExt store higher 8 bit of sge_count
3276 io_request
->RaidContext
.raid_context
.num_sge
= sge_count
;
3277 io_request
->RaidContext
.raid_context
.num_sge_ext
=
3278 (u8
)(sge_count
>> 8);
3281 io_request
->SGLFlags
= cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING
);
3283 if (scp
->sc_data_direction
== DMA_TO_DEVICE
)
3284 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE
);
3285 else if (scp
->sc_data_direction
== DMA_FROM_DEVICE
)
3286 io_request
->Control
|= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ
);
3288 io_request
->SGLOffset0
=
3289 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
, SGL
) / 4;
3291 io_request
->SenseBufferLowAddress
=
3292 cpu_to_le32(lower_32_bits(cmd
->sense_phys_addr
));
3293 io_request
->SenseBufferLength
= SCSI_SENSE_BUFFERSIZE
;
3296 megasas_priv(scp
)->cmd_priv
= cmd
;
3301 static union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
3302 megasas_get_request_descriptor(struct megasas_instance
*instance
, u16 index
)
3305 struct fusion_context
*fusion
;
3307 fusion
= instance
->ctrl_context
;
3308 p
= fusion
->req_frames_desc
+
3309 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION
) * index
;
3311 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION
*)p
;
3315 /* megasas_prepate_secondRaid1_IO
3316 * It prepares the raid 1 second IO
3318 static void megasas_prepare_secondRaid1_IO(struct megasas_instance
*instance
,
3319 struct megasas_cmd_fusion
*cmd
,
3320 struct megasas_cmd_fusion
*r1_cmd
)
3322 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
, *req_desc2
= NULL
;
3323 struct fusion_context
*fusion
;
3324 fusion
= instance
->ctrl_context
;
3325 req_desc
= cmd
->request_desc
;
3326 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3327 memcpy(r1_cmd
->io_request
, cmd
->io_request
,
3328 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST
)));
3329 memcpy(r1_cmd
->io_request
->SGLs
, cmd
->io_request
->SGLs
,
3330 (fusion
->max_sge_in_main_msg
* sizeof(union MPI2_SGE_IO_UNION
)));
3331 /*sense buffer is different for r1 command*/
3332 r1_cmd
->io_request
->SenseBufferLowAddress
=
3333 cpu_to_le32(lower_32_bits(r1_cmd
->sense_phys_addr
));
3334 r1_cmd
->scmd
= cmd
->scmd
;
3335 req_desc2
= megasas_get_request_descriptor(instance
,
3336 (r1_cmd
->index
- 1));
3337 req_desc2
->Words
= 0;
3338 r1_cmd
->request_desc
= req_desc2
;
3339 req_desc2
->SCSIIO
.SMID
= cpu_to_le16(r1_cmd
->index
);
3340 req_desc2
->SCSIIO
.RequestFlags
= req_desc
->SCSIIO
.RequestFlags
;
3341 r1_cmd
->request_desc
->SCSIIO
.DevHandle
= cmd
->r1_alt_dev_handle
;
3342 r1_cmd
->io_request
->DevHandle
= cmd
->r1_alt_dev_handle
;
3343 r1_cmd
->r1_alt_dev_handle
= cmd
->io_request
->DevHandle
;
3344 cmd
->io_request
->RaidContext
.raid_context_g35
.flow_specific
.peer_smid
=
3345 cpu_to_le16(r1_cmd
->index
);
3346 r1_cmd
->io_request
->RaidContext
.raid_context_g35
.flow_specific
.peer_smid
=
3347 cpu_to_le16(cmd
->index
);
3348 /*MSIxIndex of both commands request descriptors should be same*/
3349 r1_cmd
->request_desc
->SCSIIO
.MSIxIndex
=
3350 cmd
->request_desc
->SCSIIO
.MSIxIndex
;
3351 /*span arm is different for r1 cmd*/
3352 r1_cmd
->io_request
->RaidContext
.raid_context_g35
.span_arm
=
3353 cmd
->io_request
->RaidContext
.raid_context_g35
.span_arm
+ 1;
3357 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3358 * issuing non IOCTL cmd
3359 * @instance: Adapter soft state
3360 * @scmd: pointer to scsi cmd from OS
3363 megasas_build_and_issue_cmd_fusion(struct megasas_instance
*instance
,
3364 struct scsi_cmnd
*scmd
)
3366 struct megasas_cmd_fusion
*cmd
, *r1_cmd
= NULL
;
3367 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
3370 if ((megasas_cmd_type(scmd
) == READ_WRITE_LDIO
) &&
3371 instance
->ldio_threshold
&&
3372 (atomic_inc_return(&instance
->ldio_outstanding
) >
3373 instance
->ldio_threshold
)) {
3374 atomic_dec(&instance
->ldio_outstanding
);
3375 return SCSI_MLQUEUE_DEVICE_BUSY
;
3378 if (atomic_inc_return(&instance
->fw_outstanding
) >
3379 instance
->host
->can_queue
) {
3380 atomic_dec(&instance
->fw_outstanding
);
3381 return SCSI_MLQUEUE_HOST_BUSY
;
3384 cmd
= megasas_get_cmd_fusion(instance
, scsi_cmd_to_rq(scmd
)->tag
);
3387 atomic_dec(&instance
->fw_outstanding
);
3388 return SCSI_MLQUEUE_HOST_BUSY
;
3393 req_desc
= megasas_get_request_descriptor(instance
, index
-1);
3395 req_desc
->Words
= 0;
3396 cmd
->request_desc
= req_desc
;
3398 if (megasas_build_io_fusion(instance
, scmd
, cmd
)) {
3399 megasas_return_cmd_fusion(instance
, cmd
);
3400 dev_err(&instance
->pdev
->dev
, "Error building command\n");
3401 cmd
->request_desc
= NULL
;
3402 atomic_dec(&instance
->fw_outstanding
);
3403 return SCSI_MLQUEUE_HOST_BUSY
;
3406 req_desc
= cmd
->request_desc
;
3407 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
3409 if (cmd
->io_request
->ChainOffset
!= 0 &&
3410 cmd
->io_request
->ChainOffset
!= 0xF)
3411 dev_err(&instance
->pdev
->dev
, "The chain offset value is not "
3412 "correct : %x\n", cmd
->io_request
->ChainOffset
);
3414 * if it is raid 1/10 fp write capable.
3415 * try to get second command from pool and construct it.
3416 * From FW, it has confirmed that lba values of two PDs
3417 * corresponds to single R1/10 LD are always same
3420 /* driver side count always should be less than max_fw_cmds
3421 * to get new command
3423 if (cmd
->r1_alt_dev_handle
!= MR_DEVHANDLE_INVALID
) {
3424 r1_cmd
= megasas_get_cmd_fusion(instance
,
3425 scsi_cmd_to_rq(scmd
)->tag
+ instance
->max_fw_cmds
);
3426 megasas_prepare_secondRaid1_IO(instance
, cmd
, r1_cmd
);
3431 * Issue the command to the FW
3434 megasas_sdev_busy_inc(instance
, scmd
);
3435 megasas_fire_cmd_fusion(instance
, req_desc
);
3438 megasas_fire_cmd_fusion(instance
, r1_cmd
->request_desc
);
3445 * megasas_complete_r1_command -
3446 * completes R1 FP write commands which has valid peer smid
3447 * @instance: Adapter soft state
3448 * @cmd: MPT command frame
3452 megasas_complete_r1_command(struct megasas_instance
*instance
,
3453 struct megasas_cmd_fusion
*cmd
)
3455 u8
*sense
, status
, ex_status
;
3458 struct fusion_context
*fusion
;
3459 struct megasas_cmd_fusion
*r1_cmd
= NULL
;
3460 struct scsi_cmnd
*scmd_local
= NULL
;
3461 struct RAID_CONTEXT_G35
*rctx_g35
;
3463 rctx_g35
= &cmd
->io_request
->RaidContext
.raid_context_g35
;
3464 fusion
= instance
->ctrl_context
;
3465 peer_smid
= le16_to_cpu(rctx_g35
->flow_specific
.peer_smid
);
3467 r1_cmd
= fusion
->cmd_list
[peer_smid
- 1];
3468 scmd_local
= cmd
->scmd
;
3469 status
= rctx_g35
->status
;
3470 ex_status
= rctx_g35
->ex_status
;
3471 data_length
= cmd
->io_request
->DataLength
;
3474 cmd
->cmd_completed
= true;
3476 /* Check if peer command is completed or not*/
3477 if (r1_cmd
->cmd_completed
) {
3478 rctx_g35
= &r1_cmd
->io_request
->RaidContext
.raid_context_g35
;
3479 if (rctx_g35
->status
!= MFI_STAT_OK
) {
3480 status
= rctx_g35
->status
;
3481 ex_status
= rctx_g35
->ex_status
;
3482 data_length
= r1_cmd
->io_request
->DataLength
;
3483 sense
= r1_cmd
->sense
;
3486 megasas_return_cmd_fusion(instance
, r1_cmd
);
3487 map_cmd_status(fusion
, scmd_local
, status
, ex_status
,
3488 le32_to_cpu(data_length
), sense
);
3489 if (instance
->ldio_threshold
&&
3490 megasas_cmd_type(scmd_local
) == READ_WRITE_LDIO
)
3491 atomic_dec(&instance
->ldio_outstanding
);
3492 megasas_priv(scmd_local
)->cmd_priv
= NULL
;
3493 megasas_return_cmd_fusion(instance
, cmd
);
3494 scsi_dma_unmap(scmd_local
);
3495 megasas_sdev_busy_dec(instance
, scmd_local
);
3496 scsi_done(scmd_local
);
3501 * access_irq_context: Access to reply processing
3502 * @irq_context: IRQ context
3504 * Synchronize access to reply processing.
3506 * Return: true on success, false on failure.
3509 bool access_irq_context(struct megasas_irq_context
*irq_context
)
3514 if (atomic_add_unless(&irq_context
->in_used
, 1, 1))
3521 * release_irq_context: Release reply processing
3522 * @irq_context: IRQ context
3524 * Release access of reply processing.
3529 void release_irq_context(struct megasas_irq_context
*irq_context
)
3532 atomic_dec(&irq_context
->in_used
);
3536 * complete_cmd_fusion - Completes command
3537 * @instance: Adapter soft state
3538 * @MSIxIndex: MSI number
3539 * @irq_context: IRQ context
3541 * Completes all commands that is in reply descriptor queue
3544 complete_cmd_fusion(struct megasas_instance
*instance
, u32 MSIxIndex
,
3545 struct megasas_irq_context
*irq_context
)
3547 union MPI2_REPLY_DESCRIPTORS_UNION
*desc
;
3548 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*reply_desc
;
3549 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
3550 struct fusion_context
*fusion
;
3551 struct megasas_cmd
*cmd_mfi
;
3552 struct megasas_cmd_fusion
*cmd_fusion
;
3553 u16 smid
, num_completed
;
3554 u8 reply_descript_type
, *sense
, status
, extStatus
;
3555 u32 device_id
, data_length
;
3556 union desc_value d_val
;
3557 struct LD_LOAD_BALANCE_INFO
*lbinfo
;
3558 int threshold_reply_count
= 0;
3559 struct scsi_cmnd
*scmd_local
= NULL
;
3560 struct MR_TASK_MANAGE_REQUEST
*mr_tm_req
;
3561 struct MPI2_SCSI_TASK_MANAGE_REQUEST
*mpi_tm_req
;
3563 fusion
= instance
->ctrl_context
;
3565 if (atomic_read(&instance
->adprecovery
) == MEGASAS_HW_CRITICAL_ERROR
)
3568 if (!access_irq_context(irq_context
))
3571 desc
= fusion
->reply_frames_desc
[MSIxIndex
] +
3572 fusion
->last_reply_idx
[MSIxIndex
];
3574 reply_desc
= (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
3576 d_val
.word
= desc
->Words
;
3578 reply_descript_type
= reply_desc
->ReplyFlags
&
3579 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
3581 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
) {
3582 release_irq_context(irq_context
);
3588 while (d_val
.u
.low
!= cpu_to_le32(UINT_MAX
) &&
3589 d_val
.u
.high
!= cpu_to_le32(UINT_MAX
)) {
3591 smid
= le16_to_cpu(reply_desc
->SMID
);
3592 cmd_fusion
= fusion
->cmd_list
[smid
- 1];
3593 scsi_io_req
= (struct MPI2_RAID_SCSI_IO_REQUEST
*)
3594 cmd_fusion
->io_request
;
3596 scmd_local
= cmd_fusion
->scmd
;
3597 status
= scsi_io_req
->RaidContext
.raid_context
.status
;
3598 extStatus
= scsi_io_req
->RaidContext
.raid_context
.ex_status
;
3599 sense
= cmd_fusion
->sense
;
3600 data_length
= scsi_io_req
->DataLength
;
3602 switch (scsi_io_req
->Function
) {
3603 case MPI2_FUNCTION_SCSI_TASK_MGMT
:
3604 mr_tm_req
= (struct MR_TASK_MANAGE_REQUEST
*)
3605 cmd_fusion
->io_request
;
3606 mpi_tm_req
= (struct MPI2_SCSI_TASK_MANAGE_REQUEST
*)
3607 &mr_tm_req
->TmRequest
;
3608 dev_dbg(&instance
->pdev
->dev
, "TM completion:"
3609 "type: 0x%x TaskMID: 0x%x\n",
3610 mpi_tm_req
->TaskType
, mpi_tm_req
->TaskMID
);
3611 complete(&cmd_fusion
->done
);
3613 case MPI2_FUNCTION_SCSI_IO_REQUEST
: /*Fast Path IO.*/
3614 /* Update load balancing info */
3615 if (fusion
->load_balance_info
&&
3616 (megasas_priv(cmd_fusion
->scmd
)->status
&
3617 MEGASAS_LOAD_BALANCE_FLAG
)) {
3618 device_id
= MEGASAS_DEV_INDEX(scmd_local
);
3619 lbinfo
= &fusion
->load_balance_info
[device_id
];
3620 atomic_dec(&lbinfo
->scsi_pending_cmds
[cmd_fusion
->pd_r1_lb
]);
3621 megasas_priv(cmd_fusion
->scmd
)->status
&=
3622 ~MEGASAS_LOAD_BALANCE_FLAG
;
3624 fallthrough
; /* and complete IO */
3625 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST
: /* LD-IO Path */
3626 atomic_dec(&instance
->fw_outstanding
);
3627 if (cmd_fusion
->r1_alt_dev_handle
== MR_DEVHANDLE_INVALID
) {
3628 map_cmd_status(fusion
, scmd_local
, status
,
3629 extStatus
, le32_to_cpu(data_length
),
3631 if (instance
->ldio_threshold
&&
3632 (megasas_cmd_type(scmd_local
) == READ_WRITE_LDIO
))
3633 atomic_dec(&instance
->ldio_outstanding
);
3634 megasas_priv(scmd_local
)->cmd_priv
= NULL
;
3635 megasas_return_cmd_fusion(instance
, cmd_fusion
);
3636 scsi_dma_unmap(scmd_local
);
3637 megasas_sdev_busy_dec(instance
, scmd_local
);
3638 scsi_done(scmd_local
);
3639 } else /* Optimal VD - R1 FP command completion. */
3640 megasas_complete_r1_command(instance
, cmd_fusion
);
3642 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
: /*MFI command */
3643 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
3644 /* Poll mode. Dummy free.
3645 * In case of Interrupt mode, caller has reverse check.
3647 if (cmd_mfi
->flags
& DRV_DCMD_POLLED_MODE
) {
3648 cmd_mfi
->flags
&= ~DRV_DCMD_POLLED_MODE
;
3649 megasas_return_cmd(instance
, cmd_mfi
);
3651 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
3655 fusion
->last_reply_idx
[MSIxIndex
]++;
3656 if (fusion
->last_reply_idx
[MSIxIndex
] >=
3657 fusion
->reply_q_depth
)
3658 fusion
->last_reply_idx
[MSIxIndex
] = 0;
3660 desc
->Words
= cpu_to_le64(ULLONG_MAX
);
3662 threshold_reply_count
++;
3664 /* Get the next reply descriptor */
3665 if (!fusion
->last_reply_idx
[MSIxIndex
])
3666 desc
= fusion
->reply_frames_desc
[MSIxIndex
];
3671 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR
*)desc
;
3673 d_val
.word
= desc
->Words
;
3675 reply_descript_type
= reply_desc
->ReplyFlags
&
3676 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK
;
3678 if (reply_descript_type
== MPI2_RPY_DESCRIPT_FLAGS_UNUSED
)
3681 * Write to reply post host index register after completing threshold
3682 * number of reply counts and still there are more replies in reply queue
3683 * pending to be completed
3685 if (threshold_reply_count
>= instance
->threshold_reply_count
) {
3686 if (instance
->msix_combined
)
3687 writel(((MSIxIndex
& 0x7) << 24) |
3688 fusion
->last_reply_idx
[MSIxIndex
],
3689 instance
->reply_post_host_index_addr
[MSIxIndex
/8]);
3691 writel((MSIxIndex
<< 24) |
3692 fusion
->last_reply_idx
[MSIxIndex
],
3693 instance
->reply_post_host_index_addr
[0]);
3694 threshold_reply_count
= 0;
3696 if (!irq_context
->irq_poll_scheduled
) {
3697 irq_context
->irq_poll_scheduled
= true;
3698 irq_context
->irq_line_enable
= true;
3699 irq_poll_sched(&irq_context
->irqpoll
);
3701 release_irq_context(irq_context
);
3702 return num_completed
;
3707 if (num_completed
) {
3709 if (instance
->msix_combined
)
3710 writel(((MSIxIndex
& 0x7) << 24) |
3711 fusion
->last_reply_idx
[MSIxIndex
],
3712 instance
->reply_post_host_index_addr
[MSIxIndex
/8]);
3714 writel((MSIxIndex
<< 24) |
3715 fusion
->last_reply_idx
[MSIxIndex
],
3716 instance
->reply_post_host_index_addr
[0]);
3717 megasas_check_and_restore_queue_depth(instance
);
3720 release_irq_context(irq_context
);
3722 return num_completed
;
3725 int megasas_blk_mq_poll(struct Scsi_Host
*shost
, unsigned int queue_num
)
3728 struct megasas_instance
*instance
;
3729 int num_entries
= 0;
3730 struct fusion_context
*fusion
;
3732 instance
= (struct megasas_instance
*)shost
->hostdata
;
3734 fusion
= instance
->ctrl_context
;
3736 queue_num
= queue_num
+ instance
->low_latency_index_start
;
3738 if (!atomic_add_unless(&fusion
->busy_mq_poll
[queue_num
], 1, 1))
3741 num_entries
= complete_cmd_fusion(instance
, queue_num
, NULL
);
3742 atomic_dec(&fusion
->busy_mq_poll
[queue_num
]);
3748 * megasas_enable_irq_poll() - enable irqpoll
3749 * @instance: Adapter soft state
3751 static void megasas_enable_irq_poll(struct megasas_instance
*instance
)
3754 struct megasas_irq_context
*irq_ctx
;
3756 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
3758 for (i
= 0; i
< count
; i
++) {
3759 irq_ctx
= &instance
->irq_context
[i
];
3760 irq_poll_enable(&irq_ctx
->irqpoll
);
3765 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3766 * @instance_addr: Adapter soft state address
3768 static void megasas_sync_irqs(unsigned long instance_addr
)
3771 struct megasas_instance
*instance
=
3772 (struct megasas_instance
*)instance_addr
;
3773 struct megasas_irq_context
*irq_ctx
;
3775 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
3777 for (i
= 0; i
< count
; i
++) {
3778 synchronize_irq(pci_irq_vector(instance
->pdev
, i
));
3779 irq_ctx
= &instance
->irq_context
[i
];
3780 irq_poll_disable(&irq_ctx
->irqpoll
);
3781 if (irq_ctx
->irq_poll_scheduled
) {
3782 irq_ctx
->irq_poll_scheduled
= false;
3783 enable_irq(irq_ctx
->os_irq
);
3784 complete_cmd_fusion(instance
, irq_ctx
->MSIxIndex
, irq_ctx
);
3790 * megasas_irqpoll() - process a queue for completed reply descriptors
3791 * @irqpoll: IRQ poll structure associated with queue to poll.
3792 * @budget: Threshold of reply descriptors to process per poll.
3794 * Return: The number of entries processed.
3797 int megasas_irqpoll(struct irq_poll
*irqpoll
, int budget
)
3799 struct megasas_irq_context
*irq_ctx
;
3800 struct megasas_instance
*instance
;
3803 irq_ctx
= container_of(irqpoll
, struct megasas_irq_context
, irqpoll
);
3804 instance
= irq_ctx
->instance
;
3806 if (irq_ctx
->irq_line_enable
) {
3807 disable_irq_nosync(irq_ctx
->os_irq
);
3808 irq_ctx
->irq_line_enable
= false;
3811 num_entries
= complete_cmd_fusion(instance
, irq_ctx
->MSIxIndex
, irq_ctx
);
3812 if (num_entries
< budget
) {
3813 irq_poll_complete(irqpoll
);
3814 irq_ctx
->irq_poll_scheduled
= false;
3815 enable_irq(irq_ctx
->os_irq
);
3816 complete_cmd_fusion(instance
, irq_ctx
->MSIxIndex
, irq_ctx
);
3823 * megasas_complete_cmd_dpc_fusion - Completes command
3824 * @instance_addr: Adapter soft state address
3826 * Tasklet to complete cmds
3829 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr
)
3831 struct megasas_instance
*instance
=
3832 (struct megasas_instance
*)instance_addr
;
3833 struct megasas_irq_context
*irq_ctx
= NULL
;
3834 u32 count
, MSIxIndex
;
3836 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
3838 /* If we have already declared adapter dead, donot complete cmds */
3839 if (atomic_read(&instance
->adprecovery
) == MEGASAS_HW_CRITICAL_ERROR
)
3842 for (MSIxIndex
= 0 ; MSIxIndex
< count
; MSIxIndex
++) {
3843 irq_ctx
= &instance
->irq_context
[MSIxIndex
];
3844 complete_cmd_fusion(instance
, MSIxIndex
, irq_ctx
);
3849 * megasas_isr_fusion - isr entry point
3851 * @devp: IRQ context
3853 static irqreturn_t
megasas_isr_fusion(int irq
, void *devp
)
3855 struct megasas_irq_context
*irq_context
= devp
;
3856 struct megasas_instance
*instance
= irq_context
->instance
;
3859 if (instance
->mask_interrupts
)
3862 if (irq_context
->irq_poll_scheduled
)
3865 if (!instance
->msix_vectors
) {
3866 mfiStatus
= instance
->instancet
->clear_intr(instance
);
3871 /* If we are resetting, bail */
3872 if (test_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
)) {
3873 instance
->instancet
->clear_intr(instance
);
3877 return complete_cmd_fusion(instance
, irq_context
->MSIxIndex
, irq_context
)
3878 ? IRQ_HANDLED
: IRQ_NONE
;
3882 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3883 * @instance: Adapter soft state
3884 * @mfi_cmd: megasas_cmd pointer
3888 build_mpt_mfi_pass_thru(struct megasas_instance
*instance
,
3889 struct megasas_cmd
*mfi_cmd
)
3891 struct MPI25_IEEE_SGE_CHAIN64
*mpi25_ieee_chain
;
3892 struct MPI2_RAID_SCSI_IO_REQUEST
*io_req
;
3893 struct megasas_cmd_fusion
*cmd
;
3894 struct fusion_context
*fusion
;
3895 struct megasas_header
*frame_hdr
= &mfi_cmd
->frame
->hdr
;
3897 fusion
= instance
->ctrl_context
;
3899 cmd
= megasas_get_cmd_fusion(instance
,
3900 instance
->max_scsi_cmds
+ mfi_cmd
->index
);
3902 /* Save the smid. To be used for returning the cmd */
3903 mfi_cmd
->context
.smid
= cmd
->index
;
3906 * For cmds where the flag is set, store the flag and check
3907 * on completion. For cmds with this flag, don't call
3908 * megasas_complete_cmd
3911 if (frame_hdr
->flags
& cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE
))
3912 mfi_cmd
->flags
|= DRV_DCMD_POLLED_MODE
;
3914 io_req
= cmd
->io_request
;
3916 if (instance
->adapter_type
>= INVADER_SERIES
) {
3917 struct MPI25_IEEE_SGE_CHAIN64
*sgl_ptr_end
=
3918 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
;
3919 sgl_ptr_end
+= fusion
->max_sge_in_main_msg
- 1;
3920 sgl_ptr_end
->Flags
= 0;
3924 (struct MPI25_IEEE_SGE_CHAIN64
*)&io_req
->SGL
.IeeeChain
;
3926 io_req
->Function
= MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST
;
3927 io_req
->SGLOffset0
= offsetof(struct MPI2_RAID_SCSI_IO_REQUEST
,
3929 io_req
->ChainOffset
= fusion
->chain_offset_mfi_pthru
;
3931 mpi25_ieee_chain
->Address
= cpu_to_le64(mfi_cmd
->frame_phys_addr
);
3933 mpi25_ieee_chain
->Flags
= IEEE_SGE_FLAGS_CHAIN_ELEMENT
|
3934 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR
;
3936 mpi25_ieee_chain
->Length
= cpu_to_le32(instance
->mfi_frame_size
);
3940 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3941 * @instance: Adapter soft state
3942 * @cmd: mfi cmd to build
3945 static union MEGASAS_REQUEST_DESCRIPTOR_UNION
*
3946 build_mpt_cmd(struct megasas_instance
*instance
, struct megasas_cmd
*cmd
)
3948 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
= NULL
;
3951 build_mpt_mfi_pass_thru(instance
, cmd
);
3952 index
= cmd
->context
.smid
;
3954 req_desc
= megasas_get_request_descriptor(instance
, index
- 1);
3956 req_desc
->Words
= 0;
3957 req_desc
->SCSIIO
.RequestFlags
= (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO
<<
3958 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
3960 req_desc
->SCSIIO
.SMID
= cpu_to_le16(index
);
3966 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3967 * @instance: Adapter soft state
3968 * @cmd: mfi cmd pointer
3972 megasas_issue_dcmd_fusion(struct megasas_instance
*instance
,
3973 struct megasas_cmd
*cmd
)
3975 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
3977 req_desc
= build_mpt_cmd(instance
, cmd
);
3979 megasas_fire_cmd_fusion(instance
, req_desc
);
3984 * megasas_release_fusion - Reverses the FW initialization
3985 * @instance: Adapter soft state
3988 megasas_release_fusion(struct megasas_instance
*instance
)
3990 megasas_free_ioc_init_cmd(instance
);
3991 megasas_free_cmds(instance
);
3992 megasas_free_cmds_fusion(instance
);
3994 iounmap(instance
->reg_set
);
3996 pci_release_selected_regions(instance
->pdev
, 1<<instance
->bar
);
4000 * megasas_read_fw_status_reg_fusion - returns the current FW status value
4001 * @instance: Adapter soft state
4004 megasas_read_fw_status_reg_fusion(struct megasas_instance
*instance
)
4006 return megasas_readl(instance
, &instance
->reg_set
->outbound_scratch_pad_0
);
4010 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4011 * @instance: Controller's soft instance
4012 * @return: Number of allocated host crash buffers
4015 megasas_alloc_host_crash_buffer(struct megasas_instance
*instance
)
4019 for (i
= 0; i
< MAX_CRASH_DUMP_SIZE
; i
++) {
4020 instance
->crash_buf
[i
] = vzalloc(CRASH_DMA_BUF_SIZE
);
4021 if (!instance
->crash_buf
[i
]) {
4022 dev_info(&instance
->pdev
->dev
, "Firmware crash dump "
4023 "memory allocation failed at index %d\n", i
);
4027 instance
->drv_buf_alloc
= i
;
4031 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
4032 * @instance: Controller's soft instance
4035 megasas_free_host_crash_buffer(struct megasas_instance
*instance
)
4038 for (i
= 0; i
< instance
->drv_buf_alloc
; i
++) {
4039 vfree(instance
->crash_buf
[i
]);
4041 instance
->drv_buf_index
= 0;
4042 instance
->drv_buf_alloc
= 0;
4043 instance
->fw_crash_state
= UNAVAILABLE
;
4044 instance
->fw_crash_buffer_size
= 0;
4048 * megasas_adp_reset_fusion - For controller reset
4049 * @instance: Controller's soft instance
4050 * @regs: MFI register set
4053 megasas_adp_reset_fusion(struct megasas_instance
*instance
,
4054 struct megasas_register_set __iomem
*regs
)
4056 u32 host_diag
, abs_state
, retry
;
4058 /* Now try to reset the chip */
4059 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4060 writel(MPI2_WRSEQ_1ST_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4061 writel(MPI2_WRSEQ_2ND_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4062 writel(MPI2_WRSEQ_3RD_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4063 writel(MPI2_WRSEQ_4TH_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4064 writel(MPI2_WRSEQ_5TH_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4065 writel(MPI2_WRSEQ_6TH_KEY_VALUE
, &instance
->reg_set
->fusion_seq_offset
);
4067 /* Check that the diag write enable (DRWE) bit is on */
4068 host_diag
= megasas_readl(instance
, &instance
->reg_set
->fusion_host_diag
);
4070 while (!(host_diag
& HOST_DIAG_WRITE_ENABLE
)) {
4072 host_diag
= megasas_readl(instance
,
4073 &instance
->reg_set
->fusion_host_diag
);
4074 if (retry
++ == 100) {
4075 dev_warn(&instance
->pdev
->dev
,
4076 "Host diag unlock failed from %s %d\n",
4077 __func__
, __LINE__
);
4081 if (!(host_diag
& HOST_DIAG_WRITE_ENABLE
))
4084 /* Send chip reset command */
4085 writel(host_diag
| HOST_DIAG_RESET_ADAPTER
,
4086 &instance
->reg_set
->fusion_host_diag
);
4089 /* Make sure reset adapter bit is cleared */
4090 host_diag
= megasas_readl(instance
, &instance
->reg_set
->fusion_host_diag
);
4092 while (host_diag
& HOST_DIAG_RESET_ADAPTER
) {
4094 host_diag
= megasas_readl(instance
,
4095 &instance
->reg_set
->fusion_host_diag
);
4096 if (retry
++ == 1000) {
4097 dev_warn(&instance
->pdev
->dev
,
4098 "Diag reset adapter never cleared %s %d\n",
4099 __func__
, __LINE__
);
4103 if (host_diag
& HOST_DIAG_RESET_ADAPTER
)
4106 abs_state
= instance
->instancet
->read_fw_status_reg(instance
)
4110 while ((abs_state
<= MFI_STATE_FW_INIT
) && (retry
++ < 1000)) {
4112 abs_state
= instance
->instancet
->
4113 read_fw_status_reg(instance
) & MFI_STATE_MASK
;
4115 if (abs_state
<= MFI_STATE_FW_INIT
) {
4116 dev_warn(&instance
->pdev
->dev
,
4117 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
4118 abs_state
, __func__
, __LINE__
);
4126 * megasas_check_reset_fusion - For controller reset check
4127 * @instance: Controller's soft instance
4128 * @regs: MFI register set
4131 megasas_check_reset_fusion(struct megasas_instance
*instance
,
4132 struct megasas_register_set __iomem
*regs
)
4138 * megasas_trigger_snap_dump - Trigger snap dump in FW
4139 * @instance: Soft instance of adapter
4141 static inline void megasas_trigger_snap_dump(struct megasas_instance
*instance
)
4144 u32 fw_state
, abs_state
;
4146 if (!instance
->disableOnlineCtrlReset
) {
4147 dev_info(&instance
->pdev
->dev
, "Trigger snap dump\n");
4148 writel(MFI_ADP_TRIGGER_SNAP_DUMP
,
4149 &instance
->reg_set
->doorbell
);
4150 readl(&instance
->reg_set
->doorbell
);
4153 for (j
= 0; j
< instance
->snapdump_wait_time
; j
++) {
4154 abs_state
= instance
->instancet
->read_fw_status_reg(instance
);
4155 fw_state
= abs_state
& MFI_STATE_MASK
;
4156 if (fw_state
== MFI_STATE_FAULT
) {
4157 dev_printk(KERN_ERR
, &instance
->pdev
->dev
,
4158 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4159 abs_state
& MFI_STATE_FAULT_CODE
,
4160 abs_state
& MFI_STATE_FAULT_SUBCODE
, __func__
);
4167 /* This function waits for outstanding commands on fusion to complete */
4169 megasas_wait_for_outstanding_fusion(struct megasas_instance
*instance
,
4170 int reason
, int *convert
)
4172 int i
, outstanding
, retval
= 0, hb_seconds_missed
= 0;
4173 u32 fw_state
, abs_state
;
4174 u32 waittime_for_io_completion
;
4176 waittime_for_io_completion
=
4177 min_t(u32
, resetwaittime
,
4178 (resetwaittime
- instance
->snapdump_wait_time
));
4180 if (reason
== MFI_IO_TIMEOUT_OCR
) {
4181 dev_info(&instance
->pdev
->dev
,
4182 "MFI command is timed out\n");
4183 megasas_complete_cmd_dpc_fusion((unsigned long)instance
);
4184 if (instance
->snapdump_wait_time
)
4185 megasas_trigger_snap_dump(instance
);
4190 for (i
= 0; i
< waittime_for_io_completion
; i
++) {
4191 /* Check if firmware is in fault state */
4192 abs_state
= instance
->instancet
->read_fw_status_reg(instance
);
4193 fw_state
= abs_state
& MFI_STATE_MASK
;
4194 if (fw_state
== MFI_STATE_FAULT
) {
4195 dev_printk(KERN_ERR
, &instance
->pdev
->dev
,
4196 "FW in FAULT state Fault code:0x%x subcode:0x%x func:%s\n",
4197 abs_state
& MFI_STATE_FAULT_CODE
,
4198 abs_state
& MFI_STATE_FAULT_SUBCODE
, __func__
);
4199 megasas_complete_cmd_dpc_fusion((unsigned long)instance
);
4200 if (instance
->requestorId
&& reason
) {
4201 dev_warn(&instance
->pdev
->dev
, "SR-IOV Found FW in FAULT"
4202 " state while polling during"
4203 " I/O timeout handling for %d\n",
4204 instance
->host
->host_no
);
4213 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
4214 if (instance
->requestorId
&& !reason
) {
4219 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
4220 if (instance
->requestorId
&& (reason
== SCSIIO_TIMEOUT_OCR
)) {
4221 if (instance
->hb_host_mem
->HB
.fwCounter
!=
4222 instance
->hb_host_mem
->HB
.driverCounter
) {
4223 instance
->hb_host_mem
->HB
.driverCounter
=
4224 instance
->hb_host_mem
->HB
.fwCounter
;
4225 hb_seconds_missed
= 0;
4227 hb_seconds_missed
++;
4228 if (hb_seconds_missed
==
4229 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF
/HZ
)) {
4230 dev_warn(&instance
->pdev
->dev
, "SR-IOV:"
4231 " Heartbeat never completed "
4232 " while polling during I/O "
4233 " timeout handling for "
4235 instance
->host
->host_no
);
4243 megasas_complete_cmd_dpc_fusion((unsigned long)instance
);
4244 outstanding
= atomic_read(&instance
->fw_outstanding
);
4248 if (!(i
% MEGASAS_RESET_NOTICE_INTERVAL
)) {
4249 dev_notice(&instance
->pdev
->dev
, "[%2d]waiting for %d "
4250 "commands to complete for scsi%d\n", i
,
4251 outstanding
, instance
->host
->host_no
);
4256 if (instance
->snapdump_wait_time
) {
4257 megasas_trigger_snap_dump(instance
);
4262 if (atomic_read(&instance
->fw_outstanding
)) {
4263 dev_err(&instance
->pdev
->dev
, "pending commands remain after waiting, "
4264 "will reset adapter scsi%d.\n",
4265 instance
->host
->host_no
);
4271 if (!retval
&& reason
== SCSIIO_TIMEOUT_OCR
)
4272 dev_info(&instance
->pdev
->dev
, "IO is completed, no OCR is required\n");
4277 void megasas_reset_reply_desc(struct megasas_instance
*instance
)
4280 struct fusion_context
*fusion
;
4281 union MPI2_REPLY_DESCRIPTORS_UNION
*reply_desc
;
4283 fusion
= instance
->ctrl_context
;
4284 count
= instance
->msix_vectors
> 0 ? instance
->msix_vectors
: 1;
4285 count
+= instance
->iopoll_q_count
;
4287 for (i
= 0 ; i
< count
; i
++) {
4288 fusion
->last_reply_idx
[i
] = 0;
4289 reply_desc
= fusion
->reply_frames_desc
[i
];
4290 for (j
= 0 ; j
< fusion
->reply_q_depth
; j
++, reply_desc
++)
4291 reply_desc
->Words
= cpu_to_le64(ULLONG_MAX
);
4296 * megasas_refire_mgmt_cmd : Re-fire management commands
4297 * @instance: Controller's soft instance
4299 static void megasas_refire_mgmt_cmd(struct megasas_instance
*instance
,
4303 struct megasas_cmd_fusion
*cmd_fusion
;
4304 struct fusion_context
*fusion
;
4305 struct megasas_cmd
*cmd_mfi
;
4306 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
4307 struct MPI2_RAID_SCSI_IO_REQUEST
*scsi_io_req
;
4309 bool refire_cmd
= false;
4313 fusion
= instance
->ctrl_context
;
4315 /* Re-fire management commands.
4316 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4318 for (j
= instance
->max_scsi_cmds
; j
< instance
->max_fw_cmds
; j
++) {
4319 cmd_fusion
= fusion
->cmd_list
[j
];
4320 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
4321 smid
= le16_to_cpu(cmd_mfi
->context
.smid
);
4322 result
= REFIRE_CMD
;
4327 req_desc
= megasas_get_request_descriptor(instance
, smid
- 1);
4329 switch (cmd_mfi
->frame
->hdr
.cmd
) {
4331 opcode
= le32_to_cpu(cmd_mfi
->frame
->dcmd
.opcode
);
4332 /* Do not refire shutdown command */
4333 if (opcode
== MR_DCMD_CTRL_SHUTDOWN
) {
4334 cmd_mfi
->frame
->dcmd
.cmd_status
= MFI_STAT_OK
;
4335 result
= COMPLETE_CMD
;
4339 refire_cmd
= ((opcode
!= MR_DCMD_LD_MAP_GET_INFO
)) &&
4340 (opcode
!= MR_DCMD_SYSTEM_PD_MAP_GET_INFO
) &&
4341 !(cmd_mfi
->flags
& DRV_DCMD_SKIP_REFIRE
);
4344 result
= RETURN_CMD
;
4348 if (!instance
->support_nvme_passthru
) {
4349 cmd_mfi
->frame
->hdr
.cmd_status
= MFI_STAT_INVALID_CMD
;
4350 result
= COMPLETE_CMD
;
4354 case MFI_CMD_TOOLBOX
:
4355 if (!instance
->support_pci_lane_margining
) {
4356 cmd_mfi
->frame
->hdr
.cmd_status
= MFI_STAT_INVALID_CMD
;
4357 result
= COMPLETE_CMD
;
4365 if (return_ioctl
&& cmd_mfi
->sync_cmd
&&
4366 cmd_mfi
->frame
->hdr
.cmd
!= MFI_CMD_ABORT
) {
4367 dev_err(&instance
->pdev
->dev
,
4368 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x\n",
4369 __func__
, __LINE__
, cmd_mfi
->frame
->hdr
.cmd
,
4370 le32_to_cpu(cmd_mfi
->frame
->dcmd
.opcode
));
4371 cmd_mfi
->cmd_status_drv
= DCMD_BUSY
;
4372 result
= COMPLETE_CMD
;
4375 scsi_io_req
= (struct MPI2_RAID_SCSI_IO_REQUEST
*)
4376 cmd_fusion
->io_request
;
4377 if (scsi_io_req
->Function
== MPI2_FUNCTION_SCSI_TASK_MGMT
)
4378 result
= RETURN_CMD
;
4382 megasas_fire_cmd_fusion(instance
, req_desc
);
4385 megasas_return_cmd(instance
, cmd_mfi
);
4388 megasas_complete_cmd(instance
, cmd_mfi
, DID_OK
);
4395 * megasas_return_polled_cmds: Return polled mode commands back to the pool
4396 * before initiating an OCR.
4397 * @instance: Controller's soft instance
4400 megasas_return_polled_cmds(struct megasas_instance
*instance
)
4403 struct megasas_cmd_fusion
*cmd_fusion
;
4404 struct fusion_context
*fusion
;
4405 struct megasas_cmd
*cmd_mfi
;
4407 fusion
= instance
->ctrl_context
;
4409 for (i
= instance
->max_scsi_cmds
; i
< instance
->max_fw_cmds
; i
++) {
4410 cmd_fusion
= fusion
->cmd_list
[i
];
4411 cmd_mfi
= instance
->cmd_list
[cmd_fusion
->sync_cmd_idx
];
4413 if (cmd_mfi
->flags
& DRV_DCMD_POLLED_MODE
) {
4414 if (megasas_dbg_lvl
& OCR_DEBUG
)
4415 dev_info(&instance
->pdev
->dev
,
4416 "%s %d return cmd 0x%x opcode 0x%x\n",
4417 __func__
, __LINE__
, cmd_mfi
->frame
->hdr
.cmd
,
4418 le32_to_cpu(cmd_mfi
->frame
->dcmd
.opcode
));
4419 cmd_mfi
->flags
&= ~DRV_DCMD_POLLED_MODE
;
4420 megasas_return_cmd(instance
, cmd_mfi
);
4426 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4427 * @instance: per adapter struct
4428 * @channel: the channel assigned by the OS
4429 * @id: the id assigned by the OS
4431 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4434 static int megasas_track_scsiio(struct megasas_instance
*instance
,
4435 int id
, int channel
)
4438 struct megasas_cmd_fusion
*cmd_fusion
;
4439 struct fusion_context
*fusion
;
4440 fusion
= instance
->ctrl_context
;
4442 for (i
= 0 ; i
< instance
->max_scsi_cmds
; i
++) {
4443 cmd_fusion
= fusion
->cmd_list
[i
];
4444 if (cmd_fusion
->scmd
&&
4445 (cmd_fusion
->scmd
->device
->id
== id
&&
4446 cmd_fusion
->scmd
->device
->channel
== channel
)) {
4447 dev_info(&instance
->pdev
->dev
,
4448 "SCSI commands pending to target"
4449 "channel %d id %d \tSMID: 0x%x\n",
4450 channel
, id
, cmd_fusion
->index
);
4451 scsi_print_command(cmd_fusion
->scmd
);
4457 return found
? FAILED
: SUCCESS
;
4461 * megasas_tm_response_code - translation of device response code
4462 * @instance: Controller's soft instance
4463 * @mpi_reply: MPI reply returned by firmware
4468 megasas_tm_response_code(struct megasas_instance
*instance
,
4469 struct MPI2_SCSI_TASK_MANAGE_REPLY
*mpi_reply
)
4473 switch (mpi_reply
->ResponseCode
) {
4474 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE
:
4475 desc
= "task management request completed";
4477 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME
:
4478 desc
= "invalid frame";
4480 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED
:
4481 desc
= "task management request not supported";
4483 case MPI2_SCSITASKMGMT_RSP_TM_FAILED
:
4484 desc
= "task management request failed";
4486 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED
:
4487 desc
= "task management request succeeded";
4489 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN
:
4490 desc
= "invalid lun";
4493 desc
= "overlapped tag attempted";
4495 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC
:
4496 desc
= "task queued, however not sent to target";
4502 dev_dbg(&instance
->pdev
->dev
, "response_code(%01x): %s\n",
4503 mpi_reply
->ResponseCode
, desc
);
4504 dev_dbg(&instance
->pdev
->dev
,
4505 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4506 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4507 mpi_reply
->TerminationCount
, mpi_reply
->DevHandle
,
4508 mpi_reply
->Function
, mpi_reply
->TaskType
,
4509 mpi_reply
->IOCStatus
, mpi_reply
->IOCLogInfo
);
4513 * megasas_issue_tm - main routine for sending tm requests
4514 * @instance: per adapter struct
4515 * @device_handle: device handle
4516 * @channel: the channel assigned by the OS
4517 * @id: the id assigned by the OS
4518 * @smid_task: smid assigned to the task
4519 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4520 * @mr_device_priv_data: private data
4523 * MegaRaid use MPT interface for Task Magement request.
4524 * A generic API for sending task management requests to firmware.
4526 * Return SUCCESS or FAILED.
4529 megasas_issue_tm(struct megasas_instance
*instance
, u16 device_handle
,
4530 uint channel
, uint id
, u16 smid_task
, u8 type
,
4531 struct MR_PRIV_DEVICE
*mr_device_priv_data
)
4533 struct MR_TASK_MANAGE_REQUEST
*mr_request
;
4534 struct MPI2_SCSI_TASK_MANAGE_REQUEST
*mpi_request
;
4535 unsigned long timeleft
;
4536 struct megasas_cmd_fusion
*cmd_fusion
;
4537 struct megasas_cmd
*cmd_mfi
;
4538 union MEGASAS_REQUEST_DESCRIPTOR_UNION
*req_desc
;
4539 struct fusion_context
*fusion
= NULL
;
4540 struct megasas_cmd_fusion
*scsi_lookup
;
4542 int timeout
= MEGASAS_DEFAULT_TM_TIMEOUT
;
4543 struct MPI2_SCSI_TASK_MANAGE_REPLY
*mpi_reply
;
4545 fusion
= instance
->ctrl_context
;
4547 cmd_mfi
= megasas_get_cmd(instance
);
4550 dev_err(&instance
->pdev
->dev
, "Failed from %s %d\n",
4551 __func__
, __LINE__
);
4555 cmd_fusion
= megasas_get_cmd_fusion(instance
,
4556 instance
->max_scsi_cmds
+ cmd_mfi
->index
);
4558 /* Save the smid. To be used for returning the cmd */
4559 cmd_mfi
->context
.smid
= cmd_fusion
->index
;
4561 req_desc
= megasas_get_request_descriptor(instance
,
4562 (cmd_fusion
->index
- 1));
4564 cmd_fusion
->request_desc
= req_desc
;
4565 req_desc
->Words
= 0;
4567 mr_request
= (struct MR_TASK_MANAGE_REQUEST
*) cmd_fusion
->io_request
;
4568 memset(mr_request
, 0, sizeof(struct MR_TASK_MANAGE_REQUEST
));
4569 mpi_request
= (struct MPI2_SCSI_TASK_MANAGE_REQUEST
*) &mr_request
->TmRequest
;
4570 mpi_request
->Function
= MPI2_FUNCTION_SCSI_TASK_MGMT
;
4571 mpi_request
->DevHandle
= cpu_to_le16(device_handle
);
4572 mpi_request
->TaskType
= type
;
4573 mpi_request
->TaskMID
= cpu_to_le16(smid_task
);
4574 mpi_request
->LUN
[1] = 0;
4577 req_desc
= cmd_fusion
->request_desc
;
4578 req_desc
->HighPriority
.SMID
= cpu_to_le16(cmd_fusion
->index
);
4579 req_desc
->HighPriority
.RequestFlags
=
4580 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
<<
4581 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT
);
4582 req_desc
->HighPriority
.MSIxIndex
= 0;
4583 req_desc
->HighPriority
.LMID
= 0;
4584 req_desc
->HighPriority
.Reserved1
= 0;
4586 if (channel
< MEGASAS_MAX_PD_CHANNELS
)
4587 mr_request
->tmReqFlags
.isTMForPD
= 1;
4589 mr_request
->tmReqFlags
.isTMForLD
= 1;
4591 init_completion(&cmd_fusion
->done
);
4592 megasas_fire_cmd_fusion(instance
, req_desc
);
4595 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK
:
4596 timeout
= mr_device_priv_data
->task_abort_tmo
;
4598 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET
:
4599 timeout
= mr_device_priv_data
->target_reset_tmo
;
4603 timeleft
= wait_for_completion_timeout(&cmd_fusion
->done
, timeout
* HZ
);
4606 dev_err(&instance
->pdev
->dev
,
4607 "task mgmt type 0x%x timed out\n", type
);
4608 mutex_unlock(&instance
->reset_mutex
);
4609 rc
= megasas_reset_fusion(instance
->host
, MFI_IO_TIMEOUT_OCR
);
4610 mutex_lock(&instance
->reset_mutex
);
4614 mpi_reply
= (struct MPI2_SCSI_TASK_MANAGE_REPLY
*) &mr_request
->TMReply
;
4615 megasas_tm_response_code(instance
, mpi_reply
);
4617 megasas_return_cmd(instance
, cmd_mfi
);
4620 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK
:
4621 scsi_lookup
= fusion
->cmd_list
[smid_task
- 1];
4623 if (scsi_lookup
->scmd
== NULL
)
4626 instance
->instancet
->disable_intr(instance
);
4627 megasas_sync_irqs((unsigned long)instance
);
4628 instance
->instancet
->enable_intr(instance
);
4629 megasas_enable_irq_poll(instance
);
4630 if (scsi_lookup
->scmd
== NULL
)
4636 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET
:
4637 if ((channel
== 0xFFFFFFFF) && (id
== 0xFFFFFFFF))
4639 instance
->instancet
->disable_intr(instance
);
4640 megasas_sync_irqs((unsigned long)instance
);
4641 rc
= megasas_track_scsiio(instance
, id
, channel
);
4642 instance
->instancet
->enable_intr(instance
);
4643 megasas_enable_irq_poll(instance
);
4646 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET
:
4647 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK
:
4659 * megasas_fusion_smid_lookup : Look for fusion command corresponding to SCSI
4660 * @instance: per adapter struct
4662 * Return Non Zero index, if SMID found in outstanding commands
4664 static u16
megasas_fusion_smid_lookup(struct scsi_cmnd
*scmd
)
4667 struct megasas_instance
*instance
;
4668 struct megasas_cmd_fusion
*cmd_fusion
;
4669 struct fusion_context
*fusion
;
4671 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
4673 fusion
= instance
->ctrl_context
;
4675 for (i
= 0; i
< instance
->max_scsi_cmds
; i
++) {
4676 cmd_fusion
= fusion
->cmd_list
[i
];
4677 if (cmd_fusion
->scmd
&& (cmd_fusion
->scmd
== scmd
)) {
4678 scmd_printk(KERN_NOTICE
, scmd
, "Abort request is for"
4679 " SMID: %d\n", cmd_fusion
->index
);
4680 ret
= cmd_fusion
->index
;
4689 * megasas_get_tm_devhandle - Get devhandle for TM request
4690 * @sdev- OS provided scsi device
4692 * Returns- devhandle/targetID of SCSI device
4694 static u16
megasas_get_tm_devhandle(struct scsi_device
*sdev
)
4698 struct megasas_instance
*instance
;
4699 struct fusion_context
*fusion
;
4700 struct MR_PD_CFG_SEQ_NUM_SYNC
*pd_sync
;
4701 u16 devhandle
= (u16
)ULONG_MAX
;
4703 instance
= (struct megasas_instance
*)sdev
->host
->hostdata
;
4704 fusion
= instance
->ctrl_context
;
4706 if (!MEGASAS_IS_LOGICAL(sdev
)) {
4707 if (instance
->use_seqnum_jbod_fp
) {
4708 pd_index
= (sdev
->channel
* MEGASAS_MAX_DEV_PER_CHANNEL
)
4710 pd_sync
= (void *)fusion
->pd_seq_sync
4711 [(instance
->pd_seq_map_id
- 1) & 1];
4712 devhandle
= pd_sync
->seq
[pd_index
].devHandle
;
4714 sdev_printk(KERN_ERR
, sdev
, "Firmware expose tmCapable"
4715 " without JBOD MAP support from %s %d\n", __func__
, __LINE__
);
4717 device_id
= ((sdev
->channel
% 2) * MEGASAS_MAX_DEV_PER_CHANNEL
)
4719 devhandle
= device_id
;
4726 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4727 * @scmd : pointer to scsi command object
4729 * Return SUCCESS, if command aborted else FAILED
4732 int megasas_task_abort_fusion(struct scsi_cmnd
*scmd
)
4734 struct megasas_instance
*instance
;
4735 u16 smid
, devhandle
;
4737 struct MR_PRIV_DEVICE
*mr_device_priv_data
;
4738 mr_device_priv_data
= scmd
->device
->hostdata
;
4740 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
4742 if (atomic_read(&instance
->adprecovery
) != MEGASAS_HBA_OPERATIONAL
) {
4743 dev_err(&instance
->pdev
->dev
, "Controller is not OPERATIONAL,"
4744 "SCSI host:%d\n", instance
->host
->host_no
);
4749 if (!mr_device_priv_data
) {
4750 sdev_printk(KERN_INFO
, scmd
->device
, "device been deleted! "
4751 "scmd(%p)\n", scmd
);
4752 scmd
->result
= DID_NO_CONNECT
<< 16;
4757 if (!mr_device_priv_data
->is_tm_capable
) {
4762 mutex_lock(&instance
->reset_mutex
);
4764 smid
= megasas_fusion_smid_lookup(scmd
);
4768 scmd_printk(KERN_NOTICE
, scmd
, "Command for which abort is"
4769 " issued is not found in outstanding commands\n");
4770 mutex_unlock(&instance
->reset_mutex
);
4774 devhandle
= megasas_get_tm_devhandle(scmd
->device
);
4776 if (devhandle
== (u16
)ULONG_MAX
) {
4778 sdev_printk(KERN_INFO
, scmd
->device
,
4779 "task abort issued for invalid devhandle\n");
4780 mutex_unlock(&instance
->reset_mutex
);
4783 sdev_printk(KERN_INFO
, scmd
->device
,
4784 "attempting task abort! scmd(0x%p) tm_dev_handle 0x%x\n",
4787 mr_device_priv_data
->tm_busy
= true;
4788 ret
= megasas_issue_tm(instance
, devhandle
,
4789 scmd
->device
->channel
, scmd
->device
->id
, smid
,
4790 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK
,
4791 mr_device_priv_data
);
4792 mr_device_priv_data
->tm_busy
= false;
4794 mutex_unlock(&instance
->reset_mutex
);
4795 scmd_printk(KERN_INFO
, scmd
, "task abort %s!! scmd(0x%p)\n",
4796 ((ret
== SUCCESS
) ? "SUCCESS" : "FAILED"), scmd
);
4798 scsi_print_command(scmd
);
4799 if (megasas_dbg_lvl
& TM_DEBUG
)
4800 megasas_dump_fusion_io(scmd
);
4806 * megasas_reset_target_fusion : target reset function for fusion adapters
4807 * scmd: SCSI command pointer
4809 * Returns SUCCESS if all commands associated with target aborted else FAILED
4812 int megasas_reset_target_fusion(struct scsi_cmnd
*scmd
)
4815 struct megasas_instance
*instance
;
4818 struct MR_PRIV_DEVICE
*mr_device_priv_data
;
4819 mr_device_priv_data
= scmd
->device
->hostdata
;
4821 instance
= (struct megasas_instance
*)scmd
->device
->host
->hostdata
;
4823 if (atomic_read(&instance
->adprecovery
) != MEGASAS_HBA_OPERATIONAL
) {
4824 dev_err(&instance
->pdev
->dev
, "Controller is not OPERATIONAL,"
4825 "SCSI host:%d\n", instance
->host
->host_no
);
4830 if (!mr_device_priv_data
) {
4831 sdev_printk(KERN_INFO
, scmd
->device
,
4832 "device been deleted! scmd: (0x%p)\n", scmd
);
4833 scmd
->result
= DID_NO_CONNECT
<< 16;
4838 if (!mr_device_priv_data
->is_tm_capable
) {
4843 mutex_lock(&instance
->reset_mutex
);
4844 devhandle
= megasas_get_tm_devhandle(scmd
->device
);
4846 if (devhandle
== (u16
)ULONG_MAX
) {
4848 sdev_printk(KERN_INFO
, scmd
->device
,
4849 "target reset issued for invalid devhandle\n");
4850 mutex_unlock(&instance
->reset_mutex
);
4854 sdev_printk(KERN_INFO
, scmd
->device
,
4855 "attempting target reset! scmd(0x%p) tm_dev_handle: 0x%x\n",
4857 mr_device_priv_data
->tm_busy
= true;
4858 ret
= megasas_issue_tm(instance
, devhandle
,
4859 scmd
->device
->channel
, scmd
->device
->id
, 0,
4860 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET
,
4861 mr_device_priv_data
);
4862 mr_device_priv_data
->tm_busy
= false;
4863 mutex_unlock(&instance
->reset_mutex
);
4864 scmd_printk(KERN_NOTICE
, scmd
, "target reset %s!!\n",
4865 (ret
== SUCCESS
) ? "SUCCESS" : "FAILED");
4871 /*SRIOV get other instance in cluster if any*/
4873 megasas_instance
*megasas_get_peer_instance(struct megasas_instance
*instance
)
4877 for (i
= 0; i
< MAX_MGMT_ADAPTERS
; i
++) {
4878 if (megasas_mgmt_info
.instance
[i
] &&
4879 (megasas_mgmt_info
.instance
[i
] != instance
) &&
4880 megasas_mgmt_info
.instance
[i
]->requestorId
&&
4881 megasas_mgmt_info
.instance
[i
]->peerIsPresent
&&
4882 (memcmp((megasas_mgmt_info
.instance
[i
]->clusterId
),
4883 instance
->clusterId
, MEGASAS_CLUSTER_ID_SIZE
) == 0))
4884 return megasas_mgmt_info
.instance
[i
];
4889 /* Check for a second path that is currently UP */
4890 int megasas_check_mpio_paths(struct megasas_instance
*instance
,
4891 struct scsi_cmnd
*scmd
)
4893 struct megasas_instance
*peer_instance
= NULL
;
4894 int retval
= (DID_REQUEUE
<< 16);
4896 if (instance
->peerIsPresent
) {
4897 peer_instance
= megasas_get_peer_instance(instance
);
4898 if ((peer_instance
) &&
4899 (atomic_read(&peer_instance
->adprecovery
) ==
4900 MEGASAS_HBA_OPERATIONAL
))
4901 retval
= (DID_NO_CONNECT
<< 16);
4906 /* Core fusion reset function */
4907 int megasas_reset_fusion(struct Scsi_Host
*shost
, int reason
)
4909 int retval
= SUCCESS
, i
, j
, convert
= 0;
4910 struct megasas_instance
*instance
;
4911 struct megasas_cmd_fusion
*cmd_fusion
, *r1_cmd
;
4912 struct fusion_context
*fusion
;
4913 u32 abs_state
, status_reg
, reset_adapter
, fpio_count
= 0;
4914 u32 io_timeout_in_crash_mode
= 0;
4915 struct scsi_cmnd
*scmd_local
= NULL
;
4916 struct scsi_device
*sdev
;
4917 int ret_target_prop
= DCMD_FAILED
;
4918 bool is_target_prop
= false;
4919 bool do_adp_reset
= true;
4920 int max_reset_tries
= MEGASAS_FUSION_MAX_RESET_TRIES
;
4922 instance
= (struct megasas_instance
*)shost
->hostdata
;
4923 fusion
= instance
->ctrl_context
;
4925 mutex_lock(&instance
->reset_mutex
);
4927 if (atomic_read(&instance
->adprecovery
) == MEGASAS_HW_CRITICAL_ERROR
) {
4928 dev_warn(&instance
->pdev
->dev
, "Hardware critical error, "
4929 "returning FAILED for scsi%d.\n",
4930 instance
->host
->host_no
);
4931 mutex_unlock(&instance
->reset_mutex
);
4934 status_reg
= instance
->instancet
->read_fw_status_reg(instance
);
4935 abs_state
= status_reg
& MFI_STATE_MASK
;
4937 /* IO timeout detected, forcibly put FW in FAULT state */
4938 if (abs_state
!= MFI_STATE_FAULT
&& instance
->crash_dump_buf
&&
4939 instance
->crash_dump_app_support
&& reason
) {
4940 dev_info(&instance
->pdev
->dev
, "IO/DCMD timeout is detected, "
4941 "forcibly FAULT Firmware\n");
4942 atomic_set(&instance
->adprecovery
, MEGASAS_ADPRESET_SM_INFAULT
);
4943 status_reg
= megasas_readl(instance
, &instance
->reg_set
->doorbell
);
4944 writel(status_reg
| MFI_STATE_FORCE_OCR
,
4945 &instance
->reg_set
->doorbell
);
4946 readl(&instance
->reg_set
->doorbell
);
4947 mutex_unlock(&instance
->reset_mutex
);
4950 io_timeout_in_crash_mode
++;
4951 dev_dbg(&instance
->pdev
->dev
, "waiting for [%d] "
4952 "seconds for crash dump collection and OCR "
4953 "to be done\n", (io_timeout_in_crash_mode
* 3));
4954 } while ((atomic_read(&instance
->adprecovery
) != MEGASAS_HBA_OPERATIONAL
) &&
4955 (io_timeout_in_crash_mode
< 80));
4957 if (atomic_read(&instance
->adprecovery
) == MEGASAS_HBA_OPERATIONAL
) {
4958 dev_info(&instance
->pdev
->dev
, "OCR done for IO "
4962 dev_info(&instance
->pdev
->dev
, "Controller is not "
4963 "operational after 240 seconds wait for IO "
4964 "timeout case in FW crash dump mode\n do "
4965 "OCR/kill adapter\n");
4966 retval
= megasas_reset_fusion(shost
, 0);
4971 if (instance
->requestorId
&& !instance
->skip_heartbeat_timer_del
)
4972 del_timer_sync(&instance
->sriov_heartbeat_timer
);
4973 set_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
4974 set_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE
, &instance
->reset_flags
);
4975 atomic_set(&instance
->adprecovery
, MEGASAS_ADPRESET_SM_POLLING
);
4976 instance
->instancet
->disable_intr(instance
);
4977 megasas_sync_irqs((unsigned long)instance
);
4979 /* First try waiting for commands to complete */
4980 if (megasas_wait_for_outstanding_fusion(instance
, reason
,
4982 atomic_set(&instance
->adprecovery
, MEGASAS_ADPRESET_SM_INFAULT
);
4983 dev_warn(&instance
->pdev
->dev
, "resetting fusion "
4984 "adapter scsi%d.\n", instance
->host
->host_no
);
4988 if (megasas_dbg_lvl
& OCR_DEBUG
)
4989 dev_info(&instance
->pdev
->dev
, "\nPending SCSI commands:\n");
4991 /* Now return commands back to the OS */
4992 for (i
= 0 ; i
< instance
->max_scsi_cmds
; i
++) {
4993 cmd_fusion
= fusion
->cmd_list
[i
];
4994 /*check for extra commands issued by driver*/
4995 if (instance
->adapter_type
>= VENTURA_SERIES
) {
4996 r1_cmd
= fusion
->cmd_list
[i
+ instance
->max_fw_cmds
];
4997 megasas_return_cmd_fusion(instance
, r1_cmd
);
4999 scmd_local
= cmd_fusion
->scmd
;
5000 if (cmd_fusion
->scmd
) {
5001 if (megasas_dbg_lvl
& OCR_DEBUG
) {
5002 sdev_printk(KERN_INFO
,
5003 cmd_fusion
->scmd
->device
, "SMID: 0x%x\n",
5005 megasas_dump_fusion_io(cmd_fusion
->scmd
);
5008 if (cmd_fusion
->io_request
->Function
==
5009 MPI2_FUNCTION_SCSI_IO_REQUEST
)
5012 scmd_local
->result
=
5013 megasas_check_mpio_paths(instance
,
5015 if (instance
->ldio_threshold
&&
5016 megasas_cmd_type(scmd_local
) == READ_WRITE_LDIO
)
5017 atomic_dec(&instance
->ldio_outstanding
);
5018 megasas_return_cmd_fusion(instance
, cmd_fusion
);
5019 scsi_dma_unmap(scmd_local
);
5020 scsi_done(scmd_local
);
5024 dev_info(&instance
->pdev
->dev
, "Outstanding fastpath IOs: %d\n",
5027 atomic_set(&instance
->fw_outstanding
, 0);
5029 status_reg
= instance
->instancet
->read_fw_status_reg(instance
);
5030 abs_state
= status_reg
& MFI_STATE_MASK
;
5031 reset_adapter
= status_reg
& MFI_RESET_ADAPTER
;
5032 if (instance
->disableOnlineCtrlReset
||
5033 (abs_state
== MFI_STATE_FAULT
&& !reset_adapter
)) {
5034 /* Reset not supported, kill adapter */
5035 dev_warn(&instance
->pdev
->dev
, "Reset not supported"
5036 ", killing adapter scsi%d.\n",
5037 instance
->host
->host_no
);
5041 /* Let SR-IOV VF & PF sync up if there was a HB failure */
5042 if (instance
->requestorId
&& !reason
) {
5043 msleep(MEGASAS_OCR_SETTLE_TIME_VF
);
5044 do_adp_reset
= false;
5045 max_reset_tries
= MEGASAS_SRIOV_MAX_RESET_TRIES_VF
;
5048 /* Now try to reset the chip */
5049 for (i
= 0; i
< max_reset_tries
; i
++) {
5051 * Do adp reset and wait for
5052 * controller to transition to ready
5054 if (megasas_adp_reset_wait_for_ready(instance
,
5055 do_adp_reset
, 1) == FAILED
)
5058 /* Wait for FW to become ready */
5059 if (megasas_transition_to_ready(instance
, 1)) {
5060 dev_warn(&instance
->pdev
->dev
,
5061 "Failed to transition controller to ready for "
5062 "scsi%d.\n", instance
->host
->host_no
);
5065 megasas_reset_reply_desc(instance
);
5066 megasas_fusion_update_can_queue(instance
, OCR_CONTEXT
);
5068 if (megasas_ioc_init_fusion(instance
)) {
5072 if (megasas_get_ctrl_info(instance
)) {
5073 dev_info(&instance
->pdev
->dev
,
5074 "Failed from %s %d\n",
5075 __func__
, __LINE__
);
5079 megasas_refire_mgmt_cmd(instance
,
5080 (i
== (MEGASAS_FUSION_MAX_RESET_TRIES
- 1)
5083 /* Reset load balance info */
5084 if (fusion
->load_balance_info
)
5085 memset(fusion
->load_balance_info
, 0,
5086 (sizeof(struct LD_LOAD_BALANCE_INFO
) *
5087 MAX_LOGICAL_DRIVES_EXT
));
5089 if (!megasas_get_map_info(instance
)) {
5090 megasas_sync_map_info(instance
);
5093 * Return pending polled mode cmds before
5096 megasas_return_polled_cmds(instance
);
5100 megasas_setup_jbod_map(instance
);
5102 /* reset stream detection array */
5103 if (instance
->adapter_type
>= VENTURA_SERIES
) {
5104 for (j
= 0; j
< MAX_LOGICAL_DRIVES_EXT
; ++j
) {
5105 memset(fusion
->stream_detect_by_ld
[j
],
5106 0, sizeof(struct LD_STREAM_DETECT
));
5107 fusion
->stream_detect_by_ld
[j
]->mru_bit_map
5112 clear_bit(MEGASAS_FUSION_IN_RESET
,
5113 &instance
->reset_flags
);
5114 instance
->instancet
->enable_intr(instance
);
5115 megasas_enable_irq_poll(instance
);
5116 shost_for_each_device(sdev
, shost
) {
5117 if ((instance
->tgt_prop
) &&
5118 (instance
->nvme_page_size
))
5119 ret_target_prop
= megasas_get_target_prop(instance
, sdev
);
5121 is_target_prop
= (ret_target_prop
== DCMD_SUCCESS
) ? true : false;
5122 megasas_set_dynamic_target_properties(sdev
, NULL
,
5126 status_reg
= instance
->instancet
->read_fw_status_reg
5128 abs_state
= status_reg
& MFI_STATE_MASK
;
5129 if (abs_state
!= MFI_STATE_OPERATIONAL
) {
5130 dev_info(&instance
->pdev
->dev
,
5131 "Adapter is not OPERATIONAL, state 0x%x for scsi:%d\n",
5132 abs_state
, instance
->host
->host_no
);
5135 atomic_set(&instance
->adprecovery
, MEGASAS_HBA_OPERATIONAL
);
5137 dev_info(&instance
->pdev
->dev
,
5138 "Adapter is OPERATIONAL for scsi:%d\n",
5139 instance
->host
->host_no
);
5141 /* Restart SR-IOV heartbeat */
5142 if (instance
->requestorId
) {
5143 if (!megasas_sriov_start_heartbeat(instance
, 0))
5144 megasas_start_timer(instance
);
5146 instance
->skip_heartbeat_timer_del
= 1;
5149 if (instance
->crash_dump_drv_support
&&
5150 instance
->crash_dump_app_support
)
5151 megasas_set_crash_dump_params(instance
,
5152 MR_CRASH_BUF_TURN_ON
);
5154 megasas_set_crash_dump_params(instance
,
5155 MR_CRASH_BUF_TURN_OFF
);
5157 if (instance
->snapdump_wait_time
) {
5158 megasas_get_snapdump_properties(instance
);
5159 dev_info(&instance
->pdev
->dev
,
5160 "Snap dump wait time\t: %d\n",
5161 instance
->snapdump_wait_time
);
5166 /* Adapter reset completed successfully */
5167 dev_warn(&instance
->pdev
->dev
,
5168 "Reset successful for scsi%d.\n",
5169 instance
->host
->host_no
);
5173 /* Reset failed, kill the adapter */
5174 dev_warn(&instance
->pdev
->dev
, "Reset failed, killing "
5175 "adapter scsi%d.\n", instance
->host
->host_no
);
5178 /* For VF: Restart HB timer if we didn't OCR */
5179 if (instance
->requestorId
) {
5180 megasas_start_timer(instance
);
5182 clear_bit(MEGASAS_FUSION_IN_RESET
, &instance
->reset_flags
);
5183 instance
->instancet
->enable_intr(instance
);
5184 megasas_enable_irq_poll(instance
);
5185 atomic_set(&instance
->adprecovery
, MEGASAS_HBA_OPERATIONAL
);
5189 megaraid_sas_kill_hba(instance
);
5190 megasas_enable_irq_poll(instance
);
5191 instance
->skip_heartbeat_timer_del
= 1;
5194 clear_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE
, &instance
->reset_flags
);
5195 mutex_unlock(&instance
->reset_mutex
);
5199 /* Fusion Crash dump collection */
5200 static void megasas_fusion_crash_dump(struct megasas_instance
*instance
)
5203 u8 partial_copy
= 0;
5207 status_reg
= instance
->instancet
->read_fw_status_reg(instance
);
5210 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
5211 * to host crash buffers
5213 if (instance
->drv_buf_index
== 0) {
5214 /* Buffer is already allocated for old Crash dump.
5215 * Do OCR and do not wait for crash dump collection
5217 if (instance
->drv_buf_alloc
) {
5218 dev_info(&instance
->pdev
->dev
, "earlier crash dump is "
5219 "not yet copied by application, ignoring this "
5220 "crash dump and initiating OCR\n");
5221 status_reg
|= MFI_STATE_CRASH_DUMP_DONE
;
5223 &instance
->reg_set
->outbound_scratch_pad_0
);
5224 readl(&instance
->reg_set
->outbound_scratch_pad_0
);
5227 megasas_alloc_host_crash_buffer(instance
);
5228 dev_info(&instance
->pdev
->dev
, "Number of host crash buffers "
5229 "allocated: %d\n", instance
->drv_buf_alloc
);
5232 while (!(status_reg
& MFI_STATE_CRASH_DUMP_DONE
) &&
5233 (wait
< MEGASAS_WATCHDOG_WAIT_COUNT
)) {
5234 if (!(status_reg
& MFI_STATE_DMADONE
)) {
5236 * Next crash dump buffer is not yet DMA'd by FW
5237 * Check after 10ms. Wait for 1 second for FW to
5238 * post the next buffer. If not bail out.
5241 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS
);
5242 status_reg
= instance
->instancet
->read_fw_status_reg(
5248 if (instance
->drv_buf_index
>= instance
->drv_buf_alloc
) {
5249 dev_info(&instance
->pdev
->dev
,
5250 "Driver is done copying the buffer: %d\n",
5251 instance
->drv_buf_alloc
);
5252 status_reg
|= MFI_STATE_CRASH_DUMP_DONE
;
5256 memcpy(instance
->crash_buf
[instance
->drv_buf_index
],
5257 instance
->crash_dump_buf
, CRASH_DMA_BUF_SIZE
);
5258 instance
->drv_buf_index
++;
5259 status_reg
&= ~MFI_STATE_DMADONE
;
5262 writel(status_reg
, &instance
->reg_set
->outbound_scratch_pad_0
);
5263 readl(&instance
->reg_set
->outbound_scratch_pad_0
);
5265 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS
);
5266 status_reg
= instance
->instancet
->read_fw_status_reg(instance
);
5269 if (status_reg
& MFI_STATE_CRASH_DUMP_DONE
) {
5270 dev_info(&instance
->pdev
->dev
, "Crash Dump is available,number "
5271 "of copied buffers: %d\n", instance
->drv_buf_index
);
5272 instance
->fw_crash_buffer_size
= instance
->drv_buf_index
;
5273 instance
->fw_crash_state
= AVAILABLE
;
5274 instance
->drv_buf_index
= 0;
5275 writel(status_reg
, &instance
->reg_set
->outbound_scratch_pad_0
);
5276 readl(&instance
->reg_set
->outbound_scratch_pad_0
);
5278 megasas_reset_fusion(instance
->host
, 0);
5283 /* Fusion OCR work queue */
5284 void megasas_fusion_ocr_wq(struct work_struct
*work
)
5286 struct megasas_instance
*instance
=
5287 container_of(work
, struct megasas_instance
, work_init
);
5289 megasas_reset_fusion(instance
->host
, 0);
5292 /* Allocate fusion context */
5294 megasas_alloc_fusion_context(struct megasas_instance
*instance
)
5296 struct fusion_context
*fusion
;
5298 instance
->ctrl_context
= kzalloc(sizeof(struct fusion_context
),
5300 if (!instance
->ctrl_context
) {
5301 dev_err(&instance
->pdev
->dev
, "Failed from %s %d\n",
5302 __func__
, __LINE__
);
5306 fusion
= instance
->ctrl_context
;
5308 fusion
->log_to_span_pages
= get_order(MAX_LOGICAL_DRIVES_EXT
*
5309 sizeof(LD_SPAN_INFO
));
5310 fusion
->log_to_span
=
5311 (PLD_SPAN_INFO
)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
5312 fusion
->log_to_span_pages
);
5313 if (!fusion
->log_to_span
) {
5314 fusion
->log_to_span
=
5315 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT
,
5316 sizeof(LD_SPAN_INFO
)));
5317 if (!fusion
->log_to_span
) {
5318 dev_err(&instance
->pdev
->dev
, "Failed from %s %d\n",
5319 __func__
, __LINE__
);
5324 fusion
->load_balance_info_pages
= get_order(MAX_LOGICAL_DRIVES_EXT
*
5325 sizeof(struct LD_LOAD_BALANCE_INFO
));
5326 fusion
->load_balance_info
=
5327 (struct LD_LOAD_BALANCE_INFO
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
5328 fusion
->load_balance_info_pages
);
5329 if (!fusion
->load_balance_info
) {
5330 fusion
->load_balance_info
=
5331 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT
,
5332 sizeof(struct LD_LOAD_BALANCE_INFO
)));
5333 if (!fusion
->load_balance_info
)
5334 dev_err(&instance
->pdev
->dev
, "Failed to allocate load_balance_info, "
5335 "continuing without Load Balance support\n");
5342 megasas_free_fusion_context(struct megasas_instance
*instance
)
5344 struct fusion_context
*fusion
= instance
->ctrl_context
;
5347 if (fusion
->load_balance_info
) {
5348 if (is_vmalloc_addr(fusion
->load_balance_info
))
5349 vfree(fusion
->load_balance_info
);
5351 free_pages((ulong
)fusion
->load_balance_info
,
5352 fusion
->load_balance_info_pages
);
5355 if (fusion
->log_to_span
) {
5356 if (is_vmalloc_addr(fusion
->log_to_span
))
5357 vfree(fusion
->log_to_span
);
5359 free_pages((ulong
)fusion
->log_to_span
,
5360 fusion
->log_to_span_pages
);
5367 struct megasas_instance_template megasas_instance_template_fusion
= {
5368 .enable_intr
= megasas_enable_intr_fusion
,
5369 .disable_intr
= megasas_disable_intr_fusion
,
5370 .clear_intr
= megasas_clear_intr_fusion
,
5371 .read_fw_status_reg
= megasas_read_fw_status_reg_fusion
,
5372 .adp_reset
= megasas_adp_reset_fusion
,
5373 .check_reset
= megasas_check_reset_fusion
,
5374 .service_isr
= megasas_isr_fusion
,
5375 .tasklet
= megasas_complete_cmd_dpc_fusion
,
5376 .init_adapter
= megasas_init_adapter_fusion
,
5377 .build_and_issue_cmd
= megasas_build_and_issue_cmd_fusion
,
5378 .issue_dcmd
= megasas_issue_dcmd_fusion
,