2 * Linux MegaRAID driver for SAS based RAID controllers
4 * Copyright (c) 2003-2013 LSI Corporation
5 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Authors: Broadcom Inc.
26 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
29 * Send feedback to: megaraidlinux.pdl@broadcom.com
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/pci.h>
35 #include <linux/list.h>
36 #include <linux/moduleparam.h>
37 #include <linux/module.h>
38 #include <linux/spinlock.h>
39 #include <linux/interrupt.h>
40 #include <linux/delay.h>
41 #include <linux/uio.h>
42 #include <linux/slab.h>
43 #include <linux/uaccess.h>
44 #include <asm/unaligned.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>
51 #include <linux/irq_poll.h>
53 #include <scsi/scsi.h>
54 #include <scsi/scsi_cmnd.h>
55 #include <scsi/scsi_device.h>
56 #include <scsi/scsi_host.h>
57 #include <scsi/scsi_tcq.h>
58 #include <scsi/scsi_dbg.h>
59 #include "megaraid_sas_fusion.h"
60 #include "megaraid_sas.h"
63 * Number of sectors per IO command
64 * Will be set in megasas_init_mfi if user does not provide
66 static unsigned int max_sectors;
67 module_param_named(max_sectors, max_sectors, int, 0);
68 MODULE_PARM_DESC(max_sectors,
69 "Maximum number of sectors per IO command");
71 static int msix_disable;
72 module_param(msix_disable, int, S_IRUGO);
73 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
75 static unsigned int msix_vectors;
76 module_param(msix_vectors, int, S_IRUGO);
77 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
79 static int allow_vf_ioctls;
80 module_param(allow_vf_ioctls, int, S_IRUGO);
81 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
83 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
84 module_param(throttlequeuedepth, int, S_IRUGO);
85 MODULE_PARM_DESC(throttlequeuedepth,
86 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
88 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
89 module_param(resetwaittime, int, S_IRUGO);
90 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
92 int smp_affinity_enable = 1;
93 module_param(smp_affinity_enable, int, S_IRUGO);
94 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
97 module_param(rdpq_enable, int, S_IRUGO);
98 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
100 unsigned int dual_qdepth_disable;
101 module_param(dual_qdepth_disable, int, S_IRUGO);
102 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
104 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
105 module_param(scmd_timeout, int, S_IRUGO);
106 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
108 MODULE_LICENSE("GPL");
109 MODULE_VERSION(MEGASAS_VERSION);
110 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
111 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
113 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
114 static int megasas_get_pd_list(struct megasas_instance *instance);
115 static int megasas_ld_list_query(struct megasas_instance *instance,
117 static int megasas_issue_init_mfi(struct megasas_instance *instance);
118 static int megasas_register_aen(struct megasas_instance *instance,
119 u32 seq_num, u32 class_locale_word);
120 static void megasas_get_pd_info(struct megasas_instance *instance,
121 struct scsi_device *sdev);
124 * PCI ID table for all supported controllers
126 static struct pci_device_id megasas_pci_table[] = {
128 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
130 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
132 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
134 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
143 /* xscale IOP, vega */
144 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
157 /* Intruder 24 port*/
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
159 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
161 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
163 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
165 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
174 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
176 static int megasas_mgmt_majorno;
177 struct megasas_mgmt_info megasas_mgmt_info;
178 static struct fasync_struct *megasas_async_queue;
179 static DEFINE_MUTEX(megasas_async_queue_mutex);
181 static int megasas_poll_wait_aen;
182 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
183 static u32 support_poll_for_event;
185 static u32 support_device_change;
186 static bool support_nvme_encapsulation;
188 /* define lock for aen poll */
189 spinlock_t poll_aen_lock;
192 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
195 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
197 megasas_adp_reset_gen2(struct megasas_instance *instance,
198 struct megasas_register_set __iomem *reg_set);
199 static irqreturn_t megasas_isr(int irq, void *devp);
201 megasas_init_adapter_mfi(struct megasas_instance *instance);
203 megasas_build_and_issue_cmd(struct megasas_instance *instance,
204 struct scsi_cmnd *scmd);
205 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
207 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
209 void megasas_fusion_ocr_wq(struct work_struct *work);
210 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
213 megasas_set_dma_mask(struct megasas_instance *instance);
215 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
217 megasas_free_ctrl_mem(struct megasas_instance *instance);
219 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
221 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
223 megasas_init_ctrl_params(struct megasas_instance *instance);
225 u32 megasas_readl(struct megasas_instance *instance,
226 const volatile void __iomem *addr)
230 * Due to a HW errata in Aero controllers, reads to certain
231 * Fusion registers could intermittently return all zeroes.
232 * This behavior is transient in nature and subsequent reads will
233 * return valid value. As a workaround in driver, retry readl for
234 * upto three times until a non-zero value is read.
236 if (instance->adapter_type == AERO_SERIES) {
238 ret_val = readl(addr);
240 } while (ret_val == 0 && i < 3);
248 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
249 * @instance: Adapter soft state
250 * @dcmd: DCMD frame inside MFI command
251 * @dma_addr: DMA address of buffer to be passed to FW
252 * @dma_len: Length of DMA buffer to be passed to FW
255 void megasas_set_dma_settings(struct megasas_instance *instance,
256 struct megasas_dcmd_frame *dcmd,
257 dma_addr_t dma_addr, u32 dma_len)
259 if (instance->consistent_mask_64bit) {
260 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
261 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
262 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
265 dcmd->sgl.sge32[0].phys_addr =
266 cpu_to_le32(lower_32_bits(dma_addr));
267 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
268 dcmd->flags = cpu_to_le16(dcmd->flags);
273 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
275 instance->instancet->fire_cmd(instance,
276 cmd->frame_phys_addr, 0, instance->reg_set);
281 * megasas_get_cmd - Get a command from the free pool
282 * @instance: Adapter soft state
284 * Returns a free command from the pool
286 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
290 struct megasas_cmd *cmd = NULL;
292 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
294 if (!list_empty(&instance->cmd_pool)) {
295 cmd = list_entry((&instance->cmd_pool)->next,
296 struct megasas_cmd, list);
297 list_del_init(&cmd->list);
299 dev_err(&instance->pdev->dev, "Command pool empty!\n");
302 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
307 * megasas_return_cmd - Return a cmd to free command pool
308 * @instance: Adapter soft state
309 * @cmd: Command packet to be returned to free command pool
312 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
316 struct megasas_cmd_fusion *cmd_fusion;
317 struct fusion_context *fusion = instance->ctrl_context;
319 /* This flag is used only for fusion adapter.
320 * Wait for Interrupt for Polled mode DCMD
322 if (cmd->flags & DRV_DCMD_POLLED_MODE)
325 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
328 blk_tags = instance->max_scsi_cmds + cmd->index;
329 cmd_fusion = fusion->cmd_list[blk_tags];
330 megasas_return_cmd_fusion(instance, cmd_fusion);
333 cmd->frame_count = 0;
335 memset(cmd->frame, 0, instance->mfi_frame_size);
336 cmd->frame->io.context = cpu_to_le32(cmd->index);
337 if (!fusion && reset_devices)
338 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
339 list_add(&cmd->list, (&instance->cmd_pool)->next);
341 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
346 format_timestamp(uint32_t timestamp)
348 static char buffer[32];
350 if ((timestamp & 0xff000000) == 0xff000000)
351 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
354 snprintf(buffer, sizeof(buffer), "%us", timestamp);
359 format_class(int8_t class)
361 static char buffer[6];
364 case MFI_EVT_CLASS_DEBUG:
366 case MFI_EVT_CLASS_PROGRESS:
368 case MFI_EVT_CLASS_INFO:
370 case MFI_EVT_CLASS_WARNING:
372 case MFI_EVT_CLASS_CRITICAL:
374 case MFI_EVT_CLASS_FATAL:
376 case MFI_EVT_CLASS_DEAD:
379 snprintf(buffer, sizeof(buffer), "%d", class);
385 * megasas_decode_evt: Decode FW AEN event and print critical event
387 * @instance: Adapter soft state
390 megasas_decode_evt(struct megasas_instance *instance)
392 struct megasas_evt_detail *evt_detail = instance->evt_detail;
393 union megasas_evt_class_locale class_locale;
394 class_locale.word = le32_to_cpu(evt_detail->cl.word);
396 if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
397 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
398 le32_to_cpu(evt_detail->seq_num),
399 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
400 (class_locale.members.locale),
401 format_class(class_locale.members.class),
402 evt_detail->description);
406 * The following functions are defined for xscale
407 * (deviceid : 1064R, PERC5) controllers
411 * megasas_enable_intr_xscale - Enables interrupts
412 * @regs: MFI register set
415 megasas_enable_intr_xscale(struct megasas_instance *instance)
417 struct megasas_register_set __iomem *regs;
419 regs = instance->reg_set;
420 writel(0, &(regs)->outbound_intr_mask);
422 /* Dummy readl to force pci flush */
423 readl(®s->outbound_intr_mask);
427 * megasas_disable_intr_xscale -Disables interrupt
428 * @regs: MFI register set
431 megasas_disable_intr_xscale(struct megasas_instance *instance)
433 struct megasas_register_set __iomem *regs;
436 regs = instance->reg_set;
437 writel(mask, ®s->outbound_intr_mask);
438 /* Dummy readl to force pci flush */
439 readl(®s->outbound_intr_mask);
443 * megasas_read_fw_status_reg_xscale - returns the current FW status value
444 * @regs: MFI register set
447 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
449 return readl(&instance->reg_set->outbound_msg_0);
452 * megasas_clear_interrupt_xscale - Check & clear interrupt
453 * @regs: MFI register set
456 megasas_clear_intr_xscale(struct megasas_instance *instance)
460 struct megasas_register_set __iomem *regs;
461 regs = instance->reg_set;
464 * Check if it is our interrupt
466 status = readl(®s->outbound_intr_status);
468 if (status & MFI_OB_INTR_STATUS_MASK)
469 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
470 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
471 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
474 * Clear the interrupt by writing back the same value
477 writel(status, ®s->outbound_intr_status);
479 /* Dummy readl to force pci flush */
480 readl(®s->outbound_intr_status);
486 * megasas_fire_cmd_xscale - Sends command to the FW
487 * @frame_phys_addr : Physical address of cmd
488 * @frame_count : Number of frames for the command
489 * @regs : MFI register set
492 megasas_fire_cmd_xscale(struct megasas_instance *instance,
493 dma_addr_t frame_phys_addr,
495 struct megasas_register_set __iomem *regs)
499 spin_lock_irqsave(&instance->hba_lock, flags);
500 writel((frame_phys_addr >> 3)|(frame_count),
501 &(regs)->inbound_queue_port);
502 spin_unlock_irqrestore(&instance->hba_lock, flags);
506 * megasas_adp_reset_xscale - For controller reset
507 * @regs: MFI register set
510 megasas_adp_reset_xscale(struct megasas_instance *instance,
511 struct megasas_register_set __iomem *regs)
516 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
518 for (i = 0; i < 3; i++)
519 msleep(1000); /* sleep for 3 secs */
521 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
522 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
524 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
526 pci_write_config_dword(instance->pdev,
527 MFI_1068_PCSR_OFFSET, pcidata);
529 for (i = 0; i < 2; i++)
530 msleep(1000); /* need to wait 2 secs again */
533 pci_read_config_dword(instance->pdev,
534 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
535 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
536 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
537 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
539 pci_write_config_dword(instance->pdev,
540 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
547 * megasas_check_reset_xscale - For controller reset check
548 * @regs: MFI register set
551 megasas_check_reset_xscale(struct megasas_instance *instance,
552 struct megasas_register_set __iomem *regs)
554 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
555 (le32_to_cpu(*instance->consumer) ==
556 MEGASAS_ADPRESET_INPROG_SIGN))
561 static struct megasas_instance_template megasas_instance_template_xscale = {
563 .fire_cmd = megasas_fire_cmd_xscale,
564 .enable_intr = megasas_enable_intr_xscale,
565 .disable_intr = megasas_disable_intr_xscale,
566 .clear_intr = megasas_clear_intr_xscale,
567 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
568 .adp_reset = megasas_adp_reset_xscale,
569 .check_reset = megasas_check_reset_xscale,
570 .service_isr = megasas_isr,
571 .tasklet = megasas_complete_cmd_dpc,
572 .init_adapter = megasas_init_adapter_mfi,
573 .build_and_issue_cmd = megasas_build_and_issue_cmd,
574 .issue_dcmd = megasas_issue_dcmd,
578 * This is the end of set of functions & definitions specific
579 * to xscale (deviceid : 1064R, PERC5) controllers
583 * The following functions are defined for ppc (deviceid : 0x60)
588 * megasas_enable_intr_ppc - Enables interrupts
589 * @regs: MFI register set
592 megasas_enable_intr_ppc(struct megasas_instance *instance)
594 struct megasas_register_set __iomem *regs;
596 regs = instance->reg_set;
597 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
599 writel(~0x80000000, &(regs)->outbound_intr_mask);
601 /* Dummy readl to force pci flush */
602 readl(®s->outbound_intr_mask);
606 * megasas_disable_intr_ppc - Disable interrupt
607 * @regs: MFI register set
610 megasas_disable_intr_ppc(struct megasas_instance *instance)
612 struct megasas_register_set __iomem *regs;
613 u32 mask = 0xFFFFFFFF;
615 regs = instance->reg_set;
616 writel(mask, ®s->outbound_intr_mask);
617 /* Dummy readl to force pci flush */
618 readl(®s->outbound_intr_mask);
622 * megasas_read_fw_status_reg_ppc - returns the current FW status value
623 * @regs: MFI register set
626 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
628 return readl(&instance->reg_set->outbound_scratch_pad_0);
632 * megasas_clear_interrupt_ppc - Check & clear interrupt
633 * @regs: MFI register set
636 megasas_clear_intr_ppc(struct megasas_instance *instance)
638 u32 status, mfiStatus = 0;
639 struct megasas_register_set __iomem *regs;
640 regs = instance->reg_set;
643 * Check if it is our interrupt
645 status = readl(®s->outbound_intr_status);
647 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
648 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
650 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
651 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
654 * Clear the interrupt by writing back the same value
656 writel(status, ®s->outbound_doorbell_clear);
658 /* Dummy readl to force pci flush */
659 readl(®s->outbound_doorbell_clear);
665 * megasas_fire_cmd_ppc - Sends command to the FW
666 * @frame_phys_addr : Physical address of cmd
667 * @frame_count : Number of frames for the command
668 * @regs : MFI register set
671 megasas_fire_cmd_ppc(struct megasas_instance *instance,
672 dma_addr_t frame_phys_addr,
674 struct megasas_register_set __iomem *regs)
678 spin_lock_irqsave(&instance->hba_lock, flags);
679 writel((frame_phys_addr | (frame_count<<1))|1,
680 &(regs)->inbound_queue_port);
681 spin_unlock_irqrestore(&instance->hba_lock, flags);
685 * megasas_check_reset_ppc - For controller reset check
686 * @regs: MFI register set
689 megasas_check_reset_ppc(struct megasas_instance *instance,
690 struct megasas_register_set __iomem *regs)
692 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
698 static struct megasas_instance_template megasas_instance_template_ppc = {
700 .fire_cmd = megasas_fire_cmd_ppc,
701 .enable_intr = megasas_enable_intr_ppc,
702 .disable_intr = megasas_disable_intr_ppc,
703 .clear_intr = megasas_clear_intr_ppc,
704 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
705 .adp_reset = megasas_adp_reset_xscale,
706 .check_reset = megasas_check_reset_ppc,
707 .service_isr = megasas_isr,
708 .tasklet = megasas_complete_cmd_dpc,
709 .init_adapter = megasas_init_adapter_mfi,
710 .build_and_issue_cmd = megasas_build_and_issue_cmd,
711 .issue_dcmd = megasas_issue_dcmd,
715 * megasas_enable_intr_skinny - Enables interrupts
716 * @regs: MFI register set
719 megasas_enable_intr_skinny(struct megasas_instance *instance)
721 struct megasas_register_set __iomem *regs;
723 regs = instance->reg_set;
724 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
726 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
728 /* Dummy readl to force pci flush */
729 readl(®s->outbound_intr_mask);
733 * megasas_disable_intr_skinny - Disables interrupt
734 * @regs: MFI register set
737 megasas_disable_intr_skinny(struct megasas_instance *instance)
739 struct megasas_register_set __iomem *regs;
740 u32 mask = 0xFFFFFFFF;
742 regs = instance->reg_set;
743 writel(mask, ®s->outbound_intr_mask);
744 /* Dummy readl to force pci flush */
745 readl(®s->outbound_intr_mask);
749 * megasas_read_fw_status_reg_skinny - returns the current FW status value
750 * @regs: MFI register set
753 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
755 return readl(&instance->reg_set->outbound_scratch_pad_0);
759 * megasas_clear_interrupt_skinny - Check & clear interrupt
760 * @regs: MFI register set
763 megasas_clear_intr_skinny(struct megasas_instance *instance)
767 struct megasas_register_set __iomem *regs;
768 regs = instance->reg_set;
771 * Check if it is our interrupt
773 status = readl(®s->outbound_intr_status);
775 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
780 * Check if it is our interrupt
782 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
784 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
786 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
789 * Clear the interrupt by writing back the same value
791 writel(status, ®s->outbound_intr_status);
794 * dummy read to flush PCI
796 readl(®s->outbound_intr_status);
802 * megasas_fire_cmd_skinny - Sends command to the FW
803 * @frame_phys_addr : Physical address of cmd
804 * @frame_count : Number of frames for the command
805 * @regs : MFI register set
808 megasas_fire_cmd_skinny(struct megasas_instance *instance,
809 dma_addr_t frame_phys_addr,
811 struct megasas_register_set __iomem *regs)
815 spin_lock_irqsave(&instance->hba_lock, flags);
816 writel(upper_32_bits(frame_phys_addr),
817 &(regs)->inbound_high_queue_port);
818 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
819 &(regs)->inbound_low_queue_port);
820 spin_unlock_irqrestore(&instance->hba_lock, flags);
824 * megasas_check_reset_skinny - For controller reset check
825 * @regs: MFI register set
828 megasas_check_reset_skinny(struct megasas_instance *instance,
829 struct megasas_register_set __iomem *regs)
831 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
837 static struct megasas_instance_template megasas_instance_template_skinny = {
839 .fire_cmd = megasas_fire_cmd_skinny,
840 .enable_intr = megasas_enable_intr_skinny,
841 .disable_intr = megasas_disable_intr_skinny,
842 .clear_intr = megasas_clear_intr_skinny,
843 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
844 .adp_reset = megasas_adp_reset_gen2,
845 .check_reset = megasas_check_reset_skinny,
846 .service_isr = megasas_isr,
847 .tasklet = megasas_complete_cmd_dpc,
848 .init_adapter = megasas_init_adapter_mfi,
849 .build_and_issue_cmd = megasas_build_and_issue_cmd,
850 .issue_dcmd = megasas_issue_dcmd,
855 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
860 * megasas_enable_intr_gen2 - Enables interrupts
861 * @regs: MFI register set
864 megasas_enable_intr_gen2(struct megasas_instance *instance)
866 struct megasas_register_set __iomem *regs;
868 regs = instance->reg_set;
869 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
871 /* write ~0x00000005 (4 & 1) to the intr mask*/
872 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
874 /* Dummy readl to force pci flush */
875 readl(®s->outbound_intr_mask);
879 * megasas_disable_intr_gen2 - Disables interrupt
880 * @regs: MFI register set
883 megasas_disable_intr_gen2(struct megasas_instance *instance)
885 struct megasas_register_set __iomem *regs;
886 u32 mask = 0xFFFFFFFF;
888 regs = instance->reg_set;
889 writel(mask, ®s->outbound_intr_mask);
890 /* Dummy readl to force pci flush */
891 readl(®s->outbound_intr_mask);
895 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
896 * @regs: MFI register set
899 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
901 return readl(&instance->reg_set->outbound_scratch_pad_0);
905 * megasas_clear_interrupt_gen2 - Check & clear interrupt
906 * @regs: MFI register set
909 megasas_clear_intr_gen2(struct megasas_instance *instance)
913 struct megasas_register_set __iomem *regs;
914 regs = instance->reg_set;
917 * Check if it is our interrupt
919 status = readl(®s->outbound_intr_status);
921 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
922 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
924 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
925 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
929 * Clear the interrupt by writing back the same value
932 writel(status, ®s->outbound_doorbell_clear);
934 /* Dummy readl to force pci flush */
935 readl(®s->outbound_intr_status);
940 * megasas_fire_cmd_gen2 - Sends command to the FW
941 * @frame_phys_addr : Physical address of cmd
942 * @frame_count : Number of frames for the command
943 * @regs : MFI register set
946 megasas_fire_cmd_gen2(struct megasas_instance *instance,
947 dma_addr_t frame_phys_addr,
949 struct megasas_register_set __iomem *regs)
953 spin_lock_irqsave(&instance->hba_lock, flags);
954 writel((frame_phys_addr | (frame_count<<1))|1,
955 &(regs)->inbound_queue_port);
956 spin_unlock_irqrestore(&instance->hba_lock, flags);
960 * megasas_adp_reset_gen2 - For controller reset
961 * @regs: MFI register set
964 megasas_adp_reset_gen2(struct megasas_instance *instance,
965 struct megasas_register_set __iomem *reg_set)
969 u32 __iomem *seq_offset = ®_set->seq_offset;
970 u32 __iomem *hostdiag_offset = ®_set->host_diag;
972 if (instance->instancet == &megasas_instance_template_skinny) {
973 seq_offset = ®_set->fusion_seq_offset;
974 hostdiag_offset = ®_set->fusion_host_diag;
977 writel(0, seq_offset);
978 writel(4, seq_offset);
979 writel(0xb, seq_offset);
980 writel(2, seq_offset);
981 writel(7, seq_offset);
982 writel(0xd, seq_offset);
986 HostDiag = (u32)readl(hostdiag_offset);
988 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
990 HostDiag = (u32)readl(hostdiag_offset);
991 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
999 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1001 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1005 HostDiag = (u32)readl(hostdiag_offset);
1006 while (HostDiag & DIAG_RESET_ADAPTER) {
1008 HostDiag = (u32)readl(hostdiag_offset);
1009 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1012 if (retry++ >= 1000)
1020 * megasas_check_reset_gen2 - For controller reset check
1021 * @regs: MFI register set
1024 megasas_check_reset_gen2(struct megasas_instance *instance,
1025 struct megasas_register_set __iomem *regs)
1027 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1033 static struct megasas_instance_template megasas_instance_template_gen2 = {
1035 .fire_cmd = megasas_fire_cmd_gen2,
1036 .enable_intr = megasas_enable_intr_gen2,
1037 .disable_intr = megasas_disable_intr_gen2,
1038 .clear_intr = megasas_clear_intr_gen2,
1039 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1040 .adp_reset = megasas_adp_reset_gen2,
1041 .check_reset = megasas_check_reset_gen2,
1042 .service_isr = megasas_isr,
1043 .tasklet = megasas_complete_cmd_dpc,
1044 .init_adapter = megasas_init_adapter_mfi,
1045 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1046 .issue_dcmd = megasas_issue_dcmd,
1050 * This is the end of set of functions & definitions
1051 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1055 * Template added for TB (Fusion)
1057 extern struct megasas_instance_template megasas_instance_template_fusion;
1060 * megasas_issue_polled - Issues a polling command
1061 * @instance: Adapter soft state
1062 * @cmd: Command packet to be issued
1064 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1067 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1069 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1071 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1072 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1074 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1075 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1076 __func__, __LINE__);
1077 return DCMD_NOT_FIRED;
1080 instance->instancet->issue_dcmd(instance, cmd);
1082 return wait_and_poll(instance, cmd, instance->requestorId ?
1083 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1087 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1088 * @instance: Adapter soft state
1089 * @cmd: Command to be issued
1090 * @timeout: Timeout in seconds
1092 * This function waits on an event for the command to be returned from ISR.
1093 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1094 * Used to issue ioctl commands.
1097 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1098 struct megasas_cmd *cmd, int timeout)
1101 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1103 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1104 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1105 __func__, __LINE__);
1106 return DCMD_NOT_FIRED;
1109 instance->instancet->issue_dcmd(instance, cmd);
1112 ret = wait_event_timeout(instance->int_cmd_wait_q,
1113 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1115 dev_err(&instance->pdev->dev,
1116 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1117 cmd->frame->dcmd.opcode, __func__);
1118 return DCMD_TIMEOUT;
1121 wait_event(instance->int_cmd_wait_q,
1122 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1124 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1125 DCMD_SUCCESS : DCMD_FAILED;
1129 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1130 * @instance: Adapter soft state
1131 * @cmd_to_abort: Previously issued cmd to be aborted
1132 * @timeout: Timeout in seconds
1134 * MFI firmware can abort previously issued AEN comamnd (automatic event
1135 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1136 * cmd and waits for return status.
1137 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1140 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1141 struct megasas_cmd *cmd_to_abort, int timeout)
1143 struct megasas_cmd *cmd;
1144 struct megasas_abort_frame *abort_fr;
1148 cmd = megasas_get_cmd(instance);
1153 abort_fr = &cmd->frame->abort;
1156 * Prepare and issue the abort frame
1158 abort_fr->cmd = MFI_CMD_ABORT;
1159 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1160 abort_fr->flags = cpu_to_le16(0);
1161 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1162 abort_fr->abort_mfi_phys_addr_lo =
1163 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1164 abort_fr->abort_mfi_phys_addr_hi =
1165 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1168 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1170 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1171 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1172 __func__, __LINE__);
1173 return DCMD_NOT_FIRED;
1176 instance->instancet->issue_dcmd(instance, cmd);
1179 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1180 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1182 opcode = cmd_to_abort->frame->dcmd.opcode;
1183 dev_err(&instance->pdev->dev,
1184 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1186 return DCMD_TIMEOUT;
1189 wait_event(instance->abort_cmd_wait_q,
1190 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1194 megasas_return_cmd(instance, cmd);
1195 return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1196 DCMD_SUCCESS : DCMD_FAILED;
1200 * megasas_make_sgl32 - Prepares 32-bit SGL
1201 * @instance: Adapter soft state
1202 * @scp: SCSI command from the mid-layer
1203 * @mfi_sgl: SGL to be filled in
1205 * If successful, this function returns the number of SG elements. Otherwise,
1209 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1210 union megasas_sgl *mfi_sgl)
1214 struct scatterlist *os_sgl;
1216 sge_count = scsi_dma_map(scp);
1217 BUG_ON(sge_count < 0);
1220 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1221 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1222 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1229 * megasas_make_sgl64 - Prepares 64-bit SGL
1230 * @instance: Adapter soft state
1231 * @scp: SCSI command from the mid-layer
1232 * @mfi_sgl: SGL to be filled in
1234 * If successful, this function returns the number of SG elements. Otherwise,
1238 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1239 union megasas_sgl *mfi_sgl)
1243 struct scatterlist *os_sgl;
1245 sge_count = scsi_dma_map(scp);
1246 BUG_ON(sge_count < 0);
1249 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1250 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1251 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1258 * megasas_make_sgl_skinny - Prepares IEEE SGL
1259 * @instance: Adapter soft state
1260 * @scp: SCSI command from the mid-layer
1261 * @mfi_sgl: SGL to be filled in
1263 * If successful, this function returns the number of SG elements. Otherwise,
1267 megasas_make_sgl_skinny(struct megasas_instance *instance,
1268 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1272 struct scatterlist *os_sgl;
1274 sge_count = scsi_dma_map(scp);
1277 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1278 mfi_sgl->sge_skinny[i].length =
1279 cpu_to_le32(sg_dma_len(os_sgl));
1280 mfi_sgl->sge_skinny[i].phys_addr =
1281 cpu_to_le64(sg_dma_address(os_sgl));
1282 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1289 * megasas_get_frame_count - Computes the number of frames
1290 * @frame_type : type of frame- io or pthru frame
1291 * @sge_count : number of sg elements
1293 * Returns the number of frames required for numnber of sge's (sge_count)
1296 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1297 u8 sge_count, u8 frame_type)
1302 u32 frame_count = 0;
1304 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1305 sizeof(struct megasas_sge32);
1307 if (instance->flag_ieee) {
1308 sge_sz = sizeof(struct megasas_sge_skinny);
1312 * Main frame can contain 2 SGEs for 64-bit SGLs and
1313 * 3 SGEs for 32-bit SGLs for ldio &
1314 * 1 SGEs for 64-bit SGLs and
1315 * 2 SGEs for 32-bit SGLs for pthru frame
1317 if (unlikely(frame_type == PTHRU_FRAME)) {
1318 if (instance->flag_ieee == 1) {
1319 num_cnt = sge_count - 1;
1320 } else if (IS_DMA64)
1321 num_cnt = sge_count - 1;
1323 num_cnt = sge_count - 2;
1325 if (instance->flag_ieee == 1) {
1326 num_cnt = sge_count - 1;
1327 } else if (IS_DMA64)
1328 num_cnt = sge_count - 2;
1330 num_cnt = sge_count - 3;
1334 sge_bytes = sge_sz * num_cnt;
1336 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1337 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1342 if (frame_count > 7)
1348 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1349 * @instance: Adapter soft state
1350 * @scp: SCSI command
1351 * @cmd: Command to be prepared in
1353 * This function prepares CDB commands. These are typcially pass-through
1354 * commands to the devices.
1357 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358 struct megasas_cmd *cmd)
1363 struct megasas_pthru_frame *pthru;
1365 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1366 device_id = MEGASAS_DEV_INDEX(scp);
1367 pthru = (struct megasas_pthru_frame *)cmd->frame;
1369 if (scp->sc_data_direction == DMA_TO_DEVICE)
1370 flags = MFI_FRAME_DIR_WRITE;
1371 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1372 flags = MFI_FRAME_DIR_READ;
1373 else if (scp->sc_data_direction == DMA_NONE)
1374 flags = MFI_FRAME_DIR_NONE;
1376 if (instance->flag_ieee == 1) {
1377 flags |= MFI_FRAME_IEEE;
1381 * Prepare the DCDB frame
1383 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1384 pthru->cmd_status = 0x0;
1385 pthru->scsi_status = 0x0;
1386 pthru->target_id = device_id;
1387 pthru->lun = scp->device->lun;
1388 pthru->cdb_len = scp->cmd_len;
1391 pthru->flags = cpu_to_le16(flags);
1392 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1394 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1397 * If the command is for the tape device, set the
1398 * pthru timeout to the os layer timeout value.
1400 if (scp->device->type == TYPE_TAPE) {
1401 if ((scp->request->timeout / HZ) > 0xFFFF)
1402 pthru->timeout = cpu_to_le16(0xFFFF);
1404 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1410 if (instance->flag_ieee == 1) {
1411 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1412 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1414 } else if (IS_DMA64) {
1415 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1416 pthru->sge_count = megasas_make_sgl64(instance, scp,
1419 pthru->sge_count = megasas_make_sgl32(instance, scp,
1422 if (pthru->sge_count > instance->max_num_sge) {
1423 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1429 * Sense info specific
1431 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1432 pthru->sense_buf_phys_addr_hi =
1433 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1434 pthru->sense_buf_phys_addr_lo =
1435 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1438 * Compute the total number of frames this command consumes. FW uses
1439 * this number to pull sufficient number of frames from host memory.
1441 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1444 return cmd->frame_count;
1448 * megasas_build_ldio - Prepares IOs to logical devices
1449 * @instance: Adapter soft state
1450 * @scp: SCSI command
1451 * @cmd: Command to be prepared
1453 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1456 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1457 struct megasas_cmd *cmd)
1460 u8 sc = scp->cmnd[0];
1462 struct megasas_io_frame *ldio;
1464 device_id = MEGASAS_DEV_INDEX(scp);
1465 ldio = (struct megasas_io_frame *)cmd->frame;
1467 if (scp->sc_data_direction == DMA_TO_DEVICE)
1468 flags = MFI_FRAME_DIR_WRITE;
1469 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1470 flags = MFI_FRAME_DIR_READ;
1472 if (instance->flag_ieee == 1) {
1473 flags |= MFI_FRAME_IEEE;
1477 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1479 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1480 ldio->cmd_status = 0x0;
1481 ldio->scsi_status = 0x0;
1482 ldio->target_id = device_id;
1484 ldio->reserved_0 = 0;
1486 ldio->flags = cpu_to_le16(flags);
1487 ldio->start_lba_hi = 0;
1488 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1491 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1493 if (scp->cmd_len == 6) {
1494 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1495 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1496 ((u32) scp->cmnd[2] << 8) |
1497 (u32) scp->cmnd[3]);
1499 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1503 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1505 else if (scp->cmd_len == 10) {
1506 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1507 ((u32) scp->cmnd[7] << 8));
1508 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1509 ((u32) scp->cmnd[3] << 16) |
1510 ((u32) scp->cmnd[4] << 8) |
1511 (u32) scp->cmnd[5]);
1515 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1517 else if (scp->cmd_len == 12) {
1518 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1519 ((u32) scp->cmnd[7] << 16) |
1520 ((u32) scp->cmnd[8] << 8) |
1521 (u32) scp->cmnd[9]);
1523 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1524 ((u32) scp->cmnd[3] << 16) |
1525 ((u32) scp->cmnd[4] << 8) |
1526 (u32) scp->cmnd[5]);
1530 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1532 else if (scp->cmd_len == 16) {
1533 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1534 ((u32) scp->cmnd[11] << 16) |
1535 ((u32) scp->cmnd[12] << 8) |
1536 (u32) scp->cmnd[13]);
1538 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1539 ((u32) scp->cmnd[7] << 16) |
1540 ((u32) scp->cmnd[8] << 8) |
1541 (u32) scp->cmnd[9]);
1543 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1544 ((u32) scp->cmnd[3] << 16) |
1545 ((u32) scp->cmnd[4] << 8) |
1546 (u32) scp->cmnd[5]);
1553 if (instance->flag_ieee) {
1554 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1555 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1557 } else if (IS_DMA64) {
1558 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1559 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1561 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1563 if (ldio->sge_count > instance->max_num_sge) {
1564 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1570 * Sense info specific
1572 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1573 ldio->sense_buf_phys_addr_hi = 0;
1574 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1577 * Compute the total number of frames this command consumes. FW uses
1578 * this number to pull sufficient number of frames from host memory.
1580 cmd->frame_count = megasas_get_frame_count(instance,
1581 ldio->sge_count, IO_FRAME);
1583 return cmd->frame_count;
1587 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1588 * and whether it's RW or non RW
1589 * @scmd: SCSI command
1592 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1596 switch (cmd->cmnd[0]) {
1605 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1606 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1609 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1610 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1616 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1618 * @instance: Adapter soft state
1621 megasas_dump_pending_frames(struct megasas_instance *instance)
1623 struct megasas_cmd *cmd;
1625 union megasas_sgl *mfi_sgl;
1626 struct megasas_io_frame *ldio;
1627 struct megasas_pthru_frame *pthru;
1629 u16 max_cmd = instance->max_fw_cmds;
1631 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1632 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1634 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1636 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1638 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1639 for (i = 0; i < max_cmd; i++) {
1640 cmd = instance->cmd_list[i];
1643 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1644 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1645 ldio = (struct megasas_io_frame *)cmd->frame;
1646 mfi_sgl = &ldio->sgl;
1647 sgcount = ldio->sge_count;
1648 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1649 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1650 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1651 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1652 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1654 pthru = (struct megasas_pthru_frame *) cmd->frame;
1655 mfi_sgl = &pthru->sgl;
1656 sgcount = pthru->sge_count;
1657 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1658 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1659 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1660 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1661 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1663 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1664 for (n = 0; n < sgcount; n++) {
1666 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1667 le32_to_cpu(mfi_sgl->sge64[n].length),
1668 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1670 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1671 le32_to_cpu(mfi_sgl->sge32[n].length),
1672 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1676 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1677 for (i = 0; i < max_cmd; i++) {
1679 cmd = instance->cmd_list[i];
1681 if (cmd->sync_cmd == 1)
1682 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1684 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1688 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1689 struct scsi_cmnd *scmd)
1691 struct megasas_cmd *cmd;
1694 cmd = megasas_get_cmd(instance);
1696 return SCSI_MLQUEUE_HOST_BUSY;
1699 * Logical drive command
1701 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1702 frame_count = megasas_build_ldio(instance, scmd, cmd);
1704 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1707 goto out_return_cmd;
1710 scmd->SCp.ptr = (char *)cmd;
1713 * Issue the command to the FW
1715 atomic_inc(&instance->fw_outstanding);
1717 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1718 cmd->frame_count-1, instance->reg_set);
1722 megasas_return_cmd(instance, cmd);
1723 return SCSI_MLQUEUE_HOST_BUSY;
1728 * megasas_queue_command - Queue entry point
1729 * @scmd: SCSI command to be queued
1730 * @done: Callback entry point
1733 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1735 struct megasas_instance *instance;
1736 struct MR_PRIV_DEVICE *mr_device_priv_data;
1738 instance = (struct megasas_instance *)
1739 scmd->device->host->hostdata;
1741 if (instance->unload == 1) {
1742 scmd->result = DID_NO_CONNECT << 16;
1743 scmd->scsi_done(scmd);
1747 if (instance->issuepend_done == 0)
1748 return SCSI_MLQUEUE_HOST_BUSY;
1751 /* Check for an mpio path and adjust behavior */
1752 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1753 if (megasas_check_mpio_paths(instance, scmd) ==
1754 (DID_REQUEUE << 16)) {
1755 return SCSI_MLQUEUE_HOST_BUSY;
1757 scmd->result = DID_NO_CONNECT << 16;
1758 scmd->scsi_done(scmd);
1763 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1764 scmd->result = DID_NO_CONNECT << 16;
1765 scmd->scsi_done(scmd);
1769 mr_device_priv_data = scmd->device->hostdata;
1770 if (!mr_device_priv_data) {
1771 scmd->result = DID_NO_CONNECT << 16;
1772 scmd->scsi_done(scmd);
1776 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1777 return SCSI_MLQUEUE_HOST_BUSY;
1779 if (mr_device_priv_data->tm_busy)
1780 return SCSI_MLQUEUE_DEVICE_BUSY;
1785 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1786 (scmd->device->id >= instance->fw_supported_vd_count ||
1787 scmd->device->lun)) {
1788 scmd->result = DID_BAD_TARGET << 16;
1792 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1793 MEGASAS_IS_LOGICAL(scmd->device) &&
1794 (!instance->fw_sync_cache_support)) {
1795 scmd->result = DID_OK << 16;
1799 return instance->instancet->build_and_issue_cmd(instance, scmd);
1802 scmd->scsi_done(scmd);
1806 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1810 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1812 if ((megasas_mgmt_info.instance[i]) &&
1813 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1814 return megasas_mgmt_info.instance[i];
1821 * megasas_set_dynamic_target_properties -
1822 * Device property set by driver may not be static and it is required to be
1826 * set dma alignment (only for eedp protection enable vd).
1828 * @sdev: OS provided scsi device
1832 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1833 bool is_target_prop)
1835 u16 pd_index = 0, ld;
1837 struct megasas_instance *instance;
1838 struct fusion_context *fusion;
1839 struct MR_PRIV_DEVICE *mr_device_priv_data;
1840 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1841 struct MR_LD_RAID *raid;
1842 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1844 instance = megasas_lookup_instance(sdev->host->host_no);
1845 fusion = instance->ctrl_context;
1846 mr_device_priv_data = sdev->hostdata;
1848 if (!fusion || !mr_device_priv_data)
1851 if (MEGASAS_IS_LOGICAL(sdev)) {
1852 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1854 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1855 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1856 if (ld >= instance->fw_supported_vd_count)
1858 raid = MR_LdRaidGet(ld, local_map_ptr);
1860 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1861 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1863 mr_device_priv_data->is_tm_capable =
1864 raid->capability.tmCapable;
1865 } else if (instance->use_seqnum_jbod_fp) {
1866 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1868 pd_sync = (void *)fusion->pd_seq_sync
1869 [(instance->pd_seq_map_id - 1) & 1];
1870 mr_device_priv_data->is_tm_capable =
1871 pd_sync->seq[pd_index].capability.tmCapable;
1874 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1876 * If FW provides a target reset timeout value, driver will use
1877 * it. If not set, fallback to default values.
1879 mr_device_priv_data->target_reset_tmo =
1880 min_t(u8, instance->max_reset_tmo,
1881 instance->tgt_prop->reset_tmo);
1882 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1884 mr_device_priv_data->target_reset_tmo =
1885 MEGASAS_DEFAULT_TM_TIMEOUT;
1886 mr_device_priv_data->task_abort_tmo =
1887 MEGASAS_DEFAULT_TM_TIMEOUT;
1892 * megasas_set_nvme_device_properties -
1894 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1895 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1897 * MR firmware provides value in KB. Caller of this function converts
1900 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1901 * MR firmware provides value 128 as (32 * 4K) = 128K.
1903 * @sdev: scsi device
1904 * @max_io_size: maximum io transfer size
1908 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1910 struct megasas_instance *instance;
1911 u32 mr_nvme_pg_size;
1913 instance = (struct megasas_instance *)sdev->host->hostdata;
1914 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1915 MR_DEFAULT_NVME_PAGE_SIZE);
1917 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1919 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1920 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1925 * megasas_set_static_target_properties -
1926 * Device property set by driver are static and it is not required to be
1927 * updated after OCR.
1930 * set device queue depth
1931 * set nvme device properties. see - megasas_set_nvme_device_properties
1933 * @sdev: scsi device
1934 * @is_target_prop true, if fw provided target properties.
1936 static void megasas_set_static_target_properties(struct scsi_device *sdev,
1937 bool is_target_prop)
1940 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1941 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
1943 struct megasas_instance *instance;
1944 struct MR_PRIV_DEVICE *mr_device_priv_data;
1946 instance = megasas_lookup_instance(sdev->host->host_no);
1947 mr_device_priv_data = sdev->hostdata;
1948 interface_type = mr_device_priv_data->interface_type;
1951 * The RAID firmware may require extended timeouts.
1953 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
1955 switch (interface_type) {
1957 device_qd = MEGASAS_SAS_QD;
1960 device_qd = MEGASAS_SATA_QD;
1963 device_qd = MEGASAS_NVME_QD;
1967 if (is_target_prop) {
1968 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1969 if (tgt_device_qd &&
1970 (tgt_device_qd <= instance->host->can_queue))
1971 device_qd = tgt_device_qd;
1973 /* max_io_size_kb will be set to non zero for
1974 * nvme based vd and syspd.
1976 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1979 if (instance->nvme_page_size && max_io_size_kb)
1980 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
1982 scsi_change_queue_depth(sdev, device_qd);
1987 static int megasas_slave_configure(struct scsi_device *sdev)
1990 struct megasas_instance *instance;
1991 int ret_target_prop = DCMD_FAILED;
1992 bool is_target_prop = false;
1994 instance = megasas_lookup_instance(sdev->host->host_no);
1995 if (instance->pd_list_not_supported) {
1996 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
1997 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1999 if (instance->pd_list[pd_index].driveState !=
2005 mutex_lock(&instance->reset_mutex);
2006 /* Send DCMD to Firmware and cache the information */
2007 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2008 megasas_get_pd_info(instance, sdev);
2010 /* Some ventura firmware may not have instance->nvme_page_size set.
2011 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2013 if ((instance->tgt_prop) && (instance->nvme_page_size))
2014 ret_target_prop = megasas_get_target_prop(instance, sdev);
2016 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2017 megasas_set_static_target_properties(sdev, is_target_prop);
2019 /* This sdev property may change post OCR */
2020 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2022 mutex_unlock(&instance->reset_mutex);
2027 static int megasas_slave_alloc(struct scsi_device *sdev)
2030 struct megasas_instance *instance ;
2031 struct MR_PRIV_DEVICE *mr_device_priv_data;
2033 instance = megasas_lookup_instance(sdev->host->host_no);
2034 if (!MEGASAS_IS_LOGICAL(sdev)) {
2036 * Open the OS scan to the SYSTEM PD
2039 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2041 if ((instance->pd_list_not_supported ||
2042 instance->pd_list[pd_index].driveState ==
2043 MR_PD_STATE_SYSTEM)) {
2050 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2052 if (!mr_device_priv_data)
2054 sdev->hostdata = mr_device_priv_data;
2056 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2057 instance->r1_ldio_hint_default);
2061 static void megasas_slave_destroy(struct scsi_device *sdev)
2063 kfree(sdev->hostdata);
2064 sdev->hostdata = NULL;
2068 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2070 * @instance: Adapter soft state
2073 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2076 struct megasas_cmd *cmd_mfi;
2077 struct megasas_cmd_fusion *cmd_fusion;
2078 struct fusion_context *fusion = instance->ctrl_context;
2080 /* Find all outstanding ioctls */
2082 for (i = 0; i < instance->max_fw_cmds; i++) {
2083 cmd_fusion = fusion->cmd_list[i];
2084 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2085 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2086 if (cmd_mfi->sync_cmd &&
2087 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2088 cmd_mfi->frame->hdr.cmd_status =
2089 MFI_STAT_WRONG_STATE;
2090 megasas_complete_cmd(instance,
2096 for (i = 0; i < instance->max_fw_cmds; i++) {
2097 cmd_mfi = instance->cmd_list[i];
2098 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2100 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2106 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2108 /* Set critical error to block I/O & ioctls in case caller didn't */
2109 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2110 /* Wait 1 second to ensure IO or ioctls in build have posted */
2112 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2113 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2114 (instance->adapter_type != MFI_SERIES)) {
2115 if (!instance->requestorId) {
2116 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2118 readl(&instance->reg_set->doorbell);
2120 if (instance->requestorId && instance->peerIsPresent)
2121 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2123 writel(MFI_STOP_ADP,
2124 &instance->reg_set->inbound_doorbell);
2126 /* Complete outstanding ioctls when adapter is killed */
2127 megasas_complete_outstanding_ioctls(instance);
2131 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2132 * restored to max value
2133 * @instance: Adapter soft state
2137 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2139 unsigned long flags;
2141 if (instance->flag & MEGASAS_FW_BUSY
2142 && time_after(jiffies, instance->last_time + 5 * HZ)
2143 && atomic_read(&instance->fw_outstanding) <
2144 instance->throttlequeuedepth + 1) {
2146 spin_lock_irqsave(instance->host->host_lock, flags);
2147 instance->flag &= ~MEGASAS_FW_BUSY;
2149 instance->host->can_queue = instance->cur_can_queue;
2150 spin_unlock_irqrestore(instance->host->host_lock, flags);
2155 * megasas_complete_cmd_dpc - Returns FW's controller structure
2156 * @instance_addr: Address of adapter soft state
2158 * Tasklet to complete cmds
2160 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2165 struct megasas_cmd *cmd;
2166 struct megasas_instance *instance =
2167 (struct megasas_instance *)instance_addr;
2168 unsigned long flags;
2170 /* If we have already declared adapter dead, donot complete cmds */
2171 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2174 spin_lock_irqsave(&instance->completion_lock, flags);
2176 producer = le32_to_cpu(*instance->producer);
2177 consumer = le32_to_cpu(*instance->consumer);
2179 while (consumer != producer) {
2180 context = le32_to_cpu(instance->reply_queue[consumer]);
2181 if (context >= instance->max_fw_cmds) {
2182 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2187 cmd = instance->cmd_list[context];
2189 megasas_complete_cmd(instance, cmd, DID_OK);
2192 if (consumer == (instance->max_fw_cmds + 1)) {
2197 *instance->consumer = cpu_to_le32(producer);
2199 spin_unlock_irqrestore(&instance->completion_lock, flags);
2202 * Check if we can restore can_queue
2204 megasas_check_and_restore_queue_depth(instance);
2207 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2210 * megasas_start_timer - Initializes sriov heartbeat timer object
2211 * @instance: Adapter soft state
2214 void megasas_start_timer(struct megasas_instance *instance)
2216 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2218 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2219 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2224 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2227 process_fw_state_change_wq(struct work_struct *work);
2229 void megasas_do_ocr(struct megasas_instance *instance)
2231 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2232 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2233 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2234 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2236 instance->instancet->disable_intr(instance);
2237 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2238 instance->issuepend_done = 0;
2240 atomic_set(&instance->fw_outstanding, 0);
2241 megasas_internal_reset_defer_cmds(instance);
2242 process_fw_state_change_wq(&instance->work_init);
2245 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2248 struct megasas_cmd *cmd;
2249 struct megasas_dcmd_frame *dcmd;
2250 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2251 dma_addr_t new_affiliation_111_h;
2255 cmd = megasas_get_cmd(instance);
2258 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2259 "Failed to get cmd for scsi%d\n",
2260 instance->host->host_no);
2264 dcmd = &cmd->frame->dcmd;
2266 if (!instance->vf_affiliation_111) {
2267 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2268 "affiliation for scsi%d\n", instance->host->host_no);
2269 megasas_return_cmd(instance, cmd);
2274 memset(instance->vf_affiliation_111, 0,
2275 sizeof(struct MR_LD_VF_AFFILIATION_111));
2277 new_affiliation_111 =
2278 dma_alloc_coherent(&instance->pdev->dev,
2279 sizeof(struct MR_LD_VF_AFFILIATION_111),
2280 &new_affiliation_111_h, GFP_KERNEL);
2281 if (!new_affiliation_111) {
2282 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2283 "memory for new affiliation for scsi%d\n",
2284 instance->host->host_no);
2285 megasas_return_cmd(instance, cmd);
2290 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2292 dcmd->cmd = MFI_CMD_DCMD;
2293 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2294 dcmd->sge_count = 1;
2295 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2298 dcmd->data_xfer_len =
2299 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2300 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2303 dcmd->sgl.sge32[0].phys_addr =
2304 cpu_to_le32(instance->vf_affiliation_111_h);
2306 dcmd->sgl.sge32[0].phys_addr =
2307 cpu_to_le32(new_affiliation_111_h);
2309 dcmd->sgl.sge32[0].length = cpu_to_le32(
2310 sizeof(struct MR_LD_VF_AFFILIATION_111));
2312 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2313 "scsi%d\n", instance->host->host_no);
2315 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2316 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2317 " failed with status 0x%x for scsi%d\n",
2318 dcmd->cmd_status, instance->host->host_no);
2319 retval = 1; /* Do a scan if we couldn't get affiliation */
2324 thisVf = new_affiliation_111->thisVf;
2325 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2326 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2327 new_affiliation_111->map[ld].policy[thisVf]) {
2328 dev_warn(&instance->pdev->dev, "SR-IOV: "
2329 "Got new LD/VF affiliation for scsi%d\n",
2330 instance->host->host_no);
2331 memcpy(instance->vf_affiliation_111,
2332 new_affiliation_111,
2333 sizeof(struct MR_LD_VF_AFFILIATION_111));
2339 if (new_affiliation_111) {
2340 dma_free_coherent(&instance->pdev->dev,
2341 sizeof(struct MR_LD_VF_AFFILIATION_111),
2342 new_affiliation_111,
2343 new_affiliation_111_h);
2346 megasas_return_cmd(instance, cmd);
2351 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2354 struct megasas_cmd *cmd;
2355 struct megasas_dcmd_frame *dcmd;
2356 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2357 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2358 dma_addr_t new_affiliation_h;
2359 int i, j, retval = 0, found = 0, doscan = 0;
2362 cmd = megasas_get_cmd(instance);
2365 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2366 "Failed to get cmd for scsi%d\n",
2367 instance->host->host_no);
2371 dcmd = &cmd->frame->dcmd;
2373 if (!instance->vf_affiliation) {
2374 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2375 "affiliation for scsi%d\n", instance->host->host_no);
2376 megasas_return_cmd(instance, cmd);
2381 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2382 sizeof(struct MR_LD_VF_AFFILIATION));
2385 dma_alloc_coherent(&instance->pdev->dev,
2386 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2387 &new_affiliation_h, GFP_KERNEL);
2388 if (!new_affiliation) {
2389 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2390 "memory for new affiliation for scsi%d\n",
2391 instance->host->host_no);
2392 megasas_return_cmd(instance, cmd);
2397 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2399 dcmd->cmd = MFI_CMD_DCMD;
2400 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2401 dcmd->sge_count = 1;
2402 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2405 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2406 sizeof(struct MR_LD_VF_AFFILIATION));
2407 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2410 dcmd->sgl.sge32[0].phys_addr =
2411 cpu_to_le32(instance->vf_affiliation_h);
2413 dcmd->sgl.sge32[0].phys_addr =
2414 cpu_to_le32(new_affiliation_h);
2416 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2417 sizeof(struct MR_LD_VF_AFFILIATION));
2419 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2420 "scsi%d\n", instance->host->host_no);
2423 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2424 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2425 " failed with status 0x%x for scsi%d\n",
2426 dcmd->cmd_status, instance->host->host_no);
2427 retval = 1; /* Do a scan if we couldn't get affiliation */
2432 if (!new_affiliation->ldCount) {
2433 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2434 "affiliation for passive path for scsi%d\n",
2435 instance->host->host_no);
2439 newmap = new_affiliation->map;
2440 savedmap = instance->vf_affiliation->map;
2441 thisVf = new_affiliation->thisVf;
2442 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2444 for (j = 0; j < instance->vf_affiliation->ldCount;
2446 if (newmap->ref.targetId ==
2447 savedmap->ref.targetId) {
2449 if (newmap->policy[thisVf] !=
2450 savedmap->policy[thisVf]) {
2455 savedmap = (struct MR_LD_VF_MAP *)
2456 ((unsigned char *)savedmap +
2459 if (!found && newmap->policy[thisVf] !=
2460 MR_LD_ACCESS_HIDDEN) {
2464 newmap = (struct MR_LD_VF_MAP *)
2465 ((unsigned char *)newmap + newmap->size);
2468 newmap = new_affiliation->map;
2469 savedmap = instance->vf_affiliation->map;
2471 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2473 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2474 if (savedmap->ref.targetId ==
2475 newmap->ref.targetId) {
2477 if (savedmap->policy[thisVf] !=
2478 newmap->policy[thisVf]) {
2483 newmap = (struct MR_LD_VF_MAP *)
2484 ((unsigned char *)newmap +
2487 if (!found && savedmap->policy[thisVf] !=
2488 MR_LD_ACCESS_HIDDEN) {
2492 savedmap = (struct MR_LD_VF_MAP *)
2493 ((unsigned char *)savedmap +
2499 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2500 "affiliation for scsi%d\n", instance->host->host_no);
2501 memcpy(instance->vf_affiliation, new_affiliation,
2502 new_affiliation->size);
2506 if (new_affiliation)
2507 dma_free_coherent(&instance->pdev->dev,
2508 (MAX_LOGICAL_DRIVES + 1) *
2509 sizeof(struct MR_LD_VF_AFFILIATION),
2510 new_affiliation, new_affiliation_h);
2511 megasas_return_cmd(instance, cmd);
2516 /* This function will get the current SR-IOV LD/VF affiliation */
2517 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2522 if (instance->PlasmaFW111)
2523 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2525 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2529 /* This function will tell FW to start the SR-IOV heartbeat */
2530 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2533 struct megasas_cmd *cmd;
2534 struct megasas_dcmd_frame *dcmd;
2537 cmd = megasas_get_cmd(instance);
2540 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2541 "Failed to get cmd for scsi%d\n",
2542 instance->host->host_no);
2546 dcmd = &cmd->frame->dcmd;
2549 instance->hb_host_mem =
2550 dma_alloc_coherent(&instance->pdev->dev,
2551 sizeof(struct MR_CTRL_HB_HOST_MEM),
2552 &instance->hb_host_mem_h,
2554 if (!instance->hb_host_mem) {
2555 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2556 " memory for heartbeat host memory for scsi%d\n",
2557 instance->host->host_no);
2563 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2565 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2566 dcmd->cmd = MFI_CMD_DCMD;
2567 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2568 dcmd->sge_count = 1;
2569 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2572 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2573 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2575 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2576 sizeof(struct MR_CTRL_HB_HOST_MEM));
2578 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2579 instance->host->host_no);
2581 if ((instance->adapter_type != MFI_SERIES) &&
2582 !instance->mask_interrupts)
2583 retval = megasas_issue_blocked_cmd(instance, cmd,
2584 MEGASAS_ROUTINE_WAIT_TIME_VF);
2586 retval = megasas_issue_polled(instance, cmd);
2589 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2590 "_MEM_ALLOC DCMD %s for scsi%d\n",
2591 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2592 "timed out" : "failed", instance->host->host_no);
2597 megasas_return_cmd(instance, cmd);
2602 /* Handler for SR-IOV heartbeat */
2603 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2605 struct megasas_instance *instance =
2606 from_timer(instance, t, sriov_heartbeat_timer);
2608 if (instance->hb_host_mem->HB.fwCounter !=
2609 instance->hb_host_mem->HB.driverCounter) {
2610 instance->hb_host_mem->HB.driverCounter =
2611 instance->hb_host_mem->HB.fwCounter;
2612 mod_timer(&instance->sriov_heartbeat_timer,
2613 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2615 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2616 "completed for scsi%d\n", instance->host->host_no);
2617 schedule_work(&instance->work_init);
2622 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2623 * @instance: Adapter soft state
2625 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2626 * complete all its outstanding commands. Returns error if one or more IOs
2627 * are pending after this time period. It also marks the controller dead.
2629 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2631 int i, sl, outstanding;
2633 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2634 unsigned long flags;
2635 struct list_head clist_local;
2636 struct megasas_cmd *reset_cmd;
2639 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2640 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2641 __func__, __LINE__);
2645 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2647 INIT_LIST_HEAD(&clist_local);
2648 spin_lock_irqsave(&instance->hba_lock, flags);
2649 list_splice_init(&instance->internal_reset_pending_q,
2651 spin_unlock_irqrestore(&instance->hba_lock, flags);
2653 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2654 for (i = 0; i < wait_time; i++) {
2656 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2660 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2661 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2662 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2667 while (!list_empty(&clist_local)) {
2668 reset_cmd = list_entry((&clist_local)->next,
2669 struct megasas_cmd, list);
2670 list_del_init(&reset_cmd->list);
2671 if (reset_cmd->scmd) {
2672 reset_cmd->scmd->result = DID_REQUEUE << 16;
2673 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2674 reset_index, reset_cmd,
2675 reset_cmd->scmd->cmnd[0]);
2677 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2678 megasas_return_cmd(instance, reset_cmd);
2679 } else if (reset_cmd->sync_cmd) {
2680 dev_notice(&instance->pdev->dev, "%p synch cmds"
2684 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2685 instance->instancet->fire_cmd(instance,
2686 reset_cmd->frame_phys_addr,
2687 0, instance->reg_set);
2689 dev_notice(&instance->pdev->dev, "%p unexpected"
2699 for (i = 0; i < resetwaittime; i++) {
2700 outstanding = atomic_read(&instance->fw_outstanding);
2705 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2706 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2707 "commands to complete\n",i,outstanding);
2709 * Call cmd completion routine. Cmd to be
2710 * be completed directly without depending on isr.
2712 megasas_complete_cmd_dpc((unsigned long)instance);
2719 outstanding = atomic_read(&instance->fw_outstanding);
2720 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2722 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2723 goto no_outstanding;
2725 if (instance->disableOnlineCtrlReset)
2726 goto kill_hba_and_failed;
2728 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2729 dev_info(&instance->pdev->dev,
2730 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2731 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2733 goto kill_hba_and_failed;
2734 megasas_do_ocr(instance);
2736 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2737 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2738 __func__, __LINE__);
2741 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2742 __func__, __LINE__);
2744 for (sl = 0; sl < 10; sl++)
2747 outstanding = atomic_read(&instance->fw_outstanding);
2749 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2750 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2751 goto no_outstanding;
2758 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2759 __func__, __LINE__);
2762 kill_hba_and_failed:
2764 /* Reset not supported, kill adapter */
2765 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2766 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2767 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2768 atomic_read(&instance->fw_outstanding));
2769 megasas_dump_pending_frames(instance);
2770 megaraid_sas_kill_hba(instance);
2776 * megasas_generic_reset - Generic reset routine
2777 * @scmd: Mid-layer SCSI command
2779 * This routine implements a generic reset handler for device, bus and host
2780 * reset requests. Device, bus and host specific reset handlers can use this
2781 * function after they do their specific tasks.
2783 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2786 struct megasas_instance *instance;
2788 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2790 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2791 scmd->cmnd[0], scmd->retries);
2793 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2794 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2798 ret_val = megasas_wait_for_outstanding(instance);
2799 if (ret_val == SUCCESS)
2800 dev_notice(&instance->pdev->dev, "reset successful\n");
2802 dev_err(&instance->pdev->dev, "failed to do reset\n");
2808 * megasas_reset_timer - quiesce the adapter if required
2811 * Sets the FW busy flag and reduces the host->can_queue if the
2812 * cmd has not been completed within the timeout period.
2815 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2817 struct megasas_instance *instance;
2818 unsigned long flags;
2820 if (time_after(jiffies, scmd->jiffies_at_alloc +
2821 (scmd_timeout * 2) * HZ)) {
2825 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2826 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2827 /* FW is busy, throttle IO */
2828 spin_lock_irqsave(instance->host->host_lock, flags);
2830 instance->host->can_queue = instance->throttlequeuedepth;
2831 instance->last_time = jiffies;
2832 instance->flag |= MEGASAS_FW_BUSY;
2834 spin_unlock_irqrestore(instance->host->host_lock, flags);
2836 return BLK_EH_RESET_TIMER;
2840 * megasas_dump - This function will provide hexdump
2841 * @ptr: Pointer starting which memory should be dumped
2842 * @size: Size of memory to be dumped
2845 megasas_dump(void *ptr, int sz)
2848 __le32 *loc = (__le32 *)ptr;
2850 for (i = 0; i < sz / sizeof(__le32); i++) {
2851 if (i && ((i % 8) == 0))
2853 printk("%08x ", le32_to_cpu(loc[i]));
2859 * megasas_dump_fusion_io - This function will print key details
2861 * @scmd: SCSI command pointer of SCSI IO
2864 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2866 struct megasas_cmd_fusion *cmd;
2867 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2868 struct megasas_instance *instance;
2870 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2871 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2873 scmd_printk(KERN_INFO, scmd,
2874 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2875 scmd, scmd->retries, scmd->allowed);
2876 scsi_print_command(scmd);
2879 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2880 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2881 scmd_printk(KERN_INFO, scmd,
2882 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
2883 req_desc->SCSIIO.RequestFlags,
2884 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2885 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2887 printk(KERN_INFO "IO request frame:\n");
2888 megasas_dump(cmd->io_request,
2889 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
2890 printk(KERN_INFO "Chain frame:\n");
2891 megasas_dump(cmd->sg_frame,
2892 instance->max_chain_frame_sz);
2898 * megasas_reset_bus_host - Bus & host reset handler entry point
2900 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2903 struct megasas_instance *instance;
2905 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2907 scmd_printk(KERN_INFO, scmd,
2908 "OCR is requested due to IO timeout!!\n");
2910 scmd_printk(KERN_INFO, scmd,
2911 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
2912 scmd->device->host->shost_state,
2913 scsi_host_busy(scmd->device->host),
2914 atomic_read(&instance->fw_outstanding));
2916 * First wait for all commands to complete
2918 if (instance->adapter_type == MFI_SERIES) {
2919 ret = megasas_generic_reset(scmd);
2921 megasas_dump_fusion_io(scmd);
2922 ret = megasas_reset_fusion(scmd->device->host,
2923 SCSIIO_TIMEOUT_OCR);
2930 * megasas_task_abort - Issues task abort request to firmware
2931 * (supported only for fusion adapters)
2932 * @scmd: SCSI command pointer
2934 static int megasas_task_abort(struct scsi_cmnd *scmd)
2937 struct megasas_instance *instance;
2939 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2941 if (instance->adapter_type != MFI_SERIES)
2942 ret = megasas_task_abort_fusion(scmd);
2944 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
2952 * megasas_reset_target: Issues target reset request to firmware
2953 * (supported only for fusion adapters)
2954 * @scmd: SCSI command pointer
2956 static int megasas_reset_target(struct scsi_cmnd *scmd)
2959 struct megasas_instance *instance;
2961 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2963 if (instance->adapter_type != MFI_SERIES)
2964 ret = megasas_reset_target_fusion(scmd);
2966 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
2974 * megasas_bios_param - Returns disk geometry for a disk
2975 * @sdev: device handle
2976 * @bdev: block device
2977 * @capacity: drive capacity
2978 * @geom: geometry parameters
2981 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2982 sector_t capacity, int geom[])
2989 /* Default heads (64) & sectors (32) */
2993 tmp = heads * sectors;
2994 cylinders = capacity;
2996 sector_div(cylinders, tmp);
2999 * Handle extended translation size for logical drives > 1Gb
3002 if (capacity >= 0x200000) {
3005 tmp = heads*sectors;
3006 cylinders = capacity;
3007 sector_div(cylinders, tmp);
3012 geom[2] = cylinders;
3017 static void megasas_aen_polling(struct work_struct *work);
3020 * megasas_service_aen - Processes an event notification
3021 * @instance: Adapter soft state
3022 * @cmd: AEN command completed by the ISR
3024 * For AEN, driver sends a command down to FW that is held by the FW till an
3025 * event occurs. When an event of interest occurs, FW completes the command
3026 * that it was previously holding.
3028 * This routines sends SIGIO signal to processes that have registered with the
3032 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3034 unsigned long flags;
3037 * Don't signal app if it is just an aborted previously registered aen
3039 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3040 spin_lock_irqsave(&poll_aen_lock, flags);
3041 megasas_poll_wait_aen = 1;
3042 spin_unlock_irqrestore(&poll_aen_lock, flags);
3043 wake_up(&megasas_poll_wait);
3044 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3049 instance->aen_cmd = NULL;
3051 megasas_return_cmd(instance, cmd);
3053 if ((instance->unload == 0) &&
3054 ((instance->issuepend_done == 1))) {
3055 struct megasas_aen_event *ev;
3057 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3059 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3061 ev->instance = instance;
3063 INIT_DELAYED_WORK(&ev->hotplug_work,
3064 megasas_aen_polling);
3065 schedule_delayed_work(&ev->hotplug_work, 0);
3071 megasas_fw_crash_buffer_store(struct device *cdev,
3072 struct device_attribute *attr, const char *buf, size_t count)
3074 struct Scsi_Host *shost = class_to_shost(cdev);
3075 struct megasas_instance *instance =
3076 (struct megasas_instance *) shost->hostdata;
3078 unsigned long flags;
3080 if (kstrtoint(buf, 0, &val) != 0)
3083 spin_lock_irqsave(&instance->crashdump_lock, flags);
3084 instance->fw_crash_buffer_offset = val;
3085 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3090 megasas_fw_crash_buffer_show(struct device *cdev,
3091 struct device_attribute *attr, char *buf)
3093 struct Scsi_Host *shost = class_to_shost(cdev);
3094 struct megasas_instance *instance =
3095 (struct megasas_instance *) shost->hostdata;
3097 unsigned long buff_addr;
3098 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3099 unsigned long src_addr;
3100 unsigned long flags;
3103 spin_lock_irqsave(&instance->crashdump_lock, flags);
3104 buff_offset = instance->fw_crash_buffer_offset;
3105 if (!instance->crash_dump_buf &&
3106 !((instance->fw_crash_state == AVAILABLE) ||
3107 (instance->fw_crash_state == COPYING))) {
3108 dev_err(&instance->pdev->dev,
3109 "Firmware crash dump is not available\n");
3110 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3114 buff_addr = (unsigned long) buf;
3116 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3117 dev_err(&instance->pdev->dev,
3118 "Firmware crash dump offset is out of range\n");
3119 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3123 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3124 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3126 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3127 (buff_offset % dmachunk);
3128 memcpy(buf, (void *)src_addr, size);
3129 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3135 megasas_fw_crash_buffer_size_show(struct device *cdev,
3136 struct device_attribute *attr, char *buf)
3138 struct Scsi_Host *shost = class_to_shost(cdev);
3139 struct megasas_instance *instance =
3140 (struct megasas_instance *) shost->hostdata;
3142 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3143 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3147 megasas_fw_crash_state_store(struct device *cdev,
3148 struct device_attribute *attr, const char *buf, size_t count)
3150 struct Scsi_Host *shost = class_to_shost(cdev);
3151 struct megasas_instance *instance =
3152 (struct megasas_instance *) shost->hostdata;
3154 unsigned long flags;
3156 if (kstrtoint(buf, 0, &val) != 0)
3159 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3160 dev_err(&instance->pdev->dev, "application updates invalid "
3161 "firmware crash state\n");
3165 instance->fw_crash_state = val;
3167 if ((val == COPIED) || (val == COPY_ERROR)) {
3168 spin_lock_irqsave(&instance->crashdump_lock, flags);
3169 megasas_free_host_crash_buffer(instance);
3170 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3171 if (val == COPY_ERROR)
3172 dev_info(&instance->pdev->dev, "application failed to "
3173 "copy Firmware crash dump\n");
3175 dev_info(&instance->pdev->dev, "Firmware crash dump "
3176 "copied successfully\n");
3182 megasas_fw_crash_state_show(struct device *cdev,
3183 struct device_attribute *attr, char *buf)
3185 struct Scsi_Host *shost = class_to_shost(cdev);
3186 struct megasas_instance *instance =
3187 (struct megasas_instance *) shost->hostdata;
3189 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3193 megasas_page_size_show(struct device *cdev,
3194 struct device_attribute *attr, char *buf)
3196 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3200 megasas_ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3203 struct Scsi_Host *shost = class_to_shost(cdev);
3204 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3206 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3210 megasas_fw_cmds_outstanding_show(struct device *cdev,
3211 struct device_attribute *attr, char *buf)
3213 struct Scsi_Host *shost = class_to_shost(cdev);
3214 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3216 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3219 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
3220 megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
3221 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
3222 megasas_fw_crash_buffer_size_show, NULL);
3223 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
3224 megasas_fw_crash_state_show, megasas_fw_crash_state_store);
3225 static DEVICE_ATTR(page_size, S_IRUGO,
3226 megasas_page_size_show, NULL);
3227 static DEVICE_ATTR(ldio_outstanding, S_IRUGO,
3228 megasas_ldio_outstanding_show, NULL);
3229 static DEVICE_ATTR(fw_cmds_outstanding, S_IRUGO,
3230 megasas_fw_cmds_outstanding_show, NULL);
3232 struct device_attribute *megaraid_host_attrs[] = {
3233 &dev_attr_fw_crash_buffer_size,
3234 &dev_attr_fw_crash_buffer,
3235 &dev_attr_fw_crash_state,
3236 &dev_attr_page_size,
3237 &dev_attr_ldio_outstanding,
3238 &dev_attr_fw_cmds_outstanding,
3243 * Scsi host template for megaraid_sas driver
3245 static struct scsi_host_template megasas_template = {
3247 .module = THIS_MODULE,
3248 .name = "Avago SAS based MegaRAID driver",
3249 .proc_name = "megaraid_sas",
3250 .slave_configure = megasas_slave_configure,
3251 .slave_alloc = megasas_slave_alloc,
3252 .slave_destroy = megasas_slave_destroy,
3253 .queuecommand = megasas_queue_command,
3254 .eh_target_reset_handler = megasas_reset_target,
3255 .eh_abort_handler = megasas_task_abort,
3256 .eh_host_reset_handler = megasas_reset_bus_host,
3257 .eh_timed_out = megasas_reset_timer,
3258 .shost_attrs = megaraid_host_attrs,
3259 .bios_param = megasas_bios_param,
3260 .change_queue_depth = scsi_change_queue_depth,
3265 * megasas_complete_int_cmd - Completes an internal command
3266 * @instance: Adapter soft state
3267 * @cmd: Command to be completed
3269 * The megasas_issue_blocked_cmd() function waits for a command to complete
3270 * after it issues a command. This function wakes up that waiting routine by
3271 * calling wake_up() on the wait queue.
3274 megasas_complete_int_cmd(struct megasas_instance *instance,
3275 struct megasas_cmd *cmd)
3277 cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3278 wake_up(&instance->int_cmd_wait_q);
3282 * megasas_complete_abort - Completes aborting a command
3283 * @instance: Adapter soft state
3284 * @cmd: Cmd that was issued to abort another cmd
3286 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3287 * after it issues an abort on a previously issued command. This function
3288 * wakes up all functions waiting on the same wait queue.
3291 megasas_complete_abort(struct megasas_instance *instance,
3292 struct megasas_cmd *cmd)
3294 if (cmd->sync_cmd) {
3296 cmd->cmd_status_drv = 0;
3297 wake_up(&instance->abort_cmd_wait_q);
3302 * megasas_complete_cmd - Completes a command
3303 * @instance: Adapter soft state
3304 * @cmd: Command to be completed
3305 * @alt_status: If non-zero, use this value as status to
3306 * SCSI mid-layer instead of the value returned
3307 * by the FW. This should be used if caller wants
3308 * an alternate status (as in the case of aborted
3312 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3316 struct megasas_header *hdr = &cmd->frame->hdr;
3317 unsigned long flags;
3318 struct fusion_context *fusion = instance->ctrl_context;
3321 /* flag for the retry reset */
3322 cmd->retry_for_fw_reset = 0;
3325 cmd->scmd->SCp.ptr = NULL;
3328 case MFI_CMD_INVALID:
3329 /* Some older 1068 controller FW may keep a pended
3330 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3331 when booting the kdump kernel. Ignore this command to
3332 prevent a kernel panic on shutdown of the kdump kernel. */
3333 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3335 dev_warn(&instance->pdev->dev, "If you have a controller "
3336 "other than PERC5, please upgrade your firmware\n");
3338 case MFI_CMD_PD_SCSI_IO:
3339 case MFI_CMD_LD_SCSI_IO:
3342 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3343 * issued either through an IO path or an IOCTL path. If it
3344 * was via IOCTL, we will send it to internal completion.
3346 if (cmd->sync_cmd) {
3348 megasas_complete_int_cmd(instance, cmd);
3353 case MFI_CMD_LD_READ:
3354 case MFI_CMD_LD_WRITE:
3357 cmd->scmd->result = alt_status << 16;
3363 atomic_dec(&instance->fw_outstanding);
3365 scsi_dma_unmap(cmd->scmd);
3366 cmd->scmd->scsi_done(cmd->scmd);
3367 megasas_return_cmd(instance, cmd);
3372 switch (hdr->cmd_status) {
3375 cmd->scmd->result = DID_OK << 16;
3378 case MFI_STAT_SCSI_IO_FAILED:
3379 case MFI_STAT_LD_INIT_IN_PROGRESS:
3381 (DID_ERROR << 16) | hdr->scsi_status;
3384 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3386 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3388 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3389 memset(cmd->scmd->sense_buffer, 0,
3390 SCSI_SENSE_BUFFERSIZE);
3391 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3394 cmd->scmd->result |= DRIVER_SENSE << 24;
3399 case MFI_STAT_LD_OFFLINE:
3400 case MFI_STAT_DEVICE_NOT_FOUND:
3401 cmd->scmd->result = DID_BAD_TARGET << 16;
3405 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3407 cmd->scmd->result = DID_ERROR << 16;
3411 atomic_dec(&instance->fw_outstanding);
3413 scsi_dma_unmap(cmd->scmd);
3414 cmd->scmd->scsi_done(cmd->scmd);
3415 megasas_return_cmd(instance, cmd);
3422 megasas_complete_int_cmd(instance, cmd);
3426 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3427 /* Check for LD map update */
3428 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3429 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3430 fusion->fast_path_io = 0;
3431 spin_lock_irqsave(instance->host->host_lock, flags);
3432 status = cmd->frame->hdr.cmd_status;
3433 instance->map_update_cmd = NULL;
3434 if (status != MFI_STAT_OK) {
3435 if (status != MFI_STAT_NOT_FOUND)
3436 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3437 cmd->frame->hdr.cmd_status);
3439 megasas_return_cmd(instance, cmd);
3440 spin_unlock_irqrestore(
3441 instance->host->host_lock,
3447 megasas_return_cmd(instance, cmd);
3450 * Set fast path IO to ZERO.
3451 * Validate Map will set proper value.
3452 * Meanwhile all IOs will go as LD IO.
3454 if (status == MFI_STAT_OK &&
3455 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3457 fusion->fast_path_io = 1;
3459 fusion->fast_path_io = 0;
3462 megasas_sync_map_info(instance);
3463 spin_unlock_irqrestore(instance->host->host_lock,
3467 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3468 opcode == MR_DCMD_CTRL_EVENT_GET) {
3469 spin_lock_irqsave(&poll_aen_lock, flags);
3470 megasas_poll_wait_aen = 0;
3471 spin_unlock_irqrestore(&poll_aen_lock, flags);
3474 /* FW has an updated PD sequence */
3475 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3476 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3478 spin_lock_irqsave(instance->host->host_lock, flags);
3479 status = cmd->frame->hdr.cmd_status;
3480 instance->jbod_seq_cmd = NULL;
3481 megasas_return_cmd(instance, cmd);
3483 if (status == MFI_STAT_OK) {
3484 instance->pd_seq_map_id++;
3485 /* Re-register a pd sync seq num cmd */
3486 if (megasas_sync_pd_seq_num(instance, true))
3487 instance->use_seqnum_jbod_fp = false;
3489 instance->use_seqnum_jbod_fp = false;
3491 spin_unlock_irqrestore(instance->host->host_lock, flags);
3496 * See if got an event notification
3498 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3499 megasas_service_aen(instance, cmd);
3501 megasas_complete_int_cmd(instance, cmd);
3507 * Cmd issued to abort another cmd returned
3509 megasas_complete_abort(instance, cmd);
3513 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3515 megasas_complete_int_cmd(instance, cmd);
3521 * megasas_issue_pending_cmds_again - issue all pending cmds
3522 * in FW again because of the fw reset
3523 * @instance: Adapter soft state
3526 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3528 struct megasas_cmd *cmd;
3529 struct list_head clist_local;
3530 union megasas_evt_class_locale class_locale;
3531 unsigned long flags;
3534 INIT_LIST_HEAD(&clist_local);
3535 spin_lock_irqsave(&instance->hba_lock, flags);
3536 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3537 spin_unlock_irqrestore(&instance->hba_lock, flags);
3539 while (!list_empty(&clist_local)) {
3540 cmd = list_entry((&clist_local)->next,
3541 struct megasas_cmd, list);
3542 list_del_init(&cmd->list);
3544 if (cmd->sync_cmd || cmd->scmd) {
3545 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3546 "detected to be pending while HBA reset\n",
3547 cmd, cmd->scmd, cmd->sync_cmd);
3549 cmd->retry_for_fw_reset++;
3551 if (cmd->retry_for_fw_reset == 3) {
3552 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3553 "was tried multiple times during reset."
3554 "Shutting down the HBA\n",
3555 cmd, cmd->scmd, cmd->sync_cmd);
3556 instance->instancet->disable_intr(instance);
3557 atomic_set(&instance->fw_reset_no_pci_access, 1);
3558 megaraid_sas_kill_hba(instance);
3563 if (cmd->sync_cmd == 1) {
3565 dev_notice(&instance->pdev->dev, "unexpected"
3566 "cmd attached to internal command!\n");
3568 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3569 "on the internal reset queue,"
3570 "issue it again.\n", cmd);
3571 cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3572 instance->instancet->fire_cmd(instance,
3573 cmd->frame_phys_addr,
3574 0, instance->reg_set);
3575 } else if (cmd->scmd) {
3576 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3577 "detected on the internal queue, issue again.\n",
3578 cmd, cmd->scmd->cmnd[0]);
3580 atomic_inc(&instance->fw_outstanding);
3581 instance->instancet->fire_cmd(instance,
3582 cmd->frame_phys_addr,
3583 cmd->frame_count-1, instance->reg_set);
3585 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3586 "internal reset defer list while re-issue!!\n",
3591 if (instance->aen_cmd) {
3592 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3593 megasas_return_cmd(instance, instance->aen_cmd);
3595 instance->aen_cmd = NULL;
3599 * Initiate AEN (Asynchronous Event Notification)
3601 seq_num = instance->last_seq_num;
3602 class_locale.members.reserved = 0;
3603 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3604 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3606 megasas_register_aen(instance, seq_num, class_locale.word);
3610 * Move the internal reset pending commands to a deferred queue.
3612 * We move the commands pending at internal reset time to a
3613 * pending queue. This queue would be flushed after successful
3614 * completion of the internal reset sequence. if the internal reset
3615 * did not complete in time, the kernel reset handler would flush
3619 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3621 struct megasas_cmd *cmd;
3623 u16 max_cmd = instance->max_fw_cmds;
3625 unsigned long flags;
3628 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3629 for (i = 0; i < max_cmd; i++) {
3630 cmd = instance->cmd_list[i];
3631 if (cmd->sync_cmd == 1 || cmd->scmd) {
3632 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3633 "on the defer queue as internal\n",
3634 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3636 if (!list_empty(&cmd->list)) {
3637 dev_notice(&instance->pdev->dev, "ERROR while"
3638 " moving this cmd:%p, %d %p, it was"
3639 "discovered on some list?\n",
3640 cmd, cmd->sync_cmd, cmd->scmd);
3642 list_del_init(&cmd->list);
3645 list_add_tail(&cmd->list,
3646 &instance->internal_reset_pending_q);
3649 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3654 process_fw_state_change_wq(struct work_struct *work)
3656 struct megasas_instance *instance =
3657 container_of(work, struct megasas_instance, work_init);
3659 unsigned long flags;
3661 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3662 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3663 atomic_read(&instance->adprecovery));
3667 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3668 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3669 "state, restarting it...\n");
3671 instance->instancet->disable_intr(instance);
3672 atomic_set(&instance->fw_outstanding, 0);
3674 atomic_set(&instance->fw_reset_no_pci_access, 1);
3675 instance->instancet->adp_reset(instance, instance->reg_set);
3676 atomic_set(&instance->fw_reset_no_pci_access, 0);
3678 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3679 "initiating next stage...\n");
3681 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3682 "state 2 starting...\n");
3684 /* waiting for about 20 second before start the second init */
3685 for (wait = 0; wait < 30; wait++) {
3689 if (megasas_transition_to_ready(instance, 1)) {
3690 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3692 atomic_set(&instance->fw_reset_no_pci_access, 1);
3693 megaraid_sas_kill_hba(instance);
3697 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3698 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3699 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3701 *instance->consumer = *instance->producer;
3703 *instance->consumer = 0;
3704 *instance->producer = 0;
3707 megasas_issue_init_mfi(instance);
3709 spin_lock_irqsave(&instance->hba_lock, flags);
3710 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3711 spin_unlock_irqrestore(&instance->hba_lock, flags);
3712 instance->instancet->enable_intr(instance);
3714 megasas_issue_pending_cmds_again(instance);
3715 instance->issuepend_done = 1;
3720 * megasas_deplete_reply_queue - Processes all completed commands
3721 * @instance: Adapter soft state
3722 * @alt_status: Alternate status to be returned to
3723 * SCSI mid-layer instead of the status
3724 * returned by the FW
3725 * Note: this must be called with hba lock held
3728 megasas_deplete_reply_queue(struct megasas_instance *instance,
3734 if ((mfiStatus = instance->instancet->check_reset(instance,
3735 instance->reg_set)) == 1) {
3739 mfiStatus = instance->instancet->clear_intr(instance);
3740 if (mfiStatus == 0) {
3741 /* Hardware may not set outbound_intr_status in MSI-X mode */
3742 if (!instance->msix_vectors)
3746 instance->mfiStatus = mfiStatus;
3748 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3749 fw_state = instance->instancet->read_fw_status_reg(
3750 instance) & MFI_STATE_MASK;
3752 if (fw_state != MFI_STATE_FAULT) {
3753 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3757 if ((fw_state == MFI_STATE_FAULT) &&
3758 (instance->disableOnlineCtrlReset == 0)) {
3759 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3761 if ((instance->pdev->device ==
3762 PCI_DEVICE_ID_LSI_SAS1064R) ||
3763 (instance->pdev->device ==
3764 PCI_DEVICE_ID_DELL_PERC5) ||
3765 (instance->pdev->device ==
3766 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3768 *instance->consumer =
3769 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3773 instance->instancet->disable_intr(instance);
3774 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3775 instance->issuepend_done = 0;
3777 atomic_set(&instance->fw_outstanding, 0);
3778 megasas_internal_reset_defer_cmds(instance);
3780 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3781 fw_state, atomic_read(&instance->adprecovery));
3783 schedule_work(&instance->work_init);
3787 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3788 fw_state, instance->disableOnlineCtrlReset);
3792 tasklet_schedule(&instance->isr_tasklet);
3796 * megasas_isr - isr entry point
3798 static irqreturn_t megasas_isr(int irq, void *devp)
3800 struct megasas_irq_context *irq_context = devp;
3801 struct megasas_instance *instance = irq_context->instance;
3802 unsigned long flags;
3805 if (atomic_read(&instance->fw_reset_no_pci_access))
3808 spin_lock_irqsave(&instance->hba_lock, flags);
3809 rc = megasas_deplete_reply_queue(instance, DID_OK);
3810 spin_unlock_irqrestore(&instance->hba_lock, flags);
3816 * megasas_transition_to_ready - Move the FW to READY state
3817 * @instance: Adapter soft state
3819 * During the initialization, FW passes can potentially be in any one of
3820 * several possible states. If the FW in operational, waiting-for-handshake
3821 * states, driver must take steps to bring it to ready state. Otherwise, it
3822 * has to wait for the ready state.
3825 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3831 u32 abs_state, curr_abs_state;
3833 abs_state = instance->instancet->read_fw_status_reg(instance);
3834 fw_state = abs_state & MFI_STATE_MASK;
3836 if (fw_state != MFI_STATE_READY)
3837 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3840 while (fw_state != MFI_STATE_READY) {
3844 case MFI_STATE_FAULT:
3845 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3847 max_wait = MEGASAS_RESET_WAIT_TIME;
3848 cur_state = MFI_STATE_FAULT;
3853 case MFI_STATE_WAIT_HANDSHAKE:
3855 * Set the CLR bit in inbound doorbell
3857 if ((instance->pdev->device ==
3858 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3859 (instance->pdev->device ==
3860 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3861 (instance->adapter_type != MFI_SERIES))
3863 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3864 &instance->reg_set->doorbell);
3867 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3868 &instance->reg_set->inbound_doorbell);
3870 max_wait = MEGASAS_RESET_WAIT_TIME;
3871 cur_state = MFI_STATE_WAIT_HANDSHAKE;
3874 case MFI_STATE_BOOT_MESSAGE_PENDING:
3875 if ((instance->pdev->device ==
3876 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3877 (instance->pdev->device ==
3878 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3879 (instance->adapter_type != MFI_SERIES))
3880 writel(MFI_INIT_HOTPLUG,
3881 &instance->reg_set->doorbell);
3883 writel(MFI_INIT_HOTPLUG,
3884 &instance->reg_set->inbound_doorbell);
3886 max_wait = MEGASAS_RESET_WAIT_TIME;
3887 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3890 case MFI_STATE_OPERATIONAL:
3892 * Bring it to READY state; assuming max wait 10 secs
3894 instance->instancet->disable_intr(instance);
3895 if ((instance->pdev->device ==
3896 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3897 (instance->pdev->device ==
3898 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3899 (instance->adapter_type != MFI_SERIES)) {
3900 writel(MFI_RESET_FLAGS,
3901 &instance->reg_set->doorbell);
3903 if (instance->adapter_type != MFI_SERIES) {
3904 for (i = 0; i < (10 * 1000); i += 20) {
3916 writel(MFI_RESET_FLAGS,
3917 &instance->reg_set->inbound_doorbell);
3919 max_wait = MEGASAS_RESET_WAIT_TIME;
3920 cur_state = MFI_STATE_OPERATIONAL;
3923 case MFI_STATE_UNDEFINED:
3925 * This state should not last for more than 2 seconds
3927 max_wait = MEGASAS_RESET_WAIT_TIME;
3928 cur_state = MFI_STATE_UNDEFINED;
3931 case MFI_STATE_BB_INIT:
3932 max_wait = MEGASAS_RESET_WAIT_TIME;
3933 cur_state = MFI_STATE_BB_INIT;
3936 case MFI_STATE_FW_INIT:
3937 max_wait = MEGASAS_RESET_WAIT_TIME;
3938 cur_state = MFI_STATE_FW_INIT;
3941 case MFI_STATE_FW_INIT_2:
3942 max_wait = MEGASAS_RESET_WAIT_TIME;
3943 cur_state = MFI_STATE_FW_INIT_2;
3946 case MFI_STATE_DEVICE_SCAN:
3947 max_wait = MEGASAS_RESET_WAIT_TIME;
3948 cur_state = MFI_STATE_DEVICE_SCAN;
3951 case MFI_STATE_FLUSH_CACHE:
3952 max_wait = MEGASAS_RESET_WAIT_TIME;
3953 cur_state = MFI_STATE_FLUSH_CACHE;
3957 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3963 * The cur_state should not last for more than max_wait secs
3965 for (i = 0; i < max_wait * 50; i++) {
3966 curr_abs_state = instance->instancet->
3967 read_fw_status_reg(instance);
3969 if (abs_state == curr_abs_state) {
3976 * Return error if fw_state hasn't changed after max_wait
3978 if (curr_abs_state == abs_state) {
3979 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3980 "in %d secs\n", fw_state, max_wait);
3984 abs_state = curr_abs_state;
3985 fw_state = curr_abs_state & MFI_STATE_MASK;
3987 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3993 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
3994 * @instance: Adapter soft state
3996 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3999 u16 max_cmd = instance->max_mfi_cmds;
4000 struct megasas_cmd *cmd;
4002 if (!instance->frame_dma_pool)
4006 * Return all frames to pool
4008 for (i = 0; i < max_cmd; i++) {
4010 cmd = instance->cmd_list[i];
4013 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4014 cmd->frame_phys_addr);
4017 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4018 cmd->sense_phys_addr);
4022 * Now destroy the pool itself
4024 dma_pool_destroy(instance->frame_dma_pool);
4025 dma_pool_destroy(instance->sense_dma_pool);
4027 instance->frame_dma_pool = NULL;
4028 instance->sense_dma_pool = NULL;
4032 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4033 * @instance: Adapter soft state
4035 * Each command packet has an embedded DMA memory buffer that is used for
4036 * filling MFI frame and the SG list that immediately follows the frame. This
4037 * function creates those DMA memory buffers for each command packet by using
4038 * PCI pool facility.
4040 static int megasas_create_frame_pool(struct megasas_instance *instance)
4046 struct megasas_cmd *cmd;
4048 max_cmd = instance->max_mfi_cmds;
4051 * Size of our frame is 64 bytes for MFI frame, followed by max SG
4052 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
4054 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
4055 sizeof(struct megasas_sge32);
4057 if (instance->flag_ieee)
4058 sge_sz = sizeof(struct megasas_sge_skinny);
4061 * For MFI controllers.
4063 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4064 * Total 960 byte (15 MFI frame of 64 byte)
4066 * Fusion adapter require only 3 extra frame.
4067 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4068 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4069 * Total 192 byte (3 MFI frame of 64 byte)
4071 frame_count = (instance->adapter_type == MFI_SERIES) ?
4073 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4075 * Use DMA pool facility provided by PCI layer
4077 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4078 &instance->pdev->dev,
4079 instance->mfi_frame_size, 256, 0);
4081 if (!instance->frame_dma_pool) {
4082 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4086 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4087 &instance->pdev->dev, 128,
4090 if (!instance->sense_dma_pool) {
4091 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4093 dma_pool_destroy(instance->frame_dma_pool);
4094 instance->frame_dma_pool = NULL;
4100 * Allocate and attach a frame to each of the commands in cmd_list.
4101 * By making cmd->index as the context instead of the &cmd, we can
4102 * always use 32bit context regardless of the architecture
4104 for (i = 0; i < max_cmd; i++) {
4106 cmd = instance->cmd_list[i];
4108 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4109 GFP_KERNEL, &cmd->frame_phys_addr);
4111 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4112 GFP_KERNEL, &cmd->sense_phys_addr);
4115 * megasas_teardown_frame_pool() takes care of freeing
4116 * whatever has been allocated
4118 if (!cmd->frame || !cmd->sense) {
4119 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4120 megasas_teardown_frame_pool(instance);
4124 cmd->frame->io.context = cpu_to_le32(cmd->index);
4125 cmd->frame->io.pad_0 = 0;
4126 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4127 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4134 * megasas_free_cmds - Free all the cmds in the free cmd pool
4135 * @instance: Adapter soft state
4137 void megasas_free_cmds(struct megasas_instance *instance)
4141 /* First free the MFI frame pool */
4142 megasas_teardown_frame_pool(instance);
4144 /* Free all the commands in the cmd_list */
4145 for (i = 0; i < instance->max_mfi_cmds; i++)
4147 kfree(instance->cmd_list[i]);
4149 /* Free the cmd_list buffer itself */
4150 kfree(instance->cmd_list);
4151 instance->cmd_list = NULL;
4153 INIT_LIST_HEAD(&instance->cmd_pool);
4157 * megasas_alloc_cmds - Allocates the command packets
4158 * @instance: Adapter soft state
4160 * Each command that is issued to the FW, whether IO commands from the OS or
4161 * internal commands like IOCTLs, are wrapped in local data structure called
4162 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4165 * Each frame has a 32-bit field called context (tag). This context is used
4166 * to get back the megasas_cmd from the frame when a frame gets completed in
4167 * the ISR. Typically the address of the megasas_cmd itself would be used as
4168 * the context. But we wanted to keep the differences between 32 and 64 bit
4169 * systems to the mininum. We always use 32 bit integers for the context. In
4170 * this driver, the 32 bit values are the indices into an array cmd_list.
4171 * This array is used only to look up the megasas_cmd given the context. The
4172 * free commands themselves are maintained in a linked list called cmd_pool.
4174 int megasas_alloc_cmds(struct megasas_instance *instance)
4179 struct megasas_cmd *cmd;
4181 max_cmd = instance->max_mfi_cmds;
4184 * instance->cmd_list is an array of struct megasas_cmd pointers.
4185 * Allocate the dynamic array first and then allocate individual
4188 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4190 if (!instance->cmd_list) {
4191 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4195 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4197 for (i = 0; i < max_cmd; i++) {
4198 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4201 if (!instance->cmd_list[i]) {
4203 for (j = 0; j < i; j++)
4204 kfree(instance->cmd_list[j]);
4206 kfree(instance->cmd_list);
4207 instance->cmd_list = NULL;
4213 for (i = 0; i < max_cmd; i++) {
4214 cmd = instance->cmd_list[i];
4215 memset(cmd, 0, sizeof(struct megasas_cmd));
4218 cmd->instance = instance;
4220 list_add_tail(&cmd->list, &instance->cmd_pool);
4224 * Create a frame pool and assign one frame to each cmd
4226 if (megasas_create_frame_pool(instance)) {
4227 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4228 megasas_free_cmds(instance);
4236 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4237 * @instance: Adapter soft state
4239 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4240 * or FW is not under OCR.
4243 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4245 if (instance->adapter_type == MFI_SERIES)
4246 return KILL_ADAPTER;
4247 else if (instance->unload ||
4248 test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
4249 return IGNORE_TIMEOUT;
4251 return INITIATE_OCR;
4255 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4258 struct megasas_cmd *cmd;
4259 struct megasas_dcmd_frame *dcmd;
4261 struct MR_PRIV_DEVICE *mr_device_priv_data;
4264 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4265 cmd = megasas_get_cmd(instance);
4268 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4272 dcmd = &cmd->frame->dcmd;
4274 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4275 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4277 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4278 dcmd->cmd = MFI_CMD_DCMD;
4279 dcmd->cmd_status = 0xFF;
4280 dcmd->sge_count = 1;
4281 dcmd->flags = MFI_FRAME_DIR_READ;
4284 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4285 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4287 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4288 sizeof(struct MR_PD_INFO));
4290 if ((instance->adapter_type != MFI_SERIES) &&
4291 !instance->mask_interrupts)
4292 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4294 ret = megasas_issue_polled(instance, cmd);
4298 mr_device_priv_data = sdev->hostdata;
4299 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4300 mr_device_priv_data->interface_type =
4301 instance->pd_info->state.ddf.pdType.intf;
4306 switch (dcmd_timeout_ocr_possible(instance)) {
4308 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4309 megasas_reset_fusion(instance->host,
4310 MFI_IO_TIMEOUT_OCR);
4313 megaraid_sas_kill_hba(instance);
4315 case IGNORE_TIMEOUT:
4316 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4317 __func__, __LINE__);
4324 if (ret != DCMD_TIMEOUT)
4325 megasas_return_cmd(instance, cmd);
4330 * megasas_get_pd_list_info - Returns FW's pd_list structure
4331 * @instance: Adapter soft state
4332 * @pd_list: pd_list structure
4334 * Issues an internal command (DCMD) to get the FW's controller PD
4335 * list structure. This information is mainly used to find out SYSTEM
4336 * supported by the FW.
4339 megasas_get_pd_list(struct megasas_instance *instance)
4341 int ret = 0, pd_index = 0;
4342 struct megasas_cmd *cmd;
4343 struct megasas_dcmd_frame *dcmd;
4344 struct MR_PD_LIST *ci;
4345 struct MR_PD_ADDRESS *pd_addr;
4346 dma_addr_t ci_h = 0;
4348 if (instance->pd_list_not_supported) {
4349 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4350 "not supported by firmware\n");
4354 ci = instance->pd_list_buf;
4355 ci_h = instance->pd_list_buf_h;
4357 cmd = megasas_get_cmd(instance);
4360 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4364 dcmd = &cmd->frame->dcmd;
4366 memset(ci, 0, sizeof(*ci));
4367 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4369 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4370 dcmd->mbox.b[1] = 0;
4371 dcmd->cmd = MFI_CMD_DCMD;
4372 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4373 dcmd->sge_count = 1;
4374 dcmd->flags = MFI_FRAME_DIR_READ;
4377 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4378 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4380 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4381 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4383 if ((instance->adapter_type != MFI_SERIES) &&
4384 !instance->mask_interrupts)
4385 ret = megasas_issue_blocked_cmd(instance, cmd,
4386 MFI_IO_TIMEOUT_SECS);
4388 ret = megasas_issue_polled(instance, cmd);
4392 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4393 "failed/not supported by firmware\n");
4395 if (instance->adapter_type != MFI_SERIES)
4396 megaraid_sas_kill_hba(instance);
4398 instance->pd_list_not_supported = 1;
4402 switch (dcmd_timeout_ocr_possible(instance)) {
4404 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4406 * DCMD failed from AEN path.
4407 * AEN path already hold reset_mutex to avoid PCI access
4408 * while OCR is in progress.
4410 mutex_unlock(&instance->reset_mutex);
4411 megasas_reset_fusion(instance->host,
4412 MFI_IO_TIMEOUT_OCR);
4413 mutex_lock(&instance->reset_mutex);
4416 megaraid_sas_kill_hba(instance);
4418 case IGNORE_TIMEOUT:
4419 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4420 __func__, __LINE__);
4429 if ((le32_to_cpu(ci->count) >
4430 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4433 memset(instance->local_pd_list, 0,
4434 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4436 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4437 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4438 le16_to_cpu(pd_addr->deviceId);
4439 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4440 pd_addr->scsiDevType;
4441 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4446 memcpy(instance->pd_list, instance->local_pd_list,
4447 sizeof(instance->pd_list));
4452 if (ret != DCMD_TIMEOUT)
4453 megasas_return_cmd(instance, cmd);
4459 * megasas_get_ld_list_info - Returns FW's ld_list structure
4460 * @instance: Adapter soft state
4461 * @ld_list: ld_list structure
4463 * Issues an internal command (DCMD) to get the FW's controller PD
4464 * list structure. This information is mainly used to find out SYSTEM
4465 * supported by the FW.
4468 megasas_get_ld_list(struct megasas_instance *instance)
4470 int ret = 0, ld_index = 0, ids = 0;
4471 struct megasas_cmd *cmd;
4472 struct megasas_dcmd_frame *dcmd;
4473 struct MR_LD_LIST *ci;
4474 dma_addr_t ci_h = 0;
4477 ci = instance->ld_list_buf;
4478 ci_h = instance->ld_list_buf_h;
4480 cmd = megasas_get_cmd(instance);
4483 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4487 dcmd = &cmd->frame->dcmd;
4489 memset(ci, 0, sizeof(*ci));
4490 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4492 if (instance->supportmax256vd)
4493 dcmd->mbox.b[0] = 1;
4494 dcmd->cmd = MFI_CMD_DCMD;
4495 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4496 dcmd->sge_count = 1;
4497 dcmd->flags = MFI_FRAME_DIR_READ;
4499 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4500 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4503 megasas_set_dma_settings(instance, dcmd, ci_h,
4504 sizeof(struct MR_LD_LIST));
4506 if ((instance->adapter_type != MFI_SERIES) &&
4507 !instance->mask_interrupts)
4508 ret = megasas_issue_blocked_cmd(instance, cmd,
4509 MFI_IO_TIMEOUT_SECS);
4511 ret = megasas_issue_polled(instance, cmd);
4513 ld_count = le32_to_cpu(ci->ldCount);
4517 megaraid_sas_kill_hba(instance);
4521 switch (dcmd_timeout_ocr_possible(instance)) {
4523 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4525 * DCMD failed from AEN path.
4526 * AEN path already hold reset_mutex to avoid PCI access
4527 * while OCR is in progress.
4529 mutex_unlock(&instance->reset_mutex);
4530 megasas_reset_fusion(instance->host,
4531 MFI_IO_TIMEOUT_OCR);
4532 mutex_lock(&instance->reset_mutex);
4535 megaraid_sas_kill_hba(instance);
4537 case IGNORE_TIMEOUT:
4538 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4539 __func__, __LINE__);
4546 if (ld_count > instance->fw_supported_vd_count)
4549 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4551 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4552 if (ci->ldList[ld_index].state != 0) {
4553 ids = ci->ldList[ld_index].ref.targetId;
4554 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4561 if (ret != DCMD_TIMEOUT)
4562 megasas_return_cmd(instance, cmd);
4568 * megasas_ld_list_query - Returns FW's ld_list structure
4569 * @instance: Adapter soft state
4570 * @ld_list: ld_list structure
4572 * Issues an internal command (DCMD) to get the FW's controller PD
4573 * list structure. This information is mainly used to find out SYSTEM
4574 * supported by the FW.
4577 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4579 int ret = 0, ld_index = 0, ids = 0;
4580 struct megasas_cmd *cmd;
4581 struct megasas_dcmd_frame *dcmd;
4582 struct MR_LD_TARGETID_LIST *ci;
4583 dma_addr_t ci_h = 0;
4586 ci = instance->ld_targetid_list_buf;
4587 ci_h = instance->ld_targetid_list_buf_h;
4589 cmd = megasas_get_cmd(instance);
4592 dev_warn(&instance->pdev->dev,
4593 "megasas_ld_list_query: Failed to get cmd\n");
4597 dcmd = &cmd->frame->dcmd;
4599 memset(ci, 0, sizeof(*ci));
4600 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4602 dcmd->mbox.b[0] = query_type;
4603 if (instance->supportmax256vd)
4604 dcmd->mbox.b[2] = 1;
4606 dcmd->cmd = MFI_CMD_DCMD;
4607 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4608 dcmd->sge_count = 1;
4609 dcmd->flags = MFI_FRAME_DIR_READ;
4611 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4612 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4615 megasas_set_dma_settings(instance, dcmd, ci_h,
4616 sizeof(struct MR_LD_TARGETID_LIST));
4618 if ((instance->adapter_type != MFI_SERIES) &&
4619 !instance->mask_interrupts)
4620 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4622 ret = megasas_issue_polled(instance, cmd);
4626 dev_info(&instance->pdev->dev,
4627 "DCMD not supported by firmware - %s %d\n",
4628 __func__, __LINE__);
4629 ret = megasas_get_ld_list(instance);
4632 switch (dcmd_timeout_ocr_possible(instance)) {
4634 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4636 * DCMD failed from AEN path.
4637 * AEN path already hold reset_mutex to avoid PCI access
4638 * while OCR is in progress.
4640 mutex_unlock(&instance->reset_mutex);
4641 megasas_reset_fusion(instance->host,
4642 MFI_IO_TIMEOUT_OCR);
4643 mutex_lock(&instance->reset_mutex);
4646 megaraid_sas_kill_hba(instance);
4648 case IGNORE_TIMEOUT:
4649 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4650 __func__, __LINE__);
4656 tgtid_count = le32_to_cpu(ci->count);
4658 if ((tgtid_count > (instance->fw_supported_vd_count)))
4661 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4662 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4663 ids = ci->targetId[ld_index];
4664 instance->ld_ids[ids] = ci->targetId[ld_index];
4670 if (ret != DCMD_TIMEOUT)
4671 megasas_return_cmd(instance, cmd);
4677 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4678 * dcmd.mbox - reserved
4679 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4680 * Desc: This DCMD will return the combined device list
4681 * Status: MFI_STAT_OK - List returned successfully
4682 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4684 * @instance: Adapter soft state
4685 * @is_probe: Driver probe check
4686 * Return: 0 if DCMD succeeded
4687 * non-zero if failed
4690 megasas_host_device_list_query(struct megasas_instance *instance,
4693 int ret, i, target_id;
4694 struct megasas_cmd *cmd;
4695 struct megasas_dcmd_frame *dcmd;
4696 struct MR_HOST_DEVICE_LIST *ci;
4700 ci = instance->host_device_list_buf;
4701 ci_h = instance->host_device_list_buf_h;
4703 cmd = megasas_get_cmd(instance);
4706 dev_warn(&instance->pdev->dev,
4707 "%s: failed to get cmd\n",
4712 dcmd = &cmd->frame->dcmd;
4714 memset(ci, 0, sizeof(*ci));
4715 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4717 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4718 dcmd->cmd = MFI_CMD_DCMD;
4719 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4720 dcmd->sge_count = 1;
4721 dcmd->flags = MFI_FRAME_DIR_READ;
4724 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4725 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4727 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4729 if (!instance->mask_interrupts) {
4730 ret = megasas_issue_blocked_cmd(instance, cmd,
4731 MFI_IO_TIMEOUT_SECS);
4733 ret = megasas_issue_polled(instance, cmd);
4734 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4739 /* Fill the internal pd_list and ld_ids array based on
4740 * targetIds returned by FW
4742 count = le32_to_cpu(ci->count);
4744 memset(instance->local_pd_list, 0,
4745 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4746 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4747 for (i = 0; i < count; i++) {
4748 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4749 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4750 instance->local_pd_list[target_id].tid = target_id;
4751 instance->local_pd_list[target_id].driveType =
4752 ci->host_device_list[i].scsi_type;
4753 instance->local_pd_list[target_id].driveState =
4756 instance->ld_ids[target_id] = target_id;
4760 memcpy(instance->pd_list, instance->local_pd_list,
4761 sizeof(instance->pd_list));
4765 switch (dcmd_timeout_ocr_possible(instance)) {
4767 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4768 megasas_reset_fusion(instance->host,
4769 MFI_IO_TIMEOUT_OCR);
4772 megaraid_sas_kill_hba(instance);
4774 case IGNORE_TIMEOUT:
4775 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4776 __func__, __LINE__);
4781 dev_err(&instance->pdev->dev,
4782 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4787 if (ret != DCMD_TIMEOUT)
4788 megasas_return_cmd(instance, cmd);
4794 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4795 * instance : Controller's instance
4797 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4799 struct fusion_context *fusion;
4800 u32 ventura_map_sz = 0;
4802 fusion = instance->ctrl_context;
4803 /* For MFI based controllers return dummy success */
4807 instance->supportmax256vd =
4808 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
4809 /* Below is additional check to address future FW enhancement */
4810 if (instance->ctrl_info_buf->max_lds > 64)
4811 instance->supportmax256vd = 1;
4813 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4814 * MEGASAS_MAX_DEV_PER_CHANNEL;
4815 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4816 * MEGASAS_MAX_DEV_PER_CHANNEL;
4817 if (instance->supportmax256vd) {
4818 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4819 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4821 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4822 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4825 dev_info(&instance->pdev->dev,
4826 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
4827 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
4828 instance->ctrl_info_buf->max_lds);
4830 if (instance->max_raid_mapsize) {
4831 ventura_map_sz = instance->max_raid_mapsize *
4832 MR_MIN_MAP_SIZE; /* 64k */
4833 fusion->current_map_sz = ventura_map_sz;
4834 fusion->max_map_sz = ventura_map_sz;
4836 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4837 (sizeof(struct MR_LD_SPAN_MAP) *
4838 (instance->fw_supported_vd_count - 1));
4839 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4841 fusion->max_map_sz =
4842 max(fusion->old_map_sz, fusion->new_map_sz);
4844 if (instance->supportmax256vd)
4845 fusion->current_map_sz = fusion->new_map_sz;
4847 fusion->current_map_sz = fusion->old_map_sz;
4849 /* irrespective of FW raid maps, driver raid map is constant */
4850 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
4854 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
4855 * dcmd.hdr.length - number of bytes to read
4856 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
4857 * Desc: Fill in snapdump properties
4858 * Status: MFI_STAT_OK- Command successful
4860 void megasas_get_snapdump_properties(struct megasas_instance *instance)
4863 struct megasas_cmd *cmd;
4864 struct megasas_dcmd_frame *dcmd;
4865 struct MR_SNAPDUMP_PROPERTIES *ci;
4866 dma_addr_t ci_h = 0;
4868 ci = instance->snapdump_prop;
4869 ci_h = instance->snapdump_prop_h;
4874 cmd = megasas_get_cmd(instance);
4877 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
4881 dcmd = &cmd->frame->dcmd;
4883 memset(ci, 0, sizeof(*ci));
4884 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4886 dcmd->cmd = MFI_CMD_DCMD;
4887 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4888 dcmd->sge_count = 1;
4889 dcmd->flags = MFI_FRAME_DIR_READ;
4892 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
4893 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
4895 megasas_set_dma_settings(instance, dcmd, ci_h,
4896 sizeof(struct MR_SNAPDUMP_PROPERTIES));
4898 if (!instance->mask_interrupts) {
4899 ret = megasas_issue_blocked_cmd(instance, cmd,
4900 MFI_IO_TIMEOUT_SECS);
4902 ret = megasas_issue_polled(instance, cmd);
4903 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4908 instance->snapdump_wait_time =
4909 min_t(u8, ci->trigger_min_num_sec_before_ocr,
4910 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
4914 switch (dcmd_timeout_ocr_possible(instance)) {
4916 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4917 megasas_reset_fusion(instance->host,
4918 MFI_IO_TIMEOUT_OCR);
4921 megaraid_sas_kill_hba(instance);
4923 case IGNORE_TIMEOUT:
4924 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4925 __func__, __LINE__);
4930 if (ret != DCMD_TIMEOUT)
4931 megasas_return_cmd(instance, cmd);
4935 * megasas_get_controller_info - Returns FW's controller structure
4936 * @instance: Adapter soft state
4938 * Issues an internal command (DCMD) to get the FW's controller structure.
4939 * This information is mainly used to find out the maximum IO transfer per
4940 * command supported by the FW.
4943 megasas_get_ctrl_info(struct megasas_instance *instance)
4946 struct megasas_cmd *cmd;
4947 struct megasas_dcmd_frame *dcmd;
4948 struct megasas_ctrl_info *ci;
4949 dma_addr_t ci_h = 0;
4951 ci = instance->ctrl_info_buf;
4952 ci_h = instance->ctrl_info_buf_h;
4954 cmd = megasas_get_cmd(instance);
4957 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4961 dcmd = &cmd->frame->dcmd;
4963 memset(ci, 0, sizeof(*ci));
4964 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4966 dcmd->cmd = MFI_CMD_DCMD;
4967 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4968 dcmd->sge_count = 1;
4969 dcmd->flags = MFI_FRAME_DIR_READ;
4972 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4973 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4974 dcmd->mbox.b[0] = 1;
4976 megasas_set_dma_settings(instance, dcmd, ci_h,
4977 sizeof(struct megasas_ctrl_info));
4979 if ((instance->adapter_type != MFI_SERIES) &&
4980 !instance->mask_interrupts) {
4981 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4983 ret = megasas_issue_polled(instance, cmd);
4984 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4989 /* Save required controller information in
4990 * CPU endianness format.
4992 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
4993 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
4994 le32_to_cpus((u32 *)&ci->adapterOperations2);
4995 le32_to_cpus((u32 *)&ci->adapterOperations3);
4996 le16_to_cpus((u16 *)&ci->adapter_operations4);
4998 /* Update the latest Ext VD info.
4999 * From Init path, store current firmware details.
5000 * From OCR path, detect any firmware properties changes.
5001 * in case of Firmware upgrade without system reboot.
5003 megasas_update_ext_vd_details(instance);
5004 instance->use_seqnum_jbod_fp =
5005 ci->adapterOperations3.useSeqNumJbodFP;
5006 instance->support_morethan256jbod =
5007 ci->adapter_operations4.support_pd_map_target_id;
5008 instance->support_nvme_passthru =
5009 ci->adapter_operations4.support_nvme_passthru;
5010 instance->task_abort_tmo = ci->TaskAbortTO;
5011 instance->max_reset_tmo = ci->MaxResetTO;
5013 /*Check whether controller is iMR or MR */
5014 instance->is_imr = (ci->memory_size ? 0 : 1);
5016 instance->snapdump_wait_time =
5017 (ci->properties.on_off_properties2.enable_snap_dump ?
5018 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5020 instance->enable_fw_dev_list =
5021 ci->properties.on_off_properties2.enable_fw_dev_list;
5023 dev_info(&instance->pdev->dev,
5024 "controller type\t: %s(%dMB)\n",
5025 instance->is_imr ? "iMR" : "MR",
5026 le16_to_cpu(ci->memory_size));
5028 instance->disableOnlineCtrlReset =
5029 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5030 instance->secure_jbod_support =
5031 ci->adapterOperations3.supportSecurityonJBOD;
5032 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5033 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5034 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5035 instance->secure_jbod_support ? "Yes" : "No");
5036 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5037 instance->support_nvme_passthru ? "Yes" : "No");
5038 dev_info(&instance->pdev->dev,
5039 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5040 instance->task_abort_tmo, instance->max_reset_tmo);
5045 switch (dcmd_timeout_ocr_possible(instance)) {
5047 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5048 megasas_reset_fusion(instance->host,
5049 MFI_IO_TIMEOUT_OCR);
5052 megaraid_sas_kill_hba(instance);
5054 case IGNORE_TIMEOUT:
5055 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5056 __func__, __LINE__);
5061 megaraid_sas_kill_hba(instance);
5066 if (ret != DCMD_TIMEOUT)
5067 megasas_return_cmd(instance, cmd);
5073 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5076 * @instance: Adapter soft state
5077 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5078 MR_CRASH_BUF_TURN_OFF = 0
5079 MR_CRASH_BUF_TURN_ON = 1
5080 * @return 0 on success non-zero on failure.
5081 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5082 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5083 * that driver supports crash dump feature. This DCMD will be sent only if
5084 * crash dump feature is supported by the FW.
5087 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5091 struct megasas_cmd *cmd;
5092 struct megasas_dcmd_frame *dcmd;
5094 cmd = megasas_get_cmd(instance);
5097 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5102 dcmd = &cmd->frame->dcmd;
5104 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5105 dcmd->mbox.b[0] = crash_buf_state;
5106 dcmd->cmd = MFI_CMD_DCMD;
5107 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5108 dcmd->sge_count = 1;
5109 dcmd->flags = MFI_FRAME_DIR_NONE;
5112 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5113 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5115 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5116 CRASH_DMA_BUF_SIZE);
5118 if ((instance->adapter_type != MFI_SERIES) &&
5119 !instance->mask_interrupts)
5120 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5122 ret = megasas_issue_polled(instance, cmd);
5124 if (ret == DCMD_TIMEOUT) {
5125 switch (dcmd_timeout_ocr_possible(instance)) {
5127 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5128 megasas_reset_fusion(instance->host,
5129 MFI_IO_TIMEOUT_OCR);
5132 megaraid_sas_kill_hba(instance);
5134 case IGNORE_TIMEOUT:
5135 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5136 __func__, __LINE__);
5140 megasas_return_cmd(instance, cmd);
5146 * megasas_issue_init_mfi - Initializes the FW
5147 * @instance: Adapter soft state
5149 * Issues the INIT MFI cmd
5152 megasas_issue_init_mfi(struct megasas_instance *instance)
5155 struct megasas_cmd *cmd;
5156 struct megasas_init_frame *init_frame;
5157 struct megasas_init_queue_info *initq_info;
5158 dma_addr_t init_frame_h;
5159 dma_addr_t initq_info_h;
5162 * Prepare a init frame. Note the init frame points to queue info
5163 * structure. Each frame has SGL allocated after first 64 bytes. For
5164 * this frame - since we don't need any SGL - we use SGL's space as
5165 * queue info structure
5167 * We will not get a NULL command below. We just created the pool.
5169 cmd = megasas_get_cmd(instance);
5171 init_frame = (struct megasas_init_frame *)cmd->frame;
5172 initq_info = (struct megasas_init_queue_info *)
5173 ((unsigned long)init_frame + 64);
5175 init_frame_h = cmd->frame_phys_addr;
5176 initq_info_h = init_frame_h + 64;
5178 context = init_frame->context;
5179 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5180 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5181 init_frame->context = context;
5183 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5184 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5186 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5187 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5189 init_frame->cmd = MFI_CMD_INIT;
5190 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5191 init_frame->queue_info_new_phys_addr_lo =
5192 cpu_to_le32(lower_32_bits(initq_info_h));
5193 init_frame->queue_info_new_phys_addr_hi =
5194 cpu_to_le32(upper_32_bits(initq_info_h));
5196 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5199 * disable the intr before firing the init frame to FW
5201 instance->instancet->disable_intr(instance);
5204 * Issue the init frame in polled mode
5207 if (megasas_issue_polled(instance, cmd)) {
5208 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5209 megasas_return_cmd(instance, cmd);
5213 megasas_return_cmd(instance, cmd);
5222 megasas_init_adapter_mfi(struct megasas_instance *instance)
5228 * Get various operational parameters from status register
5230 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5232 * Reduce the max supported cmds by 1. This is to ensure that the
5233 * reply_q_sz (1 more than the max cmd that driver may send)
5234 * does not exceed max cmds that the FW can support
5236 instance->max_fw_cmds = instance->max_fw_cmds-1;
5237 instance->max_mfi_cmds = instance->max_fw_cmds;
5238 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5241 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5242 * are reserved for IOCTL + driver's internal DCMDs.
5244 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5245 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5246 instance->max_scsi_cmds = (instance->max_fw_cmds -
5247 MEGASAS_SKINNY_INT_CMDS);
5248 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5250 instance->max_scsi_cmds = (instance->max_fw_cmds -
5252 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5255 instance->cur_can_queue = instance->max_scsi_cmds;
5257 * Create a pool of commands
5259 if (megasas_alloc_cmds(instance))
5260 goto fail_alloc_cmds;
5263 * Allocate memory for reply queue. Length of reply queue should
5264 * be _one_ more than the maximum commands handled by the firmware.
5266 * Note: When FW completes commands, it places corresponding contex
5267 * values in this circular reply queue. This circular queue is a fairly
5268 * typical producer-consumer queue. FW is the producer (of completed
5269 * commands) and the driver is the consumer.
5271 context_sz = sizeof(u32);
5272 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5274 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5275 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5277 if (!instance->reply_queue) {
5278 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5279 goto fail_reply_queue;
5282 if (megasas_issue_init_mfi(instance))
5285 if (megasas_get_ctrl_info(instance)) {
5286 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5287 "Fail from %s %d\n", instance->unique_id,
5288 __func__, __LINE__);
5292 instance->fw_support_ieee = 0;
5293 instance->fw_support_ieee =
5294 (instance->instancet->read_fw_status_reg(instance) &
5297 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5298 instance->fw_support_ieee);
5300 if (instance->fw_support_ieee)
5301 instance->flag_ieee = 1;
5307 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5308 instance->reply_queue, instance->reply_queue_h);
5310 megasas_free_cmds(instance);
5317 void megasas_setup_irq_poll(struct megasas_instance *instance)
5319 struct megasas_irq_context *irq_ctx;
5322 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5324 /* Initialize IRQ poll */
5325 for (i = 0; i < count; i++) {
5326 irq_ctx = &instance->irq_context[i];
5327 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5328 irq_ctx->irq_poll_scheduled = false;
5329 irq_poll_init(&irq_ctx->irqpoll,
5330 instance->threshold_reply_count,
5336 * megasas_setup_irqs_ioapic - register legacy interrupts.
5337 * @instance: Adapter soft state
5339 * Do not enable interrupt, only setup ISRs.
5341 * Return 0 on success.
5344 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5346 struct pci_dev *pdev;
5348 pdev = instance->pdev;
5349 instance->irq_context[0].instance = instance;
5350 instance->irq_context[0].MSIxIndex = 0;
5351 if (request_irq(pci_irq_vector(pdev, 0),
5352 instance->instancet->service_isr, IRQF_SHARED,
5353 "megasas", &instance->irq_context[0])) {
5354 dev_err(&instance->pdev->dev,
5355 "Failed to register IRQ from %s %d\n",
5356 __func__, __LINE__);
5363 * megasas_setup_irqs_msix - register MSI-x interrupts.
5364 * @instance: Adapter soft state
5365 * @is_probe: Driver probe check
5367 * Do not enable interrupt, only setup ISRs.
5369 * Return 0 on success.
5372 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5375 struct pci_dev *pdev;
5377 pdev = instance->pdev;
5380 for (i = 0; i < instance->msix_vectors; i++) {
5381 instance->irq_context[i].instance = instance;
5382 instance->irq_context[i].MSIxIndex = i;
5383 if (request_irq(pci_irq_vector(pdev, i),
5384 instance->instancet->service_isr, 0, "megasas",
5385 &instance->irq_context[i])) {
5386 dev_err(&instance->pdev->dev,
5387 "Failed to register IRQ for vector %d.\n", i);
5388 for (j = 0; j < i; j++)
5389 free_irq(pci_irq_vector(pdev, j),
5390 &instance->irq_context[j]);
5391 /* Retry irq register for IO_APIC*/
5392 instance->msix_vectors = 0;
5393 instance->msix_load_balance = false;
5395 pci_free_irq_vectors(instance->pdev);
5396 return megasas_setup_irqs_ioapic(instance);
5407 * megasas_destroy_irqs- unregister interrupts.
5408 * @instance: Adapter soft state
5412 megasas_destroy_irqs(struct megasas_instance *instance) {
5416 struct megasas_irq_context *irq_ctx;
5418 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5419 if (instance->adapter_type != MFI_SERIES) {
5420 for (i = 0; i < count; i++) {
5421 irq_ctx = &instance->irq_context[i];
5422 irq_poll_disable(&irq_ctx->irqpoll);
5426 if (instance->msix_vectors)
5427 for (i = 0; i < instance->msix_vectors; i++) {
5428 free_irq(pci_irq_vector(instance->pdev, i),
5429 &instance->irq_context[i]);
5432 free_irq(pci_irq_vector(instance->pdev, 0),
5433 &instance->irq_context[0]);
5437 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5438 * @instance: Adapter soft state
5439 * @is_probe: Driver probe check
5441 * Return 0 on success.
5444 megasas_setup_jbod_map(struct megasas_instance *instance)
5447 struct fusion_context *fusion = instance->ctrl_context;
5450 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5451 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5453 if (reset_devices || !fusion ||
5454 !instance->ctrl_info_buf->adapterOperations3.useSeqNumJbodFP) {
5455 dev_info(&instance->pdev->dev,
5456 "Jbod map is not supported %s %d\n",
5457 __func__, __LINE__);
5458 instance->use_seqnum_jbod_fp = false;
5462 if (fusion->pd_seq_sync[0])
5465 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5466 fusion->pd_seq_sync[i] = dma_alloc_coherent
5467 (&instance->pdev->dev, pd_seq_map_sz,
5468 &fusion->pd_seq_phys[i], GFP_KERNEL);
5469 if (!fusion->pd_seq_sync[i]) {
5470 dev_err(&instance->pdev->dev,
5471 "Failed to allocate memory from %s %d\n",
5472 __func__, __LINE__);
5474 dma_free_coherent(&instance->pdev->dev,
5475 pd_seq_map_sz, fusion->pd_seq_sync[0],
5476 fusion->pd_seq_phys[0]);
5477 fusion->pd_seq_sync[0] = NULL;
5479 instance->use_seqnum_jbod_fp = false;
5485 if (!megasas_sync_pd_seq_num(instance, false) &&
5486 !megasas_sync_pd_seq_num(instance, true))
5487 instance->use_seqnum_jbod_fp = true;
5489 instance->use_seqnum_jbod_fp = false;
5492 static void megasas_setup_reply_map(struct megasas_instance *instance)
5494 const struct cpumask *mask;
5495 unsigned int queue, cpu;
5497 for (queue = 0; queue < instance->msix_vectors; queue++) {
5498 mask = pci_irq_get_affinity(instance->pdev, queue);
5502 for_each_cpu(cpu, mask)
5503 instance->reply_map[cpu] = queue;
5508 for_each_possible_cpu(cpu)
5509 instance->reply_map[cpu] = cpu % instance->msix_vectors;
5513 * megasas_get_device_list - Get the PD and LD device list from FW.
5514 * @instance: Adapter soft state
5515 * @return: Success or failure
5517 * Issue DCMDs to Firmware to get the PD and LD list.
5518 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5519 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5522 int megasas_get_device_list(struct megasas_instance *instance)
5524 memset(instance->pd_list, 0,
5525 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5526 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5528 if (instance->enable_fw_dev_list) {
5529 if (megasas_host_device_list_query(instance, true))
5532 if (megasas_get_pd_list(instance) < 0) {
5533 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5537 if (megasas_ld_list_query(instance,
5538 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5539 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5547 * megasas_init_fw - Initializes the FW
5548 * @instance: Adapter soft state
5550 * This is the main function for initializing firmware
5553 static int megasas_init_fw(struct megasas_instance *instance)
5556 u32 max_sectors_2, tmp_sectors, msix_enable;
5557 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5558 resource_size_t base_addr;
5559 struct megasas_ctrl_info *ctrl_info = NULL;
5560 unsigned long bar_list;
5561 int i, j, loop, fw_msix_count = 0;
5562 struct IOV_111 *iovPtr;
5563 struct fusion_context *fusion;
5565 fusion = instance->ctrl_context;
5567 /* Find first memory bar */
5568 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5569 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5570 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5572 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5576 base_addr = pci_resource_start(instance->pdev, instance->bar);
5577 instance->reg_set = ioremap_nocache(base_addr, 8192);
5579 if (!instance->reg_set) {
5580 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5584 if (instance->adapter_type != MFI_SERIES)
5585 instance->instancet = &megasas_instance_template_fusion;
5587 switch (instance->pdev->device) {
5588 case PCI_DEVICE_ID_LSI_SAS1078R:
5589 case PCI_DEVICE_ID_LSI_SAS1078DE:
5590 instance->instancet = &megasas_instance_template_ppc;
5592 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5593 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5594 instance->instancet = &megasas_instance_template_gen2;
5596 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5597 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5598 instance->instancet = &megasas_instance_template_skinny;
5600 case PCI_DEVICE_ID_LSI_SAS1064R:
5601 case PCI_DEVICE_ID_DELL_PERC5:
5603 instance->instancet = &megasas_instance_template_xscale;
5604 instance->pd_list_not_supported = 1;
5609 if (megasas_transition_to_ready(instance, 0)) {
5610 dev_info(&instance->pdev->dev,
5611 "Failed to transition controller to ready from %s!\n",
5613 if (instance->adapter_type != MFI_SERIES) {
5614 status_reg = instance->instancet->read_fw_status_reg(
5616 if (status_reg & MFI_RESET_ADAPTER) {
5617 if (megasas_adp_reset_wait_for_ready
5618 (instance, true, 0) == FAILED)
5619 goto fail_ready_state;
5621 goto fail_ready_state;
5624 atomic_set(&instance->fw_reset_no_pci_access, 1);
5625 instance->instancet->adp_reset
5626 (instance, instance->reg_set);
5627 atomic_set(&instance->fw_reset_no_pci_access, 0);
5629 /*waiting for about 30 second before retry*/
5632 if (megasas_transition_to_ready(instance, 0))
5633 goto fail_ready_state;
5636 dev_info(&instance->pdev->dev,
5637 "FW restarted successfully from %s!\n",
5641 megasas_init_ctrl_params(instance);
5643 if (megasas_set_dma_mask(instance))
5644 goto fail_ready_state;
5646 if (megasas_alloc_ctrl_mem(instance))
5647 goto fail_alloc_dma_buf;
5649 if (megasas_alloc_ctrl_dma_buffers(instance))
5650 goto fail_alloc_dma_buf;
5652 fusion = instance->ctrl_context;
5654 if (instance->adapter_type >= VENTURA_SERIES) {
5656 megasas_readl(instance,
5657 &instance->reg_set->outbound_scratch_pad_2);
5658 instance->max_raid_mapsize = ((scratch_pad_2 >>
5659 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5660 MR_MAX_RAID_MAP_SIZE_MASK);
5663 /* Check if MSI-X is supported while in ready state */
5664 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
5666 if (msix_enable && !msix_disable) {
5667 int irq_flags = PCI_IRQ_MSIX;
5669 scratch_pad_1 = megasas_readl
5670 (instance, &instance->reg_set->outbound_scratch_pad_1);
5671 /* Check max MSI-X vectors */
5673 if (instance->adapter_type == THUNDERBOLT_SERIES) {
5674 /* Thunderbolt Series*/
5675 instance->msix_vectors = (scratch_pad_1
5676 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5677 fw_msix_count = instance->msix_vectors;
5679 instance->msix_vectors = ((scratch_pad_1
5680 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5681 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5684 * For Invader series, > 8 MSI-x vectors
5685 * supported by FW/HW implies combined
5686 * reply queue mode is enabled.
5687 * For Ventura series, > 16 MSI-x vectors
5688 * supported by FW/HW implies combined
5689 * reply queue mode is enabled.
5691 switch (instance->adapter_type) {
5692 case INVADER_SERIES:
5693 if (instance->msix_vectors > 8)
5694 instance->msix_combined = true;
5697 case VENTURA_SERIES:
5698 if (instance->msix_vectors > 16)
5699 instance->msix_combined = true;
5704 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
5707 if (!instance->msix_combined) {
5708 instance->msix_load_balance = true;
5709 instance->smp_affinity_enable = false;
5712 fw_msix_count = instance->msix_vectors;
5713 /* Save 1-15 reply post index address to local memory
5714 * Index 0 is already saved from reg offset
5715 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
5717 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
5718 instance->reply_post_host_index_addr[loop] =
5720 ((u8 __iomem *)instance->reg_set +
5721 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
5726 instance->msix_vectors = min(msix_vectors,
5727 instance->msix_vectors);
5728 } else /* MFI adapters */
5729 instance->msix_vectors = 1;
5731 /* Don't bother allocating more MSI-X vectors than cpus */
5732 instance->msix_vectors = min(instance->msix_vectors,
5733 (unsigned int)num_online_cpus());
5734 if (instance->smp_affinity_enable)
5735 irq_flags |= PCI_IRQ_AFFINITY;
5736 i = pci_alloc_irq_vectors(instance->pdev, 1,
5737 instance->msix_vectors, irq_flags);
5739 instance->msix_vectors = i;
5741 instance->msix_vectors = 0;
5742 instance->msix_load_balance = false;
5746 * MSI-X host index 0 is common for all adapter.
5747 * It is used for all MPT based Adapters.
5749 if (instance->msix_combined) {
5750 instance->reply_post_host_index_addr[0] =
5751 (u32 *)((u8 *)instance->reg_set +
5752 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
5754 instance->reply_post_host_index_addr[0] =
5755 (u32 *)((u8 *)instance->reg_set +
5756 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
5759 if (!instance->msix_vectors) {
5760 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
5762 goto fail_init_adapter;
5765 megasas_setup_reply_map(instance);
5767 dev_info(&instance->pdev->dev,
5768 "firmware supports msix\t: (%d)", fw_msix_count);
5769 dev_info(&instance->pdev->dev,
5770 "current msix/online cpus\t: (%d/%d)\n",
5771 instance->msix_vectors, (unsigned int)num_online_cpus());
5772 dev_info(&instance->pdev->dev,
5773 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
5775 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5776 (unsigned long)instance);
5779 * Below are default value for legacy Firmware.
5780 * non-fusion based controllers
5782 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5783 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5784 /* Get operational params, sge flags, send init cmd to controller */
5785 if (instance->instancet->init_adapter(instance))
5786 goto fail_init_adapter;
5788 if (instance->adapter_type >= VENTURA_SERIES) {
5790 megasas_readl(instance,
5791 &instance->reg_set->outbound_scratch_pad_3);
5792 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
5793 MR_DEFAULT_NVME_PAGE_SHIFT)
5794 instance->nvme_page_size =
5795 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
5797 dev_info(&instance->pdev->dev,
5798 "NVME page size\t: (%d)\n", instance->nvme_page_size);
5801 if (instance->msix_vectors ?
5802 megasas_setup_irqs_msix(instance, 1) :
5803 megasas_setup_irqs_ioapic(instance))
5804 goto fail_init_adapter;
5806 if (instance->adapter_type != MFI_SERIES)
5807 megasas_setup_irq_poll(instance);
5809 instance->instancet->enable_intr(instance);
5811 dev_info(&instance->pdev->dev, "INIT adapter done\n");
5813 megasas_setup_jbod_map(instance);
5815 if (megasas_get_device_list(instance) != SUCCESS) {
5816 dev_err(&instance->pdev->dev,
5817 "%s: megasas_get_device_list failed\n",
5819 goto fail_get_ld_pd_list;
5822 /* stream detection initialization */
5823 if (instance->adapter_type >= VENTURA_SERIES) {
5824 fusion->stream_detect_by_ld =
5825 kcalloc(MAX_LOGICAL_DRIVES_EXT,
5826 sizeof(struct LD_STREAM_DETECT *),
5828 if (!fusion->stream_detect_by_ld) {
5829 dev_err(&instance->pdev->dev,
5830 "unable to allocate stream detection for pool of LDs\n");
5831 goto fail_get_ld_pd_list;
5833 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
5834 fusion->stream_detect_by_ld[i] =
5835 kzalloc(sizeof(struct LD_STREAM_DETECT),
5837 if (!fusion->stream_detect_by_ld[i]) {
5838 dev_err(&instance->pdev->dev,
5839 "unable to allocate stream detect by LD\n ");
5840 for (j = 0; j < i; ++j)
5841 kfree(fusion->stream_detect_by_ld[j]);
5842 kfree(fusion->stream_detect_by_ld);
5843 fusion->stream_detect_by_ld = NULL;
5844 goto fail_get_ld_pd_list;
5846 fusion->stream_detect_by_ld[i]->mru_bit_map
5852 * Compute the max allowed sectors per IO: The controller info has two
5853 * limits on max sectors. Driver should use the minimum of these two.
5855 * 1 << stripe_sz_ops.min = max sectors per strip
5857 * Note that older firmwares ( < FW ver 30) didn't report information
5858 * to calculate max_sectors_1. So the number ended up as zero always.
5861 ctrl_info = instance->ctrl_info_buf;
5863 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
5864 le16_to_cpu(ctrl_info->max_strips_per_io);
5865 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
5867 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
5869 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
5870 instance->passive = ctrl_info->cluster.passive;
5871 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
5872 instance->UnevenSpanSupport =
5873 ctrl_info->adapterOperations2.supportUnevenSpans;
5874 if (instance->UnevenSpanSupport) {
5875 struct fusion_context *fusion = instance->ctrl_context;
5876 if (MR_ValidateMapInfo(instance, instance->map_id))
5877 fusion->fast_path_io = 1;
5879 fusion->fast_path_io = 0;
5882 if (ctrl_info->host_interface.SRIOV) {
5883 instance->requestorId = ctrl_info->iov.requestorId;
5884 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
5885 if (!ctrl_info->adapterOperations2.activePassive)
5886 instance->PlasmaFW111 = 1;
5888 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
5889 instance->PlasmaFW111 ? "1.11" : "new");
5891 if (instance->PlasmaFW111) {
5892 iovPtr = (struct IOV_111 *)
5893 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
5894 instance->requestorId = iovPtr->requestorId;
5897 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
5898 instance->requestorId);
5901 instance->crash_dump_fw_support =
5902 ctrl_info->adapterOperations3.supportCrashDump;
5903 instance->crash_dump_drv_support =
5904 (instance->crash_dump_fw_support &&
5905 instance->crash_dump_buf);
5906 if (instance->crash_dump_drv_support)
5907 megasas_set_crash_dump_params(instance,
5908 MR_CRASH_BUF_TURN_OFF);
5911 if (instance->crash_dump_buf)
5912 dma_free_coherent(&instance->pdev->dev,
5914 instance->crash_dump_buf,
5915 instance->crash_dump_h);
5916 instance->crash_dump_buf = NULL;
5919 if (instance->snapdump_wait_time) {
5920 megasas_get_snapdump_properties(instance);
5921 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
5922 instance->snapdump_wait_time);
5925 dev_info(&instance->pdev->dev,
5926 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
5927 le16_to_cpu(ctrl_info->pci.vendor_id),
5928 le16_to_cpu(ctrl_info->pci.device_id),
5929 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
5930 le16_to_cpu(ctrl_info->pci.sub_device_id));
5931 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
5932 instance->UnevenSpanSupport ? "yes" : "no");
5933 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
5934 instance->crash_dump_drv_support ? "yes" : "no");
5935 dev_info(&instance->pdev->dev, "jbod sync map : %s\n",
5936 instance->use_seqnum_jbod_fp ? "yes" : "no");
5938 instance->max_sectors_per_req = instance->max_num_sge *
5939 SGE_BUFFER_SIZE / 512;
5940 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
5941 instance->max_sectors_per_req = tmp_sectors;
5943 /* Check for valid throttlequeuedepth module parameter */
5944 if (throttlequeuedepth &&
5945 throttlequeuedepth <= instance->max_scsi_cmds)
5946 instance->throttlequeuedepth = throttlequeuedepth;
5948 instance->throttlequeuedepth =
5949 MEGASAS_THROTTLE_QUEUE_DEPTH;
5951 if ((resetwaittime < 1) ||
5952 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
5953 resetwaittime = MEGASAS_RESET_WAIT_TIME;
5955 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
5956 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
5958 /* Launch SR-IOV heartbeat timer */
5959 if (instance->requestorId) {
5960 if (!megasas_sriov_start_heartbeat(instance, 1)) {
5961 megasas_start_timer(instance);
5963 instance->skip_heartbeat_timer_del = 1;
5964 goto fail_get_ld_pd_list;
5969 * Create and start watchdog thread which will monitor
5970 * controller state every 1 sec and trigger OCR when
5971 * it enters fault state
5973 if (instance->adapter_type != MFI_SERIES)
5974 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
5975 goto fail_start_watchdog;
5979 fail_start_watchdog:
5980 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5981 del_timer_sync(&instance->sriov_heartbeat_timer);
5982 fail_get_ld_pd_list:
5983 instance->instancet->disable_intr(instance);
5984 megasas_destroy_irqs(instance);
5986 if (instance->msix_vectors)
5987 pci_free_irq_vectors(instance->pdev);
5988 instance->msix_vectors = 0;
5990 megasas_free_ctrl_dma_buffers(instance);
5991 megasas_free_ctrl_mem(instance);
5993 iounmap(instance->reg_set);
5996 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
5998 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
5999 __func__, __LINE__);
6004 * megasas_release_mfi - Reverses the FW initialization
6005 * @instance: Adapter soft state
6007 static void megasas_release_mfi(struct megasas_instance *instance)
6009 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6011 if (instance->reply_queue)
6012 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6013 instance->reply_queue, instance->reply_queue_h);
6015 megasas_free_cmds(instance);
6017 iounmap(instance->reg_set);
6019 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6023 * megasas_get_seq_num - Gets latest event sequence numbers
6024 * @instance: Adapter soft state
6025 * @eli: FW event log sequence numbers information
6027 * FW maintains a log of all events in a non-volatile area. Upper layers would
6028 * usually find out the latest sequence number of the events, the seq number at
6029 * the boot etc. They would "read" all the events below the latest seq number
6030 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6031 * number), they would subsribe to AEN (asynchronous event notification) and
6032 * wait for the events to happen.
6035 megasas_get_seq_num(struct megasas_instance *instance,
6036 struct megasas_evt_log_info *eli)
6038 struct megasas_cmd *cmd;
6039 struct megasas_dcmd_frame *dcmd;
6040 struct megasas_evt_log_info *el_info;
6041 dma_addr_t el_info_h = 0;
6044 cmd = megasas_get_cmd(instance);
6050 dcmd = &cmd->frame->dcmd;
6051 el_info = dma_alloc_coherent(&instance->pdev->dev,
6052 sizeof(struct megasas_evt_log_info),
6053 &el_info_h, GFP_KERNEL);
6055 megasas_return_cmd(instance, cmd);
6059 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6061 dcmd->cmd = MFI_CMD_DCMD;
6062 dcmd->cmd_status = 0x0;
6063 dcmd->sge_count = 1;
6064 dcmd->flags = MFI_FRAME_DIR_READ;
6067 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6068 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6070 megasas_set_dma_settings(instance, dcmd, el_info_h,
6071 sizeof(struct megasas_evt_log_info));
6073 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6074 if (ret != DCMD_SUCCESS) {
6075 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6076 __func__, __LINE__);
6081 * Copy the data back into callers buffer
6083 eli->newest_seq_num = el_info->newest_seq_num;
6084 eli->oldest_seq_num = el_info->oldest_seq_num;
6085 eli->clear_seq_num = el_info->clear_seq_num;
6086 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6087 eli->boot_seq_num = el_info->boot_seq_num;
6090 dma_free_coherent(&instance->pdev->dev,
6091 sizeof(struct megasas_evt_log_info),
6092 el_info, el_info_h);
6094 megasas_return_cmd(instance, cmd);
6100 * megasas_register_aen - Registers for asynchronous event notification
6101 * @instance: Adapter soft state
6102 * @seq_num: The starting sequence number
6103 * @class_locale: Class of the event
6105 * This function subscribes for AEN for events beyond the @seq_num. It requests
6106 * to be notified if and only if the event is of type @class_locale
6109 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6110 u32 class_locale_word)
6113 struct megasas_cmd *cmd;
6114 struct megasas_dcmd_frame *dcmd;
6115 union megasas_evt_class_locale curr_aen;
6116 union megasas_evt_class_locale prev_aen;
6119 * If there an AEN pending already (aen_cmd), check if the
6120 * class_locale of that pending AEN is inclusive of the new
6121 * AEN request we currently have. If it is, then we don't have
6122 * to do anything. In other words, whichever events the current
6123 * AEN request is subscribing to, have already been subscribed
6126 * If the old_cmd is _not_ inclusive, then we have to abort
6127 * that command, form a class_locale that is superset of both
6128 * old and current and re-issue to the FW
6131 curr_aen.word = class_locale_word;
6133 if (instance->aen_cmd) {
6136 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6138 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6139 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6140 dev_info(&instance->pdev->dev,
6141 "%s %d out of range class %d send by application\n",
6142 __func__, __LINE__, curr_aen.members.class);
6147 * A class whose enum value is smaller is inclusive of all
6148 * higher values. If a PROGRESS (= -1) was previously
6149 * registered, then a new registration requests for higher
6150 * classes need not be sent to FW. They are automatically
6153 * Locale numbers don't have such hierarchy. They are bitmap
6156 if ((prev_aen.members.class <= curr_aen.members.class) &&
6157 !((prev_aen.members.locale & curr_aen.members.locale) ^
6158 curr_aen.members.locale)) {
6160 * Previously issued event registration includes
6161 * current request. Nothing to do.
6165 curr_aen.members.locale |= prev_aen.members.locale;
6167 if (prev_aen.members.class < curr_aen.members.class)
6168 curr_aen.members.class = prev_aen.members.class;
6170 instance->aen_cmd->abort_aen = 1;
6171 ret_val = megasas_issue_blocked_abort_cmd(instance,
6176 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6177 "previous AEN command\n");
6183 cmd = megasas_get_cmd(instance);
6188 dcmd = &cmd->frame->dcmd;
6190 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6193 * Prepare DCMD for aen registration
6195 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6197 dcmd->cmd = MFI_CMD_DCMD;
6198 dcmd->cmd_status = 0x0;
6199 dcmd->sge_count = 1;
6200 dcmd->flags = MFI_FRAME_DIR_READ;
6203 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6204 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6205 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6206 instance->last_seq_num = seq_num;
6207 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6209 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6210 sizeof(struct megasas_evt_detail));
6212 if (instance->aen_cmd != NULL) {
6213 megasas_return_cmd(instance, cmd);
6218 * Store reference to the cmd used to register for AEN. When an
6219 * application wants us to register for AEN, we have to abort this
6220 * cmd and re-register with a new EVENT LOCALE supplied by that app
6222 instance->aen_cmd = cmd;
6225 * Issue the aen registration frame
6227 instance->instancet->issue_dcmd(instance, cmd);
6232 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6234 * This DCMD will fetch few properties of LD/system PD defined
6235 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6237 * DCMD send by drivers whenever new target is added to the OS.
6239 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6240 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6241 * 0 = system PD, 1 = LD.
6242 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6243 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6245 * @instance: Adapter soft state
6246 * @sdev: OS provided scsi device
6248 * Returns 0 on success non-zero on failure.
6251 megasas_get_target_prop(struct megasas_instance *instance,
6252 struct scsi_device *sdev)
6255 struct megasas_cmd *cmd;
6256 struct megasas_dcmd_frame *dcmd;
6257 u16 targetId = (sdev->channel % 2) + sdev->id;
6259 cmd = megasas_get_cmd(instance);
6262 dev_err(&instance->pdev->dev,
6263 "Failed to get cmd %s\n", __func__);
6267 dcmd = &cmd->frame->dcmd;
6269 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6270 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6271 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6273 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6274 dcmd->cmd = MFI_CMD_DCMD;
6275 dcmd->cmd_status = 0xFF;
6276 dcmd->sge_count = 1;
6277 dcmd->flags = MFI_FRAME_DIR_READ;
6280 dcmd->data_xfer_len =
6281 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6282 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6284 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6285 sizeof(struct MR_TARGET_PROPERTIES));
6287 if ((instance->adapter_type != MFI_SERIES) &&
6288 !instance->mask_interrupts)
6289 ret = megasas_issue_blocked_cmd(instance,
6290 cmd, MFI_IO_TIMEOUT_SECS);
6292 ret = megasas_issue_polled(instance, cmd);
6296 switch (dcmd_timeout_ocr_possible(instance)) {
6298 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6299 megasas_reset_fusion(instance->host,
6300 MFI_IO_TIMEOUT_OCR);
6303 megaraid_sas_kill_hba(instance);
6305 case IGNORE_TIMEOUT:
6306 dev_info(&instance->pdev->dev,
6307 "Ignore DCMD timeout: %s %d\n",
6308 __func__, __LINE__);
6314 megasas_return_cmd(instance, cmd);
6316 if (ret != DCMD_SUCCESS)
6317 dev_err(&instance->pdev->dev,
6318 "return from %s %d return value %d\n",
6319 __func__, __LINE__, ret);
6325 * megasas_start_aen - Subscribes to AEN during driver load time
6326 * @instance: Adapter soft state
6328 static int megasas_start_aen(struct megasas_instance *instance)
6330 struct megasas_evt_log_info eli;
6331 union megasas_evt_class_locale class_locale;
6334 * Get the latest sequence number from FW
6336 memset(&eli, 0, sizeof(eli));
6338 if (megasas_get_seq_num(instance, &eli))
6342 * Register AEN with FW for latest sequence number plus 1
6344 class_locale.members.reserved = 0;
6345 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6346 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6348 return megasas_register_aen(instance,
6349 le32_to_cpu(eli.newest_seq_num) + 1,
6354 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6355 * @instance: Adapter soft state
6357 static int megasas_io_attach(struct megasas_instance *instance)
6359 struct Scsi_Host *host = instance->host;
6362 * Export parameters required by SCSI mid-layer
6364 host->unique_id = instance->unique_id;
6365 host->can_queue = instance->max_scsi_cmds;
6366 host->this_id = instance->init_id;
6367 host->sg_tablesize = instance->max_num_sge;
6369 if (instance->fw_support_ieee)
6370 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6373 * Check if the module parameter value for max_sectors can be used
6375 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6376 instance->max_sectors_per_req = max_sectors;
6379 if (((instance->pdev->device ==
6380 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6381 (instance->pdev->device ==
6382 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6383 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6384 instance->max_sectors_per_req = max_sectors;
6386 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6387 "and <= %d (or < 1MB for GEN2 controller)\n",
6388 instance->max_sectors_per_req);
6393 host->max_sectors = instance->max_sectors_per_req;
6394 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6395 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6396 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6397 host->max_lun = MEGASAS_MAX_LUN;
6398 host->max_cmd_len = 16;
6401 * Notify the mid-layer about the new controller
6403 if (scsi_add_host(host, &instance->pdev->dev)) {
6404 dev_err(&instance->pdev->dev,
6405 "Failed to add host from %s %d\n",
6406 __func__, __LINE__);
6414 * megasas_set_dma_mask - Set DMA mask for supported controllers
6416 * @instance: Adapter soft state
6419 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6422 * By default, driver/FW will operate in 32bit DMA addresses
6423 * for consistent DMA mapping but if 32 bit consistent
6424 * DMA mask fails, driver will try with 63 bit consistent
6425 * mask provided FW is true 63bit DMA capable
6427 * For older controllers(Thunderbolt and MFI based adapters)-
6428 * driver/FW will operate in 32 bit consistent DMA addresses.
6431 megasas_set_dma_mask(struct megasas_instance *instance)
6433 u64 consistent_mask;
6434 struct pci_dev *pdev;
6437 pdev = instance->pdev;
6438 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6439 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6442 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6443 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6444 goto fail_set_dma_mask;
6446 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6447 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6448 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6450 * If 32 bit DMA mask fails, then try for 64 bit mask
6451 * for FW capable of handling 64 bit DMA.
6453 scratch_pad_1 = megasas_readl
6454 (instance, &instance->reg_set->outbound_scratch_pad_1);
6456 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6457 goto fail_set_dma_mask;
6458 else if (dma_set_mask_and_coherent(&pdev->dev,
6460 goto fail_set_dma_mask;
6462 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6463 goto fail_set_dma_mask;
6465 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6466 instance->consistent_mask_64bit = false;
6468 instance->consistent_mask_64bit = true;
6470 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6471 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6472 (instance->consistent_mask_64bit ? "63" : "32"));
6477 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6483 * megasas_set_adapter_type - Set adapter type.
6484 * Supported controllers can be divided in
6485 * different categories-
6486 * enum MR_ADAPTER_TYPE {
6488 * THUNDERBOLT_SERIES = 2,
6489 * INVADER_SERIES = 3,
6490 * VENTURA_SERIES = 4,
6493 * @instance: Adapter soft state
6496 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6498 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6499 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6500 instance->adapter_type = MFI_SERIES;
6502 switch (instance->pdev->device) {
6503 case PCI_DEVICE_ID_LSI_AERO_10E1:
6504 case PCI_DEVICE_ID_LSI_AERO_10E2:
6505 case PCI_DEVICE_ID_LSI_AERO_10E5:
6506 case PCI_DEVICE_ID_LSI_AERO_10E6:
6507 instance->adapter_type = AERO_SERIES;
6509 case PCI_DEVICE_ID_LSI_VENTURA:
6510 case PCI_DEVICE_ID_LSI_CRUSADER:
6511 case PCI_DEVICE_ID_LSI_HARPOON:
6512 case PCI_DEVICE_ID_LSI_TOMCAT:
6513 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6514 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6515 instance->adapter_type = VENTURA_SERIES;
6517 case PCI_DEVICE_ID_LSI_FUSION:
6518 case PCI_DEVICE_ID_LSI_PLASMA:
6519 instance->adapter_type = THUNDERBOLT_SERIES;
6521 case PCI_DEVICE_ID_LSI_INVADER:
6522 case PCI_DEVICE_ID_LSI_INTRUDER:
6523 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6524 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6525 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6526 case PCI_DEVICE_ID_LSI_FURY:
6527 instance->adapter_type = INVADER_SERIES;
6529 default: /* For all other supported controllers */
6530 instance->adapter_type = MFI_SERIES;
6536 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6538 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6539 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6540 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6541 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
6543 if (!instance->producer || !instance->consumer) {
6544 dev_err(&instance->pdev->dev,
6545 "Failed to allocate memory for producer, consumer\n");
6549 *instance->producer = 0;
6550 *instance->consumer = 0;
6555 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6556 * structures which are not common across MFI
6557 * adapters and fusion adapters.
6558 * For MFI based adapters, allocate producer and
6559 * consumer buffers. For fusion adapters, allocate
6560 * memory for fusion context.
6561 * @instance: Adapter soft state
6562 * return: 0 for SUCCESS
6564 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6566 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
6568 if (!instance->reply_map)
6571 switch (instance->adapter_type) {
6573 if (megasas_alloc_mfi_ctrl_mem(instance))
6577 case VENTURA_SERIES:
6578 case THUNDERBOLT_SERIES:
6579 case INVADER_SERIES:
6580 if (megasas_alloc_fusion_context(instance))
6587 kfree(instance->reply_map);
6588 instance->reply_map = NULL;
6593 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
6594 * producer, consumer buffers for MFI adapters
6596 * @instance - Adapter soft instance
6599 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6601 kfree(instance->reply_map);
6602 if (instance->adapter_type == MFI_SERIES) {
6603 if (instance->producer)
6604 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6606 instance->producer_h);
6607 if (instance->consumer)
6608 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6610 instance->consumer_h);
6612 megasas_free_fusion_context(instance);
6617 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
6620 * @instance- Adapter soft instance
6621 * @return- O for SUCCESS
6624 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
6626 struct pci_dev *pdev = instance->pdev;
6627 struct fusion_context *fusion = instance->ctrl_context;
6629 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
6630 sizeof(struct megasas_evt_detail),
6631 &instance->evt_detail_h, GFP_KERNEL);
6633 if (!instance->evt_detail) {
6634 dev_err(&instance->pdev->dev,
6635 "Failed to allocate event detail buffer\n");
6640 fusion->ioc_init_request =
6641 dma_alloc_coherent(&pdev->dev,
6642 sizeof(struct MPI2_IOC_INIT_REQUEST),
6643 &fusion->ioc_init_request_phys,
6646 if (!fusion->ioc_init_request) {
6648 "Failed to allocate PD list buffer\n");
6652 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
6653 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6654 &instance->snapdump_prop_h, GFP_KERNEL);
6656 if (!instance->snapdump_prop)
6658 "Failed to allocate snapdump properties buffer\n");
6660 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
6661 HOST_DEVICE_LIST_SZ,
6662 &instance->host_device_list_buf_h,
6665 if (!instance->host_device_list_buf) {
6667 "Failed to allocate targetid list buffer\n");
6673 instance->pd_list_buf =
6674 dma_alloc_coherent(&pdev->dev,
6675 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6676 &instance->pd_list_buf_h, GFP_KERNEL);
6678 if (!instance->pd_list_buf) {
6679 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
6683 instance->ctrl_info_buf =
6684 dma_alloc_coherent(&pdev->dev,
6685 sizeof(struct megasas_ctrl_info),
6686 &instance->ctrl_info_buf_h, GFP_KERNEL);
6688 if (!instance->ctrl_info_buf) {
6690 "Failed to allocate controller info buffer\n");
6694 instance->ld_list_buf =
6695 dma_alloc_coherent(&pdev->dev,
6696 sizeof(struct MR_LD_LIST),
6697 &instance->ld_list_buf_h, GFP_KERNEL);
6699 if (!instance->ld_list_buf) {
6700 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
6704 instance->ld_targetid_list_buf =
6705 dma_alloc_coherent(&pdev->dev,
6706 sizeof(struct MR_LD_TARGETID_LIST),
6707 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
6709 if (!instance->ld_targetid_list_buf) {
6711 "Failed to allocate LD targetid list buffer\n");
6715 if (!reset_devices) {
6716 instance->system_info_buf =
6717 dma_alloc_coherent(&pdev->dev,
6718 sizeof(struct MR_DRV_SYSTEM_INFO),
6719 &instance->system_info_h, GFP_KERNEL);
6721 dma_alloc_coherent(&pdev->dev,
6722 sizeof(struct MR_PD_INFO),
6723 &instance->pd_info_h, GFP_KERNEL);
6724 instance->tgt_prop =
6725 dma_alloc_coherent(&pdev->dev,
6726 sizeof(struct MR_TARGET_PROPERTIES),
6727 &instance->tgt_prop_h, GFP_KERNEL);
6728 instance->crash_dump_buf =
6729 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6730 &instance->crash_dump_h, GFP_KERNEL);
6732 if (!instance->system_info_buf)
6733 dev_err(&instance->pdev->dev,
6734 "Failed to allocate system info buffer\n");
6736 if (!instance->pd_info)
6737 dev_err(&instance->pdev->dev,
6738 "Failed to allocate pd_info buffer\n");
6740 if (!instance->tgt_prop)
6741 dev_err(&instance->pdev->dev,
6742 "Failed to allocate tgt_prop buffer\n");
6744 if (!instance->crash_dump_buf)
6745 dev_err(&instance->pdev->dev,
6746 "Failed to allocate crash dump buffer\n");
6753 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
6754 * during driver load time
6756 * @instance- Adapter soft instance
6760 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
6762 struct pci_dev *pdev = instance->pdev;
6763 struct fusion_context *fusion = instance->ctrl_context;
6765 if (instance->evt_detail)
6766 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
6767 instance->evt_detail,
6768 instance->evt_detail_h);
6770 if (fusion && fusion->ioc_init_request)
6771 dma_free_coherent(&pdev->dev,
6772 sizeof(struct MPI2_IOC_INIT_REQUEST),
6773 fusion->ioc_init_request,
6774 fusion->ioc_init_request_phys);
6776 if (instance->pd_list_buf)
6777 dma_free_coherent(&pdev->dev,
6778 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
6779 instance->pd_list_buf,
6780 instance->pd_list_buf_h);
6782 if (instance->ld_list_buf)
6783 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
6784 instance->ld_list_buf,
6785 instance->ld_list_buf_h);
6787 if (instance->ld_targetid_list_buf)
6788 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
6789 instance->ld_targetid_list_buf,
6790 instance->ld_targetid_list_buf_h);
6792 if (instance->ctrl_info_buf)
6793 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
6794 instance->ctrl_info_buf,
6795 instance->ctrl_info_buf_h);
6797 if (instance->system_info_buf)
6798 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
6799 instance->system_info_buf,
6800 instance->system_info_h);
6802 if (instance->pd_info)
6803 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
6804 instance->pd_info, instance->pd_info_h);
6806 if (instance->tgt_prop)
6807 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
6808 instance->tgt_prop, instance->tgt_prop_h);
6810 if (instance->crash_dump_buf)
6811 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
6812 instance->crash_dump_buf,
6813 instance->crash_dump_h);
6815 if (instance->snapdump_prop)
6816 dma_free_coherent(&pdev->dev,
6817 sizeof(struct MR_SNAPDUMP_PROPERTIES),
6818 instance->snapdump_prop,
6819 instance->snapdump_prop_h);
6821 if (instance->host_device_list_buf)
6822 dma_free_coherent(&pdev->dev,
6823 HOST_DEVICE_LIST_SZ,
6824 instance->host_device_list_buf,
6825 instance->host_device_list_buf_h);
6830 * megasas_init_ctrl_params - Initialize controller's instance
6831 * parameters before FW init
6832 * @instance - Adapter soft instance
6835 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
6837 instance->fw_crash_state = UNAVAILABLE;
6839 megasas_poll_wait_aen = 0;
6840 instance->issuepend_done = 1;
6841 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
6844 * Initialize locks and queues
6846 INIT_LIST_HEAD(&instance->cmd_pool);
6847 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
6849 atomic_set(&instance->fw_outstanding, 0);
6850 atomic64_set(&instance->total_io_count, 0);
6852 init_waitqueue_head(&instance->int_cmd_wait_q);
6853 init_waitqueue_head(&instance->abort_cmd_wait_q);
6855 spin_lock_init(&instance->crashdump_lock);
6856 spin_lock_init(&instance->mfi_pool_lock);
6857 spin_lock_init(&instance->hba_lock);
6858 spin_lock_init(&instance->stream_lock);
6859 spin_lock_init(&instance->completion_lock);
6861 mutex_init(&instance->reset_mutex);
6863 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
6864 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
6865 instance->flag_ieee = 1;
6867 megasas_dbg_lvl = 0;
6869 instance->unload = 1;
6870 instance->last_time = 0;
6871 instance->disableOnlineCtrlReset = 1;
6872 instance->UnevenSpanSupport = 0;
6873 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
6874 instance->msix_load_balance = false;
6876 if (instance->adapter_type != MFI_SERIES)
6877 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
6879 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
6883 * megasas_probe_one - PCI hotplug entry point
6884 * @pdev: PCI device structure
6885 * @id: PCI ids of supported hotplugged adapter
6887 static int megasas_probe_one(struct pci_dev *pdev,
6888 const struct pci_device_id *id)
6891 struct Scsi_Host *host;
6892 struct megasas_instance *instance;
6895 switch (pdev->device) {
6896 case PCI_DEVICE_ID_LSI_AERO_10E1:
6897 case PCI_DEVICE_ID_LSI_AERO_10E5:
6898 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
6902 /* Reset MSI-X in the kdump kernel */
6903 if (reset_devices) {
6904 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
6906 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
6908 if (control & PCI_MSIX_FLAGS_ENABLE) {
6909 dev_info(&pdev->dev, "resetting MSI-X\n");
6910 pci_write_config_word(pdev,
6911 pos + PCI_MSIX_FLAGS,
6913 ~PCI_MSIX_FLAGS_ENABLE);
6919 * PCI prepping: enable device set bus mastering and dma mask
6921 rval = pci_enable_device_mem(pdev);
6927 pci_set_master(pdev);
6929 host = scsi_host_alloc(&megasas_template,
6930 sizeof(struct megasas_instance));
6933 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
6934 goto fail_alloc_instance;
6937 instance = (struct megasas_instance *)host->hostdata;
6938 memset(instance, 0, sizeof(*instance));
6939 atomic_set(&instance->fw_reset_no_pci_access, 0);
6942 * Initialize PCI related and misc parameters
6944 instance->pdev = pdev;
6945 instance->host = host;
6946 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
6947 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
6949 megasas_set_adapter_type(instance);
6952 * Initialize MFI Firmware
6954 if (megasas_init_fw(instance))
6957 if (instance->requestorId) {
6958 if (instance->PlasmaFW111) {
6959 instance->vf_affiliation_111 =
6960 dma_alloc_coherent(&pdev->dev,
6961 sizeof(struct MR_LD_VF_AFFILIATION_111),
6962 &instance->vf_affiliation_111_h,
6964 if (!instance->vf_affiliation_111)
6965 dev_warn(&pdev->dev, "Can't allocate "
6966 "memory for VF affiliation buffer\n");
6968 instance->vf_affiliation =
6969 dma_alloc_coherent(&pdev->dev,
6970 (MAX_LOGICAL_DRIVES + 1) *
6971 sizeof(struct MR_LD_VF_AFFILIATION),
6972 &instance->vf_affiliation_h,
6974 if (!instance->vf_affiliation)
6975 dev_warn(&pdev->dev, "Can't allocate "
6976 "memory for VF affiliation buffer\n");
6981 * Store instance in PCI softstate
6983 pci_set_drvdata(pdev, instance);
6986 * Add this controller to megasas_mgmt_info structure so that it
6987 * can be exported to management applications
6989 megasas_mgmt_info.count++;
6990 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
6991 megasas_mgmt_info.max_index++;
6994 * Register with SCSI mid-layer
6996 if (megasas_io_attach(instance))
6997 goto fail_io_attach;
6999 instance->unload = 0;
7001 * Trigger SCSI to scan our drives
7003 if (!instance->enable_fw_dev_list ||
7004 (instance->host_device_list_buf->count > 0))
7005 scsi_scan_host(host);
7008 * Initiate AEN (Asynchronous Event Notification)
7010 if (megasas_start_aen(instance)) {
7011 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7012 goto fail_start_aen;
7015 /* Get current SR-IOV LD/VF affiliation */
7016 if (instance->requestorId)
7017 megasas_get_ld_vf_affiliation(instance, 1);
7023 megasas_mgmt_info.count--;
7024 megasas_mgmt_info.max_index--;
7025 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7027 instance->instancet->disable_intr(instance);
7028 megasas_destroy_irqs(instance);
7030 if (instance->adapter_type != MFI_SERIES)
7031 megasas_release_fusion(instance);
7033 megasas_release_mfi(instance);
7034 if (instance->msix_vectors)
7035 pci_free_irq_vectors(instance->pdev);
7037 scsi_host_put(host);
7038 fail_alloc_instance:
7039 pci_disable_device(pdev);
7045 * megasas_flush_cache - Requests FW to flush all its caches
7046 * @instance: Adapter soft state
7048 static void megasas_flush_cache(struct megasas_instance *instance)
7050 struct megasas_cmd *cmd;
7051 struct megasas_dcmd_frame *dcmd;
7053 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7056 cmd = megasas_get_cmd(instance);
7061 dcmd = &cmd->frame->dcmd;
7063 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7065 dcmd->cmd = MFI_CMD_DCMD;
7066 dcmd->cmd_status = 0x0;
7067 dcmd->sge_count = 0;
7068 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7071 dcmd->data_xfer_len = 0;
7072 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7073 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7075 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7077 dev_err(&instance->pdev->dev,
7078 "return from %s %d\n", __func__, __LINE__);
7082 megasas_return_cmd(instance, cmd);
7086 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7087 * @instance: Adapter soft state
7088 * @opcode: Shutdown/Hibernate
7090 static void megasas_shutdown_controller(struct megasas_instance *instance,
7093 struct megasas_cmd *cmd;
7094 struct megasas_dcmd_frame *dcmd;
7096 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7099 cmd = megasas_get_cmd(instance);
7104 if (instance->aen_cmd)
7105 megasas_issue_blocked_abort_cmd(instance,
7106 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7107 if (instance->map_update_cmd)
7108 megasas_issue_blocked_abort_cmd(instance,
7109 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7110 if (instance->jbod_seq_cmd)
7111 megasas_issue_blocked_abort_cmd(instance,
7112 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7114 dcmd = &cmd->frame->dcmd;
7116 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7118 dcmd->cmd = MFI_CMD_DCMD;
7119 dcmd->cmd_status = 0x0;
7120 dcmd->sge_count = 0;
7121 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7124 dcmd->data_xfer_len = 0;
7125 dcmd->opcode = cpu_to_le32(opcode);
7127 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7129 dev_err(&instance->pdev->dev,
7130 "return from %s %d\n", __func__, __LINE__);
7134 megasas_return_cmd(instance, cmd);
7139 * megasas_suspend - driver suspend entry point
7140 * @pdev: PCI device structure
7141 * @state: PCI power state to suspend routine
7144 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7146 struct Scsi_Host *host;
7147 struct megasas_instance *instance;
7149 instance = pci_get_drvdata(pdev);
7150 host = instance->host;
7151 instance->unload = 1;
7153 /* Shutdown SR-IOV heartbeat timer */
7154 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7155 del_timer_sync(&instance->sriov_heartbeat_timer);
7157 /* Stop the FW fault detection watchdog */
7158 if (instance->adapter_type != MFI_SERIES)
7159 megasas_fusion_stop_watchdog(instance);
7161 megasas_flush_cache(instance);
7162 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7164 /* cancel the delayed work if this work still in queue */
7165 if (instance->ev != NULL) {
7166 struct megasas_aen_event *ev = instance->ev;
7167 cancel_delayed_work_sync(&ev->hotplug_work);
7168 instance->ev = NULL;
7171 tasklet_kill(&instance->isr_tasklet);
7173 pci_set_drvdata(instance->pdev, instance);
7174 instance->instancet->disable_intr(instance);
7176 megasas_destroy_irqs(instance);
7178 if (instance->msix_vectors)
7179 pci_free_irq_vectors(instance->pdev);
7181 pci_save_state(pdev);
7182 pci_disable_device(pdev);
7184 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7190 * megasas_resume- driver resume entry point
7191 * @pdev: PCI device structure
7194 megasas_resume(struct pci_dev *pdev)
7197 struct Scsi_Host *host;
7198 struct megasas_instance *instance;
7199 int irq_flags = PCI_IRQ_LEGACY;
7201 instance = pci_get_drvdata(pdev);
7202 host = instance->host;
7203 pci_set_power_state(pdev, PCI_D0);
7204 pci_enable_wake(pdev, PCI_D0, 0);
7205 pci_restore_state(pdev);
7208 * PCI prepping: enable device set bus mastering and dma mask
7210 rval = pci_enable_device_mem(pdev);
7213 dev_err(&pdev->dev, "Enable device failed\n");
7217 pci_set_master(pdev);
7220 * We expect the FW state to be READY
7222 if (megasas_transition_to_ready(instance, 0))
7223 goto fail_ready_state;
7225 if (megasas_set_dma_mask(instance))
7226 goto fail_set_dma_mask;
7229 * Initialize MFI Firmware
7232 atomic_set(&instance->fw_outstanding, 0);
7233 atomic_set(&instance->ldio_outstanding, 0);
7235 /* Now re-enable MSI-X */
7236 if (instance->msix_vectors) {
7237 irq_flags = PCI_IRQ_MSIX;
7238 if (instance->smp_affinity_enable)
7239 irq_flags |= PCI_IRQ_AFFINITY;
7241 rval = pci_alloc_irq_vectors(instance->pdev, 1,
7242 instance->msix_vectors ?
7243 instance->msix_vectors : 1, irq_flags);
7245 goto fail_reenable_msix;
7247 megasas_setup_reply_map(instance);
7249 if (instance->adapter_type != MFI_SERIES) {
7250 megasas_reset_reply_desc(instance);
7251 if (megasas_ioc_init_fusion(instance)) {
7252 megasas_free_cmds(instance);
7253 megasas_free_cmds_fusion(instance);
7256 if (!megasas_get_map_info(instance))
7257 megasas_sync_map_info(instance);
7259 *instance->producer = 0;
7260 *instance->consumer = 0;
7261 if (megasas_issue_init_mfi(instance))
7265 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7268 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7269 (unsigned long)instance);
7271 if (instance->msix_vectors ?
7272 megasas_setup_irqs_msix(instance, 0) :
7273 megasas_setup_irqs_ioapic(instance))
7276 if (instance->adapter_type != MFI_SERIES)
7277 megasas_setup_irq_poll(instance);
7279 /* Re-launch SR-IOV heartbeat timer */
7280 if (instance->requestorId) {
7281 if (!megasas_sriov_start_heartbeat(instance, 0))
7282 megasas_start_timer(instance);
7284 instance->skip_heartbeat_timer_del = 1;
7289 instance->instancet->enable_intr(instance);
7290 megasas_setup_jbod_map(instance);
7291 instance->unload = 0;
7294 * Initiate AEN (Asynchronous Event Notification)
7296 if (megasas_start_aen(instance))
7297 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7299 /* Re-launch FW fault watchdog */
7300 if (instance->adapter_type != MFI_SERIES)
7301 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7302 goto fail_start_watchdog;
7306 fail_start_watchdog:
7307 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7308 del_timer_sync(&instance->sriov_heartbeat_timer);
7310 megasas_free_ctrl_dma_buffers(instance);
7311 megasas_free_ctrl_mem(instance);
7312 scsi_host_put(host);
7318 pci_disable_device(pdev);
7323 #define megasas_suspend NULL
7324 #define megasas_resume NULL
7328 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7330 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7334 for (i = 0; i < wait_time; i++) {
7335 adp_state = atomic_read(&instance->adprecovery);
7336 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7337 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7340 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7341 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7346 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7347 dev_info(&instance->pdev->dev,
7348 "%s HBA failed to become operational, adp_state %d\n",
7349 __func__, adp_state);
7357 * megasas_detach_one - PCI hot"un"plug entry point
7358 * @pdev: PCI device structure
7360 static void megasas_detach_one(struct pci_dev *pdev)
7363 struct Scsi_Host *host;
7364 struct megasas_instance *instance;
7365 struct fusion_context *fusion;
7368 instance = pci_get_drvdata(pdev);
7369 host = instance->host;
7370 fusion = instance->ctrl_context;
7372 /* Shutdown SR-IOV heartbeat timer */
7373 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7374 del_timer_sync(&instance->sriov_heartbeat_timer);
7376 /* Stop the FW fault detection watchdog */
7377 if (instance->adapter_type != MFI_SERIES)
7378 megasas_fusion_stop_watchdog(instance);
7380 if (instance->fw_crash_state != UNAVAILABLE)
7381 megasas_free_host_crash_buffer(instance);
7382 scsi_remove_host(instance->host);
7383 instance->unload = 1;
7385 if (megasas_wait_for_adapter_operational(instance))
7386 goto skip_firing_dcmds;
7388 megasas_flush_cache(instance);
7389 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7392 /* cancel the delayed work if this work still in queue*/
7393 if (instance->ev != NULL) {
7394 struct megasas_aen_event *ev = instance->ev;
7395 cancel_delayed_work_sync(&ev->hotplug_work);
7396 instance->ev = NULL;
7399 /* cancel all wait events */
7400 wake_up_all(&instance->int_cmd_wait_q);
7402 tasklet_kill(&instance->isr_tasklet);
7405 * Take the instance off the instance array. Note that we will not
7406 * decrement the max_index. We let this array be sparse array
7408 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7409 if (megasas_mgmt_info.instance[i] == instance) {
7410 megasas_mgmt_info.count--;
7411 megasas_mgmt_info.instance[i] = NULL;
7417 instance->instancet->disable_intr(instance);
7419 megasas_destroy_irqs(instance);
7421 if (instance->msix_vectors)
7422 pci_free_irq_vectors(instance->pdev);
7424 if (instance->adapter_type >= VENTURA_SERIES) {
7425 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7426 kfree(fusion->stream_detect_by_ld[i]);
7427 kfree(fusion->stream_detect_by_ld);
7428 fusion->stream_detect_by_ld = NULL;
7432 if (instance->adapter_type != MFI_SERIES) {
7433 megasas_release_fusion(instance);
7434 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7435 (sizeof(struct MR_PD_CFG_SEQ) *
7436 (MAX_PHYSICAL_DEVICES - 1));
7437 for (i = 0; i < 2 ; i++) {
7438 if (fusion->ld_map[i])
7439 dma_free_coherent(&instance->pdev->dev,
7442 fusion->ld_map_phys[i]);
7443 if (fusion->ld_drv_map[i]) {
7444 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7445 vfree(fusion->ld_drv_map[i]);
7447 free_pages((ulong)fusion->ld_drv_map[i],
7448 fusion->drv_map_pages);
7451 if (fusion->pd_seq_sync[i])
7452 dma_free_coherent(&instance->pdev->dev,
7454 fusion->pd_seq_sync[i],
7455 fusion->pd_seq_phys[i]);
7458 megasas_release_mfi(instance);
7461 if (instance->vf_affiliation)
7462 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7463 sizeof(struct MR_LD_VF_AFFILIATION),
7464 instance->vf_affiliation,
7465 instance->vf_affiliation_h);
7467 if (instance->vf_affiliation_111)
7468 dma_free_coherent(&pdev->dev,
7469 sizeof(struct MR_LD_VF_AFFILIATION_111),
7470 instance->vf_affiliation_111,
7471 instance->vf_affiliation_111_h);
7473 if (instance->hb_host_mem)
7474 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
7475 instance->hb_host_mem,
7476 instance->hb_host_mem_h);
7478 megasas_free_ctrl_dma_buffers(instance);
7480 megasas_free_ctrl_mem(instance);
7482 scsi_host_put(host);
7484 pci_disable_device(pdev);
7488 * megasas_shutdown - Shutdown entry point
7489 * @device: Generic device structure
7491 static void megasas_shutdown(struct pci_dev *pdev)
7493 struct megasas_instance *instance = pci_get_drvdata(pdev);
7495 instance->unload = 1;
7497 if (megasas_wait_for_adapter_operational(instance))
7498 goto skip_firing_dcmds;
7500 megasas_flush_cache(instance);
7501 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7504 instance->instancet->disable_intr(instance);
7505 megasas_destroy_irqs(instance);
7507 if (instance->msix_vectors)
7508 pci_free_irq_vectors(instance->pdev);
7512 * megasas_mgmt_open - char node "open" entry point
7514 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7517 * Allow only those users with admin rights
7519 if (!capable(CAP_SYS_ADMIN))
7526 * megasas_mgmt_fasync - Async notifier registration from applications
7528 * This function adds the calling process to a driver global queue. When an
7529 * event occurs, SIGIO will be sent to all processes in this queue.
7531 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7535 mutex_lock(&megasas_async_queue_mutex);
7537 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7539 mutex_unlock(&megasas_async_queue_mutex);
7542 /* For sanity check when we get ioctl */
7543 filep->private_data = filep;
7547 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7553 * megasas_mgmt_poll - char node "poll" entry point
7555 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
7558 unsigned long flags;
7560 poll_wait(file, &megasas_poll_wait, wait);
7561 spin_lock_irqsave(&poll_aen_lock, flags);
7562 if (megasas_poll_wait_aen)
7563 mask = (EPOLLIN | EPOLLRDNORM);
7566 megasas_poll_wait_aen = 0;
7567 spin_unlock_irqrestore(&poll_aen_lock, flags);
7572 * megasas_set_crash_dump_params_ioctl:
7573 * Send CRASH_DUMP_MODE DCMD to all controllers
7574 * @cmd: MFI command frame
7577 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
7579 struct megasas_instance *local_instance;
7583 crash_support = cmd->frame->dcmd.mbox.w[0];
7585 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7586 local_instance = megasas_mgmt_info.instance[i];
7587 if (local_instance && local_instance->crash_dump_drv_support) {
7588 if ((atomic_read(&local_instance->adprecovery) ==
7589 MEGASAS_HBA_OPERATIONAL) &&
7590 !megasas_set_crash_dump_params(local_instance,
7592 local_instance->crash_dump_app_support =
7594 dev_info(&local_instance->pdev->dev,
7595 "Application firmware crash "
7596 "dump mode set success\n");
7599 dev_info(&local_instance->pdev->dev,
7600 "Application firmware crash "
7601 "dump mode set failed\n");
7610 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
7611 * @instance: Adapter soft state
7612 * @argp: User's ioctl packet
7615 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
7616 struct megasas_iocpacket __user * user_ioc,
7617 struct megasas_iocpacket *ioc)
7619 struct megasas_sge64 *kern_sge64 = NULL;
7620 struct megasas_sge32 *kern_sge32 = NULL;
7621 struct megasas_cmd *cmd;
7622 void *kbuff_arr[MAX_IOCTL_SGE];
7623 dma_addr_t buf_handle = 0;
7626 dma_addr_t sense_handle;
7627 unsigned long *sense_ptr;
7630 memset(kbuff_arr, 0, sizeof(kbuff_arr));
7632 if (ioc->sge_count > MAX_IOCTL_SGE) {
7633 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
7634 ioc->sge_count, MAX_IOCTL_SGE);
7638 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
7639 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
7640 !instance->support_nvme_passthru)) {
7641 dev_err(&instance->pdev->dev,
7642 "Received invalid ioctl command 0x%x\n",
7643 ioc->frame.hdr.cmd);
7647 cmd = megasas_get_cmd(instance);
7649 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
7654 * User's IOCTL packet has 2 frames (maximum). Copy those two
7655 * frames into our cmd's frames. cmd->frame's context will get
7656 * overwritten when we copy from user's frames. So set that value
7659 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
7660 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
7661 cmd->frame->hdr.pad_0 = 0;
7663 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
7665 if (instance->consistent_mask_64bit)
7666 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
7667 MFI_FRAME_SENSE64));
7669 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
7670 MFI_FRAME_SENSE64));
7672 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
7673 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
7675 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
7676 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
7677 megasas_return_cmd(instance, cmd);
7682 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
7683 error = megasas_set_crash_dump_params_ioctl(cmd);
7684 megasas_return_cmd(instance, cmd);
7689 * The management interface between applications and the fw uses
7690 * MFI frames. E.g, RAID configuration changes, LD property changes
7691 * etc are accomplishes through different kinds of MFI frames. The
7692 * driver needs to care only about substituting user buffers with
7693 * kernel buffers in SGLs. The location of SGL is embedded in the
7694 * struct iocpacket itself.
7696 if (instance->consistent_mask_64bit)
7697 kern_sge64 = (struct megasas_sge64 *)
7698 ((unsigned long)cmd->frame + ioc->sgl_off);
7700 kern_sge32 = (struct megasas_sge32 *)
7701 ((unsigned long)cmd->frame + ioc->sgl_off);
7704 * For each user buffer, create a mirror buffer and copy in
7706 for (i = 0; i < ioc->sge_count; i++) {
7707 if (!ioc->sgl[i].iov_len)
7710 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
7711 ioc->sgl[i].iov_len,
7712 &buf_handle, GFP_KERNEL);
7713 if (!kbuff_arr[i]) {
7714 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
7715 "kernel SGL buffer for IOCTL\n");
7721 * We don't change the dma_coherent_mask, so
7722 * dma_alloc_coherent only returns 32bit addresses
7724 if (instance->consistent_mask_64bit) {
7725 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
7726 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7728 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
7729 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
7733 * We created a kernel buffer corresponding to the
7734 * user buffer. Now copy in from the user buffer
7736 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
7737 (u32) (ioc->sgl[i].iov_len))) {
7743 if (ioc->sense_len) {
7744 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
7745 &sense_handle, GFP_KERNEL);
7752 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
7753 if (instance->consistent_mask_64bit)
7754 *sense_ptr = cpu_to_le64(sense_handle);
7756 *sense_ptr = cpu_to_le32(sense_handle);
7760 * Set the sync_cmd flag so that the ISR knows not to complete this
7761 * cmd to the SCSI mid-layer
7764 if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
7766 dev_err(&instance->pdev->dev,
7767 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
7768 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
7769 cmd->cmd_status_drv);
7775 if (instance->unload == 1) {
7776 dev_info(&instance->pdev->dev, "Driver unload is in progress "
7777 "don't submit data to application\n");
7781 * copy out the kernel buffers to user buffers
7783 for (i = 0; i < ioc->sge_count; i++) {
7784 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
7785 ioc->sgl[i].iov_len)) {
7792 * copy out the sense
7794 if (ioc->sense_len) {
7796 * sense_ptr points to the location that has the user
7797 * sense buffer address
7799 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
7802 if (copy_to_user((void __user *)((unsigned long)
7803 get_unaligned((unsigned long *)sense_ptr)),
7804 sense, ioc->sense_len)) {
7805 dev_err(&instance->pdev->dev, "Failed to copy out to user "
7813 * copy the status codes returned by the fw
7815 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
7816 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
7817 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
7823 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
7824 sense, sense_handle);
7827 for (i = 0; i < ioc->sge_count; i++) {
7829 if (instance->consistent_mask_64bit)
7830 dma_free_coherent(&instance->pdev->dev,
7831 le32_to_cpu(kern_sge64[i].length),
7833 le64_to_cpu(kern_sge64[i].phys_addr));
7835 dma_free_coherent(&instance->pdev->dev,
7836 le32_to_cpu(kern_sge32[i].length),
7838 le32_to_cpu(kern_sge32[i].phys_addr));
7839 kbuff_arr[i] = NULL;
7843 megasas_return_cmd(instance, cmd);
7847 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
7849 struct megasas_iocpacket __user *user_ioc =
7850 (struct megasas_iocpacket __user *)arg;
7851 struct megasas_iocpacket *ioc;
7852 struct megasas_instance *instance;
7855 ioc = memdup_user(user_ioc, sizeof(*ioc));
7857 return PTR_ERR(ioc);
7859 instance = megasas_lookup_instance(ioc->host_no);
7865 /* Block ioctls in VF mode */
7866 if (instance->requestorId && !allow_vf_ioctls) {
7871 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7872 dev_err(&instance->pdev->dev, "Controller in crit error\n");
7877 if (instance->unload == 1) {
7882 if (down_interruptible(&instance->ioctl_sem)) {
7883 error = -ERESTARTSYS;
7887 if (megasas_wait_for_adapter_operational(instance)) {
7892 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
7894 up(&instance->ioctl_sem);
7901 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
7903 struct megasas_instance *instance;
7904 struct megasas_aen aen;
7907 if (file->private_data != file) {
7908 printk(KERN_DEBUG "megasas: fasync_helper was not "
7913 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
7916 instance = megasas_lookup_instance(aen.host_no);
7921 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
7925 if (instance->unload == 1) {
7929 if (megasas_wait_for_adapter_operational(instance))
7932 mutex_lock(&instance->reset_mutex);
7933 error = megasas_register_aen(instance, aen.seq_num,
7934 aen.class_locale_word);
7935 mutex_unlock(&instance->reset_mutex);
7940 * megasas_mgmt_ioctl - char node ioctl entry point
7943 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
7946 case MEGASAS_IOC_FIRMWARE:
7947 return megasas_mgmt_ioctl_fw(file, arg);
7949 case MEGASAS_IOC_GET_AEN:
7950 return megasas_mgmt_ioctl_aen(file, arg);
7956 #ifdef CONFIG_COMPAT
7957 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
7959 struct compat_megasas_iocpacket __user *cioc =
7960 (struct compat_megasas_iocpacket __user *)arg;
7961 struct megasas_iocpacket __user *ioc =
7962 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
7966 u32 local_sense_off;
7967 u32 local_sense_len;
7970 if (clear_user(ioc, sizeof(*ioc)))
7973 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
7974 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
7975 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
7976 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
7977 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
7978 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
7982 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
7983 * sense_len is not null, so prepare the 64bit value under
7984 * the same condition.
7986 if (get_user(local_sense_off, &ioc->sense_off) ||
7987 get_user(local_sense_len, &ioc->sense_len) ||
7988 get_user(user_sense_off, &cioc->sense_off))
7991 if (local_sense_off != user_sense_off)
7994 if (local_sense_len) {
7995 void __user **sense_ioc_ptr =
7996 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
7997 compat_uptr_t *sense_cioc_ptr =
7998 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
7999 if (get_user(ptr, sense_cioc_ptr) ||
8000 put_user(compat_ptr(ptr), sense_ioc_ptr))
8004 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8005 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8006 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8007 copy_in_user(&ioc->sgl[i].iov_len,
8008 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8012 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8014 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8015 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8016 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8023 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8027 case MEGASAS_IOC_FIRMWARE32:
8028 return megasas_mgmt_compat_ioctl_fw(file, arg);
8029 case MEGASAS_IOC_GET_AEN:
8030 return megasas_mgmt_ioctl_aen(file, arg);
8038 * File operations structure for management interface
8040 static const struct file_operations megasas_mgmt_fops = {
8041 .owner = THIS_MODULE,
8042 .open = megasas_mgmt_open,
8043 .fasync = megasas_mgmt_fasync,
8044 .unlocked_ioctl = megasas_mgmt_ioctl,
8045 .poll = megasas_mgmt_poll,
8046 #ifdef CONFIG_COMPAT
8047 .compat_ioctl = megasas_mgmt_compat_ioctl,
8049 .llseek = noop_llseek,
8053 * PCI hotplug support registration structure
8055 static struct pci_driver megasas_pci_driver = {
8057 .name = "megaraid_sas",
8058 .id_table = megasas_pci_table,
8059 .probe = megasas_probe_one,
8060 .remove = megasas_detach_one,
8061 .suspend = megasas_suspend,
8062 .resume = megasas_resume,
8063 .shutdown = megasas_shutdown,
8067 * Sysfs driver attributes
8069 static ssize_t version_show(struct device_driver *dd, char *buf)
8071 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8074 static DRIVER_ATTR_RO(version);
8076 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8078 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8081 static DRIVER_ATTR_RO(release_date);
8083 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8085 return sprintf(buf, "%u\n", support_poll_for_event);
8087 static DRIVER_ATTR_RO(support_poll_for_event);
8089 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8091 return sprintf(buf, "%u\n", support_device_change);
8093 static DRIVER_ATTR_RO(support_device_change);
8095 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8097 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8100 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8105 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8106 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8111 static DRIVER_ATTR_RW(dbg_lvl);
8114 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8116 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8119 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8121 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8123 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8124 scsi_remove_device(sdev);
8125 scsi_device_put(sdev);
8129 * megasas_update_device_list - Update the PD and LD device list from FW
8130 * after an AEN event notification
8131 * @instance: Adapter soft state
8132 * @event_type: Indicates type of event (PD or LD event)
8134 * @return: Success or failure
8136 * Issue DCMDs to Firmware to update the internal device list in driver.
8137 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8138 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8141 int megasas_update_device_list(struct megasas_instance *instance,
8144 int dcmd_ret = DCMD_SUCCESS;
8146 if (instance->enable_fw_dev_list) {
8147 dcmd_ret = megasas_host_device_list_query(instance, false);
8148 if (dcmd_ret != DCMD_SUCCESS)
8151 if (event_type & SCAN_PD_CHANNEL) {
8152 dcmd_ret = megasas_get_pd_list(instance);
8154 if (dcmd_ret != DCMD_SUCCESS)
8158 if (event_type & SCAN_VD_CHANNEL) {
8159 if (!instance->requestorId ||
8160 (instance->requestorId &&
8161 megasas_get_ld_vf_affiliation(instance, 0))) {
8162 dcmd_ret = megasas_ld_list_query(instance,
8163 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8164 if (dcmd_ret != DCMD_SUCCESS)
8175 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8176 * after an AEN event notification
8177 * @instance: Adapter soft state
8178 * @scan_type: Indicates type of devices (PD/LD) to add
8182 void megasas_add_remove_devices(struct megasas_instance *instance,
8188 u16 channel = 0, id = 0;
8189 struct Scsi_Host *host;
8190 struct scsi_device *sdev1;
8191 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8192 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8194 host = instance->host;
8196 if (instance->enable_fw_dev_list) {
8197 targetid_list = instance->host_device_list_buf;
8198 for (i = 0; i < targetid_list->count; i++) {
8199 targetid_entry = &targetid_list->host_device_list[i];
8200 if (targetid_entry->flags.u.bits.is_sys_pd) {
8201 channel = le16_to_cpu(targetid_entry->target_id) /
8202 MEGASAS_MAX_DEV_PER_CHANNEL;
8203 id = le16_to_cpu(targetid_entry->target_id) %
8204 MEGASAS_MAX_DEV_PER_CHANNEL;
8206 channel = MEGASAS_MAX_PD_CHANNELS +
8207 (le16_to_cpu(targetid_entry->target_id) /
8208 MEGASAS_MAX_DEV_PER_CHANNEL);
8209 id = le16_to_cpu(targetid_entry->target_id) %
8210 MEGASAS_MAX_DEV_PER_CHANNEL;
8212 sdev1 = scsi_device_lookup(host, channel, id, 0);
8214 scsi_add_device(host, channel, id, 0);
8216 scsi_device_put(sdev1);
8221 if (scan_type & SCAN_PD_CHANNEL) {
8222 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8223 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8224 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8225 sdev1 = scsi_device_lookup(host, i, j, 0);
8226 if (instance->pd_list[pd_index].driveState ==
8227 MR_PD_STATE_SYSTEM) {
8229 scsi_add_device(host, i, j, 0);
8231 scsi_device_put(sdev1);
8234 megasas_remove_scsi_device(sdev1);
8240 if (scan_type & SCAN_VD_CHANNEL) {
8241 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8242 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8243 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8244 sdev1 = scsi_device_lookup(host,
8245 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8246 if (instance->ld_ids[ld_index] != 0xff) {
8248 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8250 scsi_device_put(sdev1);
8253 megasas_remove_scsi_device(sdev1);
8262 megasas_aen_polling(struct work_struct *work)
8264 struct megasas_aen_event *ev =
8265 container_of(work, struct megasas_aen_event, hotplug_work.work);
8266 struct megasas_instance *instance = ev->instance;
8267 union megasas_evt_class_locale class_locale;
8269 u32 seq_num, wait_time = MEGASAS_RESET_WAIT_TIME;
8271 u8 dcmd_ret = DCMD_SUCCESS;
8274 printk(KERN_ERR "invalid instance!\n");
8279 /* Adjust event workqueue thread wait time for VF mode */
8280 if (instance->requestorId)
8281 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
8283 /* Don't run the event workqueue thread if OCR is running */
8284 mutex_lock(&instance->reset_mutex);
8286 instance->ev = NULL;
8287 if (instance->evt_detail) {
8288 megasas_decode_evt(instance);
8290 switch (le32_to_cpu(instance->evt_detail->code)) {
8292 case MR_EVT_PD_INSERTED:
8293 case MR_EVT_PD_REMOVED:
8294 event_type = SCAN_PD_CHANNEL;
8297 case MR_EVT_LD_OFFLINE:
8298 case MR_EVT_CFG_CLEARED:
8299 case MR_EVT_LD_DELETED:
8300 case MR_EVT_LD_CREATED:
8301 event_type = SCAN_VD_CHANNEL;
8304 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8305 case MR_EVT_FOREIGN_CFG_IMPORTED:
8306 case MR_EVT_LD_STATE_CHANGE:
8307 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8308 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8309 instance->host->host_no);
8312 case MR_EVT_CTRL_PROP_CHANGED:
8313 dcmd_ret = megasas_get_ctrl_info(instance);
8314 if (dcmd_ret == DCMD_SUCCESS &&
8315 instance->snapdump_wait_time) {
8316 megasas_get_snapdump_properties(instance);
8317 dev_info(&instance->pdev->dev,
8318 "Snap dump wait time\t: %d\n",
8319 instance->snapdump_wait_time);
8327 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8328 mutex_unlock(&instance->reset_mutex);
8334 dcmd_ret = megasas_update_device_list(instance, event_type);
8336 mutex_unlock(&instance->reset_mutex);
8338 if (event_type && dcmd_ret == DCMD_SUCCESS)
8339 megasas_add_remove_devices(instance, event_type);
8341 if (dcmd_ret == DCMD_SUCCESS)
8342 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8344 seq_num = instance->last_seq_num;
8346 /* Register AEN with FW for latest sequence number plus 1 */
8347 class_locale.members.reserved = 0;
8348 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8349 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8351 if (instance->aen_cmd != NULL) {
8356 mutex_lock(&instance->reset_mutex);
8357 error = megasas_register_aen(instance, seq_num,
8360 dev_err(&instance->pdev->dev,
8361 "register aen failed error %x\n", error);
8363 mutex_unlock(&instance->reset_mutex);
8368 * megasas_init - Driver load entry point
8370 static int __init megasas_init(void)
8375 * Booted in kdump kernel, minimize memory footprints by
8376 * disabling few features
8378 if (reset_devices) {
8381 dual_qdepth_disable = 1;
8385 * Announce driver version and other information
8387 pr_info("megasas: %s\n", MEGASAS_VERSION);
8389 spin_lock_init(&poll_aen_lock);
8391 support_poll_for_event = 2;
8392 support_device_change = 1;
8393 support_nvme_encapsulation = true;
8395 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8398 * Register character device node
8400 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8403 printk(KERN_DEBUG "megasas: failed to open device node\n");
8407 megasas_mgmt_majorno = rval;
8410 * Register ourselves as PCI hotplug module
8412 rval = pci_register_driver(&megasas_pci_driver);
8415 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8419 rval = driver_create_file(&megasas_pci_driver.driver,
8420 &driver_attr_version);
8422 goto err_dcf_attr_ver;
8424 rval = driver_create_file(&megasas_pci_driver.driver,
8425 &driver_attr_release_date);
8427 goto err_dcf_rel_date;
8429 rval = driver_create_file(&megasas_pci_driver.driver,
8430 &driver_attr_support_poll_for_event);
8432 goto err_dcf_support_poll_for_event;
8434 rval = driver_create_file(&megasas_pci_driver.driver,
8435 &driver_attr_dbg_lvl);
8437 goto err_dcf_dbg_lvl;
8438 rval = driver_create_file(&megasas_pci_driver.driver,
8439 &driver_attr_support_device_change);
8441 goto err_dcf_support_device_change;
8443 rval = driver_create_file(&megasas_pci_driver.driver,
8444 &driver_attr_support_nvme_encapsulation);
8446 goto err_dcf_support_nvme_encapsulation;
8450 err_dcf_support_nvme_encapsulation:
8451 driver_remove_file(&megasas_pci_driver.driver,
8452 &driver_attr_support_device_change);
8454 err_dcf_support_device_change:
8455 driver_remove_file(&megasas_pci_driver.driver,
8456 &driver_attr_dbg_lvl);
8458 driver_remove_file(&megasas_pci_driver.driver,
8459 &driver_attr_support_poll_for_event);
8460 err_dcf_support_poll_for_event:
8461 driver_remove_file(&megasas_pci_driver.driver,
8462 &driver_attr_release_date);
8464 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8466 pci_unregister_driver(&megasas_pci_driver);
8468 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8473 * megasas_exit - Driver unload entry point
8475 static void __exit megasas_exit(void)
8477 driver_remove_file(&megasas_pci_driver.driver,
8478 &driver_attr_dbg_lvl);
8479 driver_remove_file(&megasas_pci_driver.driver,
8480 &driver_attr_support_poll_for_event);
8481 driver_remove_file(&megasas_pci_driver.driver,
8482 &driver_attr_support_device_change);
8483 driver_remove_file(&megasas_pci_driver.driver,
8484 &driver_attr_release_date);
8485 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8486 driver_remove_file(&megasas_pci_driver.driver,
8487 &driver_attr_support_nvme_encapsulation);
8489 pci_unregister_driver(&megasas_pci_driver);
8490 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8493 module_init(megasas_init);
8494 module_exit(megasas_exit);