2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * FILE: megaraid_sas_fusion.c
22 * Authors: Avago Technologies
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
28 * Send feedback to: megaraidlinux.pdl@avagotech.com
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/pci.h>
37 #include <linux/list.h>
38 #include <linux/moduleparam.h>
39 #include <linux/module.h>
40 #include <linux/spinlock.h>
41 #include <linux/interrupt.h>
42 #include <linux/delay.h>
43 #include <linux/uio.h>
44 #include <linux/uaccess.h>
46 #include <linux/compat.h>
47 #include <linux/blkdev.h>
48 #include <linux/mutex.h>
49 #include <linux/poll.h>
50 #include <linux/vmalloc.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_device.h>
55 #include <scsi/scsi_host.h>
56 #include <scsi/scsi_dbg.h>
57 #include <linux/dmi.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
63 extern void megasas_free_cmds(struct megasas_instance *instance);
64 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
67 megasas_complete_cmd(struct megasas_instance *instance,
68 struct megasas_cmd *cmd, u8 alt_status);
70 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
74 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
75 int megasas_alloc_cmds(struct megasas_instance *instance);
77 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
79 megasas_issue_polled(struct megasas_instance *instance,
80 struct megasas_cmd *cmd);
82 megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
84 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
85 void megaraid_sas_kill_hba(struct megasas_instance *instance);
87 extern u32 megasas_dbg_lvl;
88 void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
89 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
91 void megasas_start_timer(struct megasas_instance *instance,
92 struct timer_list *timer,
93 void *fn, unsigned long interval);
94 extern struct megasas_mgmt_info megasas_mgmt_info;
95 extern unsigned int resetwaittime;
96 extern unsigned int dual_qdepth_disable;
97 static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
98 static void megasas_free_reply_fusion(struct megasas_instance *instance);
103 * megasas_enable_intr_fusion - Enables interrupts
104 * @regs: MFI register set
107 megasas_enable_intr_fusion(struct megasas_instance *instance)
109 struct megasas_register_set __iomem *regs;
110 regs = instance->reg_set;
112 instance->mask_interrupts = 0;
113 /* For Thunderbolt/Invader also clear intr on enable */
114 writel(~0, ®s->outbound_intr_status);
115 readl(®s->outbound_intr_status);
117 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
119 /* Dummy readl to force pci flush */
120 readl(®s->outbound_intr_mask);
124 * megasas_disable_intr_fusion - Disables interrupt
125 * @regs: MFI register set
128 megasas_disable_intr_fusion(struct megasas_instance *instance)
130 u32 mask = 0xFFFFFFFF;
132 struct megasas_register_set __iomem *regs;
133 regs = instance->reg_set;
134 instance->mask_interrupts = 1;
136 writel(mask, ®s->outbound_intr_mask);
137 /* Dummy readl to force pci flush */
138 status = readl(®s->outbound_intr_mask);
142 megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
146 * Check if it is our interrupt
148 status = readl(®s->outbound_intr_status);
151 writel(status, ®s->outbound_intr_status);
152 readl(®s->outbound_intr_status);
155 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
162 * megasas_get_cmd_fusion - Get a command from the free pool
163 * @instance: Adapter soft state
165 * Returns a blk_tag indexed mpt frame
167 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
168 *instance, u32 blk_tag)
170 struct fusion_context *fusion;
172 fusion = instance->ctrl_context;
173 return fusion->cmd_list[blk_tag];
177 * megasas_return_cmd_fusion - Return a cmd to free command pool
178 * @instance: Adapter soft state
179 * @cmd: Command packet to be returned to free command pool
181 inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
182 struct megasas_cmd_fusion *cmd)
185 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
186 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
187 cmd->cmd_completed = false;
191 * megasas_fire_cmd_fusion - Sends command to the FW
192 * @instance: Adapter soft state
193 * @req_desc: 32bit or 64bit Request descriptor
195 * Perform PCI Write. Ventura supports 32 bit Descriptor.
196 * Prior to Ventura (12G) MR controller supports 64 bit Descriptor.
200 megasas_fire_cmd_fusion(struct megasas_instance *instance,
201 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
203 if (instance->is_ventura)
204 writel(le32_to_cpu(req_desc->u.low),
205 &instance->reg_set->inbound_single_queue_port);
207 #if defined(writeq) && defined(CONFIG_64BIT)
208 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
209 le32_to_cpu(req_desc->u.low));
211 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
214 spin_lock_irqsave(&instance->hba_lock, flags);
215 writel(le32_to_cpu(req_desc->u.low),
216 &instance->reg_set->inbound_low_queue_port);
217 writel(le32_to_cpu(req_desc->u.high),
218 &instance->reg_set->inbound_high_queue_port);
220 spin_unlock_irqrestore(&instance->hba_lock, flags);
226 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
227 * @instance: Adapter soft state
228 * fw_boot_context: Whether this function called during probe or after OCR
230 * This function is only for fusion controllers.
231 * Update host can queue, if firmware downgrade max supported firmware commands.
232 * Firmware upgrade case will be skiped because underlying firmware has
233 * more resource than exposed to the OS.
237 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
239 u16 cur_max_fw_cmds = 0;
240 u16 ldio_threshold = 0;
241 struct megasas_register_set __iomem *reg_set;
243 reg_set = instance->reg_set;
245 /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
246 if (!instance->is_ventura)
248 readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
250 if (dual_qdepth_disable || !cur_max_fw_cmds)
251 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
254 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
256 dev_info(&instance->pdev->dev,
257 "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
258 cur_max_fw_cmds, ldio_threshold);
260 if (fw_boot_context == OCR_CONTEXT) {
261 cur_max_fw_cmds = cur_max_fw_cmds - 1;
262 if (cur_max_fw_cmds < instance->max_fw_cmds) {
263 instance->cur_can_queue =
264 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
265 MEGASAS_FUSION_IOCTL_CMDS);
266 instance->host->can_queue = instance->cur_can_queue;
267 instance->ldio_threshold = ldio_threshold;
270 instance->max_fw_cmds = cur_max_fw_cmds;
271 instance->ldio_threshold = ldio_threshold;
273 if (!instance->is_rdpq)
274 instance->max_fw_cmds =
275 min_t(u16, instance->max_fw_cmds, 1024);
278 instance->max_fw_cmds = min(instance->max_fw_cmds,
279 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
281 * Reduce the max supported cmds by 1. This is to ensure that the
282 * reply_q_sz (1 more than the max cmd that driver may send)
283 * does not exceed max cmds that the FW can support
285 instance->max_fw_cmds = instance->max_fw_cmds-1;
287 instance->max_scsi_cmds = instance->max_fw_cmds -
288 (MEGASAS_FUSION_INTERNAL_CMDS +
289 MEGASAS_FUSION_IOCTL_CMDS);
290 instance->cur_can_queue = instance->max_scsi_cmds;
291 instance->host->can_queue = instance->cur_can_queue;
294 if (instance->is_ventura)
295 instance->max_mpt_cmds =
296 instance->max_fw_cmds * RAID_1_PEER_CMDS;
298 instance->max_mpt_cmds = instance->max_fw_cmds;
301 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
302 * @instance: Adapter soft state
305 megasas_free_cmds_fusion(struct megasas_instance *instance)
308 struct fusion_context *fusion = instance->ctrl_context;
309 struct megasas_cmd_fusion *cmd;
312 for (i = 0; i < instance->max_mpt_cmds; i++) {
313 cmd = fusion->cmd_list[i];
316 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
317 cmd->sg_frame_phys_addr);
319 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
320 cmd->sense_phys_addr);
324 if (fusion->sg_dma_pool) {
325 pci_pool_destroy(fusion->sg_dma_pool);
326 fusion->sg_dma_pool = NULL;
328 if (fusion->sense_dma_pool) {
329 pci_pool_destroy(fusion->sense_dma_pool);
330 fusion->sense_dma_pool = NULL;
334 /* Reply Frame, Desc*/
335 if (instance->is_rdpq)
336 megasas_free_rdpq_fusion(instance);
338 megasas_free_reply_fusion(instance);
340 /* Request Frame, Desc*/
341 if (fusion->req_frames_desc)
342 dma_free_coherent(&instance->pdev->dev,
343 fusion->request_alloc_sz, fusion->req_frames_desc,
344 fusion->req_frames_desc_phys);
345 if (fusion->io_request_frames)
346 pci_pool_free(fusion->io_request_frames_pool,
347 fusion->io_request_frames,
348 fusion->io_request_frames_phys);
349 if (fusion->io_request_frames_pool) {
350 pci_pool_destroy(fusion->io_request_frames_pool);
351 fusion->io_request_frames_pool = NULL;
356 for (i = 0; i < instance->max_mpt_cmds; i++)
357 kfree(fusion->cmd_list[i]);
359 kfree(fusion->cmd_list);
363 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
364 * @instance: Adapter soft state
367 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
371 struct fusion_context *fusion;
372 struct megasas_cmd_fusion *cmd;
374 fusion = instance->ctrl_context;
375 max_cmd = instance->max_fw_cmds;
378 fusion->sg_dma_pool =
379 pci_pool_create("mr_sg", instance->pdev,
380 instance->max_chain_frame_sz,
381 MR_DEFAULT_NVME_PAGE_SIZE, 0);
382 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
383 fusion->sense_dma_pool =
384 pci_pool_create("mr_sense", instance->pdev,
385 SCSI_SENSE_BUFFERSIZE, 64, 0);
387 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
388 dev_err(&instance->pdev->dev,
389 "Failed from %s %d\n", __func__, __LINE__);
394 * Allocate and attach a frame to each of the commands in cmd_list
396 for (i = 0; i < max_cmd; i++) {
397 cmd = fusion->cmd_list[i];
398 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
399 GFP_KERNEL, &cmd->sg_frame_phys_addr);
401 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
402 GFP_KERNEL, &cmd->sense_phys_addr);
403 if (!cmd->sg_frame || !cmd->sense) {
404 dev_err(&instance->pdev->dev,
405 "Failed from %s %d\n", __func__, __LINE__);
410 /* create sense buffer for the raid 1/10 fp */
411 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
412 cmd = fusion->cmd_list[i];
413 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
414 GFP_KERNEL, &cmd->sense_phys_addr);
416 dev_err(&instance->pdev->dev,
417 "Failed from %s %d\n", __func__, __LINE__);
426 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
428 u32 max_mpt_cmd, i, j;
429 struct fusion_context *fusion;
431 fusion = instance->ctrl_context;
433 max_mpt_cmd = instance->max_mpt_cmds;
436 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
437 * Allocate the dynamic array first and then allocate individual
441 kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
443 if (!fusion->cmd_list) {
444 dev_err(&instance->pdev->dev,
445 "Failed from %s %d\n", __func__, __LINE__);
449 for (i = 0; i < max_mpt_cmd; i++) {
450 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
452 if (!fusion->cmd_list[i]) {
453 for (j = 0; j < i; j++)
454 kfree(fusion->cmd_list[j]);
455 kfree(fusion->cmd_list);
456 dev_err(&instance->pdev->dev,
457 "Failed from %s %d\n", __func__, __LINE__);
465 megasas_alloc_request_fusion(struct megasas_instance *instance)
467 struct fusion_context *fusion;
469 fusion = instance->ctrl_context;
471 fusion->req_frames_desc =
472 dma_alloc_coherent(&instance->pdev->dev,
473 fusion->request_alloc_sz,
474 &fusion->req_frames_desc_phys, GFP_KERNEL);
475 if (!fusion->req_frames_desc) {
476 dev_err(&instance->pdev->dev,
477 "Failed from %s %d\n", __func__, __LINE__);
481 fusion->io_request_frames_pool =
482 pci_pool_create("mr_ioreq", instance->pdev,
483 fusion->io_frames_alloc_sz, 16, 0);
485 if (!fusion->io_request_frames_pool) {
486 dev_err(&instance->pdev->dev,
487 "Failed from %s %d\n", __func__, __LINE__);
491 fusion->io_request_frames =
492 pci_pool_alloc(fusion->io_request_frames_pool,
493 GFP_KERNEL, &fusion->io_request_frames_phys);
494 if (!fusion->io_request_frames) {
495 dev_err(&instance->pdev->dev,
496 "Failed from %s %d\n", __func__, __LINE__);
503 megasas_alloc_reply_fusion(struct megasas_instance *instance)
506 struct fusion_context *fusion;
507 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
508 fusion = instance->ctrl_context;
510 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
511 fusion->reply_frames_desc_pool =
512 pci_pool_create("mr_reply", instance->pdev,
513 fusion->reply_alloc_sz * count, 16, 0);
515 if (!fusion->reply_frames_desc_pool) {
516 dev_err(&instance->pdev->dev,
517 "Failed from %s %d\n", __func__, __LINE__);
521 fusion->reply_frames_desc[0] =
522 pci_pool_alloc(fusion->reply_frames_desc_pool,
523 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
524 if (!fusion->reply_frames_desc[0]) {
525 dev_err(&instance->pdev->dev,
526 "Failed from %s %d\n", __func__, __LINE__);
529 reply_desc = fusion->reply_frames_desc[0];
530 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
531 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
533 /* This is not a rdpq mode, but driver still populate
534 * reply_frame_desc array to use same msix index in ISR path.
536 for (i = 0; i < (count - 1); i++)
537 fusion->reply_frames_desc[i + 1] =
538 fusion->reply_frames_desc[i] +
539 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
545 megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
548 struct fusion_context *fusion;
549 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
551 fusion = instance->ctrl_context;
553 fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
554 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
556 if (!fusion->rdpq_virt) {
557 dev_err(&instance->pdev->dev,
558 "Failed from %s %d\n", __func__, __LINE__);
562 memset(fusion->rdpq_virt, 0,
563 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
564 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
565 fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
566 instance->pdev, fusion->reply_alloc_sz, 16, 0);
568 if (!fusion->reply_frames_desc_pool) {
569 dev_err(&instance->pdev->dev,
570 "Failed from %s %d\n", __func__, __LINE__);
574 for (i = 0; i < count; i++) {
575 fusion->reply_frames_desc[i] =
576 pci_pool_alloc(fusion->reply_frames_desc_pool,
577 GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
578 if (!fusion->reply_frames_desc[i]) {
579 dev_err(&instance->pdev->dev,
580 "Failed from %s %d\n", __func__, __LINE__);
584 fusion->rdpq_virt[i].RDPQBaseAddress =
585 cpu_to_le64(fusion->reply_frames_desc_phys[i]);
587 reply_desc = fusion->reply_frames_desc[i];
588 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
589 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
595 megasas_free_rdpq_fusion(struct megasas_instance *instance) {
598 struct fusion_context *fusion;
600 fusion = instance->ctrl_context;
602 for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
603 if (fusion->reply_frames_desc[i])
604 pci_pool_free(fusion->reply_frames_desc_pool,
605 fusion->reply_frames_desc[i],
606 fusion->reply_frames_desc_phys[i]);
609 if (fusion->reply_frames_desc_pool)
610 pci_pool_destroy(fusion->reply_frames_desc_pool);
612 if (fusion->rdpq_virt)
613 pci_free_consistent(instance->pdev,
614 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
615 fusion->rdpq_virt, fusion->rdpq_phys);
619 megasas_free_reply_fusion(struct megasas_instance *instance) {
621 struct fusion_context *fusion;
623 fusion = instance->ctrl_context;
625 if (fusion->reply_frames_desc[0])
626 pci_pool_free(fusion->reply_frames_desc_pool,
627 fusion->reply_frames_desc[0],
628 fusion->reply_frames_desc_phys[0]);
630 if (fusion->reply_frames_desc_pool)
631 pci_pool_destroy(fusion->reply_frames_desc_pool);
637 * megasas_alloc_cmds_fusion - Allocates the command packets
638 * @instance: Adapter soft state
641 * Each frame has a 32-bit field called context. This context is used to get
642 * back the megasas_cmd_fusion from the frame when a frame gets completed
643 * In this driver, the 32 bit values are the indices into an array cmd_list.
644 * This array is used only to look up the megasas_cmd_fusion given the context.
645 * The free commands themselves are maintained in a linked list called cmd_pool.
647 * cmds are formed in the io_request and sg_frame members of the
648 * megasas_cmd_fusion. The context field is used to get a request descriptor
649 * and is used as SMID of the cmd.
650 * SMID value range is from 1 to max_fw_cmds.
653 megasas_alloc_cmds_fusion(struct megasas_instance *instance)
656 struct fusion_context *fusion;
657 struct megasas_cmd_fusion *cmd;
659 dma_addr_t io_req_base_phys;
663 fusion = instance->ctrl_context;
665 if (megasas_alloc_cmdlist_fusion(instance))
668 if (megasas_alloc_request_fusion(instance))
671 if (instance->is_rdpq) {
672 if (megasas_alloc_rdpq_fusion(instance))
675 if (megasas_alloc_reply_fusion(instance))
679 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
680 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
681 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
684 * Add all the commands to command pool (fusion->cmd_pool)
687 /* SMID 0 is reserved. Set SMID/index from 1 */
688 for (i = 0; i < instance->max_mpt_cmds; i++) {
689 cmd = fusion->cmd_list[i];
690 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
691 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
695 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
696 (i - instance->max_scsi_cmds) :
697 (u32)ULONG_MAX; /* Set to Invalid */
698 cmd->instance = instance;
700 (struct MPI2_RAID_SCSI_IO_REQUEST *)
701 (io_req_base + offset);
702 memset(cmd->io_request, 0,
703 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
704 cmd->io_request_phys_addr = io_req_base_phys + offset;
705 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
708 if (megasas_create_sg_sense_fusion(instance))
714 megasas_free_cmds_fusion(instance);
719 * wait_and_poll - Issues a polling command
720 * @instance: Adapter soft state
721 * @cmd: Command packet to be issued
723 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
726 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
730 struct megasas_header *frame_hdr = &cmd->frame->hdr;
731 struct fusion_context *fusion;
733 u32 msecs = seconds * 1000;
735 fusion = instance->ctrl_context;
737 * Wait for cmd_status to change
739 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
744 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
746 else if (frame_hdr->cmd_status == MFI_STAT_OK)
753 * megasas_ioc_init_fusion - Initializes the FW
754 * @instance: Adapter soft state
756 * Issues the IOC Init cmd
759 megasas_ioc_init_fusion(struct megasas_instance *instance)
761 struct megasas_init_frame *init_frame;
762 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
763 dma_addr_t ioc_init_handle;
764 struct megasas_cmd *cmd;
765 u8 ret, cur_rdpq_mode;
766 struct fusion_context *fusion;
767 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
769 struct megasas_header *frame_hdr;
770 const char *sys_info;
771 MFI_CAPABILITIES *drv_ops;
775 fusion = instance->ctrl_context;
777 cmd = megasas_get_cmd(instance);
780 dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
785 scratch_pad_2 = readl
786 (&instance->reg_set->outbound_scratch_pad_2);
788 cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
790 if (instance->is_rdpq && !cur_rdpq_mode) {
791 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
792 " from RDPQ mode to non RDPQ mode\n");
797 instance->fw_sync_cache_support = (scratch_pad_2 &
798 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
799 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
800 instance->fw_sync_cache_support ? "Yes" : "No");
803 dma_alloc_coherent(&instance->pdev->dev,
804 sizeof(struct MPI2_IOC_INIT_REQUEST),
805 &ioc_init_handle, GFP_KERNEL);
807 if (!IOCInitMessage) {
808 dev_err(&instance->pdev->dev, "Could not allocate memory for "
814 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
816 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
817 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
818 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
819 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
820 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
822 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
823 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
824 cpu_to_le64(fusion->rdpq_phys) :
825 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
826 IOCInitMessage->MsgFlags = instance->is_rdpq ?
827 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
828 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
829 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
830 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
831 init_frame = (struct megasas_init_frame *)cmd->frame;
832 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
834 frame_hdr = &cmd->frame->hdr;
835 frame_hdr->cmd_status = 0xFF;
836 frame_hdr->flags = cpu_to_le16(
837 le16_to_cpu(frame_hdr->flags) |
838 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
840 init_frame->cmd = MFI_CMD_INIT;
841 init_frame->cmd_status = 0xFF;
843 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
845 /* driver support Extended MSIX */
846 if (fusion->adapter_type >= INVADER_SERIES)
847 drv_ops->mfi_capabilities.support_additional_msix = 1;
848 /* driver supports HA / Remote LUN over Fast Path interface */
849 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
851 drv_ops->mfi_capabilities.support_max_255lds = 1;
852 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
853 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
855 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
856 drv_ops->mfi_capabilities.support_ext_io_size = 1;
858 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
859 if (!dual_qdepth_disable)
860 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
862 drv_ops->mfi_capabilities.support_qd_throttling = 1;
863 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
864 /* Convert capability to LE32 */
865 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
867 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
868 if (instance->system_info_buf && sys_info) {
869 memcpy(instance->system_info_buf->systemId, sys_info,
870 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
871 instance->system_info_buf->systemIdLength =
872 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
873 init_frame->system_info_lo = instance->system_info_h;
874 init_frame->system_info_hi = 0;
877 init_frame->queue_info_new_phys_addr_hi =
878 cpu_to_le32(upper_32_bits(ioc_init_handle));
879 init_frame->queue_info_new_phys_addr_lo =
880 cpu_to_le32(lower_32_bits(ioc_init_handle));
881 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
883 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
884 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
885 req_desc.MFAIo.RequestFlags =
886 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
887 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
890 * disable the intr before firing the init frame
892 instance->instancet->disable_intr(instance);
894 for (i = 0; i < (10 * 1000); i += 20) {
895 if (readl(&instance->reg_set->doorbell) & 1)
901 /* For Ventura also IOC INIT required 64 bit Descriptor write. */
902 spin_lock_irqsave(&instance->hba_lock, flags);
903 writel(le32_to_cpu(req_desc.u.low),
904 &instance->reg_set->inbound_low_queue_port);
905 writel(le32_to_cpu(req_desc.u.high),
906 &instance->reg_set->inbound_high_queue_port);
908 spin_unlock_irqrestore(&instance->hba_lock, flags);
910 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
912 frame_hdr = &cmd->frame->hdr;
913 if (frame_hdr->cmd_status != 0) {
917 dev_info(&instance->pdev->dev, "Init cmd success\n");
922 megasas_return_cmd(instance, cmd);
924 dma_free_coherent(&instance->pdev->dev,
925 sizeof(struct MPI2_IOC_INIT_REQUEST),
926 IOCInitMessage, ioc_init_handle);
932 * megasas_sync_pd_seq_num - JBOD SEQ MAP
933 * @instance: Adapter soft state
934 * @pend: set to 1, if it is pended jbod map.
936 * Issue Jbod map to the firmware. If it is pended command,
937 * issue command and return. If it is first instance of jbod map
938 * issue and receive command.
941 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
944 struct megasas_cmd *cmd;
945 struct megasas_dcmd_frame *dcmd;
946 struct fusion_context *fusion = instance->ctrl_context;
947 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
950 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
951 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
952 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
953 (sizeof(struct MR_PD_CFG_SEQ) *
954 (MAX_PHYSICAL_DEVICES - 1));
956 cmd = megasas_get_cmd(instance);
958 dev_err(&instance->pdev->dev,
959 "Could not get mfi cmd. Fail from %s %d\n",
964 dcmd = &cmd->frame->dcmd;
966 memset(pd_sync, 0, pd_seq_map_sz);
967 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
968 dcmd->cmd = MFI_CMD_DCMD;
969 dcmd->cmd_status = 0xFF;
973 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
974 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
975 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
976 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
979 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
980 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
981 instance->jbod_seq_cmd = cmd;
982 instance->instancet->issue_dcmd(instance, cmd);
986 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
988 /* Below code is only for non pended DCMD */
989 if (instance->ctrl_context && !instance->mask_interrupts)
990 ret = megasas_issue_blocked_cmd(instance, cmd,
991 MFI_IO_TIMEOUT_SECS);
993 ret = megasas_issue_polled(instance, cmd);
995 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
996 dev_warn(&instance->pdev->dev,
997 "driver supports max %d JBOD, but FW reports %d\n",
998 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1002 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1003 megaraid_sas_kill_hba(instance);
1005 if (ret == DCMD_SUCCESS)
1006 instance->pd_seq_map_id++;
1008 megasas_return_cmd(instance, cmd);
1013 * megasas_get_ld_map_info - Returns FW's ld_map structure
1014 * @instance: Adapter soft state
1015 * @pend: Pend the command or not
1016 * Issues an internal command (DCMD) to get the FW's controller PD
1017 * list structure. This information is mainly used to find out SYSTEM
1018 * supported by the FW.
1019 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1020 * dcmd.mbox.b[0] - number of LDs being sync'd
1021 * dcmd.mbox.b[1] - 0 - complete command immediately.
1022 * - 1 - pend till config change
1023 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1024 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1025 * uses extended struct MR_FW_RAID_MAP_EXT
1028 megasas_get_ld_map_info(struct megasas_instance *instance)
1031 struct megasas_cmd *cmd;
1032 struct megasas_dcmd_frame *dcmd;
1034 dma_addr_t ci_h = 0;
1036 struct fusion_context *fusion;
1038 cmd = megasas_get_cmd(instance);
1041 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
1045 fusion = instance->ctrl_context;
1048 megasas_return_cmd(instance, cmd);
1052 dcmd = &cmd->frame->dcmd;
1054 size_map_info = fusion->current_map_sz;
1056 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
1057 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1060 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
1061 megasas_return_cmd(instance, cmd);
1065 memset(ci, 0, fusion->max_map_sz);
1066 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1067 dcmd->cmd = MFI_CMD_DCMD;
1068 dcmd->cmd_status = 0xFF;
1069 dcmd->sge_count = 1;
1070 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
1073 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1074 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1075 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1076 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
1078 if (instance->ctrl_context && !instance->mask_interrupts)
1079 ret = megasas_issue_blocked_cmd(instance, cmd,
1080 MFI_IO_TIMEOUT_SECS);
1082 ret = megasas_issue_polled(instance, cmd);
1084 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1085 megaraid_sas_kill_hba(instance);
1087 megasas_return_cmd(instance, cmd);
1093 megasas_get_map_info(struct megasas_instance *instance)
1095 struct fusion_context *fusion = instance->ctrl_context;
1097 fusion->fast_path_io = 0;
1098 if (!megasas_get_ld_map_info(instance)) {
1099 if (MR_ValidateMapInfo(instance)) {
1100 fusion->fast_path_io = 1;
1108 * megasas_sync_map_info - Returns FW's ld_map structure
1109 * @instance: Adapter soft state
1111 * Issues an internal command (DCMD) to get the FW's controller PD
1112 * list structure. This information is mainly used to find out SYSTEM
1113 * supported by the FW.
1116 megasas_sync_map_info(struct megasas_instance *instance)
1119 struct megasas_cmd *cmd;
1120 struct megasas_dcmd_frame *dcmd;
1123 struct fusion_context *fusion;
1124 struct MR_LD_TARGET_SYNC *ci = NULL;
1125 struct MR_DRV_RAID_MAP_ALL *map;
1126 struct MR_LD_RAID *raid;
1127 struct MR_LD_TARGET_SYNC *ld_sync;
1128 dma_addr_t ci_h = 0;
1131 cmd = megasas_get_cmd(instance);
1134 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
1138 fusion = instance->ctrl_context;
1141 megasas_return_cmd(instance, cmd);
1145 map = fusion->ld_drv_map[instance->map_id & 1];
1147 num_lds = le16_to_cpu(map->raidMap.ldCount);
1149 dcmd = &cmd->frame->dcmd;
1151 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1153 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1155 ci = (struct MR_LD_TARGET_SYNC *)
1156 fusion->ld_map[(instance->map_id - 1) & 1];
1157 memset(ci, 0, fusion->max_map_sz);
1159 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1161 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1163 for (i = 0; i < num_lds; i++, ld_sync++) {
1164 raid = MR_LdRaidGet(i, map);
1165 ld_sync->targetId = MR_GetLDTgtId(i, map);
1166 ld_sync->seqNum = raid->seqNum;
1169 size_map_info = fusion->current_map_sz;
1171 dcmd->cmd = MFI_CMD_DCMD;
1172 dcmd->cmd_status = 0xFF;
1173 dcmd->sge_count = 1;
1174 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
1177 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1178 dcmd->mbox.b[0] = num_lds;
1179 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1180 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1181 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1182 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
1184 instance->map_update_cmd = cmd;
1186 instance->instancet->issue_dcmd(instance, cmd);
1192 * meagasas_display_intel_branding - Display branding string
1193 * @instance: per adapter object
1198 megasas_display_intel_branding(struct megasas_instance *instance)
1200 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1203 switch (instance->pdev->device) {
1204 case PCI_DEVICE_ID_LSI_INVADER:
1205 switch (instance->pdev->subsystem_device) {
1206 case MEGARAID_INTEL_RS3DC080_SSDID:
1207 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1208 instance->host->host_no,
1209 MEGARAID_INTEL_RS3DC080_BRANDING);
1211 case MEGARAID_INTEL_RS3DC040_SSDID:
1212 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1213 instance->host->host_no,
1214 MEGARAID_INTEL_RS3DC040_BRANDING);
1216 case MEGARAID_INTEL_RS3SC008_SSDID:
1217 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1218 instance->host->host_no,
1219 MEGARAID_INTEL_RS3SC008_BRANDING);
1221 case MEGARAID_INTEL_RS3MC044_SSDID:
1222 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1223 instance->host->host_no,
1224 MEGARAID_INTEL_RS3MC044_BRANDING);
1230 case PCI_DEVICE_ID_LSI_FURY:
1231 switch (instance->pdev->subsystem_device) {
1232 case MEGARAID_INTEL_RS3WC080_SSDID:
1233 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1234 instance->host->host_no,
1235 MEGARAID_INTEL_RS3WC080_BRANDING);
1237 case MEGARAID_INTEL_RS3WC040_SSDID:
1238 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1239 instance->host->host_no,
1240 MEGARAID_INTEL_RS3WC040_BRANDING);
1246 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1247 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1248 switch (instance->pdev->subsystem_device) {
1249 case MEGARAID_INTEL_RMS3BC160_SSDID:
1250 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1251 instance->host->host_no,
1252 MEGARAID_INTEL_RMS3BC160_BRANDING);
1264 * megasas_init_adapter_fusion - Initializes the FW
1265 * @instance: Adapter soft state
1267 * This is the main function for initializing firmware.
1270 megasas_init_adapter_fusion(struct megasas_instance *instance)
1272 struct megasas_register_set __iomem *reg_set;
1273 struct fusion_context *fusion;
1278 fusion = instance->ctrl_context;
1280 reg_set = instance->reg_set;
1282 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
1285 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1287 instance->max_mfi_cmds =
1288 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
1290 max_cmd = instance->max_fw_cmds;
1292 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
1294 fusion->request_alloc_sz =
1295 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * instance->max_mpt_cmds;
1296 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1297 *(fusion->reply_q_depth);
1298 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1299 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1300 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1302 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1303 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1304 * Firmware support extended IO chain frame which is 4 times more than
1306 * Legacy Firmware - Frame size is (8 * 128) = 1K
1307 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1309 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1310 instance->max_chain_frame_sz =
1311 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1312 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1314 instance->max_chain_frame_sz =
1315 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1316 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1318 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1319 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1320 instance->max_chain_frame_sz,
1321 MEGASAS_CHAIN_FRAME_SZ_MIN);
1322 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1325 fusion->max_sge_in_main_msg =
1326 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1327 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1329 fusion->max_sge_in_chain =
1330 instance->max_chain_frame_sz
1331 / sizeof(union MPI2_SGE_IO_UNION);
1333 instance->max_num_sge =
1334 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1335 + fusion->max_sge_in_chain - 2);
1337 /* Used for pass thru MFI frame (DCMD) */
1338 fusion->chain_offset_mfi_pthru =
1339 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1341 fusion->chain_offset_io_request =
1342 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1343 sizeof(union MPI2_SGE_IO_UNION))/16;
1345 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1346 for (i = 0 ; i < count; i++)
1347 fusion->last_reply_idx[i] = 0;
1350 * For fusion adapters, 3 commands for IOCTL and 8 commands
1351 * for driver's internal DCMDs.
1353 instance->max_scsi_cmds = instance->max_fw_cmds -
1354 (MEGASAS_FUSION_INTERNAL_CMDS +
1355 MEGASAS_FUSION_IOCTL_CMDS);
1356 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1359 * Allocate memory for descriptors
1360 * Create a pool of commands
1362 if (megasas_alloc_cmds(instance))
1363 goto fail_alloc_mfi_cmds;
1364 if (megasas_alloc_cmds_fusion(instance))
1365 goto fail_alloc_cmds;
1367 if (megasas_ioc_init_fusion(instance))
1370 megasas_display_intel_branding(instance);
1371 if (megasas_get_ctrl_info(instance)) {
1372 dev_err(&instance->pdev->dev,
1373 "Could not get controller info. Fail from %s %d\n",
1374 __func__, __LINE__);
1378 instance->flag_ieee = 1;
1379 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
1380 fusion->fast_path_io = 0;
1382 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1383 for (i = 0; i < 2; i++) {
1384 fusion->ld_map[i] = NULL;
1385 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1386 fusion->drv_map_pages);
1387 if (!fusion->ld_drv_map[i]) {
1388 dev_err(&instance->pdev->dev, "Could not allocate "
1389 "memory for local map info for %d pages\n",
1390 fusion->drv_map_pages);
1392 free_pages((ulong)fusion->ld_drv_map[0],
1393 fusion->drv_map_pages);
1396 memset(fusion->ld_drv_map[i], 0,
1397 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
1400 for (i = 0; i < 2; i++) {
1401 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1403 &fusion->ld_map_phys[i],
1405 if (!fusion->ld_map[i]) {
1406 dev_err(&instance->pdev->dev, "Could not allocate memory "
1412 if (!megasas_get_map_info(instance))
1413 megasas_sync_map_info(instance);
1419 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
1420 fusion->ld_map[0], fusion->ld_map_phys[0]);
1422 megasas_free_cmds_fusion(instance);
1424 megasas_free_cmds(instance);
1425 fail_alloc_mfi_cmds:
1430 * map_cmd_status - Maps FW cmd status to OS cmd status
1431 * @cmd : Pointer to cmd
1432 * @status : status of cmd returned by FW
1433 * @ext_status : ext status of cmd returned by FW
1437 map_cmd_status(struct fusion_context *fusion,
1438 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1439 u32 data_length, u8 *sense)
1444 cmd_type = megasas_cmd_type(scmd);
1448 scmd->result = DID_OK << 16;
1451 case MFI_STAT_SCSI_IO_FAILED:
1452 case MFI_STAT_LD_INIT_IN_PROGRESS:
1453 scmd->result = (DID_ERROR << 16) | ext_status;
1456 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1458 scmd->result = (DID_OK << 16) | ext_status;
1459 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1460 memset(scmd->sense_buffer, 0,
1461 SCSI_SENSE_BUFFERSIZE);
1462 memcpy(scmd->sense_buffer, sense,
1463 SCSI_SENSE_BUFFERSIZE);
1464 scmd->result |= DRIVER_SENSE << 24;
1468 * If the IO request is partially completed, then MR FW will
1469 * update "io_request->DataLength" field with actual number of
1470 * bytes transferred.Driver will set residual bytes count in
1471 * SCSI command structure.
1473 resid = (scsi_bufflen(scmd) - data_length);
1474 scsi_set_resid(scmd, resid);
1477 ((cmd_type == READ_WRITE_LDIO) ||
1478 (cmd_type == READ_WRITE_SYSPDIO)))
1479 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1480 " requested/completed 0x%x/0x%x\n",
1481 status, scsi_bufflen(scmd), data_length);
1484 case MFI_STAT_LD_OFFLINE:
1485 case MFI_STAT_DEVICE_NOT_FOUND:
1486 scmd->result = DID_BAD_TARGET << 16;
1488 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1489 scmd->result = DID_IMM_RETRY << 16;
1492 scmd->result = DID_ERROR << 16;
1498 * megasas_is_prp_possible -
1499 * Checks if native NVMe PRPs can be built for the IO
1501 * @instance: Adapter soft state
1502 * @scmd: SCSI command from the mid-layer
1503 * @sge_count: scatter gather element count.
1505 * Returns: true: PRPs can be built
1506 * false: IEEE SGLs needs to be built
1509 megasas_is_prp_possible(struct megasas_instance *instance,
1510 struct scsi_cmnd *scmd, int sge_count)
1512 struct fusion_context *fusion;
1514 u32 data_length = 0;
1515 struct scatterlist *sg_scmd;
1516 bool build_prp = false;
1517 u32 mr_nvme_pg_size;
1519 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1520 MR_DEFAULT_NVME_PAGE_SIZE);
1521 fusion = instance->ctrl_context;
1522 data_length = scsi_bufflen(scmd);
1523 sg_scmd = scsi_sglist(scmd);
1526 * NVMe uses one PRP for each page (or part of a page)
1527 * look at the data length - if 4 pages or less then IEEE is OK
1528 * if > 5 pages then we need to build a native SGL
1529 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1530 * if this first size in the page is >= the residual beyond 4 pages
1531 * then use IEEE, otherwise use native SGL
1534 if (data_length > (mr_nvme_pg_size * 5)) {
1536 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1537 (data_length <= (mr_nvme_pg_size * 5))) {
1538 /* check if 1st SG entry size is < residual beyond 4 pages */
1539 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1544 * Below code detects gaps/holes in IO data buffers.
1545 * What does holes/gaps mean?
1546 * Any SGE except first one in a SGL starts at non NVME page size
1547 * aligned address OR Any SGE except last one in a SGL ends at
1548 * non NVME page size boundary.
1550 * Driver has already informed block layer by setting boundary rules for
1551 * bio merging done at NVME page size boundary calling kernel API
1552 * blk_queue_virt_boundary inside slave_config.
1553 * Still there is possibility of IO coming with holes to driver because of
1554 * IO merging done by IO scheduler.
1556 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1557 * IO scheduling so no IO merging.
1559 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1560 * then sending IOs with holes.
1562 * Though driver can request block layer to disable IO merging by calling-
1563 * queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1564 * user may tune sysfs parameter- nomerges again to 0 or 1.
1566 * If in future IO scheduling is enabled with SCSI BLK MQ,
1567 * this algorithm to detect holes will be required in driver
1568 * for SCSI BLK MQ enabled case as well.
1572 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1573 if ((i != 0) && (i != (sge_count - 1))) {
1574 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1575 mega_mod64(sg_dma_address(sg_scmd),
1578 atomic_inc(&instance->sge_holes_type1);
1583 if ((sge_count > 1) && (i == 0)) {
1584 if ((mega_mod64((sg_dma_address(sg_scmd) +
1585 sg_dma_len(sg_scmd)),
1586 mr_nvme_pg_size))) {
1588 atomic_inc(&instance->sge_holes_type2);
1593 if ((sge_count > 1) && (i == (sge_count - 1))) {
1594 if (mega_mod64(sg_dma_address(sg_scmd),
1597 atomic_inc(&instance->sge_holes_type3);
1607 * megasas_make_prp_nvme -
1608 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1610 * @instance: Adapter soft state
1611 * @scmd: SCSI command from the mid-layer
1612 * @sgl_ptr: SGL to be filled in
1613 * @cmd: Fusion command frame
1614 * @sge_count: scatter gather element count.
1616 * Returns: true: PRPs are built
1617 * false: IEEE SGLs needs to be built
1620 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
1621 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1622 struct megasas_cmd_fusion *cmd, int sge_count)
1624 int sge_len, offset, num_prp_in_chain = 0;
1625 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
1627 dma_addr_t ptr_sgl_phys;
1629 u32 page_mask, page_mask_result;
1630 struct scatterlist *sg_scmd;
1632 bool build_prp = false;
1633 int data_len = scsi_bufflen(scmd);
1634 struct fusion_context *fusion;
1635 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1636 MR_DEFAULT_NVME_PAGE_SIZE);
1638 fusion = instance->ctrl_context;
1640 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
1646 * Nvme has a very convoluted prp format. One prp is required
1647 * for each page or partial page. Driver need to split up OS sg_list
1648 * entries if it is longer than one page or cross a page
1649 * boundary. Driver also have to insert a PRP list pointer entry as
1650 * the last entry in each physical page of the PRP list.
1652 * NOTE: The first PRP "entry" is actually placed in the first
1653 * SGL entry in the main message as IEEE 64 format. The 2nd
1654 * entry in the main message is the chain element, and the rest
1655 * of the PRP entries are built in the contiguous pcie buffer.
1657 page_mask = mr_nvme_pg_size - 1;
1658 ptr_sgl = (u64 *)cmd->sg_frame;
1659 ptr_sgl_phys = cmd->sg_frame_phys_addr;
1660 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
1662 /* Build chain frame element which holds all prps except first*/
1663 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
1664 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
1666 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
1667 main_chain_element->NextChainOffset = 0;
1668 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1669 IEEE_SGE_FLAGS_SYSTEM_ADDR |
1670 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1672 /* Build first prp, sge need not to be page aligned*/
1673 ptr_first_sgl = sgl_ptr;
1674 sg_scmd = scsi_sglist(scmd);
1675 sge_addr = sg_dma_address(sg_scmd);
1676 sge_len = sg_dma_len(sg_scmd);
1678 offset = (u32)(sge_addr & page_mask);
1679 first_prp_len = mr_nvme_pg_size - offset;
1681 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1682 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1684 data_len -= first_prp_len;
1686 if (sge_len > first_prp_len) {
1687 sge_addr += first_prp_len;
1688 sge_len -= first_prp_len;
1689 } else if (sge_len == first_prp_len) {
1690 sg_scmd = sg_next(sg_scmd);
1691 sge_addr = sg_dma_address(sg_scmd);
1692 sge_len = sg_dma_len(sg_scmd);
1696 offset = (u32)(sge_addr & page_mask);
1698 /* Put PRP pointer due to page boundary*/
1699 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
1700 if (unlikely(!page_mask_result)) {
1701 scmd_printk(KERN_NOTICE,
1702 scmd, "page boundary ptr_sgl: 0x%p\n",
1705 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
1710 *ptr_sgl = cpu_to_le64(sge_addr);
1715 sge_addr += mr_nvme_pg_size;
1716 sge_len -= mr_nvme_pg_size;
1717 data_len -= mr_nvme_pg_size;
1725 sg_scmd = sg_next(sg_scmd);
1726 sge_addr = sg_dma_address(sg_scmd);
1727 sge_len = sg_dma_len(sg_scmd);
1730 main_chain_element->Length =
1731 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1733 atomic_inc(&instance->prp_sgl);
1738 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1739 * @instance: Adapter soft state
1740 * @scp: SCSI command from the mid-layer
1741 * @sgl_ptr: SGL to be filled in
1742 * @cmd: cmd we are working on
1743 * @sge_count sge count
1747 megasas_make_sgl_fusion(struct megasas_instance *instance,
1748 struct scsi_cmnd *scp,
1749 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1750 struct megasas_cmd_fusion *cmd, int sge_count)
1752 int i, sg_processed;
1753 struct scatterlist *os_sgl;
1754 struct fusion_context *fusion;
1756 fusion = instance->ctrl_context;
1758 if (fusion->adapter_type >= INVADER_SERIES) {
1759 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1760 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1761 sgl_ptr_end->Flags = 0;
1764 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1765 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1766 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
1768 if (fusion->adapter_type >= INVADER_SERIES)
1769 if (i == sge_count - 1)
1770 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1772 sg_processed = i + 1;
1774 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1775 (sge_count > fusion->max_sge_in_main_msg)) {
1777 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1778 if (fusion->adapter_type >= INVADER_SERIES) {
1779 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1780 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1781 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1782 cmd->io_request->ChainOffset =
1784 chain_offset_io_request;
1786 cmd->io_request->ChainOffset = 0;
1788 cmd->io_request->ChainOffset =
1789 fusion->chain_offset_io_request;
1792 /* Prepare chain element */
1793 sg_chain->NextChainOffset = 0;
1794 if (fusion->adapter_type >= INVADER_SERIES)
1795 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1798 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1799 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1800 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1801 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
1804 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1805 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
1808 atomic_inc(&instance->ieee_sgl);
1812 * megasas_make_sgl - Build Scatter Gather List(SGLs)
1813 * @scp: SCSI command pointer
1814 * @instance: Soft instance of controller
1815 * @cmd: Fusion command pointer
1817 * This function will build sgls based on device type.
1818 * For nvme drives, there is different way of building sgls in nvme native
1819 * format- PRPs(Physical Region Page).
1821 * Returns the number of sg lists actually used, zero if the sg lists
1822 * is NULL, or -ENOMEM if the mapping failed
1825 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
1826 struct megasas_cmd_fusion *cmd)
1829 bool build_prp = false;
1830 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
1832 sge_count = scsi_dma_map(scp);
1834 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
1837 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
1838 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1839 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
1840 (cmd->pd_interface == NVME_PD))
1841 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
1845 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
1852 * megasas_set_pd_lba - Sets PD LBA
1854 * @cdb_len: cdb length
1855 * @start_blk: Start block of IO
1857 * Used to set the PD LBA in CDB for FP IOs
1860 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1861 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1862 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1864 struct MR_LD_RAID *raid;
1866 u64 start_blk = io_info->pdBlock;
1867 u8 *cdb = io_request->CDB.CDB32;
1868 u32 num_blocks = io_info->numBlocks;
1869 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
1871 /* Check if T10 PI (DIF) is enabled for this LD */
1872 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1873 raid = MR_LdRaidGet(ld, local_map_ptr);
1874 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1875 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1876 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1877 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1879 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1880 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1882 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1883 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1886 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1887 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1888 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1889 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1890 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1891 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1892 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1893 cdb[19] = (u8)(start_blk & 0xff);
1895 /* Logical block reference tag */
1896 io_request->CDB.EEDP32.PrimaryReferenceTag =
1897 cpu_to_be32(ref_tag);
1898 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
1899 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
1901 /* Transfer length */
1902 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1903 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1904 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1905 cdb[31] = (u8)(num_blocks & 0xff);
1907 /* set SCSI IO EEDPFlags */
1908 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1909 io_request->EEDPFlags = cpu_to_le16(
1910 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1911 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1912 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1913 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1914 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
1915 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1917 io_request->EEDPFlags = cpu_to_le16(
1918 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1919 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
1921 io_request->Control |= cpu_to_le32((0x4 << 26));
1922 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
1924 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1925 if (((cdb_len == 12) || (cdb_len == 16)) &&
1926 (start_blk <= 0xffffffff)) {
1927 if (cdb_len == 16) {
1928 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1933 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1939 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1946 /* Transfer length */
1947 cdb[8] = (u8)(num_blocks & 0xff);
1948 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1950 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
1952 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1953 /* Convert to 16 byte CDB for large LBA's */
1956 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1961 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1968 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1975 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1982 /* Transfer length */
1983 cdb[13] = (u8)(num_blocks & 0xff);
1984 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1985 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1986 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1988 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
1992 /* Normal case, just load LBA here */
1996 u8 val = cdb[1] & 0xE0;
1997 cdb[3] = (u8)(start_blk & 0xff);
1998 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1999 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2003 cdb[5] = (u8)(start_blk & 0xff);
2004 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2005 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2006 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2009 cdb[5] = (u8)(start_blk & 0xff);
2010 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2011 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2012 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2015 cdb[9] = (u8)(start_blk & 0xff);
2016 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2017 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2018 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2019 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2020 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2021 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2022 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2029 * megasas_stream_detect - stream detection on read and and write IOs
2030 * @instance: Adapter soft state
2031 * @cmd: Command to be prepared
2032 * @io_info: IO Request info
2036 /** stream detection on read and and write IOs */
2037 static void megasas_stream_detect(struct megasas_instance *instance,
2038 struct megasas_cmd_fusion *cmd,
2039 struct IO_REQUEST_INFO *io_info)
2041 struct fusion_context *fusion = instance->ctrl_context;
2042 u32 device_id = io_info->ldTgtId;
2043 struct LD_STREAM_DETECT *current_ld_sd
2044 = fusion->stream_detect_by_ld[device_id];
2045 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num;
2046 u32 shifted_values, unshifted_values;
2047 u32 index_value_mask, shifted_values_mask;
2049 bool is_read_ahead = false;
2050 struct STREAM_DETECT *current_sd;
2051 /* find possible stream */
2052 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2053 stream_num = (*track_stream >>
2054 (i * BITS_PER_INDEX_STREAM)) &
2056 current_sd = ¤t_ld_sd->stream_track[stream_num];
2057 /* if we found a stream, update the raid
2058 * context and also update the mruBitMap
2060 /* boundary condition */
2061 if ((current_sd->next_seq_lba) &&
2062 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2063 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2064 (current_sd->is_read == io_info->isRead)) {
2066 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2067 ((!io_info->isRead) || (!is_read_ahead)))
2069 * Once the API availible we need to change this.
2070 * At this point we are not allowing any gap
2074 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2075 current_sd->next_seq_lba =
2076 io_info->ldStartBlock + io_info->numBlocks;
2078 * update the mruBitMap LRU
2080 shifted_values_mask =
2081 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2082 shifted_values = ((*track_stream & shifted_values_mask)
2083 << BITS_PER_INDEX_STREAM);
2085 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2087 *track_stream & ~(shifted_values_mask |
2090 unshifted_values | shifted_values | stream_num;
2095 * if we did not find any stream, create a new one
2096 * from the least recently used
2098 stream_num = (*track_stream >>
2099 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2101 current_sd = ¤t_ld_sd->stream_track[stream_num];
2102 current_sd->is_read = io_info->isRead;
2103 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2104 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2109 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2110 * affinity (cpu of the controller) and raid_flags in the raid context
2113 * @praid_context: IO RAID context
2114 * @raid: LD raid map
2115 * @fp_possible: Is fast path possible?
2116 * @is_read: Is read IO?
2120 megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2121 struct MR_LD_RAID *raid, bool fp_possible,
2122 u8 is_read, u32 scsi_buff_len)
2124 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2125 struct RAID_CONTEXT_G35 *rctx_g35;
2127 rctx_g35 = &praid_context->raid_context_g35;
2130 if ((raid->cpuAffinity.pdRead.cpu0) &&
2131 (raid->cpuAffinity.pdRead.cpu1))
2132 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2133 else if (raid->cpuAffinity.pdRead.cpu1)
2134 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2136 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2137 (raid->cpuAffinity.pdWrite.cpu1))
2138 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2139 else if (raid->cpuAffinity.pdWrite.cpu1)
2140 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2141 /* Fast path cache by pass capable R0/R1 VD */
2142 if ((raid->level <= 1) &&
2143 (raid->capability.fp_cache_bypass_capable)) {
2144 rctx_g35->routing_flags |=
2145 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
2146 rctx_g35->raid_flags =
2147 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2148 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2153 if ((raid->cpuAffinity.ldRead.cpu0) &&
2154 (raid->cpuAffinity.ldRead.cpu1))
2155 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2156 else if (raid->cpuAffinity.ldRead.cpu1)
2157 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2159 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2160 (raid->cpuAffinity.ldWrite.cpu1))
2161 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2162 else if (raid->cpuAffinity.ldWrite.cpu1)
2163 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2165 if (is_stream_detected(rctx_g35) &&
2166 ((raid->level == 5) || (raid->level == 6)) &&
2167 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2168 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2169 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2173 rctx_g35->routing_flags |=
2174 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2176 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2177 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2178 * IO Subtype is not bitmap.
2180 if ((raid->level == 1) && (!is_read)) {
2181 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2182 praid_context->raid_context_g35.raid_flags =
2183 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2184 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2189 * megasas_build_ldio_fusion - Prepares IOs to devices
2190 * @instance: Adapter soft state
2191 * @scp: SCSI command
2192 * @cmd: Command to be prepared
2194 * Prepares the io_request and chain elements (sg_frame) for IO
2195 * The IO can be for PD (Fast Path) or LD
2198 megasas_build_ldio_fusion(struct megasas_instance *instance,
2199 struct scsi_cmnd *scp,
2200 struct megasas_cmd_fusion *cmd)
2204 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2206 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2207 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2208 struct IO_REQUEST_INFO io_info;
2209 struct fusion_context *fusion;
2210 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2212 unsigned long spinlock_flags;
2213 union RAID_CONTEXT_UNION *praid_context;
2214 struct MR_LD_RAID *raid = NULL;
2215 struct MR_PRIV_DEVICE *mrdev_priv;
2217 device_id = MEGASAS_DEV_INDEX(scp);
2219 fusion = instance->ctrl_context;
2221 io_request = cmd->io_request;
2222 io_request->RaidContext.raid_context.virtual_disk_tgt_id =
2223 cpu_to_le16(device_id);
2224 io_request->RaidContext.raid_context.status = 0;
2225 io_request->RaidContext.raid_context.ex_status = 0;
2227 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2231 fp_possible = false;
2234 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2236 if (scp->cmd_len == 6) {
2237 datalength = (u32) scp->cmnd[4];
2238 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2239 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2241 start_lba_lo &= 0x1FFFFF;
2245 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2247 else if (scp->cmd_len == 10) {
2248 datalength = (u32) scp->cmnd[8] |
2249 ((u32) scp->cmnd[7] << 8);
2250 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2251 ((u32) scp->cmnd[3] << 16) |
2252 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2256 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2258 else if (scp->cmd_len == 12) {
2259 datalength = ((u32) scp->cmnd[6] << 24) |
2260 ((u32) scp->cmnd[7] << 16) |
2261 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2262 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2263 ((u32) scp->cmnd[3] << 16) |
2264 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2268 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2270 else if (scp->cmd_len == 16) {
2271 datalength = ((u32) scp->cmnd[10] << 24) |
2272 ((u32) scp->cmnd[11] << 16) |
2273 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2274 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2275 ((u32) scp->cmnd[7] << 16) |
2276 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2278 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2279 ((u32) scp->cmnd[3] << 16) |
2280 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2283 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2284 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
2285 io_info.numBlocks = datalength;
2286 io_info.ldTgtId = device_id;
2287 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2288 scsi_buff_len = scsi_bufflen(scp);
2289 io_request->DataLength = cpu_to_le32(scsi_buff_len);
2291 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2294 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2295 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2297 if (ld < instance->fw_supported_vd_count)
2298 raid = MR_LdRaidGet(ld, local_map_ptr);
2300 if (!raid || (!fusion->fast_path_io)) {
2301 io_request->RaidContext.raid_context.reg_lock_flags = 0;
2302 fp_possible = false;
2304 if (MR_BuildRaidContext(instance, &io_info,
2305 &io_request->RaidContext.raid_context,
2306 local_map_ptr, &raidLUN))
2307 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
2310 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
2311 id by default, not CPU group id, otherwise all MSI-X queues won't
2313 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
2314 raw_smp_processor_id() % instance->msix_vectors : 0;
2316 praid_context = &io_request->RaidContext;
2318 if (instance->is_ventura) {
2319 spin_lock_irqsave(&instance->stream_lock, spinlock_flags);
2320 megasas_stream_detect(instance, cmd, &io_info);
2321 spin_unlock_irqrestore(&instance->stream_lock, spinlock_flags);
2322 /* In ventura if stream detected for a read and it is read ahead
2323 * capable make this IO as LDIO
2325 if (is_stream_detected(&io_request->RaidContext.raid_context_g35) &&
2326 io_info.isRead && io_info.ra_capable)
2327 fp_possible = false;
2329 /* FP for Optimal raid level 1.
2330 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2331 * are built by the driver as LD I/Os.
2332 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2333 * (there is never a reason to process these as buffered writes)
2334 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2335 * with the SLD bit asserted.
2337 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2338 mrdev_priv = scp->device->hostdata;
2340 if (atomic_inc_return(&instance->fw_outstanding) >
2341 (instance->host->can_queue)) {
2342 fp_possible = false;
2343 atomic_dec(&instance->fw_outstanding);
2344 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2345 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
2346 fp_possible = false;
2347 atomic_dec(&instance->fw_outstanding);
2348 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2349 atomic_set(&mrdev_priv->r1_ldio_hint,
2350 instance->r1_ldio_hint_default);
2354 /* If raid is NULL, set CPU affinity to default CPU0 */
2356 megasas_set_raidflag_cpu_affinity(praid_context,
2357 raid, fp_possible, io_info.isRead,
2360 praid_context->raid_context_g35.routing_flags |=
2361 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
2365 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2366 local_map_ptr, start_lba_lo);
2367 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2368 cmd->request_desc->SCSIIO.RequestFlags =
2369 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
2370 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2371 if (fusion->adapter_type == INVADER_SERIES) {
2372 if (io_request->RaidContext.raid_context.reg_lock_flags ==
2374 cmd->request_desc->SCSIIO.RequestFlags =
2375 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2376 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2377 io_request->RaidContext.raid_context.type
2379 io_request->RaidContext.raid_context.nseg = 0x1;
2380 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2381 io_request->RaidContext.raid_context.reg_lock_flags |=
2382 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2383 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2384 } else if (instance->is_ventura) {
2385 io_request->RaidContext.raid_context_g35.nseg_type |=
2386 (1 << RAID_CONTEXT_NSEG_SHIFT);
2387 io_request->RaidContext.raid_context_g35.nseg_type |=
2388 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2389 io_request->RaidContext.raid_context_g35.routing_flags |=
2390 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2391 io_request->IoFlags |=
2392 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2394 if (fusion->load_balance_info &&
2395 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2398 get_updated_dev_handle(instance,
2399 &fusion->load_balance_info[device_id],
2400 &io_info, local_map_ptr);
2401 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
2402 cmd->pd_r1_lb = io_info.pd_after_lb;
2403 if (instance->is_ventura)
2404 io_request->RaidContext.raid_context_g35.span_arm
2407 io_request->RaidContext.raid_context.span_arm
2411 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
2413 if (instance->is_ventura)
2414 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
2416 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
2418 if ((raidLUN[0] == 1) &&
2419 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
2420 instance->dev_handle = !(instance->dev_handle);
2422 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2425 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2426 io_request->DevHandle = io_info.devHandle;
2427 cmd->pd_interface = io_info.pd_interface;
2428 /* populate the LUN field */
2429 memcpy(io_request->LUN, raidLUN, 8);
2431 io_request->RaidContext.raid_context.timeout_value =
2432 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
2433 cmd->request_desc->SCSIIO.RequestFlags =
2434 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2435 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2436 if (fusion->adapter_type == INVADER_SERIES) {
2437 if (io_info.do_fp_rlbypass ||
2438 (io_request->RaidContext.raid_context.reg_lock_flags
2439 == REGION_TYPE_UNUSED))
2440 cmd->request_desc->SCSIIO.RequestFlags =
2441 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2442 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2443 io_request->RaidContext.raid_context.type
2445 io_request->RaidContext.raid_context.reg_lock_flags |=
2446 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2447 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2448 io_request->RaidContext.raid_context.nseg = 0x1;
2449 } else if (instance->is_ventura) {
2450 io_request->RaidContext.raid_context_g35.routing_flags |=
2451 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2452 io_request->RaidContext.raid_context_g35.nseg_type |=
2453 (1 << RAID_CONTEXT_NSEG_SHIFT);
2454 io_request->RaidContext.raid_context_g35.nseg_type |=
2455 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2457 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2458 io_request->DevHandle = cpu_to_le16(device_id);
2464 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
2465 * @instance: Adapter soft state
2466 * @scp: SCSI command
2467 * @cmd: Command to be prepared
2469 * Prepares the io_request frame for non-rw io cmds for vd.
2471 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2472 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
2475 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2477 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2478 struct fusion_context *fusion = instance->ctrl_context;
2482 struct MR_LD_RAID *raid;
2483 struct RAID_CONTEXT *pRAID_Context;
2486 io_request = cmd->io_request;
2487 device_id = MEGASAS_DEV_INDEX(scmd);
2488 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2489 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2490 /* get RAID_Context pointer */
2491 pRAID_Context = &io_request->RaidContext.raid_context;
2492 /* Check with FW team */
2493 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2494 pRAID_Context->reg_lock_row_lba = 0;
2495 pRAID_Context->reg_lock_length = 0;
2497 if (fusion->fast_path_io && (
2498 device_id < instance->fw_supported_vd_count)) {
2500 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2501 if (ld >= instance->fw_supported_vd_count)
2504 raid = MR_LdRaidGet(ld, local_map_ptr);
2505 if (!(raid->capability.fpNonRWCapable))
2512 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2513 io_request->DevHandle = cpu_to_le16(device_id);
2514 io_request->LUN[1] = scmd->device->lun;
2515 pRAID_Context->timeout_value =
2516 cpu_to_le16 (scmd->request->timeout / HZ);
2517 cmd->request_desc->SCSIIO.RequestFlags =
2518 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2519 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2522 /* set RAID context values */
2523 pRAID_Context->config_seq_num = raid->seqNum;
2524 if (!instance->is_ventura)
2525 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2526 pRAID_Context->timeout_value =
2527 cpu_to_le16(raid->fpIoTimeoutForLd);
2529 /* get the DevHandle for the PD (since this is
2530 fpNonRWCapable, this is a single disk RAID0) */
2532 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2533 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2534 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2536 /* build request descriptor */
2537 cmd->request_desc->SCSIIO.RequestFlags =
2538 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2539 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2540 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2542 /* populate the LUN field */
2543 memcpy(io_request->LUN, raid->LUN, 8);
2545 /* build the raidScsiIO structure */
2546 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2547 io_request->DevHandle = devHandle;
2552 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2553 * @instance: Adapter soft state
2554 * @scp: SCSI command
2555 * @cmd: Command to be prepared
2556 * @fp_possible: parameter to detect fast path or firmware path io.
2558 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2561 megasas_build_syspd_fusion(struct megasas_instance *instance,
2562 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2566 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2568 u16 os_timeout_value;
2570 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2571 struct RAID_CONTEXT *pRAID_Context;
2572 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
2573 struct MR_PRIV_DEVICE *mr_device_priv_data;
2574 struct fusion_context *fusion = instance->ctrl_context;
2575 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
2577 device_id = MEGASAS_DEV_INDEX(scmd);
2578 pd_index = MEGASAS_PD_INDEX(scmd);
2579 os_timeout_value = scmd->request->timeout / HZ;
2580 mr_device_priv_data = scmd->device->hostdata;
2581 cmd->pd_interface = mr_device_priv_data->interface_type;
2583 io_request = cmd->io_request;
2584 /* get RAID_Context pointer */
2585 pRAID_Context = &io_request->RaidContext.raid_context;
2586 pRAID_Context->reg_lock_flags = 0;
2587 pRAID_Context->reg_lock_row_lba = 0;
2588 pRAID_Context->reg_lock_length = 0;
2589 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2590 io_request->LUN[1] = scmd->device->lun;
2591 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
2592 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2594 /* If FW supports PD sequence number */
2595 if (instance->use_seqnum_jbod_fp &&
2596 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2597 /* TgtId must be incremented by 255 as jbod seq number is index
2600 /* More than 256 PD/JBOD support for Ventura */
2601 if (instance->support_morethan256jbod)
2602 pRAID_Context->virtual_disk_tgt_id =
2603 pd_sync->seq[pd_index].pd_target_id;
2605 pRAID_Context->virtual_disk_tgt_id =
2606 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
2607 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
2608 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
2609 if (instance->is_ventura) {
2610 io_request->RaidContext.raid_context_g35.routing_flags |=
2611 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2612 io_request->RaidContext.raid_context_g35.nseg_type |=
2613 (1 << RAID_CONTEXT_NSEG_SHIFT);
2614 io_request->RaidContext.raid_context_g35.nseg_type |=
2615 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2617 pRAID_Context->type = MPI2_TYPE_CUDA;
2618 pRAID_Context->nseg = 0x1;
2619 pRAID_Context->reg_lock_flags |=
2620 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
2622 } else if (fusion->fast_path_io) {
2623 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2624 pRAID_Context->config_seq_num = 0;
2625 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2626 io_request->DevHandle =
2627 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
2629 /* Want to send all IO via FW path */
2630 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2631 pRAID_Context->config_seq_num = 0;
2632 io_request->DevHandle = cpu_to_le16(0xFFFF);
2635 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
2636 cmd->request_desc->SCSIIO.MSIxIndex =
2637 instance->msix_vectors ?
2638 (raw_smp_processor_id() % instance->msix_vectors) : 0;
2642 /* system pd firmware path */
2643 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2644 cmd->request_desc->SCSIIO.RequestFlags =
2645 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2646 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2647 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
2648 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2650 /* system pd Fast Path */
2651 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2652 timeout_limit = (scmd->device->type == TYPE_DISK) ?
2654 pRAID_Context->timeout_value =
2655 cpu_to_le16((os_timeout_value > timeout_limit) ?
2656 timeout_limit : os_timeout_value);
2657 if (fusion->adapter_type >= INVADER_SERIES)
2658 io_request->IoFlags |=
2659 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
2661 cmd->request_desc->SCSIIO.RequestFlags =
2662 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
2663 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2668 * megasas_build_io_fusion - Prepares IOs to devices
2669 * @instance: Adapter soft state
2670 * @scp: SCSI command
2671 * @cmd: Command to be prepared
2673 * Invokes helper functions to prepare request frames
2674 * and sets flags appropriate for IO/Non-IO cmd
2677 megasas_build_io_fusion(struct megasas_instance *instance,
2678 struct scsi_cmnd *scp,
2679 struct megasas_cmd_fusion *cmd)
2683 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
2684 struct MR_PRIV_DEVICE *mr_device_priv_data;
2685 mr_device_priv_data = scp->device->hostdata;
2687 /* Zero out some fields so they don't get reused */
2688 memset(io_request->LUN, 0x0, 8);
2689 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
2690 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
2691 io_request->EEDPFlags = 0;
2692 io_request->Control = 0;
2693 io_request->EEDPBlockSize = 0;
2694 io_request->ChainOffset = 0;
2695 io_request->RaidContext.raid_context.raid_flags = 0;
2696 io_request->RaidContext.raid_context.type = 0;
2697 io_request->RaidContext.raid_context.nseg = 0;
2699 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
2701 * Just the CDB length,rest of the Flags are zero
2702 * This will be modified for FP in build_ldio_fusion
2704 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
2706 switch (cmd_type = megasas_cmd_type(scp)) {
2707 case READ_WRITE_LDIO:
2708 megasas_build_ldio_fusion(instance, scp, cmd);
2710 case NON_READ_WRITE_LDIO:
2711 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
2713 case READ_WRITE_SYSPDIO:
2714 megasas_build_syspd_fusion(instance, scp, cmd, true);
2716 case NON_READ_WRITE_SYSPDIO:
2717 if (instance->secure_jbod_support ||
2718 mr_device_priv_data->is_tm_capable)
2719 megasas_build_syspd_fusion(instance, scp, cmd, false);
2721 megasas_build_syspd_fusion(instance, scp, cmd, true);
2731 sge_count = megasas_make_sgl(instance, scp, cmd);
2733 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
2734 dev_err(&instance->pdev->dev,
2735 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
2736 __func__, __LINE__, sge_count, instance->max_num_sge);
2740 if (instance->is_ventura) {
2741 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
2742 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
2743 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
2745 /* numSGE store lower 8 bit of sge_count.
2746 * numSGEExt store higher 8 bit of sge_count
2748 io_request->RaidContext.raid_context.num_sge = sge_count;
2749 io_request->RaidContext.raid_context.num_sge_ext =
2750 (u8)(sge_count >> 8);
2753 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
2755 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
2756 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
2757 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2758 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
2760 io_request->SGLOffset0 =
2761 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
2763 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
2764 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2767 scp->SCp.ptr = (char *)cmd;
2772 static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2773 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
2776 struct fusion_context *fusion;
2778 fusion = instance->ctrl_context;
2779 p = fusion->req_frames_desc +
2780 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
2782 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2786 /* megasas_prepate_secondRaid1_IO
2787 * It prepares the raid 1 second IO
2789 void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
2790 struct megasas_cmd_fusion *cmd,
2791 struct megasas_cmd_fusion *r1_cmd)
2793 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
2794 struct fusion_context *fusion;
2795 fusion = instance->ctrl_context;
2796 req_desc = cmd->request_desc;
2797 /* copy the io request frame as well as 8 SGEs data for r1 command*/
2798 memcpy(r1_cmd->io_request, cmd->io_request,
2799 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
2800 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
2801 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
2802 /*sense buffer is different for r1 command*/
2803 r1_cmd->io_request->SenseBufferLowAddress =
2804 cpu_to_le32(r1_cmd->sense_phys_addr);
2805 r1_cmd->scmd = cmd->scmd;
2806 req_desc2 = megasas_get_request_descriptor(instance,
2807 (r1_cmd->index - 1));
2808 req_desc2->Words = 0;
2809 r1_cmd->request_desc = req_desc2;
2810 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
2811 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
2812 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
2813 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
2814 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
2815 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2816 cpu_to_le16(r1_cmd->index);
2817 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2818 cpu_to_le16(cmd->index);
2819 /*MSIxIndex of both commands request descriptors should be same*/
2820 r1_cmd->request_desc->SCSIIO.MSIxIndex =
2821 cmd->request_desc->SCSIIO.MSIxIndex;
2822 /*span arm is different for r1 cmd*/
2823 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
2824 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
2828 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2829 * issuing non IOCTL cmd
2830 * @instance: Adapter soft state
2831 * @scmd: pointer to scsi cmd from OS
2834 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2835 struct scsi_cmnd *scmd)
2837 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
2838 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2840 struct fusion_context *fusion;
2842 fusion = instance->ctrl_context;
2844 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
2845 instance->ldio_threshold &&
2846 (atomic_inc_return(&instance->ldio_outstanding) >
2847 instance->ldio_threshold)) {
2848 atomic_dec(&instance->ldio_outstanding);
2849 return SCSI_MLQUEUE_DEVICE_BUSY;
2852 if (atomic_inc_return(&instance->fw_outstanding) >
2853 instance->host->can_queue) {
2854 atomic_dec(&instance->fw_outstanding);
2855 return SCSI_MLQUEUE_HOST_BUSY;
2858 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
2861 atomic_dec(&instance->fw_outstanding);
2862 return SCSI_MLQUEUE_HOST_BUSY;
2867 req_desc = megasas_get_request_descriptor(instance, index-1);
2869 req_desc->Words = 0;
2870 cmd->request_desc = req_desc;
2872 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2873 megasas_return_cmd_fusion(instance, cmd);
2874 dev_err(&instance->pdev->dev, "Error building command\n");
2875 cmd->request_desc = NULL;
2876 atomic_dec(&instance->fw_outstanding);
2877 return SCSI_MLQUEUE_HOST_BUSY;
2880 req_desc = cmd->request_desc;
2881 req_desc->SCSIIO.SMID = cpu_to_le16(index);
2883 if (cmd->io_request->ChainOffset != 0 &&
2884 cmd->io_request->ChainOffset != 0xF)
2885 dev_err(&instance->pdev->dev, "The chain offset value is not "
2886 "correct : %x\n", cmd->io_request->ChainOffset);
2888 * if it is raid 1/10 fp write capable.
2889 * try to get second command from pool and construct it.
2890 * From FW, it has confirmed that lba values of two PDs
2891 * corresponds to single R1/10 LD are always same
2894 /* driver side count always should be less than max_fw_cmds
2895 * to get new command
2897 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
2898 r1_cmd = megasas_get_cmd_fusion(instance,
2899 (scmd->request->tag + instance->max_fw_cmds));
2900 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
2905 * Issue the command to the FW
2908 megasas_fire_cmd_fusion(instance, req_desc);
2911 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
2918 * megasas_complete_r1_command -
2919 * completes R1 FP write commands which has valid peer smid
2920 * @instance: Adapter soft state
2921 * @cmd_fusion: MPT command frame
2925 megasas_complete_r1_command(struct megasas_instance *instance,
2926 struct megasas_cmd_fusion *cmd)
2928 u8 *sense, status, ex_status;
2931 struct fusion_context *fusion;
2932 struct megasas_cmd_fusion *r1_cmd = NULL;
2933 struct scsi_cmnd *scmd_local = NULL;
2934 struct RAID_CONTEXT_G35 *rctx_g35;
2936 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
2937 fusion = instance->ctrl_context;
2938 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
2940 r1_cmd = fusion->cmd_list[peer_smid - 1];
2941 scmd_local = cmd->scmd;
2942 status = rctx_g35->status;
2943 ex_status = rctx_g35->ex_status;
2944 data_length = cmd->io_request->DataLength;
2947 cmd->cmd_completed = true;
2949 /* Check if peer command is completed or not*/
2950 if (r1_cmd->cmd_completed) {
2951 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
2952 if (rctx_g35->status != MFI_STAT_OK) {
2953 status = rctx_g35->status;
2954 ex_status = rctx_g35->ex_status;
2955 data_length = r1_cmd->io_request->DataLength;
2956 sense = r1_cmd->sense;
2959 megasas_return_cmd_fusion(instance, r1_cmd);
2960 map_cmd_status(fusion, scmd_local, status, ex_status,
2961 le32_to_cpu(data_length), sense);
2962 if (instance->ldio_threshold &&
2963 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
2964 atomic_dec(&instance->ldio_outstanding);
2965 scmd_local->SCp.ptr = NULL;
2966 megasas_return_cmd_fusion(instance, cmd);
2967 scsi_dma_unmap(scmd_local);
2968 scmd_local->scsi_done(scmd_local);
2973 * complete_cmd_fusion - Completes command
2974 * @instance: Adapter soft state
2975 * Completes all commands that is in reply descriptor queue
2978 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
2980 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2981 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2982 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2983 struct fusion_context *fusion;
2984 struct megasas_cmd *cmd_mfi;
2985 struct megasas_cmd_fusion *cmd_fusion;
2986 u16 smid, num_completed;
2987 u8 reply_descript_type, *sense, status, extStatus;
2988 u32 device_id, data_length;
2989 union desc_value d_val;
2990 struct LD_LOAD_BALANCE_INFO *lbinfo;
2991 int threshold_reply_count = 0;
2992 struct scsi_cmnd *scmd_local = NULL;
2993 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
2994 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
2996 fusion = instance->ctrl_context;
2998 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
3001 desc = fusion->reply_frames_desc[MSIxIndex] +
3002 fusion->last_reply_idx[MSIxIndex];
3004 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3006 d_val.word = desc->Words;
3008 reply_descript_type = reply_desc->ReplyFlags &
3009 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3011 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3016 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3017 d_val.u.high != cpu_to_le32(UINT_MAX)) {
3019 smid = le16_to_cpu(reply_desc->SMID);
3020 cmd_fusion = fusion->cmd_list[smid - 1];
3021 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3022 cmd_fusion->io_request;
3024 scmd_local = cmd_fusion->scmd;
3025 status = scsi_io_req->RaidContext.raid_context.status;
3026 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
3027 sense = cmd_fusion->sense;
3028 data_length = scsi_io_req->DataLength;
3030 switch (scsi_io_req->Function) {
3031 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3032 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3033 cmd_fusion->io_request;
3034 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3035 &mr_tm_req->TmRequest;
3036 dev_dbg(&instance->pdev->dev, "TM completion:"
3037 "type: 0x%x TaskMID: 0x%x\n",
3038 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3039 complete(&cmd_fusion->done);
3041 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3042 /* Update load balancing info */
3043 if (fusion->load_balance_info &&
3044 (cmd_fusion->scmd->SCp.Status &
3045 MEGASAS_LOAD_BALANCE_FLAG)) {
3046 device_id = MEGASAS_DEV_INDEX(scmd_local);
3047 lbinfo = &fusion->load_balance_info[device_id];
3048 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
3049 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
3051 //Fall thru and complete IO
3052 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
3053 atomic_dec(&instance->fw_outstanding);
3054 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
3055 map_cmd_status(fusion, scmd_local, status,
3056 extStatus, le32_to_cpu(data_length),
3058 if (instance->ldio_threshold &&
3059 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
3060 atomic_dec(&instance->ldio_outstanding);
3061 scmd_local->SCp.ptr = NULL;
3062 megasas_return_cmd_fusion(instance, cmd_fusion);
3063 scsi_dma_unmap(scmd_local);
3064 scmd_local->scsi_done(scmd_local);
3065 } else /* Optimal VD - R1 FP command completion. */
3066 megasas_complete_r1_command(instance, cmd_fusion);
3068 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3069 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3070 /* Poll mode. Dummy free.
3071 * In case of Interrupt mode, caller has reverse check.
3073 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3074 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3075 megasas_return_cmd(instance, cmd_mfi);
3077 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
3081 fusion->last_reply_idx[MSIxIndex]++;
3082 if (fusion->last_reply_idx[MSIxIndex] >=
3083 fusion->reply_q_depth)
3084 fusion->last_reply_idx[MSIxIndex] = 0;
3086 desc->Words = cpu_to_le64(ULLONG_MAX);
3088 threshold_reply_count++;
3090 /* Get the next reply descriptor */
3091 if (!fusion->last_reply_idx[MSIxIndex])
3092 desc = fusion->reply_frames_desc[MSIxIndex];
3097 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3099 d_val.word = desc->Words;
3101 reply_descript_type = reply_desc->ReplyFlags &
3102 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3104 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3107 * Write to reply post host index register after completing threshold
3108 * number of reply counts and still there are more replies in reply queue
3109 * pending to be completed
3111 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
3112 if (instance->msix_combined)
3113 writel(((MSIxIndex & 0x7) << 24) |
3114 fusion->last_reply_idx[MSIxIndex],
3115 instance->reply_post_host_index_addr[MSIxIndex/8]);
3117 writel((MSIxIndex << 24) |
3118 fusion->last_reply_idx[MSIxIndex],
3119 instance->reply_post_host_index_addr[0]);
3120 threshold_reply_count = 0;
3128 if (instance->msix_combined)
3129 writel(((MSIxIndex & 0x7) << 24) |
3130 fusion->last_reply_idx[MSIxIndex],
3131 instance->reply_post_host_index_addr[MSIxIndex/8]);
3133 writel((MSIxIndex << 24) |
3134 fusion->last_reply_idx[MSIxIndex],
3135 instance->reply_post_host_index_addr[0]);
3136 megasas_check_and_restore_queue_depth(instance);
3141 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3142 * @instance: Adapter soft state
3144 void megasas_sync_irqs(unsigned long instance_addr)
3147 struct megasas_instance *instance =
3148 (struct megasas_instance *)instance_addr;
3150 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3152 for (i = 0; i < count; i++)
3153 synchronize_irq(pci_irq_vector(instance->pdev, i));
3157 * megasas_complete_cmd_dpc_fusion - Completes command
3158 * @instance: Adapter soft state
3160 * Tasklet to complete cmds
3163 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3165 struct megasas_instance *instance =
3166 (struct megasas_instance *)instance_addr;
3167 unsigned long flags;
3168 u32 count, MSIxIndex;
3170 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3172 /* If we have already declared adapter dead, donot complete cmds */
3173 spin_lock_irqsave(&instance->hba_lock, flags);
3174 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
3175 spin_unlock_irqrestore(&instance->hba_lock, flags);
3178 spin_unlock_irqrestore(&instance->hba_lock, flags);
3180 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3181 complete_cmd_fusion(instance, MSIxIndex);
3185 * megasas_isr_fusion - isr entry point
3187 irqreturn_t megasas_isr_fusion(int irq, void *devp)
3189 struct megasas_irq_context *irq_context = devp;
3190 struct megasas_instance *instance = irq_context->instance;
3191 u32 mfiStatus, fw_state, dma_state;
3193 if (instance->mask_interrupts)
3196 if (!instance->msix_vectors) {
3197 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3202 /* If we are resetting, bail */
3203 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3204 instance->instancet->clear_intr(instance->reg_set);
3208 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
3209 instance->instancet->clear_intr(instance->reg_set);
3210 /* If we didn't complete any commands, check for FW fault */
3211 fw_state = instance->instancet->read_fw_status_reg(
3212 instance->reg_set) & MFI_STATE_MASK;
3213 dma_state = instance->instancet->read_fw_status_reg
3214 (instance->reg_set) & MFI_STATE_DMADONE;
3215 if (instance->crash_dump_drv_support &&
3216 instance->crash_dump_app_support) {
3217 /* Start collecting crash, if DMA bit is done */
3218 if ((fw_state == MFI_STATE_FAULT) && dma_state)
3219 schedule_work(&instance->crash_init);
3220 else if (fw_state == MFI_STATE_FAULT) {
3221 if (instance->unload == 0)
3222 schedule_work(&instance->work_init);
3224 } else if (fw_state == MFI_STATE_FAULT) {
3225 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
3226 "for scsi%d\n", instance->host->host_no);
3227 if (instance->unload == 0)
3228 schedule_work(&instance->work_init);
3236 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3237 * @instance: Adapter soft state
3238 * mfi_cmd: megasas_cmd pointer
3242 build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3243 struct megasas_cmd *mfi_cmd)
3245 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3246 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3247 struct megasas_cmd_fusion *cmd;
3248 struct fusion_context *fusion;
3249 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3251 fusion = instance->ctrl_context;
3253 cmd = megasas_get_cmd_fusion(instance,
3254 instance->max_scsi_cmds + mfi_cmd->index);
3256 /* Save the smid. To be used for returning the cmd */
3257 mfi_cmd->context.smid = cmd->index;
3260 * For cmds where the flag is set, store the flag and check
3261 * on completion. For cmds with this flag, don't call
3262 * megasas_complete_cmd
3265 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
3266 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
3268 io_req = cmd->io_request;
3270 if (fusion->adapter_type >= INVADER_SERIES) {
3271 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3272 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3273 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3274 sgl_ptr_end->Flags = 0;
3278 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3280 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3281 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3283 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3285 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
3287 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3288 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3290 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
3294 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3295 * @instance: Adapter soft state
3296 * @cmd: mfi cmd to build
3299 union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3300 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3302 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
3305 build_mpt_mfi_pass_thru(instance, cmd);
3306 index = cmd->context.smid;
3308 req_desc = megasas_get_request_descriptor(instance, index - 1);
3310 req_desc->Words = 0;
3311 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3312 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3314 req_desc->SCSIIO.SMID = cpu_to_le16(index);
3320 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3321 * @instance: Adapter soft state
3322 * @cmd: mfi cmd pointer
3326 megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3327 struct megasas_cmd *cmd)
3329 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3331 req_desc = build_mpt_cmd(instance, cmd);
3333 megasas_fire_cmd_fusion(instance, req_desc);
3338 * megasas_release_fusion - Reverses the FW initialization
3339 * @instance: Adapter soft state
3342 megasas_release_fusion(struct megasas_instance *instance)
3344 megasas_free_cmds(instance);
3345 megasas_free_cmds_fusion(instance);
3347 iounmap(instance->reg_set);
3349 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
3353 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3354 * @regs: MFI register set
3357 megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3359 return readl(&(regs)->outbound_scratch_pad);
3363 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3364 * @instance: Controller's soft instance
3365 * return: Number of allocated host crash buffers
3368 megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3372 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
3373 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3374 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
3375 instance->crash_buf_pages);
3376 if (!instance->crash_buf[i]) {
3377 dev_info(&instance->pdev->dev, "Firmware crash dump "
3378 "memory allocation failed at index %d\n", i);
3381 memset(instance->crash_buf[i], 0,
3382 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
3384 instance->drv_buf_alloc = i;
3388 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3389 * @instance: Controller's soft instance
3392 megasas_free_host_crash_buffer(struct megasas_instance *instance)
3396 for (i = 0; i < instance->drv_buf_alloc; i++) {
3397 if (instance->crash_buf[i])
3398 free_pages((ulong)instance->crash_buf[i],
3399 instance->crash_buf_pages);
3401 instance->drv_buf_index = 0;
3402 instance->drv_buf_alloc = 0;
3403 instance->fw_crash_state = UNAVAILABLE;
3404 instance->fw_crash_buffer_size = 0;
3408 * megasas_adp_reset_fusion - For controller reset
3409 * @regs: MFI register set
3412 megasas_adp_reset_fusion(struct megasas_instance *instance,
3413 struct megasas_register_set __iomem *regs)
3415 u32 host_diag, abs_state, retry;
3417 /* Now try to reset the chip */
3418 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3419 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3420 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3421 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3422 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3423 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3424 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3426 /* Check that the diag write enable (DRWE) bit is on */
3427 host_diag = readl(&instance->reg_set->fusion_host_diag);
3429 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3431 host_diag = readl(&instance->reg_set->fusion_host_diag);
3432 if (retry++ == 100) {
3433 dev_warn(&instance->pdev->dev,
3434 "Host diag unlock failed from %s %d\n",
3435 __func__, __LINE__);
3439 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3442 /* Send chip reset command */
3443 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3444 &instance->reg_set->fusion_host_diag);
3447 /* Make sure reset adapter bit is cleared */
3448 host_diag = readl(&instance->reg_set->fusion_host_diag);
3450 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3452 host_diag = readl(&instance->reg_set->fusion_host_diag);
3453 if (retry++ == 1000) {
3454 dev_warn(&instance->pdev->dev,
3455 "Diag reset adapter never cleared %s %d\n",
3456 __func__, __LINE__);
3460 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3463 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3467 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3469 abs_state = instance->instancet->
3470 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3472 if (abs_state <= MFI_STATE_FW_INIT) {
3473 dev_warn(&instance->pdev->dev,
3474 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3475 abs_state, __func__, __LINE__);
3483 * megasas_check_reset_fusion - For controller reset check
3484 * @regs: MFI register set
3487 megasas_check_reset_fusion(struct megasas_instance *instance,
3488 struct megasas_register_set __iomem *regs)
3493 /* This function waits for outstanding commands on fusion to complete */
3494 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
3495 int reason, int *convert)
3497 int i, outstanding, retval = 0, hb_seconds_missed = 0;
3500 for (i = 0; i < resetwaittime; i++) {
3501 /* Check if firmware is in fault state */
3502 fw_state = instance->instancet->read_fw_status_reg(
3503 instance->reg_set) & MFI_STATE_MASK;
3504 if (fw_state == MFI_STATE_FAULT) {
3505 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
3506 " will reset adapter scsi%d.\n",
3507 instance->host->host_no);
3508 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3509 if (instance->requestorId && reason) {
3510 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3511 " state while polling during"
3512 " I/O timeout handling for %d\n",
3513 instance->host->host_no);
3521 if (reason == MFI_IO_TIMEOUT_OCR) {
3522 dev_info(&instance->pdev->dev,
3523 "MFI IO is timed out, initiating OCR\n");
3524 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3529 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
3530 if (instance->requestorId && !reason) {
3535 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
3536 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
3537 if (instance->hb_host_mem->HB.fwCounter !=
3538 instance->hb_host_mem->HB.driverCounter) {
3539 instance->hb_host_mem->HB.driverCounter =
3540 instance->hb_host_mem->HB.fwCounter;
3541 hb_seconds_missed = 0;
3543 hb_seconds_missed++;
3544 if (hb_seconds_missed ==
3545 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
3546 dev_warn(&instance->pdev->dev, "SR-IOV:"
3547 " Heartbeat never completed "
3548 " while polling during I/O "
3549 " timeout handling for "
3551 instance->host->host_no);
3559 outstanding = atomic_read(&instance->fw_outstanding);
3563 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
3564 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
3565 "commands to complete for scsi%d\n", i,
3566 outstanding, instance->host->host_no);
3567 megasas_complete_cmd_dpc_fusion(
3568 (unsigned long)instance);
3573 if (atomic_read(&instance->fw_outstanding)) {
3574 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
3575 "will reset adapter scsi%d.\n",
3576 instance->host->host_no);
3584 void megasas_reset_reply_desc(struct megasas_instance *instance)
3587 struct fusion_context *fusion;
3588 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3590 fusion = instance->ctrl_context;
3591 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3592 for (i = 0 ; i < count ; i++) {
3593 fusion->last_reply_idx[i] = 0;
3594 reply_desc = fusion->reply_frames_desc[i];
3595 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3596 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
3601 * megasas_refire_mgmt_cmd : Re-fire management commands
3602 * @instance: Controller's soft instance
3604 void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
3607 struct megasas_cmd_fusion *cmd_fusion;
3608 struct fusion_context *fusion;
3609 struct megasas_cmd *cmd_mfi;
3610 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3612 bool refire_cmd = 0;
3614 fusion = instance->ctrl_context;
3616 /* Re-fire management commands.
3617 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
3619 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
3620 cmd_fusion = fusion->cmd_list[j];
3621 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3622 smid = le16_to_cpu(cmd_mfi->context.smid);
3626 req_desc = megasas_get_request_descriptor
3627 (instance, smid - 1);
3628 refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
3629 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
3630 (cmd_mfi->frame->dcmd.opcode !=
3631 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
3632 && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
3634 megasas_fire_cmd_fusion(instance, req_desc);
3636 megasas_return_cmd(instance, cmd_mfi);
3641 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
3642 * @instance: per adapter struct
3643 * @channel: the channel assigned by the OS
3644 * @id: the id assigned by the OS
3646 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
3649 static int megasas_track_scsiio(struct megasas_instance *instance,
3650 int id, int channel)
3653 struct megasas_cmd_fusion *cmd_fusion;
3654 struct fusion_context *fusion;
3655 fusion = instance->ctrl_context;
3657 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
3658 cmd_fusion = fusion->cmd_list[i];
3659 if (cmd_fusion->scmd &&
3660 (cmd_fusion->scmd->device->id == id &&
3661 cmd_fusion->scmd->device->channel == channel)) {
3662 dev_info(&instance->pdev->dev,
3663 "SCSI commands pending to target"
3664 "channel %d id %d \tSMID: 0x%x\n",
3665 channel, id, cmd_fusion->index);
3666 scsi_print_command(cmd_fusion->scmd);
3672 return found ? FAILED : SUCCESS;
3676 * megasas_tm_response_code - translation of device response code
3677 * @ioc: per adapter object
3678 * @mpi_reply: MPI reply returned by firmware
3683 megasas_tm_response_code(struct megasas_instance *instance,
3684 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
3688 switch (mpi_reply->ResponseCode) {
3689 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
3690 desc = "task management request completed";
3692 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
3693 desc = "invalid frame";
3695 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
3696 desc = "task management request not supported";
3698 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
3699 desc = "task management request failed";
3701 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
3702 desc = "task management request succeeded";
3704 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
3705 desc = "invalid lun";
3708 desc = "overlapped tag attempted";
3710 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
3711 desc = "task queued, however not sent to target";
3717 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
3718 mpi_reply->ResponseCode, desc);
3719 dev_dbg(&instance->pdev->dev,
3720 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
3721 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
3722 mpi_reply->TerminationCount, mpi_reply->DevHandle,
3723 mpi_reply->Function, mpi_reply->TaskType,
3724 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
3728 * megasas_issue_tm - main routine for sending tm requests
3729 * @instance: per adapter struct
3730 * @device_handle: device handle
3731 * @channel: the channel assigned by the OS
3732 * @id: the id assigned by the OS
3733 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
3734 * @smid_task: smid assigned to the task
3735 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
3738 * MegaRaid use MPT interface for Task Magement request.
3739 * A generic API for sending task management requests to firmware.
3741 * Return SUCCESS or FAILED.
3744 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
3745 uint channel, uint id, u16 smid_task, u8 type)
3747 struct MR_TASK_MANAGE_REQUEST *mr_request;
3748 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
3749 unsigned long timeleft;
3750 struct megasas_cmd_fusion *cmd_fusion;
3751 struct megasas_cmd *cmd_mfi;
3752 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3753 struct fusion_context *fusion;
3754 struct megasas_cmd_fusion *scsi_lookup;
3756 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
3758 fusion = instance->ctrl_context;
3760 cmd_mfi = megasas_get_cmd(instance);
3763 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
3764 __func__, __LINE__);
3768 cmd_fusion = megasas_get_cmd_fusion(instance,
3769 instance->max_scsi_cmds + cmd_mfi->index);
3771 /* Save the smid. To be used for returning the cmd */
3772 cmd_mfi->context.smid = cmd_fusion->index;
3774 req_desc = megasas_get_request_descriptor(instance,
3775 (cmd_fusion->index - 1));
3777 cmd_fusion->request_desc = req_desc;
3778 req_desc->Words = 0;
3780 scsi_lookup = fusion->cmd_list[smid_task - 1];
3782 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
3783 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
3784 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
3785 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3786 mpi_request->DevHandle = cpu_to_le16(device_handle);
3787 mpi_request->TaskType = type;
3788 mpi_request->TaskMID = cpu_to_le16(smid_task);
3789 mpi_request->LUN[1] = 0;
3792 req_desc = cmd_fusion->request_desc;
3793 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
3794 req_desc->HighPriority.RequestFlags =
3795 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
3796 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3797 req_desc->HighPriority.MSIxIndex = 0;
3798 req_desc->HighPriority.LMID = 0;
3799 req_desc->HighPriority.Reserved1 = 0;
3801 if (channel < MEGASAS_MAX_PD_CHANNELS)
3802 mr_request->tmReqFlags.isTMForPD = 1;
3804 mr_request->tmReqFlags.isTMForLD = 1;
3806 init_completion(&cmd_fusion->done);
3807 megasas_fire_cmd_fusion(instance, req_desc);
3809 timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
3812 dev_err(&instance->pdev->dev,
3813 "task mgmt type 0x%x timed out\n", type);
3814 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
3815 mutex_unlock(&instance->reset_mutex);
3816 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
3817 mutex_lock(&instance->reset_mutex);
3821 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
3822 megasas_tm_response_code(instance, mpi_reply);
3824 megasas_return_cmd(instance, cmd_mfi);
3827 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3828 if (scsi_lookup->scmd == NULL)
3831 instance->instancet->disable_intr(instance);
3832 megasas_sync_irqs((unsigned long)instance);
3833 megasas_complete_cmd_dpc_fusion
3834 ((unsigned long)instance);
3835 instance->instancet->enable_intr(instance);
3836 if (scsi_lookup->scmd == NULL)
3842 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3843 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
3845 instance->instancet->disable_intr(instance);
3847 megasas_complete_cmd_dpc_fusion
3848 ((unsigned long)instance);
3849 rc = megasas_track_scsiio(instance, id, channel);
3850 instance->instancet->enable_intr(instance);
3853 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3854 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3866 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
3867 * @instance: per adapter struct
3869 * Return Non Zero index, if SMID found in outstanding commands
3871 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
3874 struct megasas_instance *instance;
3875 struct megasas_cmd_fusion *cmd_fusion;
3876 struct fusion_context *fusion;
3878 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3880 fusion = instance->ctrl_context;
3882 for (i = 0; i < instance->max_scsi_cmds; i++) {
3883 cmd_fusion = fusion->cmd_list[i];
3884 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
3885 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
3886 " SMID: %d\n", cmd_fusion->index);
3887 ret = cmd_fusion->index;
3896 * megasas_get_tm_devhandle - Get devhandle for TM request
3897 * @sdev- OS provided scsi device
3899 * Returns- devhandle/targetID of SCSI device
3901 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
3905 struct megasas_instance *instance;
3906 struct fusion_context *fusion;
3907 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3908 u16 devhandle = (u16)ULONG_MAX;
3910 instance = (struct megasas_instance *)sdev->host->hostdata;
3911 fusion = instance->ctrl_context;
3913 if (!MEGASAS_IS_LOGICAL(sdev)) {
3914 if (instance->use_seqnum_jbod_fp) {
3915 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
3917 pd_sync = (void *)fusion->pd_seq_sync
3918 [(instance->pd_seq_map_id - 1) & 1];
3919 devhandle = pd_sync->seq[pd_index].devHandle;
3921 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
3922 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
3924 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
3926 devhandle = device_id;
3933 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
3934 * @scmd : pointer to scsi command object
3936 * Return SUCCESS, if command aborted else FAILED
3939 int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
3941 struct megasas_instance *instance;
3942 u16 smid, devhandle;
3943 struct fusion_context *fusion;
3945 struct MR_PRIV_DEVICE *mr_device_priv_data;
3946 mr_device_priv_data = scmd->device->hostdata;
3949 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3950 fusion = instance->ctrl_context;
3952 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
3953 scsi_print_command(scmd);
3955 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
3956 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
3957 "SCSI host:%d\n", instance->host->host_no);
3962 if (!mr_device_priv_data) {
3963 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
3964 "scmd(%p)\n", scmd);
3965 scmd->result = DID_NO_CONNECT << 16;
3971 if (!mr_device_priv_data->is_tm_capable) {
3976 mutex_lock(&instance->reset_mutex);
3978 smid = megasas_fusion_smid_lookup(scmd);
3982 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
3983 " issued is not found in oustanding commands\n");
3984 mutex_unlock(&instance->reset_mutex);
3988 devhandle = megasas_get_tm_devhandle(scmd->device);
3990 if (devhandle == (u16)ULONG_MAX) {
3992 sdev_printk(KERN_INFO, scmd->device,
3993 "task abort issued for invalid devhandle\n");
3994 mutex_unlock(&instance->reset_mutex);
3997 sdev_printk(KERN_INFO, scmd->device,
3998 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
4001 mr_device_priv_data->tm_busy = 1;
4002 ret = megasas_issue_tm(instance, devhandle,
4003 scmd->device->channel, scmd->device->id, smid,
4004 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
4005 mr_device_priv_data->tm_busy = 0;
4007 mutex_unlock(&instance->reset_mutex);
4009 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4010 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4016 * megasas_reset_target_fusion : target reset function for fusion adapters
4017 * scmd: SCSI command pointer
4019 * Returns SUCCESS if all commands associated with target aborted else FAILED
4022 int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4025 struct megasas_instance *instance;
4028 struct fusion_context *fusion;
4029 struct MR_PRIV_DEVICE *mr_device_priv_data;
4030 mr_device_priv_data = scmd->device->hostdata;
4032 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4033 fusion = instance->ctrl_context;
4035 sdev_printk(KERN_INFO, scmd->device,
4036 "target reset called for scmd(%p)\n", scmd);
4038 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
4039 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4040 "SCSI host:%d\n", instance->host->host_no);
4045 if (!mr_device_priv_data) {
4046 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4047 "scmd(%p)\n", scmd);
4048 scmd->result = DID_NO_CONNECT << 16;
4054 if (!mr_device_priv_data->is_tm_capable) {
4059 mutex_lock(&instance->reset_mutex);
4060 devhandle = megasas_get_tm_devhandle(scmd->device);
4062 if (devhandle == (u16)ULONG_MAX) {
4064 sdev_printk(KERN_INFO, scmd->device,
4065 "target reset issued for invalid devhandle\n");
4066 mutex_unlock(&instance->reset_mutex);
4070 sdev_printk(KERN_INFO, scmd->device,
4071 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4073 mr_device_priv_data->tm_busy = 1;
4074 ret = megasas_issue_tm(instance, devhandle,
4075 scmd->device->channel, scmd->device->id, 0,
4076 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
4077 mr_device_priv_data->tm_busy = 0;
4078 mutex_unlock(&instance->reset_mutex);
4080 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4081 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4086 /*SRIOV get other instance in cluster if any*/
4087 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4091 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4092 if (megasas_mgmt_info.instance[i] &&
4093 (megasas_mgmt_info.instance[i] != instance) &&
4094 megasas_mgmt_info.instance[i]->requestorId &&
4095 megasas_mgmt_info.instance[i]->peerIsPresent &&
4096 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4097 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4098 return megasas_mgmt_info.instance[i];
4103 /* Check for a second path that is currently UP */
4104 int megasas_check_mpio_paths(struct megasas_instance *instance,
4105 struct scsi_cmnd *scmd)
4107 struct megasas_instance *peer_instance = NULL;
4108 int retval = (DID_REQUEUE << 16);
4110 if (instance->peerIsPresent) {
4111 peer_instance = megasas_get_peer_instance(instance);
4112 if ((peer_instance) &&
4113 (atomic_read(&peer_instance->adprecovery) ==
4114 MEGASAS_HBA_OPERATIONAL))
4115 retval = (DID_NO_CONNECT << 16);
4120 /* Core fusion reset function */
4121 int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
4123 int retval = SUCCESS, i, j, convert = 0;
4124 struct megasas_instance *instance;
4125 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
4126 struct fusion_context *fusion;
4127 u32 abs_state, status_reg, reset_adapter;
4128 u32 io_timeout_in_crash_mode = 0;
4129 struct scsi_cmnd *scmd_local = NULL;
4130 struct scsi_device *sdev;
4132 instance = (struct megasas_instance *)shost->hostdata;
4133 fusion = instance->ctrl_context;
4135 mutex_lock(&instance->reset_mutex);
4137 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
4138 dev_warn(&instance->pdev->dev, "Hardware critical error, "
4139 "returning FAILED for scsi%d.\n",
4140 instance->host->host_no);
4141 mutex_unlock(&instance->reset_mutex);
4144 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4145 abs_state = status_reg & MFI_STATE_MASK;
4147 /* IO timeout detected, forcibly put FW in FAULT state */
4148 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
4149 instance->crash_dump_app_support && reason) {
4150 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
4151 "forcibly FAULT Firmware\n");
4152 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4153 status_reg = readl(&instance->reg_set->doorbell);
4154 writel(status_reg | MFI_STATE_FORCE_OCR,
4155 &instance->reg_set->doorbell);
4156 readl(&instance->reg_set->doorbell);
4157 mutex_unlock(&instance->reset_mutex);
4160 io_timeout_in_crash_mode++;
4161 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4162 "seconds for crash dump collection and OCR "
4163 "to be done\n", (io_timeout_in_crash_mode * 3));
4164 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
4165 (io_timeout_in_crash_mode < 80));
4167 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
4168 dev_info(&instance->pdev->dev, "OCR done for IO "
4172 dev_info(&instance->pdev->dev, "Controller is not "
4173 "operational after 240 seconds wait for IO "
4174 "timeout case in FW crash dump mode\n do "
4175 "OCR/kill adapter\n");
4176 retval = megasas_reset_fusion(shost, 0);
4181 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4182 del_timer_sync(&instance->sriov_heartbeat_timer);
4183 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4184 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
4185 instance->instancet->disable_intr(instance);
4186 megasas_sync_irqs((unsigned long)instance);
4188 /* First try waiting for commands to complete */
4189 if (megasas_wait_for_outstanding_fusion(instance, reason,
4191 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4192 dev_warn(&instance->pdev->dev, "resetting fusion "
4193 "adapter scsi%d.\n", instance->host->host_no);
4197 if (megasas_dbg_lvl & OCR_LOGS)
4198 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4200 /* Now return commands back to the OS */
4201 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4202 cmd_fusion = fusion->cmd_list[i];
4203 /*check for extra commands issued by driver*/
4204 if (instance->is_ventura) {
4205 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4206 megasas_return_cmd_fusion(instance, r1_cmd);
4208 scmd_local = cmd_fusion->scmd;
4209 if (cmd_fusion->scmd) {
4210 if (megasas_dbg_lvl & OCR_LOGS) {
4211 sdev_printk(KERN_INFO,
4212 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4214 scsi_print_command(cmd_fusion->scmd);
4217 scmd_local->result =
4218 megasas_check_mpio_paths(instance,
4220 if (instance->ldio_threshold &&
4221 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
4222 atomic_dec(&instance->ldio_outstanding);
4223 megasas_return_cmd_fusion(instance, cmd_fusion);
4224 scsi_dma_unmap(scmd_local);
4225 scmd_local->scsi_done(scmd_local);
4229 atomic_set(&instance->fw_outstanding, 0);
4231 status_reg = instance->instancet->read_fw_status_reg(
4233 abs_state = status_reg & MFI_STATE_MASK;
4234 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4235 if (instance->disableOnlineCtrlReset ||
4236 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
4237 /* Reset not supported, kill adapter */
4238 dev_warn(&instance->pdev->dev, "Reset not supported"
4239 ", killing adapter scsi%d.\n",
4240 instance->host->host_no);
4241 megaraid_sas_kill_hba(instance);
4242 instance->skip_heartbeat_timer_del = 1;
4247 /* Let SR-IOV VF & PF sync up if there was a HB failure */
4248 if (instance->requestorId && !reason) {
4249 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
4250 goto transition_to_ready;
4253 /* Now try to reset the chip */
4254 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
4256 if (instance->instancet->adp_reset
4257 (instance, instance->reg_set))
4259 transition_to_ready:
4260 /* Wait for FW to become ready */
4261 if (megasas_transition_to_ready(instance, 1)) {
4262 dev_warn(&instance->pdev->dev,
4263 "Failed to transition controller to ready for "
4264 "scsi%d.\n", instance->host->host_no);
4265 if (instance->requestorId && !reason)
4266 goto fail_kill_adapter;
4270 megasas_reset_reply_desc(instance);
4271 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4273 if (megasas_ioc_init_fusion(instance)) {
4274 dev_warn(&instance->pdev->dev,
4275 "megasas_ioc_init_fusion() failed! for "
4276 "scsi%d\n", instance->host->host_no);
4277 if (instance->requestorId && !reason)
4278 goto fail_kill_adapter;
4283 megasas_refire_mgmt_cmd(instance);
4285 if (megasas_get_ctrl_info(instance)) {
4286 dev_info(&instance->pdev->dev,
4287 "Failed from %s %d\n",
4288 __func__, __LINE__);
4289 megaraid_sas_kill_hba(instance);
4293 /* Reset load balance info */
4294 if (fusion->load_balance_info)
4295 memset(fusion->load_balance_info, 0,
4296 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4297 MAX_LOGICAL_DRIVES_EXT));
4299 if (!megasas_get_map_info(instance))
4300 megasas_sync_map_info(instance);
4302 megasas_setup_jbod_map(instance);
4304 shost_for_each_device(sdev, shost)
4305 megasas_set_dynamic_target_properties(sdev);
4307 /* reset stream detection array */
4308 if (instance->is_ventura) {
4309 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4310 memset(fusion->stream_detect_by_ld[j],
4311 0, sizeof(struct LD_STREAM_DETECT));
4312 fusion->stream_detect_by_ld[j]->mru_bit_map
4317 clear_bit(MEGASAS_FUSION_IN_RESET,
4318 &instance->reset_flags);
4319 instance->instancet->enable_intr(instance);
4320 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4322 /* Restart SR-IOV heartbeat */
4323 if (instance->requestorId) {
4324 if (!megasas_sriov_start_heartbeat(instance, 0))
4325 megasas_start_timer(instance,
4326 &instance->sriov_heartbeat_timer,
4327 megasas_sriov_heartbeat_handler,
4328 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4330 instance->skip_heartbeat_timer_del = 1;
4333 /* Adapter reset completed successfully */
4334 dev_warn(&instance->pdev->dev, "Reset "
4335 "successful for scsi%d.\n",
4336 instance->host->host_no);
4338 if (instance->crash_dump_drv_support &&
4339 instance->crash_dump_app_support)
4340 megasas_set_crash_dump_params(instance,
4341 MR_CRASH_BUF_TURN_ON);
4343 megasas_set_crash_dump_params(instance,
4344 MR_CRASH_BUF_TURN_OFF);
4350 /* Reset failed, kill the adapter */
4351 dev_warn(&instance->pdev->dev, "Reset failed, killing "
4352 "adapter scsi%d.\n", instance->host->host_no);
4353 megaraid_sas_kill_hba(instance);
4354 instance->skip_heartbeat_timer_del = 1;
4357 /* For VF: Restart HB timer if we didn't OCR */
4358 if (instance->requestorId) {
4359 megasas_start_timer(instance,
4360 &instance->sriov_heartbeat_timer,
4361 megasas_sriov_heartbeat_handler,
4362 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4364 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4365 instance->instancet->enable_intr(instance);
4366 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4369 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4370 mutex_unlock(&instance->reset_mutex);
4374 /* Fusion Crash dump collection work queue */
4375 void megasas_fusion_crash_dump_wq(struct work_struct *work)
4377 struct megasas_instance *instance =
4378 container_of(work, struct megasas_instance, crash_init);
4380 u8 partial_copy = 0;
4383 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4386 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4387 * to host crash buffers
4389 if (instance->drv_buf_index == 0) {
4390 /* Buffer is already allocated for old Crash dump.
4391 * Do OCR and do not wait for crash dump collection
4393 if (instance->drv_buf_alloc) {
4394 dev_info(&instance->pdev->dev, "earlier crash dump is "
4395 "not yet copied by application, ignoring this "
4396 "crash dump and initiating OCR\n");
4397 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4399 &instance->reg_set->outbound_scratch_pad);
4400 readl(&instance->reg_set->outbound_scratch_pad);
4403 megasas_alloc_host_crash_buffer(instance);
4404 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4405 "allocated: %d\n", instance->drv_buf_alloc);
4409 * Driver has allocated max buffers, which can be allocated
4410 * and FW has more crash dump data, then driver will
4413 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
4414 dev_info(&instance->pdev->dev, "Driver is done copying "
4415 "the buffer: %d\n", instance->drv_buf_alloc);
4416 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4419 memcpy(instance->crash_buf[instance->drv_buf_index],
4420 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4421 instance->drv_buf_index++;
4422 status_reg &= ~MFI_STATE_DMADONE;
4425 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4426 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4427 "of copied buffers: %d\n", instance->drv_buf_index);
4428 instance->fw_crash_buffer_size = instance->drv_buf_index;
4429 instance->fw_crash_state = AVAILABLE;
4430 instance->drv_buf_index = 0;
4431 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4432 readl(&instance->reg_set->outbound_scratch_pad);
4434 megasas_reset_fusion(instance->host, 0);
4436 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4437 readl(&instance->reg_set->outbound_scratch_pad);
4442 /* Fusion OCR work queue */
4443 void megasas_fusion_ocr_wq(struct work_struct *work)
4445 struct megasas_instance *instance =
4446 container_of(work, struct megasas_instance, work_init);
4448 megasas_reset_fusion(instance->host, 0);
4451 /* Allocate fusion context */
4453 megasas_alloc_fusion_context(struct megasas_instance *instance)
4455 struct fusion_context *fusion;
4457 instance->ctrl_context_pages = get_order(sizeof(struct fusion_context));
4458 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4459 instance->ctrl_context_pages);
4460 if (!instance->ctrl_context) {
4461 /* fall back to using vmalloc for fusion_context */
4462 instance->ctrl_context = vzalloc(sizeof(struct fusion_context));
4463 if (!instance->ctrl_context) {
4464 dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
4469 fusion = instance->ctrl_context;
4471 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4472 sizeof(struct LD_LOAD_BALANCE_INFO));
4473 fusion->load_balance_info =
4474 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4475 fusion->load_balance_info_pages);
4476 if (!fusion->load_balance_info) {
4477 fusion->load_balance_info = vzalloc(MAX_LOGICAL_DRIVES_EXT *
4478 sizeof(struct LD_LOAD_BALANCE_INFO));
4479 if (!fusion->load_balance_info)
4480 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4481 "continuing without Load Balance support\n");
4488 megasas_free_fusion_context(struct megasas_instance *instance)
4490 struct fusion_context *fusion = instance->ctrl_context;
4493 if (fusion->load_balance_info) {
4494 if (is_vmalloc_addr(fusion->load_balance_info))
4495 vfree(fusion->load_balance_info);
4497 free_pages((ulong)fusion->load_balance_info,
4498 fusion->load_balance_info_pages);
4501 if (is_vmalloc_addr(fusion))
4504 free_pages((ulong)fusion,
4505 instance->ctrl_context_pages);
4509 struct megasas_instance_template megasas_instance_template_fusion = {
4510 .enable_intr = megasas_enable_intr_fusion,
4511 .disable_intr = megasas_disable_intr_fusion,
4512 .clear_intr = megasas_clear_intr_fusion,
4513 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4514 .adp_reset = megasas_adp_reset_fusion,
4515 .check_reset = megasas_check_reset_fusion,
4516 .service_isr = megasas_isr_fusion,
4517 .tasklet = megasas_complete_cmd_dpc_fusion,
4518 .init_adapter = megasas_init_adapter_fusion,
4519 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
4520 .issue_dcmd = megasas_issue_dcmd_fusion,