1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2013 LSI Corporation
6 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
9 * Authors: Broadcom Inc.
14 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
17 * Send feedback to: megaraidlinux.pdl@broadcom.com
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <asm/unaligned.h>
34 #include <linux/compat.h>
35 #include <linux/blkdev.h>
36 #include <linux/mutex.h>
37 #include <linux/poll.h>
38 #include <linux/vmalloc.h>
39 #include <linux/irq_poll.h>
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_tcq.h>
46 #include <scsi/scsi_dbg.h>
47 #include "megaraid_sas_fusion.h"
48 #include "megaraid_sas.h"
51 * Number of sectors per IO command
52 * Will be set in megasas_init_mfi if user does not provide
54 static unsigned int max_sectors;
55 module_param_named(max_sectors, max_sectors, int, 0444);
56 MODULE_PARM_DESC(max_sectors,
57 "Maximum number of sectors per IO command");
59 static int msix_disable;
60 module_param(msix_disable, int, 0444);
61 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
63 static unsigned int msix_vectors;
64 module_param(msix_vectors, int, 0444);
65 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
67 static int allow_vf_ioctls;
68 module_param(allow_vf_ioctls, int, 0444);
69 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
71 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
72 module_param(throttlequeuedepth, int, 0444);
73 MODULE_PARM_DESC(throttlequeuedepth,
74 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
76 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
77 module_param(resetwaittime, int, 0444);
78 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
80 static int smp_affinity_enable = 1;
81 module_param(smp_affinity_enable, int, 0444);
82 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
84 static int rdpq_enable = 1;
85 module_param(rdpq_enable, int, 0444);
86 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
88 unsigned int dual_qdepth_disable;
89 module_param(dual_qdepth_disable, int, 0444);
90 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
92 static unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
93 module_param(scmd_timeout, int, 0444);
94 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
97 module_param(perf_mode, int, 0444);
98 MODULE_PARM_DESC(perf_mode, "Performance mode (only for Aero adapters), options:\n\t\t"
99 "0 - balanced: High iops and low latency queues are allocated &\n\t\t"
100 "interrupt coalescing is enabled only on high iops queues\n\t\t"
101 "1 - iops: High iops queues are not allocated &\n\t\t"
102 "interrupt coalescing is enabled on all queues\n\t\t"
103 "2 - latency: High iops queues are not allocated &\n\t\t"
104 "interrupt coalescing is disabled on all queues\n\t\t"
105 "default mode is 'balanced'"
108 int event_log_level = MFI_EVT_CLASS_CRITICAL;
109 module_param(event_log_level, int, 0644);
110 MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
112 unsigned int enable_sdev_max_qd;
113 module_param(enable_sdev_max_qd, int, 0444);
114 MODULE_PARM_DESC(enable_sdev_max_qd, "Enable sdev max qd as can_queue. Default: 0");
116 MODULE_LICENSE("GPL");
117 MODULE_VERSION(MEGASAS_VERSION);
118 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
119 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
121 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
122 static int megasas_get_pd_list(struct megasas_instance *instance);
123 static int megasas_ld_list_query(struct megasas_instance *instance,
125 static int megasas_issue_init_mfi(struct megasas_instance *instance);
126 static int megasas_register_aen(struct megasas_instance *instance,
127 u32 seq_num, u32 class_locale_word);
128 static void megasas_get_pd_info(struct megasas_instance *instance,
129 struct scsi_device *sdev);
131 megasas_set_ld_removed_by_fw(struct megasas_instance *instance);
134 * PCI ID table for all supported controllers
136 static struct pci_device_id megasas_pci_table[] = {
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
153 /* xscale IOP, vega */
154 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
167 /* Intruder 24 port*/
168 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
171 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
172 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
173 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
174 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
175 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
176 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
177 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
178 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
179 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
180 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
181 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
182 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
183 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
184 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
188 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
190 static int megasas_mgmt_majorno;
191 struct megasas_mgmt_info megasas_mgmt_info;
192 static struct fasync_struct *megasas_async_queue;
193 static DEFINE_MUTEX(megasas_async_queue_mutex);
195 static int megasas_poll_wait_aen;
196 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
197 static u32 support_poll_for_event;
199 static u32 support_device_change;
200 static bool support_nvme_encapsulation;
201 static bool support_pci_lane_margining;
203 /* define lock for aen poll */
204 static spinlock_t poll_aen_lock;
206 extern struct dentry *megasas_debugfs_root;
209 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
212 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
214 megasas_adp_reset_gen2(struct megasas_instance *instance,
215 struct megasas_register_set __iomem *reg_set);
216 static irqreturn_t megasas_isr(int irq, void *devp);
218 megasas_init_adapter_mfi(struct megasas_instance *instance);
220 megasas_build_and_issue_cmd(struct megasas_instance *instance,
221 struct scsi_cmnd *scmd);
222 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
224 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
226 void megasas_fusion_ocr_wq(struct work_struct *work);
227 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
230 megasas_set_dma_mask(struct megasas_instance *instance);
232 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
234 megasas_free_ctrl_mem(struct megasas_instance *instance);
236 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
238 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
240 megasas_init_ctrl_params(struct megasas_instance *instance);
242 u32 megasas_readl(struct megasas_instance *instance,
243 const volatile void __iomem *addr)
247 * Due to a HW errata in Aero controllers, reads to certain
248 * Fusion registers could intermittently return all zeroes.
249 * This behavior is transient in nature and subsequent reads will
250 * return valid value. As a workaround in driver, retry readl for
251 * up to thirty times until a non-zero value is read.
253 if (instance->adapter_type == AERO_SERIES) {
255 ret_val = readl(addr);
257 } while (ret_val == 0 && i < 30);
265 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
266 * @instance: Adapter soft state
267 * @dcmd: DCMD frame inside MFI command
268 * @dma_addr: DMA address of buffer to be passed to FW
269 * @dma_len: Length of DMA buffer to be passed to FW
272 void megasas_set_dma_settings(struct megasas_instance *instance,
273 struct megasas_dcmd_frame *dcmd,
274 dma_addr_t dma_addr, u32 dma_len)
276 if (instance->consistent_mask_64bit) {
277 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
278 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
279 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
282 dcmd->sgl.sge32[0].phys_addr =
283 cpu_to_le32(lower_32_bits(dma_addr));
284 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
285 dcmd->flags = cpu_to_le16(dcmd->flags);
290 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
292 instance->instancet->fire_cmd(instance,
293 cmd->frame_phys_addr, 0, instance->reg_set);
298 * megasas_get_cmd - Get a command from the free pool
299 * @instance: Adapter soft state
301 * Returns a free command from the pool
303 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
307 struct megasas_cmd *cmd = NULL;
309 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
311 if (!list_empty(&instance->cmd_pool)) {
312 cmd = list_entry((&instance->cmd_pool)->next,
313 struct megasas_cmd, list);
314 list_del_init(&cmd->list);
316 dev_err(&instance->pdev->dev, "Command pool empty!\n");
319 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
324 * megasas_return_cmd - Return a cmd to free command pool
325 * @instance: Adapter soft state
326 * @cmd: Command packet to be returned to free command pool
329 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
333 struct megasas_cmd_fusion *cmd_fusion;
334 struct fusion_context *fusion = instance->ctrl_context;
336 /* This flag is used only for fusion adapter.
337 * Wait for Interrupt for Polled mode DCMD
339 if (cmd->flags & DRV_DCMD_POLLED_MODE)
342 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
345 blk_tags = instance->max_scsi_cmds + cmd->index;
346 cmd_fusion = fusion->cmd_list[blk_tags];
347 megasas_return_cmd_fusion(instance, cmd_fusion);
350 cmd->frame_count = 0;
352 memset(cmd->frame, 0, instance->mfi_frame_size);
353 cmd->frame->io.context = cpu_to_le32(cmd->index);
354 if (!fusion && reset_devices)
355 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
356 list_add(&cmd->list, (&instance->cmd_pool)->next);
358 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
363 format_timestamp(uint32_t timestamp)
365 static char buffer[32];
367 if ((timestamp & 0xff000000) == 0xff000000)
368 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
371 snprintf(buffer, sizeof(buffer), "%us", timestamp);
376 format_class(int8_t class)
378 static char buffer[6];
381 case MFI_EVT_CLASS_DEBUG:
383 case MFI_EVT_CLASS_PROGRESS:
385 case MFI_EVT_CLASS_INFO:
387 case MFI_EVT_CLASS_WARNING:
389 case MFI_EVT_CLASS_CRITICAL:
391 case MFI_EVT_CLASS_FATAL:
393 case MFI_EVT_CLASS_DEAD:
396 snprintf(buffer, sizeof(buffer), "%d", class);
402 * megasas_decode_evt: Decode FW AEN event and print critical event
404 * @instance: Adapter soft state
407 megasas_decode_evt(struct megasas_instance *instance)
409 struct megasas_evt_detail *evt_detail = instance->evt_detail;
410 union megasas_evt_class_locale class_locale;
411 class_locale.word = le32_to_cpu(evt_detail->cl.word);
413 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
414 (event_log_level > MFI_EVT_CLASS_DEAD)) {
415 printk(KERN_WARNING "megaraid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
416 event_log_level = MFI_EVT_CLASS_CRITICAL;
419 if (class_locale.members.class >= event_log_level)
420 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
421 le32_to_cpu(evt_detail->seq_num),
422 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
423 (class_locale.members.locale),
424 format_class(class_locale.members.class),
425 evt_detail->description);
427 if (megasas_dbg_lvl & LD_PD_DEBUG)
428 dev_info(&instance->pdev->dev,
429 "evt_detail.args.ld.target_id/index %d/%d\n",
430 evt_detail->args.ld.target_id, evt_detail->args.ld.ld_index);
435 * The following functions are defined for xscale
436 * (deviceid : 1064R, PERC5) controllers
440 * megasas_enable_intr_xscale - Enables interrupts
441 * @instance: Adapter soft state
444 megasas_enable_intr_xscale(struct megasas_instance *instance)
446 struct megasas_register_set __iomem *regs;
448 regs = instance->reg_set;
449 writel(0, &(regs)->outbound_intr_mask);
451 /* Dummy readl to force pci flush */
452 readl(®s->outbound_intr_mask);
456 * megasas_disable_intr_xscale -Disables interrupt
457 * @instance: Adapter soft state
460 megasas_disable_intr_xscale(struct megasas_instance *instance)
462 struct megasas_register_set __iomem *regs;
465 regs = instance->reg_set;
466 writel(mask, ®s->outbound_intr_mask);
467 /* Dummy readl to force pci flush */
468 readl(®s->outbound_intr_mask);
472 * megasas_read_fw_status_reg_xscale - returns the current FW status value
473 * @instance: Adapter soft state
476 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
478 return readl(&instance->reg_set->outbound_msg_0);
481 * megasas_clear_interrupt_xscale - Check & clear interrupt
482 * @instance: Adapter soft state
485 megasas_clear_intr_xscale(struct megasas_instance *instance)
489 struct megasas_register_set __iomem *regs;
490 regs = instance->reg_set;
493 * Check if it is our interrupt
495 status = readl(®s->outbound_intr_status);
497 if (status & MFI_OB_INTR_STATUS_MASK)
498 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
499 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
500 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
503 * Clear the interrupt by writing back the same value
506 writel(status, ®s->outbound_intr_status);
508 /* Dummy readl to force pci flush */
509 readl(®s->outbound_intr_status);
515 * megasas_fire_cmd_xscale - Sends command to the FW
516 * @instance: Adapter soft state
517 * @frame_phys_addr : Physical address of cmd
518 * @frame_count : Number of frames for the command
519 * @regs : MFI register set
522 megasas_fire_cmd_xscale(struct megasas_instance *instance,
523 dma_addr_t frame_phys_addr,
525 struct megasas_register_set __iomem *regs)
529 spin_lock_irqsave(&instance->hba_lock, flags);
530 writel((frame_phys_addr >> 3)|(frame_count),
531 &(regs)->inbound_queue_port);
532 spin_unlock_irqrestore(&instance->hba_lock, flags);
536 * megasas_adp_reset_xscale - For controller reset
537 * @instance: Adapter soft state
538 * @regs: MFI register set
541 megasas_adp_reset_xscale(struct megasas_instance *instance,
542 struct megasas_register_set __iomem *regs)
547 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
549 for (i = 0; i < 3; i++)
550 msleep(1000); /* sleep for 3 secs */
552 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
553 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
555 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
557 pci_write_config_dword(instance->pdev,
558 MFI_1068_PCSR_OFFSET, pcidata);
560 for (i = 0; i < 2; i++)
561 msleep(1000); /* need to wait 2 secs again */
564 pci_read_config_dword(instance->pdev,
565 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
566 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
567 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
568 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
570 pci_write_config_dword(instance->pdev,
571 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
578 * megasas_check_reset_xscale - For controller reset check
579 * @instance: Adapter soft state
580 * @regs: MFI register set
583 megasas_check_reset_xscale(struct megasas_instance *instance,
584 struct megasas_register_set __iomem *regs)
586 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
587 (le32_to_cpu(*instance->consumer) ==
588 MEGASAS_ADPRESET_INPROG_SIGN))
593 static struct megasas_instance_template megasas_instance_template_xscale = {
595 .fire_cmd = megasas_fire_cmd_xscale,
596 .enable_intr = megasas_enable_intr_xscale,
597 .disable_intr = megasas_disable_intr_xscale,
598 .clear_intr = megasas_clear_intr_xscale,
599 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
600 .adp_reset = megasas_adp_reset_xscale,
601 .check_reset = megasas_check_reset_xscale,
602 .service_isr = megasas_isr,
603 .tasklet = megasas_complete_cmd_dpc,
604 .init_adapter = megasas_init_adapter_mfi,
605 .build_and_issue_cmd = megasas_build_and_issue_cmd,
606 .issue_dcmd = megasas_issue_dcmd,
610 * This is the end of set of functions & definitions specific
611 * to xscale (deviceid : 1064R, PERC5) controllers
615 * The following functions are defined for ppc (deviceid : 0x60)
620 * megasas_enable_intr_ppc - Enables interrupts
621 * @instance: Adapter soft state
624 megasas_enable_intr_ppc(struct megasas_instance *instance)
626 struct megasas_register_set __iomem *regs;
628 regs = instance->reg_set;
629 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
631 writel(~0x80000000, &(regs)->outbound_intr_mask);
633 /* Dummy readl to force pci flush */
634 readl(®s->outbound_intr_mask);
638 * megasas_disable_intr_ppc - Disable interrupt
639 * @instance: Adapter soft state
642 megasas_disable_intr_ppc(struct megasas_instance *instance)
644 struct megasas_register_set __iomem *regs;
645 u32 mask = 0xFFFFFFFF;
647 regs = instance->reg_set;
648 writel(mask, ®s->outbound_intr_mask);
649 /* Dummy readl to force pci flush */
650 readl(®s->outbound_intr_mask);
654 * megasas_read_fw_status_reg_ppc - returns the current FW status value
655 * @instance: Adapter soft state
658 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
660 return readl(&instance->reg_set->outbound_scratch_pad_0);
664 * megasas_clear_interrupt_ppc - Check & clear interrupt
665 * @instance: Adapter soft state
668 megasas_clear_intr_ppc(struct megasas_instance *instance)
670 u32 status, mfiStatus = 0;
671 struct megasas_register_set __iomem *regs;
672 regs = instance->reg_set;
675 * Check if it is our interrupt
677 status = readl(®s->outbound_intr_status);
679 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
680 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
682 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
683 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
686 * Clear the interrupt by writing back the same value
688 writel(status, ®s->outbound_doorbell_clear);
690 /* Dummy readl to force pci flush */
691 readl(®s->outbound_doorbell_clear);
697 * megasas_fire_cmd_ppc - Sends command to the FW
698 * @instance: Adapter soft state
699 * @frame_phys_addr: Physical address of cmd
700 * @frame_count: Number of frames for the command
701 * @regs: MFI register set
704 megasas_fire_cmd_ppc(struct megasas_instance *instance,
705 dma_addr_t frame_phys_addr,
707 struct megasas_register_set __iomem *regs)
711 spin_lock_irqsave(&instance->hba_lock, flags);
712 writel((frame_phys_addr | (frame_count<<1))|1,
713 &(regs)->inbound_queue_port);
714 spin_unlock_irqrestore(&instance->hba_lock, flags);
718 * megasas_check_reset_ppc - For controller reset check
719 * @instance: Adapter soft state
720 * @regs: MFI register set
723 megasas_check_reset_ppc(struct megasas_instance *instance,
724 struct megasas_register_set __iomem *regs)
726 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
732 static struct megasas_instance_template megasas_instance_template_ppc = {
734 .fire_cmd = megasas_fire_cmd_ppc,
735 .enable_intr = megasas_enable_intr_ppc,
736 .disable_intr = megasas_disable_intr_ppc,
737 .clear_intr = megasas_clear_intr_ppc,
738 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
739 .adp_reset = megasas_adp_reset_xscale,
740 .check_reset = megasas_check_reset_ppc,
741 .service_isr = megasas_isr,
742 .tasklet = megasas_complete_cmd_dpc,
743 .init_adapter = megasas_init_adapter_mfi,
744 .build_and_issue_cmd = megasas_build_and_issue_cmd,
745 .issue_dcmd = megasas_issue_dcmd,
749 * megasas_enable_intr_skinny - Enables interrupts
750 * @instance: Adapter soft state
753 megasas_enable_intr_skinny(struct megasas_instance *instance)
755 struct megasas_register_set __iomem *regs;
757 regs = instance->reg_set;
758 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
760 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
762 /* Dummy readl to force pci flush */
763 readl(®s->outbound_intr_mask);
767 * megasas_disable_intr_skinny - Disables interrupt
768 * @instance: Adapter soft state
771 megasas_disable_intr_skinny(struct megasas_instance *instance)
773 struct megasas_register_set __iomem *regs;
774 u32 mask = 0xFFFFFFFF;
776 regs = instance->reg_set;
777 writel(mask, ®s->outbound_intr_mask);
778 /* Dummy readl to force pci flush */
779 readl(®s->outbound_intr_mask);
783 * megasas_read_fw_status_reg_skinny - returns the current FW status value
784 * @instance: Adapter soft state
787 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
789 return readl(&instance->reg_set->outbound_scratch_pad_0);
793 * megasas_clear_interrupt_skinny - Check & clear interrupt
794 * @instance: Adapter soft state
797 megasas_clear_intr_skinny(struct megasas_instance *instance)
801 struct megasas_register_set __iomem *regs;
802 regs = instance->reg_set;
805 * Check if it is our interrupt
807 status = readl(®s->outbound_intr_status);
809 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
814 * Check if it is our interrupt
816 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
818 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
820 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
823 * Clear the interrupt by writing back the same value
825 writel(status, ®s->outbound_intr_status);
828 * dummy read to flush PCI
830 readl(®s->outbound_intr_status);
836 * megasas_fire_cmd_skinny - Sends command to the FW
837 * @instance: Adapter soft state
838 * @frame_phys_addr: Physical address of cmd
839 * @frame_count: Number of frames for the command
840 * @regs: MFI register set
843 megasas_fire_cmd_skinny(struct megasas_instance *instance,
844 dma_addr_t frame_phys_addr,
846 struct megasas_register_set __iomem *regs)
850 spin_lock_irqsave(&instance->hba_lock, flags);
851 writel(upper_32_bits(frame_phys_addr),
852 &(regs)->inbound_high_queue_port);
853 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
854 &(regs)->inbound_low_queue_port);
855 spin_unlock_irqrestore(&instance->hba_lock, flags);
859 * megasas_check_reset_skinny - For controller reset check
860 * @instance: Adapter soft state
861 * @regs: MFI register set
864 megasas_check_reset_skinny(struct megasas_instance *instance,
865 struct megasas_register_set __iomem *regs)
867 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
873 static struct megasas_instance_template megasas_instance_template_skinny = {
875 .fire_cmd = megasas_fire_cmd_skinny,
876 .enable_intr = megasas_enable_intr_skinny,
877 .disable_intr = megasas_disable_intr_skinny,
878 .clear_intr = megasas_clear_intr_skinny,
879 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
880 .adp_reset = megasas_adp_reset_gen2,
881 .check_reset = megasas_check_reset_skinny,
882 .service_isr = megasas_isr,
883 .tasklet = megasas_complete_cmd_dpc,
884 .init_adapter = megasas_init_adapter_mfi,
885 .build_and_issue_cmd = megasas_build_and_issue_cmd,
886 .issue_dcmd = megasas_issue_dcmd,
891 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
896 * megasas_enable_intr_gen2 - Enables interrupts
897 * @instance: Adapter soft state
900 megasas_enable_intr_gen2(struct megasas_instance *instance)
902 struct megasas_register_set __iomem *regs;
904 regs = instance->reg_set;
905 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
907 /* write ~0x00000005 (4 & 1) to the intr mask*/
908 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
910 /* Dummy readl to force pci flush */
911 readl(®s->outbound_intr_mask);
915 * megasas_disable_intr_gen2 - Disables interrupt
916 * @instance: Adapter soft state
919 megasas_disable_intr_gen2(struct megasas_instance *instance)
921 struct megasas_register_set __iomem *regs;
922 u32 mask = 0xFFFFFFFF;
924 regs = instance->reg_set;
925 writel(mask, ®s->outbound_intr_mask);
926 /* Dummy readl to force pci flush */
927 readl(®s->outbound_intr_mask);
931 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
932 * @instance: Adapter soft state
935 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
937 return readl(&instance->reg_set->outbound_scratch_pad_0);
941 * megasas_clear_interrupt_gen2 - Check & clear interrupt
942 * @instance: Adapter soft state
945 megasas_clear_intr_gen2(struct megasas_instance *instance)
949 struct megasas_register_set __iomem *regs;
950 regs = instance->reg_set;
953 * Check if it is our interrupt
955 status = readl(®s->outbound_intr_status);
957 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
958 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
960 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
961 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
965 * Clear the interrupt by writing back the same value
968 writel(status, ®s->outbound_doorbell_clear);
970 /* Dummy readl to force pci flush */
971 readl(®s->outbound_intr_status);
977 * megasas_fire_cmd_gen2 - Sends command to the FW
978 * @instance: Adapter soft state
979 * @frame_phys_addr: Physical address of cmd
980 * @frame_count: Number of frames for the command
981 * @regs: MFI register set
984 megasas_fire_cmd_gen2(struct megasas_instance *instance,
985 dma_addr_t frame_phys_addr,
987 struct megasas_register_set __iomem *regs)
991 spin_lock_irqsave(&instance->hba_lock, flags);
992 writel((frame_phys_addr | (frame_count<<1))|1,
993 &(regs)->inbound_queue_port);
994 spin_unlock_irqrestore(&instance->hba_lock, flags);
998 * megasas_adp_reset_gen2 - For controller reset
999 * @instance: Adapter soft state
1000 * @reg_set: MFI register set
1003 megasas_adp_reset_gen2(struct megasas_instance *instance,
1004 struct megasas_register_set __iomem *reg_set)
1008 u32 __iomem *seq_offset = ®_set->seq_offset;
1009 u32 __iomem *hostdiag_offset = ®_set->host_diag;
1011 if (instance->instancet == &megasas_instance_template_skinny) {
1012 seq_offset = ®_set->fusion_seq_offset;
1013 hostdiag_offset = ®_set->fusion_host_diag;
1016 writel(0, seq_offset);
1017 writel(4, seq_offset);
1018 writel(0xb, seq_offset);
1019 writel(2, seq_offset);
1020 writel(7, seq_offset);
1021 writel(0xd, seq_offset);
1025 HostDiag = (u32)readl(hostdiag_offset);
1027 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1029 HostDiag = (u32)readl(hostdiag_offset);
1030 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1038 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1040 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1044 HostDiag = (u32)readl(hostdiag_offset);
1045 while (HostDiag & DIAG_RESET_ADAPTER) {
1047 HostDiag = (u32)readl(hostdiag_offset);
1048 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1051 if (retry++ >= 1000)
1059 * megasas_check_reset_gen2 - For controller reset check
1060 * @instance: Adapter soft state
1061 * @regs: MFI register set
1064 megasas_check_reset_gen2(struct megasas_instance *instance,
1065 struct megasas_register_set __iomem *regs)
1067 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1073 static struct megasas_instance_template megasas_instance_template_gen2 = {
1075 .fire_cmd = megasas_fire_cmd_gen2,
1076 .enable_intr = megasas_enable_intr_gen2,
1077 .disable_intr = megasas_disable_intr_gen2,
1078 .clear_intr = megasas_clear_intr_gen2,
1079 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1080 .adp_reset = megasas_adp_reset_gen2,
1081 .check_reset = megasas_check_reset_gen2,
1082 .service_isr = megasas_isr,
1083 .tasklet = megasas_complete_cmd_dpc,
1084 .init_adapter = megasas_init_adapter_mfi,
1085 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1086 .issue_dcmd = megasas_issue_dcmd,
1090 * This is the end of set of functions & definitions
1091 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1095 * Template added for TB (Fusion)
1097 extern struct megasas_instance_template megasas_instance_template_fusion;
1100 * megasas_issue_polled - Issues a polling command
1101 * @instance: Adapter soft state
1102 * @cmd: Command packet to be issued
1104 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1107 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1109 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1111 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1112 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1114 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1115 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1116 __func__, __LINE__);
1120 instance->instancet->issue_dcmd(instance, cmd);
1122 return wait_and_poll(instance, cmd, instance->requestorId ?
1123 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1127 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1128 * @instance: Adapter soft state
1129 * @cmd: Command to be issued
1130 * @timeout: Timeout in seconds
1132 * This function waits on an event for the command to be returned from ISR.
1133 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1134 * Used to issue ioctl commands.
1137 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1138 struct megasas_cmd *cmd, int timeout)
1141 cmd->cmd_status_drv = DCMD_INIT;
1143 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1144 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1145 __func__, __LINE__);
1149 instance->instancet->issue_dcmd(instance, cmd);
1152 ret = wait_event_timeout(instance->int_cmd_wait_q,
1153 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1155 dev_err(&instance->pdev->dev,
1156 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1157 cmd->frame->dcmd.opcode, __func__);
1158 return DCMD_TIMEOUT;
1161 wait_event(instance->int_cmd_wait_q,
1162 cmd->cmd_status_drv != DCMD_INIT);
1164 return cmd->cmd_status_drv;
1168 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1169 * @instance: Adapter soft state
1170 * @cmd_to_abort: Previously issued cmd to be aborted
1171 * @timeout: Timeout in seconds
1173 * MFI firmware can abort previously issued AEN comamnd (automatic event
1174 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1175 * cmd and waits for return status.
1176 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1179 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1180 struct megasas_cmd *cmd_to_abort, int timeout)
1182 struct megasas_cmd *cmd;
1183 struct megasas_abort_frame *abort_fr;
1187 cmd = megasas_get_cmd(instance);
1192 abort_fr = &cmd->frame->abort;
1195 * Prepare and issue the abort frame
1197 abort_fr->cmd = MFI_CMD_ABORT;
1198 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1199 abort_fr->flags = cpu_to_le16(0);
1200 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1201 abort_fr->abort_mfi_phys_addr_lo =
1202 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1203 abort_fr->abort_mfi_phys_addr_hi =
1204 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1207 cmd->cmd_status_drv = DCMD_INIT;
1209 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1210 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1211 __func__, __LINE__);
1215 instance->instancet->issue_dcmd(instance, cmd);
1218 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1219 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1221 opcode = cmd_to_abort->frame->dcmd.opcode;
1222 dev_err(&instance->pdev->dev,
1223 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1225 return DCMD_TIMEOUT;
1228 wait_event(instance->abort_cmd_wait_q,
1229 cmd->cmd_status_drv != DCMD_INIT);
1233 megasas_return_cmd(instance, cmd);
1234 return cmd->cmd_status_drv;
1238 * megasas_make_sgl32 - Prepares 32-bit SGL
1239 * @instance: Adapter soft state
1240 * @scp: SCSI command from the mid-layer
1241 * @mfi_sgl: SGL to be filled in
1243 * If successful, this function returns the number of SG elements. Otherwise,
1247 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1248 union megasas_sgl *mfi_sgl)
1252 struct scatterlist *os_sgl;
1254 sge_count = scsi_dma_map(scp);
1255 BUG_ON(sge_count < 0);
1258 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1259 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1260 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1267 * megasas_make_sgl64 - Prepares 64-bit SGL
1268 * @instance: Adapter soft state
1269 * @scp: SCSI command from the mid-layer
1270 * @mfi_sgl: SGL to be filled in
1272 * If successful, this function returns the number of SG elements. Otherwise,
1276 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1277 union megasas_sgl *mfi_sgl)
1281 struct scatterlist *os_sgl;
1283 sge_count = scsi_dma_map(scp);
1284 BUG_ON(sge_count < 0);
1287 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1288 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1289 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1296 * megasas_make_sgl_skinny - Prepares IEEE SGL
1297 * @instance: Adapter soft state
1298 * @scp: SCSI command from the mid-layer
1299 * @mfi_sgl: SGL to be filled in
1301 * If successful, this function returns the number of SG elements. Otherwise,
1305 megasas_make_sgl_skinny(struct megasas_instance *instance,
1306 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1310 struct scatterlist *os_sgl;
1312 sge_count = scsi_dma_map(scp);
1315 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1316 mfi_sgl->sge_skinny[i].length =
1317 cpu_to_le32(sg_dma_len(os_sgl));
1318 mfi_sgl->sge_skinny[i].phys_addr =
1319 cpu_to_le64(sg_dma_address(os_sgl));
1320 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1327 * megasas_get_frame_count - Computes the number of frames
1328 * @frame_type : type of frame- io or pthru frame
1329 * @sge_count : number of sg elements
1331 * Returns the number of frames required for numnber of sge's (sge_count)
1334 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1335 u8 sge_count, u8 frame_type)
1340 u32 frame_count = 0;
1342 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1343 sizeof(struct megasas_sge32);
1345 if (instance->flag_ieee) {
1346 sge_sz = sizeof(struct megasas_sge_skinny);
1350 * Main frame can contain 2 SGEs for 64-bit SGLs and
1351 * 3 SGEs for 32-bit SGLs for ldio &
1352 * 1 SGEs for 64-bit SGLs and
1353 * 2 SGEs for 32-bit SGLs for pthru frame
1355 if (unlikely(frame_type == PTHRU_FRAME)) {
1356 if (instance->flag_ieee == 1) {
1357 num_cnt = sge_count - 1;
1358 } else if (IS_DMA64)
1359 num_cnt = sge_count - 1;
1361 num_cnt = sge_count - 2;
1363 if (instance->flag_ieee == 1) {
1364 num_cnt = sge_count - 1;
1365 } else if (IS_DMA64)
1366 num_cnt = sge_count - 2;
1368 num_cnt = sge_count - 3;
1372 sge_bytes = sge_sz * num_cnt;
1374 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1375 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1380 if (frame_count > 7)
1386 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1387 * @instance: Adapter soft state
1388 * @scp: SCSI command
1389 * @cmd: Command to be prepared in
1391 * This function prepares CDB commands. These are typcially pass-through
1392 * commands to the devices.
1395 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1396 struct megasas_cmd *cmd)
1401 struct megasas_pthru_frame *pthru;
1403 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1404 device_id = MEGASAS_DEV_INDEX(scp);
1405 pthru = (struct megasas_pthru_frame *)cmd->frame;
1407 if (scp->sc_data_direction == DMA_TO_DEVICE)
1408 flags = MFI_FRAME_DIR_WRITE;
1409 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1410 flags = MFI_FRAME_DIR_READ;
1411 else if (scp->sc_data_direction == DMA_NONE)
1412 flags = MFI_FRAME_DIR_NONE;
1414 if (instance->flag_ieee == 1) {
1415 flags |= MFI_FRAME_IEEE;
1419 * Prepare the DCDB frame
1421 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1422 pthru->cmd_status = 0x0;
1423 pthru->scsi_status = 0x0;
1424 pthru->target_id = device_id;
1425 pthru->lun = scp->device->lun;
1426 pthru->cdb_len = scp->cmd_len;
1429 pthru->flags = cpu_to_le16(flags);
1430 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1432 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1435 * If the command is for the tape device, set the
1436 * pthru timeout to the os layer timeout value.
1438 if (scp->device->type == TYPE_TAPE) {
1439 if ((scp->request->timeout / HZ) > 0xFFFF)
1440 pthru->timeout = cpu_to_le16(0xFFFF);
1442 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1448 if (instance->flag_ieee == 1) {
1449 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1450 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1452 } else if (IS_DMA64) {
1453 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1454 pthru->sge_count = megasas_make_sgl64(instance, scp,
1457 pthru->sge_count = megasas_make_sgl32(instance, scp,
1460 if (pthru->sge_count > instance->max_num_sge) {
1461 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1467 * Sense info specific
1469 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1470 pthru->sense_buf_phys_addr_hi =
1471 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1472 pthru->sense_buf_phys_addr_lo =
1473 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1476 * Compute the total number of frames this command consumes. FW uses
1477 * this number to pull sufficient number of frames from host memory.
1479 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1482 return cmd->frame_count;
1486 * megasas_build_ldio - Prepares IOs to logical devices
1487 * @instance: Adapter soft state
1488 * @scp: SCSI command
1489 * @cmd: Command to be prepared
1491 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1494 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1495 struct megasas_cmd *cmd)
1498 u8 sc = scp->cmnd[0];
1500 struct megasas_io_frame *ldio;
1502 device_id = MEGASAS_DEV_INDEX(scp);
1503 ldio = (struct megasas_io_frame *)cmd->frame;
1505 if (scp->sc_data_direction == DMA_TO_DEVICE)
1506 flags = MFI_FRAME_DIR_WRITE;
1507 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1508 flags = MFI_FRAME_DIR_READ;
1510 if (instance->flag_ieee == 1) {
1511 flags |= MFI_FRAME_IEEE;
1515 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1517 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1518 ldio->cmd_status = 0x0;
1519 ldio->scsi_status = 0x0;
1520 ldio->target_id = device_id;
1522 ldio->reserved_0 = 0;
1524 ldio->flags = cpu_to_le16(flags);
1525 ldio->start_lba_hi = 0;
1526 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1529 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1531 if (scp->cmd_len == 6) {
1532 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1533 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1534 ((u32) scp->cmnd[2] << 8) |
1535 (u32) scp->cmnd[3]);
1537 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1541 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1543 else if (scp->cmd_len == 10) {
1544 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1545 ((u32) scp->cmnd[7] << 8));
1546 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1547 ((u32) scp->cmnd[3] << 16) |
1548 ((u32) scp->cmnd[4] << 8) |
1549 (u32) scp->cmnd[5]);
1553 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1555 else if (scp->cmd_len == 12) {
1556 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1557 ((u32) scp->cmnd[7] << 16) |
1558 ((u32) scp->cmnd[8] << 8) |
1559 (u32) scp->cmnd[9]);
1561 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1562 ((u32) scp->cmnd[3] << 16) |
1563 ((u32) scp->cmnd[4] << 8) |
1564 (u32) scp->cmnd[5]);
1568 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1570 else if (scp->cmd_len == 16) {
1571 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1572 ((u32) scp->cmnd[11] << 16) |
1573 ((u32) scp->cmnd[12] << 8) |
1574 (u32) scp->cmnd[13]);
1576 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1577 ((u32) scp->cmnd[7] << 16) |
1578 ((u32) scp->cmnd[8] << 8) |
1579 (u32) scp->cmnd[9]);
1581 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1582 ((u32) scp->cmnd[3] << 16) |
1583 ((u32) scp->cmnd[4] << 8) |
1584 (u32) scp->cmnd[5]);
1591 if (instance->flag_ieee) {
1592 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1593 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1595 } else if (IS_DMA64) {
1596 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1597 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1599 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1601 if (ldio->sge_count > instance->max_num_sge) {
1602 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1608 * Sense info specific
1610 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1611 ldio->sense_buf_phys_addr_hi = 0;
1612 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1615 * Compute the total number of frames this command consumes. FW uses
1616 * this number to pull sufficient number of frames from host memory.
1618 cmd->frame_count = megasas_get_frame_count(instance,
1619 ldio->sge_count, IO_FRAME);
1621 return cmd->frame_count;
1625 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1626 * and whether it's RW or non RW
1627 * @cmd: SCSI command
1630 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1634 switch (cmd->cmnd[0]) {
1643 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1644 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1647 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1648 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1654 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1656 * @instance: Adapter soft state
1659 megasas_dump_pending_frames(struct megasas_instance *instance)
1661 struct megasas_cmd *cmd;
1663 union megasas_sgl *mfi_sgl;
1664 struct megasas_io_frame *ldio;
1665 struct megasas_pthru_frame *pthru;
1667 u16 max_cmd = instance->max_fw_cmds;
1669 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1670 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1672 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1674 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1676 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1677 for (i = 0; i < max_cmd; i++) {
1678 cmd = instance->cmd_list[i];
1681 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1682 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1683 ldio = (struct megasas_io_frame *)cmd->frame;
1684 mfi_sgl = &ldio->sgl;
1685 sgcount = ldio->sge_count;
1686 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1687 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1688 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1689 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1690 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1692 pthru = (struct megasas_pthru_frame *) cmd->frame;
1693 mfi_sgl = &pthru->sgl;
1694 sgcount = pthru->sge_count;
1695 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1696 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1697 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1698 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1699 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1701 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1702 for (n = 0; n < sgcount; n++) {
1704 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1705 le32_to_cpu(mfi_sgl->sge64[n].length),
1706 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1708 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1709 le32_to_cpu(mfi_sgl->sge32[n].length),
1710 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1714 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1715 for (i = 0; i < max_cmd; i++) {
1717 cmd = instance->cmd_list[i];
1719 if (cmd->sync_cmd == 1)
1720 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1722 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1726 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1727 struct scsi_cmnd *scmd)
1729 struct megasas_cmd *cmd;
1732 cmd = megasas_get_cmd(instance);
1734 return SCSI_MLQUEUE_HOST_BUSY;
1737 * Logical drive command
1739 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1740 frame_count = megasas_build_ldio(instance, scmd, cmd);
1742 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1745 goto out_return_cmd;
1748 scmd->SCp.ptr = (char *)cmd;
1751 * Issue the command to the FW
1753 atomic_inc(&instance->fw_outstanding);
1755 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1756 cmd->frame_count-1, instance->reg_set);
1760 megasas_return_cmd(instance, cmd);
1761 return SCSI_MLQUEUE_HOST_BUSY;
1766 * megasas_queue_command - Queue entry point
1767 * @shost: adapter SCSI host
1768 * @scmd: SCSI command to be queued
1771 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1773 struct megasas_instance *instance;
1774 struct MR_PRIV_DEVICE *mr_device_priv_data;
1777 instance = (struct megasas_instance *)
1778 scmd->device->host->hostdata;
1780 if (instance->unload == 1) {
1781 scmd->result = DID_NO_CONNECT << 16;
1782 scmd->scsi_done(scmd);
1786 if (instance->issuepend_done == 0)
1787 return SCSI_MLQUEUE_HOST_BUSY;
1790 /* Check for an mpio path and adjust behavior */
1791 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1792 if (megasas_check_mpio_paths(instance, scmd) ==
1793 (DID_REQUEUE << 16)) {
1794 return SCSI_MLQUEUE_HOST_BUSY;
1796 scmd->result = DID_NO_CONNECT << 16;
1797 scmd->scsi_done(scmd);
1802 mr_device_priv_data = scmd->device->hostdata;
1803 if (!mr_device_priv_data ||
1804 (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)) {
1805 scmd->result = DID_NO_CONNECT << 16;
1806 scmd->scsi_done(scmd);
1810 if (MEGASAS_IS_LOGICAL(scmd->device)) {
1811 ld_tgt_id = MEGASAS_TARGET_ID(scmd->device);
1812 if (instance->ld_tgtid_status[ld_tgt_id] == LD_TARGET_ID_DELETED) {
1813 scmd->result = DID_NO_CONNECT << 16;
1814 scmd->scsi_done(scmd);
1819 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1820 return SCSI_MLQUEUE_HOST_BUSY;
1822 if (mr_device_priv_data->tm_busy)
1823 return SCSI_MLQUEUE_DEVICE_BUSY;
1828 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1829 (scmd->device->id >= instance->fw_supported_vd_count ||
1830 scmd->device->lun)) {
1831 scmd->result = DID_BAD_TARGET << 16;
1835 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1836 MEGASAS_IS_LOGICAL(scmd->device) &&
1837 (!instance->fw_sync_cache_support)) {
1838 scmd->result = DID_OK << 16;
1842 return instance->instancet->build_and_issue_cmd(instance, scmd);
1845 scmd->scsi_done(scmd);
1849 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1853 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1855 if ((megasas_mgmt_info.instance[i]) &&
1856 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1857 return megasas_mgmt_info.instance[i];
1864 * megasas_set_dynamic_target_properties -
1865 * Device property set by driver may not be static and it is required to be
1869 * set dma alignment (only for eedp protection enable vd).
1871 * @sdev: OS provided scsi device
1875 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1876 bool is_target_prop)
1878 u16 pd_index = 0, ld;
1880 struct megasas_instance *instance;
1881 struct fusion_context *fusion;
1882 struct MR_PRIV_DEVICE *mr_device_priv_data;
1883 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1884 struct MR_LD_RAID *raid;
1885 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1887 instance = megasas_lookup_instance(sdev->host->host_no);
1888 fusion = instance->ctrl_context;
1889 mr_device_priv_data = sdev->hostdata;
1891 if (!fusion || !mr_device_priv_data)
1894 if (MEGASAS_IS_LOGICAL(sdev)) {
1895 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1897 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1898 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1899 if (ld >= instance->fw_supported_vd_count)
1901 raid = MR_LdRaidGet(ld, local_map_ptr);
1903 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1904 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1906 mr_device_priv_data->is_tm_capable =
1907 raid->capability.tmCapable;
1909 if (!raid->flags.isEPD)
1910 sdev->no_write_same = 1;
1912 } else if (instance->use_seqnum_jbod_fp) {
1913 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1915 pd_sync = (void *)fusion->pd_seq_sync
1916 [(instance->pd_seq_map_id - 1) & 1];
1917 mr_device_priv_data->is_tm_capable =
1918 pd_sync->seq[pd_index].capability.tmCapable;
1921 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1923 * If FW provides a target reset timeout value, driver will use
1924 * it. If not set, fallback to default values.
1926 mr_device_priv_data->target_reset_tmo =
1927 min_t(u8, instance->max_reset_tmo,
1928 instance->tgt_prop->reset_tmo);
1929 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1931 mr_device_priv_data->target_reset_tmo =
1932 MEGASAS_DEFAULT_TM_TIMEOUT;
1933 mr_device_priv_data->task_abort_tmo =
1934 MEGASAS_DEFAULT_TM_TIMEOUT;
1939 * megasas_set_nvme_device_properties -
1941 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1942 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1944 * MR firmware provides value in KB. Caller of this function converts
1947 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1948 * MR firmware provides value 128 as (32 * 4K) = 128K.
1950 * @sdev: scsi device
1951 * @max_io_size: maximum io transfer size
1955 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1957 struct megasas_instance *instance;
1958 u32 mr_nvme_pg_size;
1960 instance = (struct megasas_instance *)sdev->host->hostdata;
1961 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1962 MR_DEFAULT_NVME_PAGE_SIZE);
1964 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1966 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1967 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1971 * megasas_set_fw_assisted_qd -
1972 * set device queue depth to can_queue
1973 * set device queue depth to fw assisted qd
1975 * @sdev: scsi device
1976 * @is_target_prop true, if fw provided target properties.
1978 static void megasas_set_fw_assisted_qd(struct scsi_device *sdev,
1979 bool is_target_prop)
1982 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1984 struct megasas_instance *instance;
1985 struct MR_PRIV_DEVICE *mr_device_priv_data;
1987 instance = megasas_lookup_instance(sdev->host->host_no);
1988 mr_device_priv_data = sdev->hostdata;
1989 interface_type = mr_device_priv_data->interface_type;
1991 switch (interface_type) {
1993 device_qd = MEGASAS_SAS_QD;
1996 device_qd = MEGASAS_SATA_QD;
1999 device_qd = MEGASAS_NVME_QD;
2003 if (is_target_prop) {
2004 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
2006 device_qd = min(instance->host->can_queue,
2007 (int)tgt_device_qd);
2010 if (instance->enable_sdev_max_qd && interface_type != UNKNOWN_DRIVE)
2011 device_qd = instance->host->can_queue;
2013 scsi_change_queue_depth(sdev, device_qd);
2017 * megasas_set_static_target_properties -
2018 * Device property set by driver are static and it is not required to be
2019 * updated after OCR.
2022 * set device queue depth
2023 * set nvme device properties. see - megasas_set_nvme_device_properties
2025 * @sdev: scsi device
2026 * @is_target_prop true, if fw provided target properties.
2028 static void megasas_set_static_target_properties(struct scsi_device *sdev,
2029 bool is_target_prop)
2031 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
2032 struct megasas_instance *instance;
2034 instance = megasas_lookup_instance(sdev->host->host_no);
2037 * The RAID firmware may require extended timeouts.
2039 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
2041 /* max_io_size_kb will be set to non zero for
2042 * nvme based vd and syspd.
2045 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
2047 if (instance->nvme_page_size && max_io_size_kb)
2048 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2050 megasas_set_fw_assisted_qd(sdev, is_target_prop);
2054 static int megasas_slave_configure(struct scsi_device *sdev)
2057 struct megasas_instance *instance;
2058 int ret_target_prop = DCMD_FAILED;
2059 bool is_target_prop = false;
2061 instance = megasas_lookup_instance(sdev->host->host_no);
2062 if (instance->pd_list_not_supported) {
2063 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2064 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2066 if (instance->pd_list[pd_index].driveState !=
2072 mutex_lock(&instance->reset_mutex);
2073 /* Send DCMD to Firmware and cache the information */
2074 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2075 megasas_get_pd_info(instance, sdev);
2077 /* Some ventura firmware may not have instance->nvme_page_size set.
2078 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2080 if ((instance->tgt_prop) && (instance->nvme_page_size))
2081 ret_target_prop = megasas_get_target_prop(instance, sdev);
2083 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2084 megasas_set_static_target_properties(sdev, is_target_prop);
2086 /* This sdev property may change post OCR */
2087 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2089 mutex_unlock(&instance->reset_mutex);
2094 static int megasas_slave_alloc(struct scsi_device *sdev)
2096 u16 pd_index = 0, ld_tgt_id;
2097 struct megasas_instance *instance ;
2098 struct MR_PRIV_DEVICE *mr_device_priv_data;
2100 instance = megasas_lookup_instance(sdev->host->host_no);
2101 if (!MEGASAS_IS_LOGICAL(sdev)) {
2103 * Open the OS scan to the SYSTEM PD
2106 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2108 if ((instance->pd_list_not_supported ||
2109 instance->pd_list[pd_index].driveState ==
2110 MR_PD_STATE_SYSTEM)) {
2114 } else if (!MEGASAS_IS_LUN_VALID(sdev)) {
2115 sdev_printk(KERN_INFO, sdev, "%s: invalid LUN\n", __func__);
2120 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2122 if (!mr_device_priv_data)
2125 if (MEGASAS_IS_LOGICAL(sdev)) {
2126 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2127 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_ACTIVE;
2128 if (megasas_dbg_lvl & LD_PD_DEBUG)
2129 sdev_printk(KERN_INFO, sdev, "LD target ID %d created.\n", ld_tgt_id);
2132 sdev->hostdata = mr_device_priv_data;
2134 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2135 instance->r1_ldio_hint_default);
2139 static void megasas_slave_destroy(struct scsi_device *sdev)
2142 struct megasas_instance *instance;
2144 instance = megasas_lookup_instance(sdev->host->host_no);
2146 if (MEGASAS_IS_LOGICAL(sdev)) {
2147 if (!MEGASAS_IS_LUN_VALID(sdev)) {
2148 sdev_printk(KERN_INFO, sdev, "%s: invalid LUN\n", __func__);
2151 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2152 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_DELETED;
2153 if (megasas_dbg_lvl & LD_PD_DEBUG)
2154 sdev_printk(KERN_INFO, sdev,
2155 "LD target ID %d removed from OS stack\n", ld_tgt_id);
2158 kfree(sdev->hostdata);
2159 sdev->hostdata = NULL;
2163 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2165 * @instance: Adapter soft state
2168 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2171 struct megasas_cmd *cmd_mfi;
2172 struct megasas_cmd_fusion *cmd_fusion;
2173 struct fusion_context *fusion = instance->ctrl_context;
2175 /* Find all outstanding ioctls */
2177 for (i = 0; i < instance->max_fw_cmds; i++) {
2178 cmd_fusion = fusion->cmd_list[i];
2179 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2180 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2181 if (cmd_mfi->sync_cmd &&
2182 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2183 cmd_mfi->frame->hdr.cmd_status =
2184 MFI_STAT_WRONG_STATE;
2185 megasas_complete_cmd(instance,
2191 for (i = 0; i < instance->max_fw_cmds; i++) {
2192 cmd_mfi = instance->cmd_list[i];
2193 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2195 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2201 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2203 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2204 dev_warn(&instance->pdev->dev,
2205 "Adapter already dead, skipping kill HBA\n");
2209 /* Set critical error to block I/O & ioctls in case caller didn't */
2210 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2211 /* Wait 1 second to ensure IO or ioctls in build have posted */
2213 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2214 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2215 (instance->adapter_type != MFI_SERIES)) {
2216 if (!instance->requestorId) {
2217 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2219 readl(&instance->reg_set->doorbell);
2221 if (instance->requestorId && instance->peerIsPresent)
2222 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2224 writel(MFI_STOP_ADP,
2225 &instance->reg_set->inbound_doorbell);
2227 /* Complete outstanding ioctls when adapter is killed */
2228 megasas_complete_outstanding_ioctls(instance);
2232 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2233 * restored to max value
2234 * @instance: Adapter soft state
2238 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2240 unsigned long flags;
2242 if (instance->flag & MEGASAS_FW_BUSY
2243 && time_after(jiffies, instance->last_time + 5 * HZ)
2244 && atomic_read(&instance->fw_outstanding) <
2245 instance->throttlequeuedepth + 1) {
2247 spin_lock_irqsave(instance->host->host_lock, flags);
2248 instance->flag &= ~MEGASAS_FW_BUSY;
2250 instance->host->can_queue = instance->cur_can_queue;
2251 spin_unlock_irqrestore(instance->host->host_lock, flags);
2256 * megasas_complete_cmd_dpc - Returns FW's controller structure
2257 * @instance_addr: Address of adapter soft state
2259 * Tasklet to complete cmds
2261 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2266 struct megasas_cmd *cmd;
2267 struct megasas_instance *instance =
2268 (struct megasas_instance *)instance_addr;
2269 unsigned long flags;
2271 /* If we have already declared adapter dead, donot complete cmds */
2272 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2275 spin_lock_irqsave(&instance->completion_lock, flags);
2277 producer = le32_to_cpu(*instance->producer);
2278 consumer = le32_to_cpu(*instance->consumer);
2280 while (consumer != producer) {
2281 context = le32_to_cpu(instance->reply_queue[consumer]);
2282 if (context >= instance->max_fw_cmds) {
2283 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2288 cmd = instance->cmd_list[context];
2290 megasas_complete_cmd(instance, cmd, DID_OK);
2293 if (consumer == (instance->max_fw_cmds + 1)) {
2298 *instance->consumer = cpu_to_le32(producer);
2300 spin_unlock_irqrestore(&instance->completion_lock, flags);
2303 * Check if we can restore can_queue
2305 megasas_check_and_restore_queue_depth(instance);
2308 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2311 * megasas_start_timer - Initializes sriov heartbeat timer object
2312 * @instance: Adapter soft state
2315 void megasas_start_timer(struct megasas_instance *instance)
2317 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2319 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2320 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2325 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2328 process_fw_state_change_wq(struct work_struct *work);
2330 static void megasas_do_ocr(struct megasas_instance *instance)
2332 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2333 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2334 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2335 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2337 instance->instancet->disable_intr(instance);
2338 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2339 instance->issuepend_done = 0;
2341 atomic_set(&instance->fw_outstanding, 0);
2342 megasas_internal_reset_defer_cmds(instance);
2343 process_fw_state_change_wq(&instance->work_init);
2346 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2349 struct megasas_cmd *cmd;
2350 struct megasas_dcmd_frame *dcmd;
2351 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2352 dma_addr_t new_affiliation_111_h;
2356 cmd = megasas_get_cmd(instance);
2359 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2360 "Failed to get cmd for scsi%d\n",
2361 instance->host->host_no);
2365 dcmd = &cmd->frame->dcmd;
2367 if (!instance->vf_affiliation_111) {
2368 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2369 "affiliation for scsi%d\n", instance->host->host_no);
2370 megasas_return_cmd(instance, cmd);
2375 memset(instance->vf_affiliation_111, 0,
2376 sizeof(struct MR_LD_VF_AFFILIATION_111));
2378 new_affiliation_111 =
2379 dma_alloc_coherent(&instance->pdev->dev,
2380 sizeof(struct MR_LD_VF_AFFILIATION_111),
2381 &new_affiliation_111_h, GFP_KERNEL);
2382 if (!new_affiliation_111) {
2383 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2384 "memory for new affiliation for scsi%d\n",
2385 instance->host->host_no);
2386 megasas_return_cmd(instance, cmd);
2391 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2393 dcmd->cmd = MFI_CMD_DCMD;
2394 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2395 dcmd->sge_count = 1;
2396 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2399 dcmd->data_xfer_len =
2400 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2401 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2404 dcmd->sgl.sge32[0].phys_addr =
2405 cpu_to_le32(instance->vf_affiliation_111_h);
2407 dcmd->sgl.sge32[0].phys_addr =
2408 cpu_to_le32(new_affiliation_111_h);
2410 dcmd->sgl.sge32[0].length = cpu_to_le32(
2411 sizeof(struct MR_LD_VF_AFFILIATION_111));
2413 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2414 "scsi%d\n", instance->host->host_no);
2416 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2417 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2418 " failed with status 0x%x for scsi%d\n",
2419 dcmd->cmd_status, instance->host->host_no);
2420 retval = 1; /* Do a scan if we couldn't get affiliation */
2425 thisVf = new_affiliation_111->thisVf;
2426 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2427 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2428 new_affiliation_111->map[ld].policy[thisVf]) {
2429 dev_warn(&instance->pdev->dev, "SR-IOV: "
2430 "Got new LD/VF affiliation for scsi%d\n",
2431 instance->host->host_no);
2432 memcpy(instance->vf_affiliation_111,
2433 new_affiliation_111,
2434 sizeof(struct MR_LD_VF_AFFILIATION_111));
2440 if (new_affiliation_111) {
2441 dma_free_coherent(&instance->pdev->dev,
2442 sizeof(struct MR_LD_VF_AFFILIATION_111),
2443 new_affiliation_111,
2444 new_affiliation_111_h);
2447 megasas_return_cmd(instance, cmd);
2452 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2455 struct megasas_cmd *cmd;
2456 struct megasas_dcmd_frame *dcmd;
2457 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2458 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2459 dma_addr_t new_affiliation_h;
2460 int i, j, retval = 0, found = 0, doscan = 0;
2463 cmd = megasas_get_cmd(instance);
2466 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2467 "Failed to get cmd for scsi%d\n",
2468 instance->host->host_no);
2472 dcmd = &cmd->frame->dcmd;
2474 if (!instance->vf_affiliation) {
2475 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2476 "affiliation for scsi%d\n", instance->host->host_no);
2477 megasas_return_cmd(instance, cmd);
2482 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2483 sizeof(struct MR_LD_VF_AFFILIATION));
2486 dma_alloc_coherent(&instance->pdev->dev,
2487 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2488 &new_affiliation_h, GFP_KERNEL);
2489 if (!new_affiliation) {
2490 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2491 "memory for new affiliation for scsi%d\n",
2492 instance->host->host_no);
2493 megasas_return_cmd(instance, cmd);
2498 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2500 dcmd->cmd = MFI_CMD_DCMD;
2501 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2502 dcmd->sge_count = 1;
2503 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2506 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2507 sizeof(struct MR_LD_VF_AFFILIATION));
2508 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2511 dcmd->sgl.sge32[0].phys_addr =
2512 cpu_to_le32(instance->vf_affiliation_h);
2514 dcmd->sgl.sge32[0].phys_addr =
2515 cpu_to_le32(new_affiliation_h);
2517 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2518 sizeof(struct MR_LD_VF_AFFILIATION));
2520 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2521 "scsi%d\n", instance->host->host_no);
2524 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2525 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2526 " failed with status 0x%x for scsi%d\n",
2527 dcmd->cmd_status, instance->host->host_no);
2528 retval = 1; /* Do a scan if we couldn't get affiliation */
2533 if (!new_affiliation->ldCount) {
2534 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2535 "affiliation for passive path for scsi%d\n",
2536 instance->host->host_no);
2540 newmap = new_affiliation->map;
2541 savedmap = instance->vf_affiliation->map;
2542 thisVf = new_affiliation->thisVf;
2543 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2545 for (j = 0; j < instance->vf_affiliation->ldCount;
2547 if (newmap->ref.targetId ==
2548 savedmap->ref.targetId) {
2550 if (newmap->policy[thisVf] !=
2551 savedmap->policy[thisVf]) {
2556 savedmap = (struct MR_LD_VF_MAP *)
2557 ((unsigned char *)savedmap +
2560 if (!found && newmap->policy[thisVf] !=
2561 MR_LD_ACCESS_HIDDEN) {
2565 newmap = (struct MR_LD_VF_MAP *)
2566 ((unsigned char *)newmap + newmap->size);
2569 newmap = new_affiliation->map;
2570 savedmap = instance->vf_affiliation->map;
2572 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2574 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2575 if (savedmap->ref.targetId ==
2576 newmap->ref.targetId) {
2578 if (savedmap->policy[thisVf] !=
2579 newmap->policy[thisVf]) {
2584 newmap = (struct MR_LD_VF_MAP *)
2585 ((unsigned char *)newmap +
2588 if (!found && savedmap->policy[thisVf] !=
2589 MR_LD_ACCESS_HIDDEN) {
2593 savedmap = (struct MR_LD_VF_MAP *)
2594 ((unsigned char *)savedmap +
2600 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2601 "affiliation for scsi%d\n", instance->host->host_no);
2602 memcpy(instance->vf_affiliation, new_affiliation,
2603 new_affiliation->size);
2607 if (new_affiliation)
2608 dma_free_coherent(&instance->pdev->dev,
2609 (MAX_LOGICAL_DRIVES + 1) *
2610 sizeof(struct MR_LD_VF_AFFILIATION),
2611 new_affiliation, new_affiliation_h);
2612 megasas_return_cmd(instance, cmd);
2617 /* This function will get the current SR-IOV LD/VF affiliation */
2618 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2623 if (instance->PlasmaFW111)
2624 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2626 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2630 /* This function will tell FW to start the SR-IOV heartbeat */
2631 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2634 struct megasas_cmd *cmd;
2635 struct megasas_dcmd_frame *dcmd;
2638 cmd = megasas_get_cmd(instance);
2641 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2642 "Failed to get cmd for scsi%d\n",
2643 instance->host->host_no);
2647 dcmd = &cmd->frame->dcmd;
2650 instance->hb_host_mem =
2651 dma_alloc_coherent(&instance->pdev->dev,
2652 sizeof(struct MR_CTRL_HB_HOST_MEM),
2653 &instance->hb_host_mem_h,
2655 if (!instance->hb_host_mem) {
2656 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2657 " memory for heartbeat host memory for scsi%d\n",
2658 instance->host->host_no);
2664 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2666 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2667 dcmd->cmd = MFI_CMD_DCMD;
2668 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2669 dcmd->sge_count = 1;
2670 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2673 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2674 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2676 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2677 sizeof(struct MR_CTRL_HB_HOST_MEM));
2679 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2680 instance->host->host_no);
2682 if ((instance->adapter_type != MFI_SERIES) &&
2683 !instance->mask_interrupts)
2684 retval = megasas_issue_blocked_cmd(instance, cmd,
2685 MEGASAS_ROUTINE_WAIT_TIME_VF);
2687 retval = megasas_issue_polled(instance, cmd);
2690 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2691 "_MEM_ALLOC DCMD %s for scsi%d\n",
2692 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2693 "timed out" : "failed", instance->host->host_no);
2698 megasas_return_cmd(instance, cmd);
2703 /* Handler for SR-IOV heartbeat */
2704 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2706 struct megasas_instance *instance =
2707 from_timer(instance, t, sriov_heartbeat_timer);
2709 if (instance->hb_host_mem->HB.fwCounter !=
2710 instance->hb_host_mem->HB.driverCounter) {
2711 instance->hb_host_mem->HB.driverCounter =
2712 instance->hb_host_mem->HB.fwCounter;
2713 mod_timer(&instance->sriov_heartbeat_timer,
2714 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2716 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2717 "completed for scsi%d\n", instance->host->host_no);
2718 schedule_work(&instance->work_init);
2723 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2724 * @instance: Adapter soft state
2726 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2727 * complete all its outstanding commands. Returns error if one or more IOs
2728 * are pending after this time period. It also marks the controller dead.
2730 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2732 int i, sl, outstanding;
2734 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2735 unsigned long flags;
2736 struct list_head clist_local;
2737 struct megasas_cmd *reset_cmd;
2740 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2741 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2742 __func__, __LINE__);
2746 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2748 INIT_LIST_HEAD(&clist_local);
2749 spin_lock_irqsave(&instance->hba_lock, flags);
2750 list_splice_init(&instance->internal_reset_pending_q,
2752 spin_unlock_irqrestore(&instance->hba_lock, flags);
2754 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2755 for (i = 0; i < wait_time; i++) {
2757 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2761 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2762 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2763 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2768 while (!list_empty(&clist_local)) {
2769 reset_cmd = list_entry((&clist_local)->next,
2770 struct megasas_cmd, list);
2771 list_del_init(&reset_cmd->list);
2772 if (reset_cmd->scmd) {
2773 reset_cmd->scmd->result = DID_REQUEUE << 16;
2774 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2775 reset_index, reset_cmd,
2776 reset_cmd->scmd->cmnd[0]);
2778 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2779 megasas_return_cmd(instance, reset_cmd);
2780 } else if (reset_cmd->sync_cmd) {
2781 dev_notice(&instance->pdev->dev, "%p synch cmds"
2785 reset_cmd->cmd_status_drv = DCMD_INIT;
2786 instance->instancet->fire_cmd(instance,
2787 reset_cmd->frame_phys_addr,
2788 0, instance->reg_set);
2790 dev_notice(&instance->pdev->dev, "%p unexpected"
2800 for (i = 0; i < resetwaittime; i++) {
2801 outstanding = atomic_read(&instance->fw_outstanding);
2806 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2807 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2808 "commands to complete\n",i,outstanding);
2810 * Call cmd completion routine. Cmd to be
2811 * be completed directly without depending on isr.
2813 megasas_complete_cmd_dpc((unsigned long)instance);
2820 outstanding = atomic_read(&instance->fw_outstanding);
2821 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2823 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2824 goto no_outstanding;
2826 if (instance->disableOnlineCtrlReset)
2827 goto kill_hba_and_failed;
2829 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2830 dev_info(&instance->pdev->dev,
2831 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2832 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2834 goto kill_hba_and_failed;
2835 megasas_do_ocr(instance);
2837 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2838 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2839 __func__, __LINE__);
2842 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2843 __func__, __LINE__);
2845 for (sl = 0; sl < 10; sl++)
2848 outstanding = atomic_read(&instance->fw_outstanding);
2850 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2851 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2852 goto no_outstanding;
2859 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2860 __func__, __LINE__);
2863 kill_hba_and_failed:
2865 /* Reset not supported, kill adapter */
2866 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2867 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2868 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2869 atomic_read(&instance->fw_outstanding));
2870 megasas_dump_pending_frames(instance);
2871 megaraid_sas_kill_hba(instance);
2877 * megasas_generic_reset - Generic reset routine
2878 * @scmd: Mid-layer SCSI command
2880 * This routine implements a generic reset handler for device, bus and host
2881 * reset requests. Device, bus and host specific reset handlers can use this
2882 * function after they do their specific tasks.
2884 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2887 struct megasas_instance *instance;
2889 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2891 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2892 scmd->cmnd[0], scmd->retries);
2894 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2895 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2899 ret_val = megasas_wait_for_outstanding(instance);
2900 if (ret_val == SUCCESS)
2901 dev_notice(&instance->pdev->dev, "reset successful\n");
2903 dev_err(&instance->pdev->dev, "failed to do reset\n");
2909 * megasas_reset_timer - quiesce the adapter if required
2912 * Sets the FW busy flag and reduces the host->can_queue if the
2913 * cmd has not been completed within the timeout period.
2916 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2918 struct megasas_instance *instance;
2919 unsigned long flags;
2921 if (time_after(jiffies, scmd->jiffies_at_alloc +
2922 (scmd_timeout * 2) * HZ)) {
2926 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2927 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2928 /* FW is busy, throttle IO */
2929 spin_lock_irqsave(instance->host->host_lock, flags);
2931 instance->host->can_queue = instance->throttlequeuedepth;
2932 instance->last_time = jiffies;
2933 instance->flag |= MEGASAS_FW_BUSY;
2935 spin_unlock_irqrestore(instance->host->host_lock, flags);
2937 return BLK_EH_RESET_TIMER;
2941 * megasas_dump - This function will print hexdump of provided buffer.
2942 * @buf: Buffer to be dumped
2943 * @sz: Size in bytes
2944 * @format: Different formats of dumping e.g. format=n will
2945 * cause only 'n' 32 bit words to be dumped in a single
2949 megasas_dump(void *buf, int sz, int format)
2952 __le32 *buf_loc = (__le32 *)buf;
2954 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2955 if ((i % format) == 0) {
2957 printk(KERN_CONT "\n");
2958 printk(KERN_CONT "%08x: ", (i * 4));
2960 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2962 printk(KERN_CONT "\n");
2966 * megasas_dump_reg_set - This function will print hexdump of register set
2967 * @reg_set: Register set to be dumped
2970 megasas_dump_reg_set(void __iomem *reg_set)
2972 unsigned int i, sz = 256;
2973 u32 __iomem *reg = (u32 __iomem *)reg_set;
2975 for (i = 0; i < (sz / sizeof(u32)); i++)
2976 printk("%08x: %08x\n", (i * 4), readl(®[i]));
2980 * megasas_dump_fusion_io - This function will print key details
2982 * @scmd: SCSI command pointer of SCSI IO
2985 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2987 struct megasas_cmd_fusion *cmd;
2988 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2989 struct megasas_instance *instance;
2991 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2992 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2994 scmd_printk(KERN_INFO, scmd,
2995 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2996 scmd, scmd->retries, scmd->allowed);
2997 scsi_print_command(scmd);
3000 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
3001 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
3002 scmd_printk(KERN_INFO, scmd,
3003 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
3004 req_desc->SCSIIO.RequestFlags,
3005 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
3006 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
3008 printk(KERN_INFO "IO request frame:\n");
3009 megasas_dump(cmd->io_request,
3010 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
3011 printk(KERN_INFO "Chain frame:\n");
3012 megasas_dump(cmd->sg_frame,
3013 instance->max_chain_frame_sz, 8);
3019 * megasas_dump_sys_regs - This function will dump system registers through
3021 * @reg_set: Pointer to System register set.
3022 * @buf: Buffer to which output is to be written.
3023 * @return: Number of bytes written to buffer.
3025 static inline ssize_t
3026 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
3028 unsigned int i, sz = 256;
3029 int bytes_wrote = 0;
3030 char *loc = (char *)buf;
3031 u32 __iomem *reg = (u32 __iomem *)reg_set;
3033 for (i = 0; i < sz / sizeof(u32); i++) {
3034 bytes_wrote += scnprintf(loc + bytes_wrote,
3035 PAGE_SIZE - bytes_wrote,
3036 "%08x: %08x\n", (i * 4),
3043 * megasas_reset_bus_host - Bus & host reset handler entry point
3044 * @scmd: Mid-layer SCSI command
3046 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
3049 struct megasas_instance *instance;
3051 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3053 scmd_printk(KERN_INFO, scmd,
3054 "OCR is requested due to IO timeout!!\n");
3056 scmd_printk(KERN_INFO, scmd,
3057 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
3058 scmd->device->host->shost_state,
3059 scsi_host_busy(scmd->device->host),
3060 atomic_read(&instance->fw_outstanding));
3062 * First wait for all commands to complete
3064 if (instance->adapter_type == MFI_SERIES) {
3065 ret = megasas_generic_reset(scmd);
3067 megasas_dump_fusion_io(scmd);
3068 ret = megasas_reset_fusion(scmd->device->host,
3069 SCSIIO_TIMEOUT_OCR);
3076 * megasas_task_abort - Issues task abort request to firmware
3077 * (supported only for fusion adapters)
3078 * @scmd: SCSI command pointer
3080 static int megasas_task_abort(struct scsi_cmnd *scmd)
3083 struct megasas_instance *instance;
3085 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3087 if (instance->adapter_type != MFI_SERIES)
3088 ret = megasas_task_abort_fusion(scmd);
3090 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3098 * megasas_reset_target: Issues target reset request to firmware
3099 * (supported only for fusion adapters)
3100 * @scmd: SCSI command pointer
3102 static int megasas_reset_target(struct scsi_cmnd *scmd)
3105 struct megasas_instance *instance;
3107 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3109 if (instance->adapter_type != MFI_SERIES)
3110 ret = megasas_reset_target_fusion(scmd);
3112 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3120 * megasas_bios_param - Returns disk geometry for a disk
3121 * @sdev: device handle
3122 * @bdev: block device
3123 * @capacity: drive capacity
3124 * @geom: geometry parameters
3127 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3128 sector_t capacity, int geom[])
3135 /* Default heads (64) & sectors (32) */
3139 tmp = heads * sectors;
3140 cylinders = capacity;
3142 sector_div(cylinders, tmp);
3145 * Handle extended translation size for logical drives > 1Gb
3148 if (capacity >= 0x200000) {
3151 tmp = heads*sectors;
3152 cylinders = capacity;
3153 sector_div(cylinders, tmp);
3158 geom[2] = cylinders;
3163 static void megasas_aen_polling(struct work_struct *work);
3166 * megasas_service_aen - Processes an event notification
3167 * @instance: Adapter soft state
3168 * @cmd: AEN command completed by the ISR
3170 * For AEN, driver sends a command down to FW that is held by the FW till an
3171 * event occurs. When an event of interest occurs, FW completes the command
3172 * that it was previously holding.
3174 * This routines sends SIGIO signal to processes that have registered with the
3178 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3180 unsigned long flags;
3183 * Don't signal app if it is just an aborted previously registered aen
3185 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3186 spin_lock_irqsave(&poll_aen_lock, flags);
3187 megasas_poll_wait_aen = 1;
3188 spin_unlock_irqrestore(&poll_aen_lock, flags);
3189 wake_up(&megasas_poll_wait);
3190 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3195 instance->aen_cmd = NULL;
3197 megasas_return_cmd(instance, cmd);
3199 if ((instance->unload == 0) &&
3200 ((instance->issuepend_done == 1))) {
3201 struct megasas_aen_event *ev;
3203 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3205 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3207 ev->instance = instance;
3209 INIT_DELAYED_WORK(&ev->hotplug_work,
3210 megasas_aen_polling);
3211 schedule_delayed_work(&ev->hotplug_work, 0);
3217 fw_crash_buffer_store(struct device *cdev,
3218 struct device_attribute *attr, const char *buf, size_t count)
3220 struct Scsi_Host *shost = class_to_shost(cdev);
3221 struct megasas_instance *instance =
3222 (struct megasas_instance *) shost->hostdata;
3225 if (kstrtoint(buf, 0, &val) != 0)
3228 mutex_lock(&instance->crashdump_lock);
3229 instance->fw_crash_buffer_offset = val;
3230 mutex_unlock(&instance->crashdump_lock);
3235 fw_crash_buffer_show(struct device *cdev,
3236 struct device_attribute *attr, char *buf)
3238 struct Scsi_Host *shost = class_to_shost(cdev);
3239 struct megasas_instance *instance =
3240 (struct megasas_instance *) shost->hostdata;
3242 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3243 unsigned long chunk_left_bytes;
3244 unsigned long src_addr;
3247 mutex_lock(&instance->crashdump_lock);
3248 buff_offset = instance->fw_crash_buffer_offset;
3249 if (!instance->crash_dump_buf ||
3250 !((instance->fw_crash_state == AVAILABLE) ||
3251 (instance->fw_crash_state == COPYING))) {
3252 dev_err(&instance->pdev->dev,
3253 "Firmware crash dump is not available\n");
3254 mutex_unlock(&instance->crashdump_lock);
3258 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3259 dev_err(&instance->pdev->dev,
3260 "Firmware crash dump offset is out of range\n");
3261 mutex_unlock(&instance->crashdump_lock);
3265 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3266 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3267 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3268 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3270 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3271 (buff_offset % dmachunk);
3272 memcpy(buf, (void *)src_addr, size);
3273 mutex_unlock(&instance->crashdump_lock);
3279 fw_crash_buffer_size_show(struct device *cdev,
3280 struct device_attribute *attr, char *buf)
3282 struct Scsi_Host *shost = class_to_shost(cdev);
3283 struct megasas_instance *instance =
3284 (struct megasas_instance *) shost->hostdata;
3286 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3287 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3291 fw_crash_state_store(struct device *cdev,
3292 struct device_attribute *attr, const char *buf, size_t count)
3294 struct Scsi_Host *shost = class_to_shost(cdev);
3295 struct megasas_instance *instance =
3296 (struct megasas_instance *) shost->hostdata;
3299 if (kstrtoint(buf, 0, &val) != 0)
3302 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3303 dev_err(&instance->pdev->dev, "application updates invalid "
3304 "firmware crash state\n");
3308 instance->fw_crash_state = val;
3310 if ((val == COPIED) || (val == COPY_ERROR)) {
3311 mutex_lock(&instance->crashdump_lock);
3312 megasas_free_host_crash_buffer(instance);
3313 mutex_unlock(&instance->crashdump_lock);
3314 if (val == COPY_ERROR)
3315 dev_info(&instance->pdev->dev, "application failed to "
3316 "copy Firmware crash dump\n");
3318 dev_info(&instance->pdev->dev, "Firmware crash dump "
3319 "copied successfully\n");
3325 fw_crash_state_show(struct device *cdev,
3326 struct device_attribute *attr, char *buf)
3328 struct Scsi_Host *shost = class_to_shost(cdev);
3329 struct megasas_instance *instance =
3330 (struct megasas_instance *) shost->hostdata;
3332 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3336 page_size_show(struct device *cdev,
3337 struct device_attribute *attr, char *buf)
3339 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3343 ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3346 struct Scsi_Host *shost = class_to_shost(cdev);
3347 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3349 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3353 fw_cmds_outstanding_show(struct device *cdev,
3354 struct device_attribute *attr, char *buf)
3356 struct Scsi_Host *shost = class_to_shost(cdev);
3357 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3359 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3363 enable_sdev_max_qd_show(struct device *cdev,
3364 struct device_attribute *attr, char *buf)
3366 struct Scsi_Host *shost = class_to_shost(cdev);
3367 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3369 return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3373 enable_sdev_max_qd_store(struct device *cdev,
3374 struct device_attribute *attr, const char *buf, size_t count)
3376 struct Scsi_Host *shost = class_to_shost(cdev);
3377 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3379 bool is_target_prop;
3380 int ret_target_prop = DCMD_FAILED;
3381 struct scsi_device *sdev;
3383 if (kstrtou32(buf, 0, &val) != 0) {
3384 pr_err("megasas: could not set enable_sdev_max_qd\n");
3388 mutex_lock(&instance->reset_mutex);
3390 instance->enable_sdev_max_qd = true;
3392 instance->enable_sdev_max_qd = false;
3394 shost_for_each_device(sdev, shost) {
3395 ret_target_prop = megasas_get_target_prop(instance, sdev);
3396 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3397 megasas_set_fw_assisted_qd(sdev, is_target_prop);
3399 mutex_unlock(&instance->reset_mutex);
3405 dump_system_regs_show(struct device *cdev,
3406 struct device_attribute *attr, char *buf)
3408 struct Scsi_Host *shost = class_to_shost(cdev);
3409 struct megasas_instance *instance =
3410 (struct megasas_instance *)shost->hostdata;
3412 return megasas_dump_sys_regs(instance->reg_set, buf);
3416 raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3419 struct Scsi_Host *shost = class_to_shost(cdev);
3420 struct megasas_instance *instance =
3421 (struct megasas_instance *)shost->hostdata;
3423 return snprintf(buf, PAGE_SIZE, "%ld\n",
3424 (unsigned long)instance->map_id);
3427 static DEVICE_ATTR_RW(fw_crash_buffer);
3428 static DEVICE_ATTR_RO(fw_crash_buffer_size);
3429 static DEVICE_ATTR_RW(fw_crash_state);
3430 static DEVICE_ATTR_RO(page_size);
3431 static DEVICE_ATTR_RO(ldio_outstanding);
3432 static DEVICE_ATTR_RO(fw_cmds_outstanding);
3433 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3434 static DEVICE_ATTR_RO(dump_system_regs);
3435 static DEVICE_ATTR_RO(raid_map_id);
3437 static struct device_attribute *megaraid_host_attrs[] = {
3438 &dev_attr_fw_crash_buffer_size,
3439 &dev_attr_fw_crash_buffer,
3440 &dev_attr_fw_crash_state,
3441 &dev_attr_page_size,
3442 &dev_attr_ldio_outstanding,
3443 &dev_attr_fw_cmds_outstanding,
3444 &dev_attr_enable_sdev_max_qd,
3445 &dev_attr_dump_system_regs,
3446 &dev_attr_raid_map_id,
3451 * Scsi host template for megaraid_sas driver
3453 static struct scsi_host_template megasas_template = {
3455 .module = THIS_MODULE,
3456 .name = "Avago SAS based MegaRAID driver",
3457 .proc_name = "megaraid_sas",
3458 .slave_configure = megasas_slave_configure,
3459 .slave_alloc = megasas_slave_alloc,
3460 .slave_destroy = megasas_slave_destroy,
3461 .queuecommand = megasas_queue_command,
3462 .eh_target_reset_handler = megasas_reset_target,
3463 .eh_abort_handler = megasas_task_abort,
3464 .eh_host_reset_handler = megasas_reset_bus_host,
3465 .eh_timed_out = megasas_reset_timer,
3466 .shost_attrs = megaraid_host_attrs,
3467 .bios_param = megasas_bios_param,
3468 .change_queue_depth = scsi_change_queue_depth,
3469 .max_segment_size = 0xffffffff,
3473 * megasas_complete_int_cmd - Completes an internal command
3474 * @instance: Adapter soft state
3475 * @cmd: Command to be completed
3477 * The megasas_issue_blocked_cmd() function waits for a command to complete
3478 * after it issues a command. This function wakes up that waiting routine by
3479 * calling wake_up() on the wait queue.
3482 megasas_complete_int_cmd(struct megasas_instance *instance,
3483 struct megasas_cmd *cmd)
3485 if (cmd->cmd_status_drv == DCMD_INIT)
3486 cmd->cmd_status_drv =
3487 (cmd->frame->io.cmd_status == MFI_STAT_OK) ?
3488 DCMD_SUCCESS : DCMD_FAILED;
3490 wake_up(&instance->int_cmd_wait_q);
3494 * megasas_complete_abort - Completes aborting a command
3495 * @instance: Adapter soft state
3496 * @cmd: Cmd that was issued to abort another cmd
3498 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3499 * after it issues an abort on a previously issued command. This function
3500 * wakes up all functions waiting on the same wait queue.
3503 megasas_complete_abort(struct megasas_instance *instance,
3504 struct megasas_cmd *cmd)
3506 if (cmd->sync_cmd) {
3508 cmd->cmd_status_drv = DCMD_SUCCESS;
3509 wake_up(&instance->abort_cmd_wait_q);
3514 megasas_set_ld_removed_by_fw(struct megasas_instance *instance)
3518 for (i = 0; (i < MEGASAS_MAX_LD_IDS); i++) {
3519 if (instance->ld_ids_prev[i] != 0xff &&
3520 instance->ld_ids_from_raidmap[i] == 0xff) {
3521 if (megasas_dbg_lvl & LD_PD_DEBUG)
3522 dev_info(&instance->pdev->dev,
3523 "LD target ID %d removed from RAID map\n", i);
3524 instance->ld_tgtid_status[i] = LD_TARGET_ID_DELETED;
3530 * megasas_complete_cmd - Completes a command
3531 * @instance: Adapter soft state
3532 * @cmd: Command to be completed
3533 * @alt_status: If non-zero, use this value as status to
3534 * SCSI mid-layer instead of the value returned
3535 * by the FW. This should be used if caller wants
3536 * an alternate status (as in the case of aborted
3540 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3544 struct megasas_header *hdr = &cmd->frame->hdr;
3545 unsigned long flags;
3546 struct fusion_context *fusion = instance->ctrl_context;
3549 /* flag for the retry reset */
3550 cmd->retry_for_fw_reset = 0;
3553 cmd->scmd->SCp.ptr = NULL;
3556 case MFI_CMD_INVALID:
3557 /* Some older 1068 controller FW may keep a pended
3558 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3559 when booting the kdump kernel. Ignore this command to
3560 prevent a kernel panic on shutdown of the kdump kernel. */
3561 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3563 dev_warn(&instance->pdev->dev, "If you have a controller "
3564 "other than PERC5, please upgrade your firmware\n");
3566 case MFI_CMD_PD_SCSI_IO:
3567 case MFI_CMD_LD_SCSI_IO:
3570 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3571 * issued either through an IO path or an IOCTL path. If it
3572 * was via IOCTL, we will send it to internal completion.
3574 if (cmd->sync_cmd) {
3576 megasas_complete_int_cmd(instance, cmd);
3581 case MFI_CMD_LD_READ:
3582 case MFI_CMD_LD_WRITE:
3585 cmd->scmd->result = alt_status << 16;
3591 atomic_dec(&instance->fw_outstanding);
3593 scsi_dma_unmap(cmd->scmd);
3594 cmd->scmd->scsi_done(cmd->scmd);
3595 megasas_return_cmd(instance, cmd);
3600 switch (hdr->cmd_status) {
3603 cmd->scmd->result = DID_OK << 16;
3606 case MFI_STAT_SCSI_IO_FAILED:
3607 case MFI_STAT_LD_INIT_IN_PROGRESS:
3609 (DID_ERROR << 16) | hdr->scsi_status;
3612 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3614 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3616 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3617 memset(cmd->scmd->sense_buffer, 0,
3618 SCSI_SENSE_BUFFERSIZE);
3619 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3622 cmd->scmd->result |= DRIVER_SENSE << 24;
3627 case MFI_STAT_LD_OFFLINE:
3628 case MFI_STAT_DEVICE_NOT_FOUND:
3629 cmd->scmd->result = DID_BAD_TARGET << 16;
3633 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3635 cmd->scmd->result = DID_ERROR << 16;
3639 atomic_dec(&instance->fw_outstanding);
3641 scsi_dma_unmap(cmd->scmd);
3642 cmd->scmd->scsi_done(cmd->scmd);
3643 megasas_return_cmd(instance, cmd);
3650 case MFI_CMD_TOOLBOX:
3651 megasas_complete_int_cmd(instance, cmd);
3655 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3656 /* Check for LD map update */
3657 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3658 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3659 fusion->fast_path_io = 0;
3660 spin_lock_irqsave(instance->host->host_lock, flags);
3661 status = cmd->frame->hdr.cmd_status;
3662 instance->map_update_cmd = NULL;
3663 if (status != MFI_STAT_OK) {
3664 if (status != MFI_STAT_NOT_FOUND)
3665 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3666 cmd->frame->hdr.cmd_status);
3668 megasas_return_cmd(instance, cmd);
3669 spin_unlock_irqrestore(
3670 instance->host->host_lock,
3676 megasas_return_cmd(instance, cmd);
3679 * Set fast path IO to ZERO.
3680 * Validate Map will set proper value.
3681 * Meanwhile all IOs will go as LD IO.
3683 if (status == MFI_STAT_OK &&
3684 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3686 fusion->fast_path_io = 1;
3688 fusion->fast_path_io = 0;
3691 if (instance->adapter_type >= INVADER_SERIES)
3692 megasas_set_ld_removed_by_fw(instance);
3694 megasas_sync_map_info(instance);
3695 spin_unlock_irqrestore(instance->host->host_lock,
3700 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3701 opcode == MR_DCMD_CTRL_EVENT_GET) {
3702 spin_lock_irqsave(&poll_aen_lock, flags);
3703 megasas_poll_wait_aen = 0;
3704 spin_unlock_irqrestore(&poll_aen_lock, flags);
3707 /* FW has an updated PD sequence */
3708 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3709 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3711 spin_lock_irqsave(instance->host->host_lock, flags);
3712 status = cmd->frame->hdr.cmd_status;
3713 instance->jbod_seq_cmd = NULL;
3714 megasas_return_cmd(instance, cmd);
3716 if (status == MFI_STAT_OK) {
3717 instance->pd_seq_map_id++;
3718 /* Re-register a pd sync seq num cmd */
3719 if (megasas_sync_pd_seq_num(instance, true))
3720 instance->use_seqnum_jbod_fp = false;
3722 instance->use_seqnum_jbod_fp = false;
3724 spin_unlock_irqrestore(instance->host->host_lock, flags);
3729 * See if got an event notification
3731 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3732 megasas_service_aen(instance, cmd);
3734 megasas_complete_int_cmd(instance, cmd);
3740 * Cmd issued to abort another cmd returned
3742 megasas_complete_abort(instance, cmd);
3746 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3748 megasas_complete_int_cmd(instance, cmd);
3754 * megasas_issue_pending_cmds_again - issue all pending cmds
3755 * in FW again because of the fw reset
3756 * @instance: Adapter soft state
3759 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3761 struct megasas_cmd *cmd;
3762 struct list_head clist_local;
3763 union megasas_evt_class_locale class_locale;
3764 unsigned long flags;
3767 INIT_LIST_HEAD(&clist_local);
3768 spin_lock_irqsave(&instance->hba_lock, flags);
3769 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3770 spin_unlock_irqrestore(&instance->hba_lock, flags);
3772 while (!list_empty(&clist_local)) {
3773 cmd = list_entry((&clist_local)->next,
3774 struct megasas_cmd, list);
3775 list_del_init(&cmd->list);
3777 if (cmd->sync_cmd || cmd->scmd) {
3778 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3779 "detected to be pending while HBA reset\n",
3780 cmd, cmd->scmd, cmd->sync_cmd);
3782 cmd->retry_for_fw_reset++;
3784 if (cmd->retry_for_fw_reset == 3) {
3785 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3786 "was tried multiple times during reset."
3787 "Shutting down the HBA\n",
3788 cmd, cmd->scmd, cmd->sync_cmd);
3789 instance->instancet->disable_intr(instance);
3790 atomic_set(&instance->fw_reset_no_pci_access, 1);
3791 megaraid_sas_kill_hba(instance);
3796 if (cmd->sync_cmd == 1) {
3798 dev_notice(&instance->pdev->dev, "unexpected"
3799 "cmd attached to internal command!\n");
3801 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3802 "on the internal reset queue,"
3803 "issue it again.\n", cmd);
3804 cmd->cmd_status_drv = DCMD_INIT;
3805 instance->instancet->fire_cmd(instance,
3806 cmd->frame_phys_addr,
3807 0, instance->reg_set);
3808 } else if (cmd->scmd) {
3809 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3810 "detected on the internal queue, issue again.\n",
3811 cmd, cmd->scmd->cmnd[0]);
3813 atomic_inc(&instance->fw_outstanding);
3814 instance->instancet->fire_cmd(instance,
3815 cmd->frame_phys_addr,
3816 cmd->frame_count-1, instance->reg_set);
3818 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3819 "internal reset defer list while re-issue!!\n",
3824 if (instance->aen_cmd) {
3825 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3826 megasas_return_cmd(instance, instance->aen_cmd);
3828 instance->aen_cmd = NULL;
3832 * Initiate AEN (Asynchronous Event Notification)
3834 seq_num = instance->last_seq_num;
3835 class_locale.members.reserved = 0;
3836 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3837 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3839 megasas_register_aen(instance, seq_num, class_locale.word);
3843 * Move the internal reset pending commands to a deferred queue.
3845 * We move the commands pending at internal reset time to a
3846 * pending queue. This queue would be flushed after successful
3847 * completion of the internal reset sequence. if the internal reset
3848 * did not complete in time, the kernel reset handler would flush
3852 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3854 struct megasas_cmd *cmd;
3856 u16 max_cmd = instance->max_fw_cmds;
3858 unsigned long flags;
3861 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3862 for (i = 0; i < max_cmd; i++) {
3863 cmd = instance->cmd_list[i];
3864 if (cmd->sync_cmd == 1 || cmd->scmd) {
3865 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3866 "on the defer queue as internal\n",
3867 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3869 if (!list_empty(&cmd->list)) {
3870 dev_notice(&instance->pdev->dev, "ERROR while"
3871 " moving this cmd:%p, %d %p, it was"
3872 "discovered on some list?\n",
3873 cmd, cmd->sync_cmd, cmd->scmd);
3875 list_del_init(&cmd->list);
3878 list_add_tail(&cmd->list,
3879 &instance->internal_reset_pending_q);
3882 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3887 process_fw_state_change_wq(struct work_struct *work)
3889 struct megasas_instance *instance =
3890 container_of(work, struct megasas_instance, work_init);
3892 unsigned long flags;
3894 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3895 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3896 atomic_read(&instance->adprecovery));
3900 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3901 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3902 "state, restarting it...\n");
3904 instance->instancet->disable_intr(instance);
3905 atomic_set(&instance->fw_outstanding, 0);
3907 atomic_set(&instance->fw_reset_no_pci_access, 1);
3908 instance->instancet->adp_reset(instance, instance->reg_set);
3909 atomic_set(&instance->fw_reset_no_pci_access, 0);
3911 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3912 "initiating next stage...\n");
3914 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3915 "state 2 starting...\n");
3917 /* waiting for about 20 second before start the second init */
3918 for (wait = 0; wait < 30; wait++) {
3922 if (megasas_transition_to_ready(instance, 1)) {
3923 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3925 atomic_set(&instance->fw_reset_no_pci_access, 1);
3926 megaraid_sas_kill_hba(instance);
3930 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3931 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3932 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3934 *instance->consumer = *instance->producer;
3936 *instance->consumer = 0;
3937 *instance->producer = 0;
3940 megasas_issue_init_mfi(instance);
3942 spin_lock_irqsave(&instance->hba_lock, flags);
3943 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3944 spin_unlock_irqrestore(&instance->hba_lock, flags);
3945 instance->instancet->enable_intr(instance);
3947 megasas_issue_pending_cmds_again(instance);
3948 instance->issuepend_done = 1;
3953 * megasas_deplete_reply_queue - Processes all completed commands
3954 * @instance: Adapter soft state
3955 * @alt_status: Alternate status to be returned to
3956 * SCSI mid-layer instead of the status
3957 * returned by the FW
3958 * Note: this must be called with hba lock held
3961 megasas_deplete_reply_queue(struct megasas_instance *instance,
3967 if ((mfiStatus = instance->instancet->check_reset(instance,
3968 instance->reg_set)) == 1) {
3972 mfiStatus = instance->instancet->clear_intr(instance);
3973 if (mfiStatus == 0) {
3974 /* Hardware may not set outbound_intr_status in MSI-X mode */
3975 if (!instance->msix_vectors)
3979 instance->mfiStatus = mfiStatus;
3981 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3982 fw_state = instance->instancet->read_fw_status_reg(
3983 instance) & MFI_STATE_MASK;
3985 if (fw_state != MFI_STATE_FAULT) {
3986 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3990 if ((fw_state == MFI_STATE_FAULT) &&
3991 (instance->disableOnlineCtrlReset == 0)) {
3992 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3994 if ((instance->pdev->device ==
3995 PCI_DEVICE_ID_LSI_SAS1064R) ||
3996 (instance->pdev->device ==
3997 PCI_DEVICE_ID_DELL_PERC5) ||
3998 (instance->pdev->device ==
3999 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
4001 *instance->consumer =
4002 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
4006 instance->instancet->disable_intr(instance);
4007 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4008 instance->issuepend_done = 0;
4010 atomic_set(&instance->fw_outstanding, 0);
4011 megasas_internal_reset_defer_cmds(instance);
4013 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
4014 fw_state, atomic_read(&instance->adprecovery));
4016 schedule_work(&instance->work_init);
4020 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
4021 fw_state, instance->disableOnlineCtrlReset);
4025 tasklet_schedule(&instance->isr_tasklet);
4030 * megasas_isr - isr entry point
4032 * @devp: IRQ context address
4034 static irqreturn_t megasas_isr(int irq, void *devp)
4036 struct megasas_irq_context *irq_context = devp;
4037 struct megasas_instance *instance = irq_context->instance;
4038 unsigned long flags;
4041 if (atomic_read(&instance->fw_reset_no_pci_access))
4044 spin_lock_irqsave(&instance->hba_lock, flags);
4045 rc = megasas_deplete_reply_queue(instance, DID_OK);
4046 spin_unlock_irqrestore(&instance->hba_lock, flags);
4052 * megasas_transition_to_ready - Move the FW to READY state
4053 * @instance: Adapter soft state
4054 * @ocr: Adapter reset state
4056 * During the initialization, FW passes can potentially be in any one of
4057 * several possible states. If the FW in operational, waiting-for-handshake
4058 * states, driver must take steps to bring it to ready state. Otherwise, it
4059 * has to wait for the ready state.
4062 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
4067 u32 abs_state, curr_abs_state;
4069 abs_state = instance->instancet->read_fw_status_reg(instance);
4070 fw_state = abs_state & MFI_STATE_MASK;
4072 if (fw_state != MFI_STATE_READY)
4073 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
4076 while (fw_state != MFI_STATE_READY) {
4080 case MFI_STATE_FAULT:
4081 dev_printk(KERN_ERR, &instance->pdev->dev,
4082 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
4083 abs_state & MFI_STATE_FAULT_CODE,
4084 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4086 max_wait = MEGASAS_RESET_WAIT_TIME;
4089 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4090 megasas_dump_reg_set(instance->reg_set);
4094 case MFI_STATE_WAIT_HANDSHAKE:
4096 * Set the CLR bit in inbound doorbell
4098 if ((instance->pdev->device ==
4099 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4100 (instance->pdev->device ==
4101 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4102 (instance->adapter_type != MFI_SERIES))
4104 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4105 &instance->reg_set->doorbell);
4108 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4109 &instance->reg_set->inbound_doorbell);
4111 max_wait = MEGASAS_RESET_WAIT_TIME;
4114 case MFI_STATE_BOOT_MESSAGE_PENDING:
4115 if ((instance->pdev->device ==
4116 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4117 (instance->pdev->device ==
4118 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4119 (instance->adapter_type != MFI_SERIES))
4120 writel(MFI_INIT_HOTPLUG,
4121 &instance->reg_set->doorbell);
4123 writel(MFI_INIT_HOTPLUG,
4124 &instance->reg_set->inbound_doorbell);
4126 max_wait = MEGASAS_RESET_WAIT_TIME;
4129 case MFI_STATE_OPERATIONAL:
4131 * Bring it to READY state; assuming max wait 10 secs
4133 instance->instancet->disable_intr(instance);
4134 if ((instance->pdev->device ==
4135 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4136 (instance->pdev->device ==
4137 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4138 (instance->adapter_type != MFI_SERIES)) {
4139 writel(MFI_RESET_FLAGS,
4140 &instance->reg_set->doorbell);
4142 if (instance->adapter_type != MFI_SERIES) {
4143 for (i = 0; i < (10 * 1000); i += 20) {
4155 writel(MFI_RESET_FLAGS,
4156 &instance->reg_set->inbound_doorbell);
4158 max_wait = MEGASAS_RESET_WAIT_TIME;
4161 case MFI_STATE_UNDEFINED:
4163 * This state should not last for more than 2 seconds
4165 max_wait = MEGASAS_RESET_WAIT_TIME;
4168 case MFI_STATE_BB_INIT:
4169 max_wait = MEGASAS_RESET_WAIT_TIME;
4172 case MFI_STATE_FW_INIT:
4173 max_wait = MEGASAS_RESET_WAIT_TIME;
4176 case MFI_STATE_FW_INIT_2:
4177 max_wait = MEGASAS_RESET_WAIT_TIME;
4180 case MFI_STATE_DEVICE_SCAN:
4181 max_wait = MEGASAS_RESET_WAIT_TIME;
4184 case MFI_STATE_FLUSH_CACHE:
4185 max_wait = MEGASAS_RESET_WAIT_TIME;
4189 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4191 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4192 megasas_dump_reg_set(instance->reg_set);
4197 * The cur_state should not last for more than max_wait secs
4199 for (i = 0; i < max_wait * 50; i++) {
4200 curr_abs_state = instance->instancet->
4201 read_fw_status_reg(instance);
4203 if (abs_state == curr_abs_state) {
4210 * Return error if fw_state hasn't changed after max_wait
4212 if (curr_abs_state == abs_state) {
4213 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4214 "in %d secs\n", fw_state, max_wait);
4215 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4216 megasas_dump_reg_set(instance->reg_set);
4220 abs_state = curr_abs_state;
4221 fw_state = curr_abs_state & MFI_STATE_MASK;
4223 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4229 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4230 * @instance: Adapter soft state
4232 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4235 u16 max_cmd = instance->max_mfi_cmds;
4236 struct megasas_cmd *cmd;
4238 if (!instance->frame_dma_pool)
4242 * Return all frames to pool
4244 for (i = 0; i < max_cmd; i++) {
4246 cmd = instance->cmd_list[i];
4249 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4250 cmd->frame_phys_addr);
4253 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4254 cmd->sense_phys_addr);
4258 * Now destroy the pool itself
4260 dma_pool_destroy(instance->frame_dma_pool);
4261 dma_pool_destroy(instance->sense_dma_pool);
4263 instance->frame_dma_pool = NULL;
4264 instance->sense_dma_pool = NULL;
4268 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4269 * @instance: Adapter soft state
4271 * Each command packet has an embedded DMA memory buffer that is used for
4272 * filling MFI frame and the SG list that immediately follows the frame. This
4273 * function creates those DMA memory buffers for each command packet by using
4274 * PCI pool facility.
4276 static int megasas_create_frame_pool(struct megasas_instance *instance)
4281 struct megasas_cmd *cmd;
4283 max_cmd = instance->max_mfi_cmds;
4286 * For MFI controllers.
4288 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4289 * Total 960 byte (15 MFI frame of 64 byte)
4291 * Fusion adapter require only 3 extra frame.
4292 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4293 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4294 * Total 192 byte (3 MFI frame of 64 byte)
4296 frame_count = (instance->adapter_type == MFI_SERIES) ?
4298 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4300 * Use DMA pool facility provided by PCI layer
4302 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4303 &instance->pdev->dev,
4304 instance->mfi_frame_size, 256, 0);
4306 if (!instance->frame_dma_pool) {
4307 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4311 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4312 &instance->pdev->dev, 128,
4315 if (!instance->sense_dma_pool) {
4316 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4318 dma_pool_destroy(instance->frame_dma_pool);
4319 instance->frame_dma_pool = NULL;
4325 * Allocate and attach a frame to each of the commands in cmd_list.
4326 * By making cmd->index as the context instead of the &cmd, we can
4327 * always use 32bit context regardless of the architecture
4329 for (i = 0; i < max_cmd; i++) {
4331 cmd = instance->cmd_list[i];
4333 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4334 GFP_KERNEL, &cmd->frame_phys_addr);
4336 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4337 GFP_KERNEL, &cmd->sense_phys_addr);
4340 * megasas_teardown_frame_pool() takes care of freeing
4341 * whatever has been allocated
4343 if (!cmd->frame || !cmd->sense) {
4344 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4345 megasas_teardown_frame_pool(instance);
4349 cmd->frame->io.context = cpu_to_le32(cmd->index);
4350 cmd->frame->io.pad_0 = 0;
4351 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4352 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4359 * megasas_free_cmds - Free all the cmds in the free cmd pool
4360 * @instance: Adapter soft state
4362 void megasas_free_cmds(struct megasas_instance *instance)
4366 /* First free the MFI frame pool */
4367 megasas_teardown_frame_pool(instance);
4369 /* Free all the commands in the cmd_list */
4370 for (i = 0; i < instance->max_mfi_cmds; i++)
4372 kfree(instance->cmd_list[i]);
4374 /* Free the cmd_list buffer itself */
4375 kfree(instance->cmd_list);
4376 instance->cmd_list = NULL;
4378 INIT_LIST_HEAD(&instance->cmd_pool);
4382 * megasas_alloc_cmds - Allocates the command packets
4383 * @instance: Adapter soft state
4385 * Each command that is issued to the FW, whether IO commands from the OS or
4386 * internal commands like IOCTLs, are wrapped in local data structure called
4387 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4390 * Each frame has a 32-bit field called context (tag). This context is used
4391 * to get back the megasas_cmd from the frame when a frame gets completed in
4392 * the ISR. Typically the address of the megasas_cmd itself would be used as
4393 * the context. But we wanted to keep the differences between 32 and 64 bit
4394 * systems to the mininum. We always use 32 bit integers for the context. In
4395 * this driver, the 32 bit values are the indices into an array cmd_list.
4396 * This array is used only to look up the megasas_cmd given the context. The
4397 * free commands themselves are maintained in a linked list called cmd_pool.
4399 int megasas_alloc_cmds(struct megasas_instance *instance)
4404 struct megasas_cmd *cmd;
4406 max_cmd = instance->max_mfi_cmds;
4409 * instance->cmd_list is an array of struct megasas_cmd pointers.
4410 * Allocate the dynamic array first and then allocate individual
4413 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4415 if (!instance->cmd_list) {
4416 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4420 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4422 for (i = 0; i < max_cmd; i++) {
4423 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4426 if (!instance->cmd_list[i]) {
4428 for (j = 0; j < i; j++)
4429 kfree(instance->cmd_list[j]);
4431 kfree(instance->cmd_list);
4432 instance->cmd_list = NULL;
4438 for (i = 0; i < max_cmd; i++) {
4439 cmd = instance->cmd_list[i];
4440 memset(cmd, 0, sizeof(struct megasas_cmd));
4443 cmd->instance = instance;
4445 list_add_tail(&cmd->list, &instance->cmd_pool);
4449 * Create a frame pool and assign one frame to each cmd
4451 if (megasas_create_frame_pool(instance)) {
4452 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4453 megasas_free_cmds(instance);
4461 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4462 * @instance: Adapter soft state
4464 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4465 * or FW is not under OCR.
4468 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4470 if (instance->adapter_type == MFI_SERIES)
4471 return KILL_ADAPTER;
4472 else if (instance->unload ||
4473 test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4474 &instance->reset_flags))
4475 return IGNORE_TIMEOUT;
4477 return INITIATE_OCR;
4481 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4484 struct megasas_cmd *cmd;
4485 struct megasas_dcmd_frame *dcmd;
4487 struct MR_PRIV_DEVICE *mr_device_priv_data;
4490 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4491 cmd = megasas_get_cmd(instance);
4494 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4498 dcmd = &cmd->frame->dcmd;
4500 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4501 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4503 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4504 dcmd->cmd = MFI_CMD_DCMD;
4505 dcmd->cmd_status = 0xFF;
4506 dcmd->sge_count = 1;
4507 dcmd->flags = MFI_FRAME_DIR_READ;
4510 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4511 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4513 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4514 sizeof(struct MR_PD_INFO));
4516 if ((instance->adapter_type != MFI_SERIES) &&
4517 !instance->mask_interrupts)
4518 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4520 ret = megasas_issue_polled(instance, cmd);
4524 mr_device_priv_data = sdev->hostdata;
4525 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4526 mr_device_priv_data->interface_type =
4527 instance->pd_info->state.ddf.pdType.intf;
4532 switch (dcmd_timeout_ocr_possible(instance)) {
4534 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4535 mutex_unlock(&instance->reset_mutex);
4536 megasas_reset_fusion(instance->host,
4537 MFI_IO_TIMEOUT_OCR);
4538 mutex_lock(&instance->reset_mutex);
4541 megaraid_sas_kill_hba(instance);
4543 case IGNORE_TIMEOUT:
4544 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4545 __func__, __LINE__);
4552 if (ret != DCMD_TIMEOUT)
4553 megasas_return_cmd(instance, cmd);
4558 * megasas_get_pd_list_info - Returns FW's pd_list structure
4559 * @instance: Adapter soft state
4560 * @pd_list: pd_list structure
4562 * Issues an internal command (DCMD) to get the FW's controller PD
4563 * list structure. This information is mainly used to find out SYSTEM
4564 * supported by the FW.
4567 megasas_get_pd_list(struct megasas_instance *instance)
4569 int ret = 0, pd_index = 0;
4570 struct megasas_cmd *cmd;
4571 struct megasas_dcmd_frame *dcmd;
4572 struct MR_PD_LIST *ci;
4573 struct MR_PD_ADDRESS *pd_addr;
4575 if (instance->pd_list_not_supported) {
4576 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4577 "not supported by firmware\n");
4581 ci = instance->pd_list_buf;
4583 cmd = megasas_get_cmd(instance);
4586 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4590 dcmd = &cmd->frame->dcmd;
4592 memset(ci, 0, sizeof(*ci));
4593 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4595 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4596 dcmd->mbox.b[1] = 0;
4597 dcmd->cmd = MFI_CMD_DCMD;
4598 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4599 dcmd->sge_count = 1;
4600 dcmd->flags = MFI_FRAME_DIR_READ;
4603 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4604 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4606 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4607 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4609 if ((instance->adapter_type != MFI_SERIES) &&
4610 !instance->mask_interrupts)
4611 ret = megasas_issue_blocked_cmd(instance, cmd,
4612 MFI_IO_TIMEOUT_SECS);
4614 ret = megasas_issue_polled(instance, cmd);
4618 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4619 "failed/not supported by firmware\n");
4621 if (instance->adapter_type != MFI_SERIES)
4622 megaraid_sas_kill_hba(instance);
4624 instance->pd_list_not_supported = 1;
4628 switch (dcmd_timeout_ocr_possible(instance)) {
4630 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4632 * DCMD failed from AEN path.
4633 * AEN path already hold reset_mutex to avoid PCI access
4634 * while OCR is in progress.
4636 mutex_unlock(&instance->reset_mutex);
4637 megasas_reset_fusion(instance->host,
4638 MFI_IO_TIMEOUT_OCR);
4639 mutex_lock(&instance->reset_mutex);
4642 megaraid_sas_kill_hba(instance);
4644 case IGNORE_TIMEOUT:
4645 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4646 __func__, __LINE__);
4654 if (megasas_dbg_lvl & LD_PD_DEBUG)
4655 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4656 __func__, le32_to_cpu(ci->count));
4658 if ((le32_to_cpu(ci->count) >
4659 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4662 memset(instance->local_pd_list, 0,
4663 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4665 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4666 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4667 le16_to_cpu(pd_addr->deviceId);
4668 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4669 pd_addr->scsiDevType;
4670 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4672 if (megasas_dbg_lvl & LD_PD_DEBUG)
4673 dev_info(&instance->pdev->dev,
4674 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4675 pd_index, le16_to_cpu(pd_addr->deviceId),
4676 pd_addr->scsiDevType);
4680 memcpy(instance->pd_list, instance->local_pd_list,
4681 sizeof(instance->pd_list));
4686 if (ret != DCMD_TIMEOUT)
4687 megasas_return_cmd(instance, cmd);
4693 * megasas_get_ld_list_info - Returns FW's ld_list structure
4694 * @instance: Adapter soft state
4695 * @ld_list: ld_list structure
4697 * Issues an internal command (DCMD) to get the FW's controller PD
4698 * list structure. This information is mainly used to find out SYSTEM
4699 * supported by the FW.
4702 megasas_get_ld_list(struct megasas_instance *instance)
4704 int ret = 0, ld_index = 0, ids = 0;
4705 struct megasas_cmd *cmd;
4706 struct megasas_dcmd_frame *dcmd;
4707 struct MR_LD_LIST *ci;
4708 dma_addr_t ci_h = 0;
4711 ci = instance->ld_list_buf;
4712 ci_h = instance->ld_list_buf_h;
4714 cmd = megasas_get_cmd(instance);
4717 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4721 dcmd = &cmd->frame->dcmd;
4723 memset(ci, 0, sizeof(*ci));
4724 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4726 if (instance->supportmax256vd)
4727 dcmd->mbox.b[0] = 1;
4728 dcmd->cmd = MFI_CMD_DCMD;
4729 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4730 dcmd->sge_count = 1;
4731 dcmd->flags = MFI_FRAME_DIR_READ;
4733 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4734 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4737 megasas_set_dma_settings(instance, dcmd, ci_h,
4738 sizeof(struct MR_LD_LIST));
4740 if ((instance->adapter_type != MFI_SERIES) &&
4741 !instance->mask_interrupts)
4742 ret = megasas_issue_blocked_cmd(instance, cmd,
4743 MFI_IO_TIMEOUT_SECS);
4745 ret = megasas_issue_polled(instance, cmd);
4747 ld_count = le32_to_cpu(ci->ldCount);
4751 megaraid_sas_kill_hba(instance);
4755 switch (dcmd_timeout_ocr_possible(instance)) {
4757 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4759 * DCMD failed from AEN path.
4760 * AEN path already hold reset_mutex to avoid PCI access
4761 * while OCR is in progress.
4763 mutex_unlock(&instance->reset_mutex);
4764 megasas_reset_fusion(instance->host,
4765 MFI_IO_TIMEOUT_OCR);
4766 mutex_lock(&instance->reset_mutex);
4769 megaraid_sas_kill_hba(instance);
4771 case IGNORE_TIMEOUT:
4772 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4773 __func__, __LINE__);
4780 if (megasas_dbg_lvl & LD_PD_DEBUG)
4781 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4782 __func__, ld_count);
4784 if (ld_count > instance->fw_supported_vd_count)
4787 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4789 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4790 if (ci->ldList[ld_index].state != 0) {
4791 ids = ci->ldList[ld_index].ref.targetId;
4792 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4793 if (megasas_dbg_lvl & LD_PD_DEBUG)
4794 dev_info(&instance->pdev->dev,
4795 "LD%d: targetID: 0x%03x\n",
4803 if (ret != DCMD_TIMEOUT)
4804 megasas_return_cmd(instance, cmd);
4810 * megasas_ld_list_query - Returns FW's ld_list structure
4811 * @instance: Adapter soft state
4812 * @query_type: ld_list structure type
4814 * Issues an internal command (DCMD) to get the FW's controller PD
4815 * list structure. This information is mainly used to find out SYSTEM
4816 * supported by the FW.
4819 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4821 int ret = 0, ld_index = 0, ids = 0;
4822 struct megasas_cmd *cmd;
4823 struct megasas_dcmd_frame *dcmd;
4824 struct MR_LD_TARGETID_LIST *ci;
4825 dma_addr_t ci_h = 0;
4828 ci = instance->ld_targetid_list_buf;
4829 ci_h = instance->ld_targetid_list_buf_h;
4831 cmd = megasas_get_cmd(instance);
4834 dev_warn(&instance->pdev->dev,
4835 "megasas_ld_list_query: Failed to get cmd\n");
4839 dcmd = &cmd->frame->dcmd;
4841 memset(ci, 0, sizeof(*ci));
4842 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4844 dcmd->mbox.b[0] = query_type;
4845 if (instance->supportmax256vd)
4846 dcmd->mbox.b[2] = 1;
4848 dcmd->cmd = MFI_CMD_DCMD;
4849 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4850 dcmd->sge_count = 1;
4851 dcmd->flags = MFI_FRAME_DIR_READ;
4853 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4854 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4857 megasas_set_dma_settings(instance, dcmd, ci_h,
4858 sizeof(struct MR_LD_TARGETID_LIST));
4860 if ((instance->adapter_type != MFI_SERIES) &&
4861 !instance->mask_interrupts)
4862 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4864 ret = megasas_issue_polled(instance, cmd);
4868 dev_info(&instance->pdev->dev,
4869 "DCMD not supported by firmware - %s %d\n",
4870 __func__, __LINE__);
4871 ret = megasas_get_ld_list(instance);
4874 switch (dcmd_timeout_ocr_possible(instance)) {
4876 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4878 * DCMD failed from AEN path.
4879 * AEN path already hold reset_mutex to avoid PCI access
4880 * while OCR is in progress.
4882 mutex_unlock(&instance->reset_mutex);
4883 megasas_reset_fusion(instance->host,
4884 MFI_IO_TIMEOUT_OCR);
4885 mutex_lock(&instance->reset_mutex);
4888 megaraid_sas_kill_hba(instance);
4890 case IGNORE_TIMEOUT:
4891 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4892 __func__, __LINE__);
4898 tgtid_count = le32_to_cpu(ci->count);
4900 if (megasas_dbg_lvl & LD_PD_DEBUG)
4901 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4902 __func__, tgtid_count);
4904 if ((tgtid_count > (instance->fw_supported_vd_count)))
4907 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4908 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4909 ids = ci->targetId[ld_index];
4910 instance->ld_ids[ids] = ci->targetId[ld_index];
4911 if (megasas_dbg_lvl & LD_PD_DEBUG)
4912 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4913 ld_index, ci->targetId[ld_index]);
4919 if (ret != DCMD_TIMEOUT)
4920 megasas_return_cmd(instance, cmd);
4926 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4927 * dcmd.mbox - reserved
4928 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4929 * Desc: This DCMD will return the combined device list
4930 * Status: MFI_STAT_OK - List returned successfully
4931 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4933 * @instance: Adapter soft state
4934 * @is_probe: Driver probe check
4935 * Return: 0 if DCMD succeeded
4936 * non-zero if failed
4939 megasas_host_device_list_query(struct megasas_instance *instance,
4942 int ret, i, target_id;
4943 struct megasas_cmd *cmd;
4944 struct megasas_dcmd_frame *dcmd;
4945 struct MR_HOST_DEVICE_LIST *ci;
4949 ci = instance->host_device_list_buf;
4950 ci_h = instance->host_device_list_buf_h;
4952 cmd = megasas_get_cmd(instance);
4955 dev_warn(&instance->pdev->dev,
4956 "%s: failed to get cmd\n",
4961 dcmd = &cmd->frame->dcmd;
4963 memset(ci, 0, sizeof(*ci));
4964 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4966 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4967 dcmd->cmd = MFI_CMD_DCMD;
4968 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4969 dcmd->sge_count = 1;
4970 dcmd->flags = MFI_FRAME_DIR_READ;
4973 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4974 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4976 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4978 if (!instance->mask_interrupts) {
4979 ret = megasas_issue_blocked_cmd(instance, cmd,
4980 MFI_IO_TIMEOUT_SECS);
4982 ret = megasas_issue_polled(instance, cmd);
4983 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4988 /* Fill the internal pd_list and ld_ids array based on
4989 * targetIds returned by FW
4991 count = le32_to_cpu(ci->count);
4993 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
4996 if (megasas_dbg_lvl & LD_PD_DEBUG)
4997 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
5000 memset(instance->local_pd_list, 0,
5001 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
5002 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
5003 for (i = 0; i < count; i++) {
5004 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
5005 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
5006 instance->local_pd_list[target_id].tid = target_id;
5007 instance->local_pd_list[target_id].driveType =
5008 ci->host_device_list[i].scsi_type;
5009 instance->local_pd_list[target_id].driveState =
5011 if (megasas_dbg_lvl & LD_PD_DEBUG)
5012 dev_info(&instance->pdev->dev,
5013 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
5014 i, target_id, ci->host_device_list[i].scsi_type);
5016 instance->ld_ids[target_id] = target_id;
5017 if (megasas_dbg_lvl & LD_PD_DEBUG)
5018 dev_info(&instance->pdev->dev,
5019 "Device %d: LD targetID: 0x%03x\n",
5024 memcpy(instance->pd_list, instance->local_pd_list,
5025 sizeof(instance->pd_list));
5029 switch (dcmd_timeout_ocr_possible(instance)) {
5031 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5032 mutex_unlock(&instance->reset_mutex);
5033 megasas_reset_fusion(instance->host,
5034 MFI_IO_TIMEOUT_OCR);
5035 mutex_lock(&instance->reset_mutex);
5038 megaraid_sas_kill_hba(instance);
5040 case IGNORE_TIMEOUT:
5041 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5042 __func__, __LINE__);
5047 dev_err(&instance->pdev->dev,
5048 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
5053 if (ret != DCMD_TIMEOUT)
5054 megasas_return_cmd(instance, cmd);
5060 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
5061 * instance : Controller's instance
5063 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
5065 struct fusion_context *fusion;
5066 u32 ventura_map_sz = 0;
5068 fusion = instance->ctrl_context;
5069 /* For MFI based controllers return dummy success */
5073 instance->supportmax256vd =
5074 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
5075 /* Below is additional check to address future FW enhancement */
5076 if (instance->ctrl_info_buf->max_lds > 64)
5077 instance->supportmax256vd = 1;
5079 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
5080 * MEGASAS_MAX_DEV_PER_CHANNEL;
5081 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
5082 * MEGASAS_MAX_DEV_PER_CHANNEL;
5083 if (instance->supportmax256vd) {
5084 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
5085 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5087 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5088 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5091 dev_info(&instance->pdev->dev,
5092 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
5093 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
5094 instance->ctrl_info_buf->max_lds);
5096 if (instance->max_raid_mapsize) {
5097 ventura_map_sz = instance->max_raid_mapsize *
5098 MR_MIN_MAP_SIZE; /* 64k */
5099 fusion->current_map_sz = ventura_map_sz;
5100 fusion->max_map_sz = ventura_map_sz;
5102 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
5103 (sizeof(struct MR_LD_SPAN_MAP) *
5104 (instance->fw_supported_vd_count - 1));
5105 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
5107 fusion->max_map_sz =
5108 max(fusion->old_map_sz, fusion->new_map_sz);
5110 if (instance->supportmax256vd)
5111 fusion->current_map_sz = fusion->new_map_sz;
5113 fusion->current_map_sz = fusion->old_map_sz;
5115 /* irrespective of FW raid maps, driver raid map is constant */
5116 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
5120 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
5121 * dcmd.hdr.length - number of bytes to read
5122 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
5123 * Desc: Fill in snapdump properties
5124 * Status: MFI_STAT_OK- Command successful
5126 void megasas_get_snapdump_properties(struct megasas_instance *instance)
5129 struct megasas_cmd *cmd;
5130 struct megasas_dcmd_frame *dcmd;
5131 struct MR_SNAPDUMP_PROPERTIES *ci;
5132 dma_addr_t ci_h = 0;
5134 ci = instance->snapdump_prop;
5135 ci_h = instance->snapdump_prop_h;
5140 cmd = megasas_get_cmd(instance);
5143 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
5147 dcmd = &cmd->frame->dcmd;
5149 memset(ci, 0, sizeof(*ci));
5150 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5152 dcmd->cmd = MFI_CMD_DCMD;
5153 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5154 dcmd->sge_count = 1;
5155 dcmd->flags = MFI_FRAME_DIR_READ;
5158 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5159 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5161 megasas_set_dma_settings(instance, dcmd, ci_h,
5162 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5164 if (!instance->mask_interrupts) {
5165 ret = megasas_issue_blocked_cmd(instance, cmd,
5166 MFI_IO_TIMEOUT_SECS);
5168 ret = megasas_issue_polled(instance, cmd);
5169 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5174 instance->snapdump_wait_time =
5175 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5176 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5180 switch (dcmd_timeout_ocr_possible(instance)) {
5182 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5183 mutex_unlock(&instance->reset_mutex);
5184 megasas_reset_fusion(instance->host,
5185 MFI_IO_TIMEOUT_OCR);
5186 mutex_lock(&instance->reset_mutex);
5189 megaraid_sas_kill_hba(instance);
5191 case IGNORE_TIMEOUT:
5192 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5193 __func__, __LINE__);
5198 if (ret != DCMD_TIMEOUT)
5199 megasas_return_cmd(instance, cmd);
5203 * megasas_get_controller_info - Returns FW's controller structure
5204 * @instance: Adapter soft state
5206 * Issues an internal command (DCMD) to get the FW's controller structure.
5207 * This information is mainly used to find out the maximum IO transfer per
5208 * command supported by the FW.
5211 megasas_get_ctrl_info(struct megasas_instance *instance)
5214 struct megasas_cmd *cmd;
5215 struct megasas_dcmd_frame *dcmd;
5216 struct megasas_ctrl_info *ci;
5217 dma_addr_t ci_h = 0;
5219 ci = instance->ctrl_info_buf;
5220 ci_h = instance->ctrl_info_buf_h;
5222 cmd = megasas_get_cmd(instance);
5225 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5229 dcmd = &cmd->frame->dcmd;
5231 memset(ci, 0, sizeof(*ci));
5232 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5234 dcmd->cmd = MFI_CMD_DCMD;
5235 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5236 dcmd->sge_count = 1;
5237 dcmd->flags = MFI_FRAME_DIR_READ;
5240 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5241 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5242 dcmd->mbox.b[0] = 1;
5244 megasas_set_dma_settings(instance, dcmd, ci_h,
5245 sizeof(struct megasas_ctrl_info));
5247 if ((instance->adapter_type != MFI_SERIES) &&
5248 !instance->mask_interrupts) {
5249 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5251 ret = megasas_issue_polled(instance, cmd);
5252 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5257 /* Save required controller information in
5258 * CPU endianness format.
5260 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5261 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5262 le32_to_cpus((u32 *)&ci->adapterOperations2);
5263 le32_to_cpus((u32 *)&ci->adapterOperations3);
5264 le16_to_cpus((u16 *)&ci->adapter_operations4);
5265 le32_to_cpus((u32 *)&ci->adapter_operations5);
5267 /* Update the latest Ext VD info.
5268 * From Init path, store current firmware details.
5269 * From OCR path, detect any firmware properties changes.
5270 * in case of Firmware upgrade without system reboot.
5272 megasas_update_ext_vd_details(instance);
5273 instance->support_seqnum_jbod_fp =
5274 ci->adapterOperations3.useSeqNumJbodFP;
5275 instance->support_morethan256jbod =
5276 ci->adapter_operations4.support_pd_map_target_id;
5277 instance->support_nvme_passthru =
5278 ci->adapter_operations4.support_nvme_passthru;
5279 instance->support_pci_lane_margining =
5280 ci->adapter_operations5.support_pci_lane_margining;
5281 instance->task_abort_tmo = ci->TaskAbortTO;
5282 instance->max_reset_tmo = ci->MaxResetTO;
5284 /*Check whether controller is iMR or MR */
5285 instance->is_imr = (ci->memory_size ? 0 : 1);
5287 instance->snapdump_wait_time =
5288 (ci->properties.on_off_properties2.enable_snap_dump ?
5289 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5291 instance->enable_fw_dev_list =
5292 ci->properties.on_off_properties2.enable_fw_dev_list;
5294 dev_info(&instance->pdev->dev,
5295 "controller type\t: %s(%dMB)\n",
5296 instance->is_imr ? "iMR" : "MR",
5297 le16_to_cpu(ci->memory_size));
5299 instance->disableOnlineCtrlReset =
5300 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5301 instance->secure_jbod_support =
5302 ci->adapterOperations3.supportSecurityonJBOD;
5303 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5304 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5305 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5306 instance->secure_jbod_support ? "Yes" : "No");
5307 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5308 instance->support_nvme_passthru ? "Yes" : "No");
5309 dev_info(&instance->pdev->dev,
5310 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5311 instance->task_abort_tmo, instance->max_reset_tmo);
5312 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5313 instance->support_seqnum_jbod_fp ? "Yes" : "No");
5314 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5315 instance->support_pci_lane_margining ? "Yes" : "No");
5320 switch (dcmd_timeout_ocr_possible(instance)) {
5322 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5323 mutex_unlock(&instance->reset_mutex);
5324 megasas_reset_fusion(instance->host,
5325 MFI_IO_TIMEOUT_OCR);
5326 mutex_lock(&instance->reset_mutex);
5329 megaraid_sas_kill_hba(instance);
5331 case IGNORE_TIMEOUT:
5332 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5333 __func__, __LINE__);
5338 megaraid_sas_kill_hba(instance);
5343 if (ret != DCMD_TIMEOUT)
5344 megasas_return_cmd(instance, cmd);
5350 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5353 * @instance: Adapter soft state
5354 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5355 MR_CRASH_BUF_TURN_OFF = 0
5356 MR_CRASH_BUF_TURN_ON = 1
5357 * @return 0 on success non-zero on failure.
5358 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5359 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5360 * that driver supports crash dump feature. This DCMD will be sent only if
5361 * crash dump feature is supported by the FW.
5364 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5368 struct megasas_cmd *cmd;
5369 struct megasas_dcmd_frame *dcmd;
5371 cmd = megasas_get_cmd(instance);
5374 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5379 dcmd = &cmd->frame->dcmd;
5381 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5382 dcmd->mbox.b[0] = crash_buf_state;
5383 dcmd->cmd = MFI_CMD_DCMD;
5384 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5385 dcmd->sge_count = 1;
5386 dcmd->flags = MFI_FRAME_DIR_NONE;
5389 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5390 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5392 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5393 CRASH_DMA_BUF_SIZE);
5395 if ((instance->adapter_type != MFI_SERIES) &&
5396 !instance->mask_interrupts)
5397 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5399 ret = megasas_issue_polled(instance, cmd);
5401 if (ret == DCMD_TIMEOUT) {
5402 switch (dcmd_timeout_ocr_possible(instance)) {
5404 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5405 megasas_reset_fusion(instance->host,
5406 MFI_IO_TIMEOUT_OCR);
5409 megaraid_sas_kill_hba(instance);
5411 case IGNORE_TIMEOUT:
5412 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5413 __func__, __LINE__);
5417 megasas_return_cmd(instance, cmd);
5423 * megasas_issue_init_mfi - Initializes the FW
5424 * @instance: Adapter soft state
5426 * Issues the INIT MFI cmd
5429 megasas_issue_init_mfi(struct megasas_instance *instance)
5432 struct megasas_cmd *cmd;
5433 struct megasas_init_frame *init_frame;
5434 struct megasas_init_queue_info *initq_info;
5435 dma_addr_t init_frame_h;
5436 dma_addr_t initq_info_h;
5439 * Prepare a init frame. Note the init frame points to queue info
5440 * structure. Each frame has SGL allocated after first 64 bytes. For
5441 * this frame - since we don't need any SGL - we use SGL's space as
5442 * queue info structure
5444 * We will not get a NULL command below. We just created the pool.
5446 cmd = megasas_get_cmd(instance);
5448 init_frame = (struct megasas_init_frame *)cmd->frame;
5449 initq_info = (struct megasas_init_queue_info *)
5450 ((unsigned long)init_frame + 64);
5452 init_frame_h = cmd->frame_phys_addr;
5453 initq_info_h = init_frame_h + 64;
5455 context = init_frame->context;
5456 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5457 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5458 init_frame->context = context;
5460 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5461 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5463 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5464 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5466 init_frame->cmd = MFI_CMD_INIT;
5467 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5468 init_frame->queue_info_new_phys_addr_lo =
5469 cpu_to_le32(lower_32_bits(initq_info_h));
5470 init_frame->queue_info_new_phys_addr_hi =
5471 cpu_to_le32(upper_32_bits(initq_info_h));
5473 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5476 * disable the intr before firing the init frame to FW
5478 instance->instancet->disable_intr(instance);
5481 * Issue the init frame in polled mode
5484 if (megasas_issue_polled(instance, cmd)) {
5485 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5486 megasas_return_cmd(instance, cmd);
5490 megasas_return_cmd(instance, cmd);
5499 megasas_init_adapter_mfi(struct megasas_instance *instance)
5505 * Get various operational parameters from status register
5507 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5509 * Reduce the max supported cmds by 1. This is to ensure that the
5510 * reply_q_sz (1 more than the max cmd that driver may send)
5511 * does not exceed max cmds that the FW can support
5513 instance->max_fw_cmds = instance->max_fw_cmds-1;
5514 instance->max_mfi_cmds = instance->max_fw_cmds;
5515 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5518 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5519 * are reserved for IOCTL + driver's internal DCMDs.
5521 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5522 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5523 instance->max_scsi_cmds = (instance->max_fw_cmds -
5524 MEGASAS_SKINNY_INT_CMDS);
5525 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5527 instance->max_scsi_cmds = (instance->max_fw_cmds -
5529 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5532 instance->cur_can_queue = instance->max_scsi_cmds;
5534 * Create a pool of commands
5536 if (megasas_alloc_cmds(instance))
5537 goto fail_alloc_cmds;
5540 * Allocate memory for reply queue. Length of reply queue should
5541 * be _one_ more than the maximum commands handled by the firmware.
5543 * Note: When FW completes commands, it places corresponding contex
5544 * values in this circular reply queue. This circular queue is a fairly
5545 * typical producer-consumer queue. FW is the producer (of completed
5546 * commands) and the driver is the consumer.
5548 context_sz = sizeof(u32);
5549 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5551 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5552 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5554 if (!instance->reply_queue) {
5555 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5556 goto fail_reply_queue;
5559 if (megasas_issue_init_mfi(instance))
5562 if (megasas_get_ctrl_info(instance)) {
5563 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5564 "Fail from %s %d\n", instance->unique_id,
5565 __func__, __LINE__);
5569 instance->fw_support_ieee = 0;
5570 instance->fw_support_ieee =
5571 (instance->instancet->read_fw_status_reg(instance) &
5574 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5575 instance->fw_support_ieee);
5577 if (instance->fw_support_ieee)
5578 instance->flag_ieee = 1;
5584 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5585 instance->reply_queue, instance->reply_queue_h);
5587 megasas_free_cmds(instance);
5594 void megasas_setup_irq_poll(struct megasas_instance *instance)
5596 struct megasas_irq_context *irq_ctx;
5599 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5601 /* Initialize IRQ poll */
5602 for (i = 0; i < count; i++) {
5603 irq_ctx = &instance->irq_context[i];
5604 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5605 irq_ctx->irq_poll_scheduled = false;
5606 irq_poll_init(&irq_ctx->irqpoll,
5607 instance->threshold_reply_count,
5613 * megasas_setup_irqs_ioapic - register legacy interrupts.
5614 * @instance: Adapter soft state
5616 * Do not enable interrupt, only setup ISRs.
5618 * Return 0 on success.
5621 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5623 struct pci_dev *pdev;
5625 pdev = instance->pdev;
5626 instance->irq_context[0].instance = instance;
5627 instance->irq_context[0].MSIxIndex = 0;
5628 snprintf(instance->irq_context->name, MEGASAS_MSIX_NAME_LEN, "%s%u",
5629 "megasas", instance->host->host_no);
5630 if (request_irq(pci_irq_vector(pdev, 0),
5631 instance->instancet->service_isr, IRQF_SHARED,
5632 instance->irq_context->name, &instance->irq_context[0])) {
5633 dev_err(&instance->pdev->dev,
5634 "Failed to register IRQ from %s %d\n",
5635 __func__, __LINE__);
5638 instance->perf_mode = MR_LATENCY_PERF_MODE;
5639 instance->low_latency_index_start = 0;
5644 * megasas_setup_irqs_msix - register MSI-x interrupts.
5645 * @instance: Adapter soft state
5646 * @is_probe: Driver probe check
5648 * Do not enable interrupt, only setup ISRs.
5650 * Return 0 on success.
5653 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5656 struct pci_dev *pdev;
5658 pdev = instance->pdev;
5661 for (i = 0; i < instance->msix_vectors; i++) {
5662 instance->irq_context[i].instance = instance;
5663 instance->irq_context[i].MSIxIndex = i;
5664 snprintf(instance->irq_context[i].name, MEGASAS_MSIX_NAME_LEN, "%s%u-msix%u",
5665 "megasas", instance->host->host_no, i);
5666 if (request_irq(pci_irq_vector(pdev, i),
5667 instance->instancet->service_isr, 0, instance->irq_context[i].name,
5668 &instance->irq_context[i])) {
5669 dev_err(&instance->pdev->dev,
5670 "Failed to register IRQ for vector %d.\n", i);
5671 for (j = 0; j < i; j++) {
5672 if (j < instance->low_latency_index_start)
5673 irq_set_affinity_hint(
5674 pci_irq_vector(pdev, j), NULL);
5675 free_irq(pci_irq_vector(pdev, j),
5676 &instance->irq_context[j]);
5678 /* Retry irq register for IO_APIC*/
5679 instance->msix_vectors = 0;
5680 instance->msix_load_balance = false;
5682 pci_free_irq_vectors(instance->pdev);
5683 return megasas_setup_irqs_ioapic(instance);
5694 * megasas_destroy_irqs- unregister interrupts.
5695 * @instance: Adapter soft state
5699 megasas_destroy_irqs(struct megasas_instance *instance) {
5703 struct megasas_irq_context *irq_ctx;
5705 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5706 if (instance->adapter_type != MFI_SERIES) {
5707 for (i = 0; i < count; i++) {
5708 irq_ctx = &instance->irq_context[i];
5709 irq_poll_disable(&irq_ctx->irqpoll);
5713 if (instance->msix_vectors)
5714 for (i = 0; i < instance->msix_vectors; i++) {
5715 if (i < instance->low_latency_index_start)
5716 irq_set_affinity_hint(
5717 pci_irq_vector(instance->pdev, i), NULL);
5718 free_irq(pci_irq_vector(instance->pdev, i),
5719 &instance->irq_context[i]);
5722 free_irq(pci_irq_vector(instance->pdev, 0),
5723 &instance->irq_context[0]);
5727 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5728 * @instance: Adapter soft state
5730 * Return 0 on success.
5733 megasas_setup_jbod_map(struct megasas_instance *instance)
5736 struct fusion_context *fusion = instance->ctrl_context;
5739 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5740 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5742 instance->use_seqnum_jbod_fp =
5743 instance->support_seqnum_jbod_fp;
5744 if (reset_devices || !fusion ||
5745 !instance->support_seqnum_jbod_fp) {
5746 dev_info(&instance->pdev->dev,
5747 "JBOD sequence map is disabled %s %d\n",
5748 __func__, __LINE__);
5749 instance->use_seqnum_jbod_fp = false;
5753 if (fusion->pd_seq_sync[0])
5756 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5757 fusion->pd_seq_sync[i] = dma_alloc_coherent
5758 (&instance->pdev->dev, pd_seq_map_sz,
5759 &fusion->pd_seq_phys[i], GFP_KERNEL);
5760 if (!fusion->pd_seq_sync[i]) {
5761 dev_err(&instance->pdev->dev,
5762 "Failed to allocate memory from %s %d\n",
5763 __func__, __LINE__);
5765 dma_free_coherent(&instance->pdev->dev,
5766 pd_seq_map_sz, fusion->pd_seq_sync[0],
5767 fusion->pd_seq_phys[0]);
5768 fusion->pd_seq_sync[0] = NULL;
5770 instance->use_seqnum_jbod_fp = false;
5776 if (!megasas_sync_pd_seq_num(instance, false) &&
5777 !megasas_sync_pd_seq_num(instance, true))
5778 instance->use_seqnum_jbod_fp = true;
5780 instance->use_seqnum_jbod_fp = false;
5783 static void megasas_setup_reply_map(struct megasas_instance *instance)
5785 const struct cpumask *mask;
5786 unsigned int queue, cpu, low_latency_index_start;
5788 low_latency_index_start = instance->low_latency_index_start;
5790 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
5791 mask = pci_irq_get_affinity(instance->pdev, queue);
5795 for_each_cpu(cpu, mask)
5796 instance->reply_map[cpu] = queue;
5801 queue = low_latency_index_start;
5802 for_each_possible_cpu(cpu) {
5803 instance->reply_map[cpu] = queue;
5804 if (queue == (instance->msix_vectors - 1))
5805 queue = low_latency_index_start;
5812 * megasas_get_device_list - Get the PD and LD device list from FW.
5813 * @instance: Adapter soft state
5814 * @return: Success or failure
5816 * Issue DCMDs to Firmware to get the PD and LD list.
5817 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5818 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5821 int megasas_get_device_list(struct megasas_instance *instance)
5823 memset(instance->pd_list, 0,
5824 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5825 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5827 if (instance->enable_fw_dev_list) {
5828 if (megasas_host_device_list_query(instance, true))
5831 if (megasas_get_pd_list(instance) < 0) {
5832 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5836 if (megasas_ld_list_query(instance,
5837 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5838 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5847 * megasas_set_high_iops_queue_affinity_hint - Set affinity hint for high IOPS queues
5848 * @instance: Adapter soft state
5852 megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5855 int local_numa_node;
5857 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
5858 local_numa_node = dev_to_node(&instance->pdev->dev);
5860 for (i = 0; i < instance->low_latency_index_start; i++)
5861 irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5862 cpumask_of_node(local_numa_node));
5867 __megasas_alloc_irq_vectors(struct megasas_instance *instance)
5870 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5871 struct irq_affinity *descp = &desc;
5873 irq_flags = PCI_IRQ_MSIX;
5875 if (instance->smp_affinity_enable)
5876 irq_flags |= PCI_IRQ_AFFINITY;
5880 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5881 instance->low_latency_index_start,
5882 instance->msix_vectors, irq_flags, descp);
5888 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5889 * @instance: Adapter soft state
5893 megasas_alloc_irq_vectors(struct megasas_instance *instance)
5896 unsigned int num_msix_req;
5898 i = __megasas_alloc_irq_vectors(instance);
5900 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
5901 (i != instance->msix_vectors)) {
5902 if (instance->msix_vectors)
5903 pci_free_irq_vectors(instance->pdev);
5904 /* Disable Balanced IOPS mode and try realloc vectors */
5905 instance->perf_mode = MR_LATENCY_PERF_MODE;
5906 instance->low_latency_index_start = 1;
5907 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5909 instance->msix_vectors = min(num_msix_req,
5910 instance->msix_vectors);
5912 i = __megasas_alloc_irq_vectors(instance);
5916 dev_info(&instance->pdev->dev,
5917 "requested/available msix %d/%d\n", instance->msix_vectors, i);
5920 instance->msix_vectors = i;
5922 instance->msix_vectors = 0;
5924 if (instance->smp_affinity_enable)
5925 megasas_set_high_iops_queue_affinity_hint(instance);
5929 * megasas_init_fw - Initializes the FW
5930 * @instance: Adapter soft state
5932 * This is the main function for initializing firmware
5935 static int megasas_init_fw(struct megasas_instance *instance)
5938 u32 max_sectors_2, tmp_sectors, msix_enable;
5939 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5940 resource_size_t base_addr;
5941 void *base_addr_phys;
5942 struct megasas_ctrl_info *ctrl_info = NULL;
5943 unsigned long bar_list;
5945 struct IOV_111 *iovPtr;
5946 struct fusion_context *fusion;
5947 bool intr_coalescing;
5948 unsigned int num_msix_req;
5951 fusion = instance->ctrl_context;
5953 /* Find first memory bar */
5954 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5955 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5956 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5958 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5962 base_addr = pci_resource_start(instance->pdev, instance->bar);
5963 instance->reg_set = ioremap(base_addr, 8192);
5965 if (!instance->reg_set) {
5966 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5970 base_addr_phys = &base_addr;
5971 dev_printk(KERN_DEBUG, &instance->pdev->dev,
5972 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
5973 instance->bar, base_addr_phys, instance->reg_set);
5975 if (instance->adapter_type != MFI_SERIES)
5976 instance->instancet = &megasas_instance_template_fusion;
5978 switch (instance->pdev->device) {
5979 case PCI_DEVICE_ID_LSI_SAS1078R:
5980 case PCI_DEVICE_ID_LSI_SAS1078DE:
5981 instance->instancet = &megasas_instance_template_ppc;
5983 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5984 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5985 instance->instancet = &megasas_instance_template_gen2;
5987 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5988 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5989 instance->instancet = &megasas_instance_template_skinny;
5991 case PCI_DEVICE_ID_LSI_SAS1064R:
5992 case PCI_DEVICE_ID_DELL_PERC5:
5994 instance->instancet = &megasas_instance_template_xscale;
5995 instance->pd_list_not_supported = 1;
6000 if (megasas_transition_to_ready(instance, 0)) {
6001 dev_info(&instance->pdev->dev,
6002 "Failed to transition controller to ready from %s!\n",
6004 if (instance->adapter_type != MFI_SERIES) {
6005 status_reg = instance->instancet->read_fw_status_reg(
6007 if (status_reg & MFI_RESET_ADAPTER) {
6008 if (megasas_adp_reset_wait_for_ready
6009 (instance, true, 0) == FAILED)
6010 goto fail_ready_state;
6012 goto fail_ready_state;
6015 atomic_set(&instance->fw_reset_no_pci_access, 1);
6016 instance->instancet->adp_reset
6017 (instance, instance->reg_set);
6018 atomic_set(&instance->fw_reset_no_pci_access, 0);
6020 /*waiting for about 30 second before retry*/
6023 if (megasas_transition_to_ready(instance, 0))
6024 goto fail_ready_state;
6027 dev_info(&instance->pdev->dev,
6028 "FW restarted successfully from %s!\n",
6032 megasas_init_ctrl_params(instance);
6034 if (megasas_set_dma_mask(instance))
6035 goto fail_ready_state;
6037 if (megasas_alloc_ctrl_mem(instance))
6038 goto fail_alloc_dma_buf;
6040 if (megasas_alloc_ctrl_dma_buffers(instance))
6041 goto fail_alloc_dma_buf;
6043 fusion = instance->ctrl_context;
6045 if (instance->adapter_type >= VENTURA_SERIES) {
6047 megasas_readl(instance,
6048 &instance->reg_set->outbound_scratch_pad_2);
6049 instance->max_raid_mapsize = ((scratch_pad_2 >>
6050 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
6051 MR_MAX_RAID_MAP_SIZE_MASK);
6054 instance->enable_sdev_max_qd = enable_sdev_max_qd;
6056 switch (instance->adapter_type) {
6057 case VENTURA_SERIES:
6058 fusion->pcie_bw_limitation = true;
6061 fusion->r56_div_offload = true;
6067 /* Check if MSI-X is supported while in ready state */
6068 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
6070 if (msix_enable && !msix_disable) {
6072 scratch_pad_1 = megasas_readl
6073 (instance, &instance->reg_set->outbound_scratch_pad_1);
6074 /* Check max MSI-X vectors */
6076 if (instance->adapter_type == THUNDERBOLT_SERIES) {
6077 /* Thunderbolt Series*/
6078 instance->msix_vectors = (scratch_pad_1
6079 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
6081 instance->msix_vectors = ((scratch_pad_1
6082 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
6083 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
6086 * For Invader series, > 8 MSI-x vectors
6087 * supported by FW/HW implies combined
6088 * reply queue mode is enabled.
6089 * For Ventura series, > 16 MSI-x vectors
6090 * supported by FW/HW implies combined
6091 * reply queue mode is enabled.
6093 switch (instance->adapter_type) {
6094 case INVADER_SERIES:
6095 if (instance->msix_vectors > 8)
6096 instance->msix_combined = true;
6099 case VENTURA_SERIES:
6100 if (instance->msix_vectors > 16)
6101 instance->msix_combined = true;
6106 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
6109 if (instance->adapter_type >= INVADER_SERIES &&
6110 !instance->msix_combined) {
6111 instance->msix_load_balance = true;
6112 instance->smp_affinity_enable = false;
6115 /* Save 1-15 reply post index address to local memory
6116 * Index 0 is already saved from reg offset
6117 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
6119 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
6120 instance->reply_post_host_index_addr[loop] =
6122 ((u8 __iomem *)instance->reg_set +
6123 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
6128 dev_info(&instance->pdev->dev,
6129 "firmware supports msix\t: (%d)",
6130 instance->msix_vectors);
6132 instance->msix_vectors = min(msix_vectors,
6133 instance->msix_vectors);
6134 } else /* MFI adapters */
6135 instance->msix_vectors = 1;
6139 * For Aero (if some conditions are met), driver will configure a
6140 * few additional reply queues with interrupt coalescing enabled.
6141 * These queues with interrupt coalescing enabled are called
6142 * High IOPS queues and rest of reply queues (based on number of
6143 * logical CPUs) are termed as Low latency queues.
6145 * Total Number of reply queues = High IOPS queues + low latency queues
6147 * For rest of fusion adapters, 1 additional reply queue will be
6148 * reserved for management commands, rest of reply queues
6149 * (based on number of logical CPUs) will be used for IOs and
6150 * referenced as IO queues.
6151 * Total Number of reply queues = 1 + IO queues
6153 * MFI adapters supports single MSI-x so single reply queue
6154 * will be used for IO and management commands.
6157 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
6159 if (intr_coalescing &&
6160 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6161 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
6162 instance->perf_mode = MR_BALANCED_PERF_MODE;
6164 instance->perf_mode = MR_LATENCY_PERF_MODE;
6167 if (instance->adapter_type == AERO_SERIES) {
6168 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6169 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6172 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6173 * in latency perf mode and enable R1 PCI bandwidth algorithm
6176 instance->perf_mode = MR_LATENCY_PERF_MODE;
6177 fusion->pcie_bw_limitation = true;
6181 * Performance mode settings provided through module parameter-perf_mode will
6182 * take affect only for:
6183 * 1. Aero family of adapters.
6184 * 2. When user sets module parameter- perf_mode in range of 0-2.
6186 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6187 (perf_mode <= MR_LATENCY_PERF_MODE))
6188 instance->perf_mode = perf_mode;
6190 * If intr coalescing is not supported by controller FW, then IOPS
6191 * and Balanced modes are not feasible.
6193 if (!intr_coalescing)
6194 instance->perf_mode = MR_LATENCY_PERF_MODE;
6198 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
6199 instance->low_latency_index_start =
6200 MR_HIGH_IOPS_QUEUE_COUNT;
6202 instance->low_latency_index_start = 1;
6204 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6206 instance->msix_vectors = min(num_msix_req,
6207 instance->msix_vectors);
6209 megasas_alloc_irq_vectors(instance);
6210 if (!instance->msix_vectors)
6211 instance->msix_load_balance = false;
6214 * MSI-X host index 0 is common for all adapter.
6215 * It is used for all MPT based Adapters.
6217 if (instance->msix_combined) {
6218 instance->reply_post_host_index_addr[0] =
6219 (u32 *)((u8 *)instance->reg_set +
6220 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6222 instance->reply_post_host_index_addr[0] =
6223 (u32 *)((u8 *)instance->reg_set +
6224 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6227 if (!instance->msix_vectors) {
6228 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6230 goto fail_init_adapter;
6233 megasas_setup_reply_map(instance);
6235 dev_info(&instance->pdev->dev,
6236 "current msix/online cpus\t: (%d/%d)\n",
6237 instance->msix_vectors, (unsigned int)num_online_cpus());
6238 dev_info(&instance->pdev->dev,
6239 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
6241 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6242 (unsigned long)instance);
6245 * Below are default value for legacy Firmware.
6246 * non-fusion based controllers
6248 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6249 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
6250 /* Get operational params, sge flags, send init cmd to controller */
6251 if (instance->instancet->init_adapter(instance))
6252 goto fail_init_adapter;
6254 if (instance->adapter_type >= VENTURA_SERIES) {
6256 megasas_readl(instance,
6257 &instance->reg_set->outbound_scratch_pad_3);
6258 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
6259 MR_DEFAULT_NVME_PAGE_SHIFT)
6260 instance->nvme_page_size =
6261 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
6263 dev_info(&instance->pdev->dev,
6264 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6267 if (instance->msix_vectors ?
6268 megasas_setup_irqs_msix(instance, 1) :
6269 megasas_setup_irqs_ioapic(instance))
6270 goto fail_init_adapter;
6272 if (instance->adapter_type != MFI_SERIES)
6273 megasas_setup_irq_poll(instance);
6275 instance->instancet->enable_intr(instance);
6277 dev_info(&instance->pdev->dev, "INIT adapter done\n");
6279 megasas_setup_jbod_map(instance);
6281 if (megasas_get_device_list(instance) != SUCCESS) {
6282 dev_err(&instance->pdev->dev,
6283 "%s: megasas_get_device_list failed\n",
6285 goto fail_get_ld_pd_list;
6288 /* stream detection initialization */
6289 if (instance->adapter_type >= VENTURA_SERIES) {
6290 fusion->stream_detect_by_ld =
6291 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6292 sizeof(struct LD_STREAM_DETECT *),
6294 if (!fusion->stream_detect_by_ld) {
6295 dev_err(&instance->pdev->dev,
6296 "unable to allocate stream detection for pool of LDs\n");
6297 goto fail_get_ld_pd_list;
6299 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6300 fusion->stream_detect_by_ld[i] =
6301 kzalloc(sizeof(struct LD_STREAM_DETECT),
6303 if (!fusion->stream_detect_by_ld[i]) {
6304 dev_err(&instance->pdev->dev,
6305 "unable to allocate stream detect by LD\n ");
6306 for (j = 0; j < i; ++j)
6307 kfree(fusion->stream_detect_by_ld[j]);
6308 kfree(fusion->stream_detect_by_ld);
6309 fusion->stream_detect_by_ld = NULL;
6310 goto fail_get_ld_pd_list;
6312 fusion->stream_detect_by_ld[i]->mru_bit_map
6318 * Compute the max allowed sectors per IO: The controller info has two
6319 * limits on max sectors. Driver should use the minimum of these two.
6321 * 1 << stripe_sz_ops.min = max sectors per strip
6323 * Note that older firmwares ( < FW ver 30) didn't report information
6324 * to calculate max_sectors_1. So the number ended up as zero always.
6327 ctrl_info = instance->ctrl_info_buf;
6329 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6330 le16_to_cpu(ctrl_info->max_strips_per_io);
6331 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
6333 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
6335 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6336 instance->passive = ctrl_info->cluster.passive;
6337 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
6338 instance->UnevenSpanSupport =
6339 ctrl_info->adapterOperations2.supportUnevenSpans;
6340 if (instance->UnevenSpanSupport) {
6341 struct fusion_context *fusion = instance->ctrl_context;
6342 if (MR_ValidateMapInfo(instance, instance->map_id))
6343 fusion->fast_path_io = 1;
6345 fusion->fast_path_io = 0;
6348 if (ctrl_info->host_interface.SRIOV) {
6349 instance->requestorId = ctrl_info->iov.requestorId;
6350 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6351 if (!ctrl_info->adapterOperations2.activePassive)
6352 instance->PlasmaFW111 = 1;
6354 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6355 instance->PlasmaFW111 ? "1.11" : "new");
6357 if (instance->PlasmaFW111) {
6358 iovPtr = (struct IOV_111 *)
6359 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6360 instance->requestorId = iovPtr->requestorId;
6363 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6364 instance->requestorId);
6367 instance->crash_dump_fw_support =
6368 ctrl_info->adapterOperations3.supportCrashDump;
6369 instance->crash_dump_drv_support =
6370 (instance->crash_dump_fw_support &&
6371 instance->crash_dump_buf);
6372 if (instance->crash_dump_drv_support)
6373 megasas_set_crash_dump_params(instance,
6374 MR_CRASH_BUF_TURN_OFF);
6377 if (instance->crash_dump_buf)
6378 dma_free_coherent(&instance->pdev->dev,
6380 instance->crash_dump_buf,
6381 instance->crash_dump_h);
6382 instance->crash_dump_buf = NULL;
6385 if (instance->snapdump_wait_time) {
6386 megasas_get_snapdump_properties(instance);
6387 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6388 instance->snapdump_wait_time);
6391 dev_info(&instance->pdev->dev,
6392 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6393 le16_to_cpu(ctrl_info->pci.vendor_id),
6394 le16_to_cpu(ctrl_info->pci.device_id),
6395 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6396 le16_to_cpu(ctrl_info->pci.sub_device_id));
6397 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6398 instance->UnevenSpanSupport ? "yes" : "no");
6399 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6400 instance->crash_dump_drv_support ? "yes" : "no");
6401 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6402 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
6404 instance->max_sectors_per_req = instance->max_num_sge *
6405 SGE_BUFFER_SIZE / 512;
6406 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6407 instance->max_sectors_per_req = tmp_sectors;
6409 /* Check for valid throttlequeuedepth module parameter */
6410 if (throttlequeuedepth &&
6411 throttlequeuedepth <= instance->max_scsi_cmds)
6412 instance->throttlequeuedepth = throttlequeuedepth;
6414 instance->throttlequeuedepth =
6415 MEGASAS_THROTTLE_QUEUE_DEPTH;
6417 if ((resetwaittime < 1) ||
6418 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6419 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6421 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6422 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6424 /* Launch SR-IOV heartbeat timer */
6425 if (instance->requestorId) {
6426 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6427 megasas_start_timer(instance);
6429 instance->skip_heartbeat_timer_del = 1;
6430 goto fail_get_ld_pd_list;
6435 * Create and start watchdog thread which will monitor
6436 * controller state every 1 sec and trigger OCR when
6437 * it enters fault state
6439 if (instance->adapter_type != MFI_SERIES)
6440 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6441 goto fail_start_watchdog;
6445 fail_start_watchdog:
6446 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6447 del_timer_sync(&instance->sriov_heartbeat_timer);
6448 fail_get_ld_pd_list:
6449 instance->instancet->disable_intr(instance);
6450 megasas_destroy_irqs(instance);
6452 if (instance->msix_vectors)
6453 pci_free_irq_vectors(instance->pdev);
6454 instance->msix_vectors = 0;
6456 megasas_free_ctrl_dma_buffers(instance);
6457 megasas_free_ctrl_mem(instance);
6459 iounmap(instance->reg_set);
6462 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6464 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6465 __func__, __LINE__);
6470 * megasas_release_mfi - Reverses the FW initialization
6471 * @instance: Adapter soft state
6473 static void megasas_release_mfi(struct megasas_instance *instance)
6475 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6477 if (instance->reply_queue)
6478 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6479 instance->reply_queue, instance->reply_queue_h);
6481 megasas_free_cmds(instance);
6483 iounmap(instance->reg_set);
6485 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6489 * megasas_get_seq_num - Gets latest event sequence numbers
6490 * @instance: Adapter soft state
6491 * @eli: FW event log sequence numbers information
6493 * FW maintains a log of all events in a non-volatile area. Upper layers would
6494 * usually find out the latest sequence number of the events, the seq number at
6495 * the boot etc. They would "read" all the events below the latest seq number
6496 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6497 * number), they would subsribe to AEN (asynchronous event notification) and
6498 * wait for the events to happen.
6501 megasas_get_seq_num(struct megasas_instance *instance,
6502 struct megasas_evt_log_info *eli)
6504 struct megasas_cmd *cmd;
6505 struct megasas_dcmd_frame *dcmd;
6506 struct megasas_evt_log_info *el_info;
6507 dma_addr_t el_info_h = 0;
6510 cmd = megasas_get_cmd(instance);
6516 dcmd = &cmd->frame->dcmd;
6517 el_info = dma_alloc_coherent(&instance->pdev->dev,
6518 sizeof(struct megasas_evt_log_info),
6519 &el_info_h, GFP_KERNEL);
6521 megasas_return_cmd(instance, cmd);
6525 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6527 dcmd->cmd = MFI_CMD_DCMD;
6528 dcmd->cmd_status = 0x0;
6529 dcmd->sge_count = 1;
6530 dcmd->flags = MFI_FRAME_DIR_READ;
6533 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6534 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6536 megasas_set_dma_settings(instance, dcmd, el_info_h,
6537 sizeof(struct megasas_evt_log_info));
6539 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6540 if (ret != DCMD_SUCCESS) {
6541 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6542 __func__, __LINE__);
6547 * Copy the data back into callers buffer
6549 eli->newest_seq_num = el_info->newest_seq_num;
6550 eli->oldest_seq_num = el_info->oldest_seq_num;
6551 eli->clear_seq_num = el_info->clear_seq_num;
6552 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6553 eli->boot_seq_num = el_info->boot_seq_num;
6556 dma_free_coherent(&instance->pdev->dev,
6557 sizeof(struct megasas_evt_log_info),
6558 el_info, el_info_h);
6560 megasas_return_cmd(instance, cmd);
6566 * megasas_register_aen - Registers for asynchronous event notification
6567 * @instance: Adapter soft state
6568 * @seq_num: The starting sequence number
6569 * @class_locale_word: Class of the event
6571 * This function subscribes for AEN for events beyond the @seq_num. It requests
6572 * to be notified if and only if the event is of type @class_locale
6575 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6576 u32 class_locale_word)
6579 struct megasas_cmd *cmd;
6580 struct megasas_dcmd_frame *dcmd;
6581 union megasas_evt_class_locale curr_aen;
6582 union megasas_evt_class_locale prev_aen;
6585 * If there an AEN pending already (aen_cmd), check if the
6586 * class_locale of that pending AEN is inclusive of the new
6587 * AEN request we currently have. If it is, then we don't have
6588 * to do anything. In other words, whichever events the current
6589 * AEN request is subscribing to, have already been subscribed
6592 * If the old_cmd is _not_ inclusive, then we have to abort
6593 * that command, form a class_locale that is superset of both
6594 * old and current and re-issue to the FW
6597 curr_aen.word = class_locale_word;
6599 if (instance->aen_cmd) {
6602 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6604 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6605 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6606 dev_info(&instance->pdev->dev,
6607 "%s %d out of range class %d send by application\n",
6608 __func__, __LINE__, curr_aen.members.class);
6613 * A class whose enum value is smaller is inclusive of all
6614 * higher values. If a PROGRESS (= -1) was previously
6615 * registered, then a new registration requests for higher
6616 * classes need not be sent to FW. They are automatically
6619 * Locale numbers don't have such hierarchy. They are bitmap
6622 if ((prev_aen.members.class <= curr_aen.members.class) &&
6623 !((prev_aen.members.locale & curr_aen.members.locale) ^
6624 curr_aen.members.locale)) {
6626 * Previously issued event registration includes
6627 * current request. Nothing to do.
6631 curr_aen.members.locale |= prev_aen.members.locale;
6633 if (prev_aen.members.class < curr_aen.members.class)
6634 curr_aen.members.class = prev_aen.members.class;
6636 instance->aen_cmd->abort_aen = 1;
6637 ret_val = megasas_issue_blocked_abort_cmd(instance,
6642 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6643 "previous AEN command\n");
6649 cmd = megasas_get_cmd(instance);
6654 dcmd = &cmd->frame->dcmd;
6656 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6659 * Prepare DCMD for aen registration
6661 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6663 dcmd->cmd = MFI_CMD_DCMD;
6664 dcmd->cmd_status = 0x0;
6665 dcmd->sge_count = 1;
6666 dcmd->flags = MFI_FRAME_DIR_READ;
6669 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6670 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6671 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6672 instance->last_seq_num = seq_num;
6673 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6675 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6676 sizeof(struct megasas_evt_detail));
6678 if (instance->aen_cmd != NULL) {
6679 megasas_return_cmd(instance, cmd);
6684 * Store reference to the cmd used to register for AEN. When an
6685 * application wants us to register for AEN, we have to abort this
6686 * cmd and re-register with a new EVENT LOCALE supplied by that app
6688 instance->aen_cmd = cmd;
6691 * Issue the aen registration frame
6693 instance->instancet->issue_dcmd(instance, cmd);
6698 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6700 * This DCMD will fetch few properties of LD/system PD defined
6701 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6703 * DCMD send by drivers whenever new target is added to the OS.
6705 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6706 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6707 * 0 = system PD, 1 = LD.
6708 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6709 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6711 * @instance: Adapter soft state
6712 * @sdev: OS provided scsi device
6714 * Returns 0 on success non-zero on failure.
6717 megasas_get_target_prop(struct megasas_instance *instance,
6718 struct scsi_device *sdev)
6721 struct megasas_cmd *cmd;
6722 struct megasas_dcmd_frame *dcmd;
6723 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6726 cmd = megasas_get_cmd(instance);
6729 dev_err(&instance->pdev->dev,
6730 "Failed to get cmd %s\n", __func__);
6734 dcmd = &cmd->frame->dcmd;
6736 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6737 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6738 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6740 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6741 dcmd->cmd = MFI_CMD_DCMD;
6742 dcmd->cmd_status = 0xFF;
6743 dcmd->sge_count = 1;
6744 dcmd->flags = MFI_FRAME_DIR_READ;
6747 dcmd->data_xfer_len =
6748 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6749 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6751 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6752 sizeof(struct MR_TARGET_PROPERTIES));
6754 if ((instance->adapter_type != MFI_SERIES) &&
6755 !instance->mask_interrupts)
6756 ret = megasas_issue_blocked_cmd(instance,
6757 cmd, MFI_IO_TIMEOUT_SECS);
6759 ret = megasas_issue_polled(instance, cmd);
6763 switch (dcmd_timeout_ocr_possible(instance)) {
6765 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6766 mutex_unlock(&instance->reset_mutex);
6767 megasas_reset_fusion(instance->host,
6768 MFI_IO_TIMEOUT_OCR);
6769 mutex_lock(&instance->reset_mutex);
6772 megaraid_sas_kill_hba(instance);
6774 case IGNORE_TIMEOUT:
6775 dev_info(&instance->pdev->dev,
6776 "Ignore DCMD timeout: %s %d\n",
6777 __func__, __LINE__);
6783 megasas_return_cmd(instance, cmd);
6785 if (ret != DCMD_SUCCESS)
6786 dev_err(&instance->pdev->dev,
6787 "return from %s %d return value %d\n",
6788 __func__, __LINE__, ret);
6794 * megasas_start_aen - Subscribes to AEN during driver load time
6795 * @instance: Adapter soft state
6797 static int megasas_start_aen(struct megasas_instance *instance)
6799 struct megasas_evt_log_info eli;
6800 union megasas_evt_class_locale class_locale;
6803 * Get the latest sequence number from FW
6805 memset(&eli, 0, sizeof(eli));
6807 if (megasas_get_seq_num(instance, &eli))
6811 * Register AEN with FW for latest sequence number plus 1
6813 class_locale.members.reserved = 0;
6814 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6815 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6817 return megasas_register_aen(instance,
6818 le32_to_cpu(eli.newest_seq_num) + 1,
6823 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6824 * @instance: Adapter soft state
6826 static int megasas_io_attach(struct megasas_instance *instance)
6828 struct Scsi_Host *host = instance->host;
6831 * Export parameters required by SCSI mid-layer
6833 host->unique_id = instance->unique_id;
6834 host->can_queue = instance->max_scsi_cmds;
6835 host->this_id = instance->init_id;
6836 host->sg_tablesize = instance->max_num_sge;
6838 if (instance->fw_support_ieee)
6839 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6842 * Check if the module parameter value for max_sectors can be used
6844 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6845 instance->max_sectors_per_req = max_sectors;
6848 if (((instance->pdev->device ==
6849 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6850 (instance->pdev->device ==
6851 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6852 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6853 instance->max_sectors_per_req = max_sectors;
6855 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6856 "and <= %d (or < 1MB for GEN2 controller)\n",
6857 instance->max_sectors_per_req);
6862 host->max_sectors = instance->max_sectors_per_req;
6863 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6864 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6865 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6866 host->max_lun = MEGASAS_MAX_LUN;
6867 host->max_cmd_len = 16;
6870 * Notify the mid-layer about the new controller
6872 if (scsi_add_host(host, &instance->pdev->dev)) {
6873 dev_err(&instance->pdev->dev,
6874 "Failed to add host from %s %d\n",
6875 __func__, __LINE__);
6883 * megasas_set_dma_mask - Set DMA mask for supported controllers
6885 * @instance: Adapter soft state
6888 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6891 * By default, driver/FW will operate in 32bit DMA addresses
6892 * for consistent DMA mapping but if 32 bit consistent
6893 * DMA mask fails, driver will try with 63 bit consistent
6894 * mask provided FW is true 63bit DMA capable
6896 * For older controllers(Thunderbolt and MFI based adapters)-
6897 * driver/FW will operate in 32 bit consistent DMA addresses.
6900 megasas_set_dma_mask(struct megasas_instance *instance)
6902 u64 consistent_mask;
6903 struct pci_dev *pdev;
6906 pdev = instance->pdev;
6907 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6908 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6911 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6912 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6913 goto fail_set_dma_mask;
6915 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6916 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6917 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6919 * If 32 bit DMA mask fails, then try for 64 bit mask
6920 * for FW capable of handling 64 bit DMA.
6922 scratch_pad_1 = megasas_readl
6923 (instance, &instance->reg_set->outbound_scratch_pad_1);
6925 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6926 goto fail_set_dma_mask;
6927 else if (dma_set_mask_and_coherent(&pdev->dev,
6929 goto fail_set_dma_mask;
6931 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6932 goto fail_set_dma_mask;
6934 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6935 instance->consistent_mask_64bit = false;
6937 instance->consistent_mask_64bit = true;
6939 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6940 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6941 (instance->consistent_mask_64bit ? "63" : "32"));
6946 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6952 * megasas_set_adapter_type - Set adapter type.
6953 * Supported controllers can be divided in
6954 * different categories-
6955 * enum MR_ADAPTER_TYPE {
6957 * THUNDERBOLT_SERIES = 2,
6958 * INVADER_SERIES = 3,
6959 * VENTURA_SERIES = 4,
6962 * @instance: Adapter soft state
6965 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6967 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6968 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6969 instance->adapter_type = MFI_SERIES;
6971 switch (instance->pdev->device) {
6972 case PCI_DEVICE_ID_LSI_AERO_10E1:
6973 case PCI_DEVICE_ID_LSI_AERO_10E2:
6974 case PCI_DEVICE_ID_LSI_AERO_10E5:
6975 case PCI_DEVICE_ID_LSI_AERO_10E6:
6976 instance->adapter_type = AERO_SERIES;
6978 case PCI_DEVICE_ID_LSI_VENTURA:
6979 case PCI_DEVICE_ID_LSI_CRUSADER:
6980 case PCI_DEVICE_ID_LSI_HARPOON:
6981 case PCI_DEVICE_ID_LSI_TOMCAT:
6982 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6983 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6984 instance->adapter_type = VENTURA_SERIES;
6986 case PCI_DEVICE_ID_LSI_FUSION:
6987 case PCI_DEVICE_ID_LSI_PLASMA:
6988 instance->adapter_type = THUNDERBOLT_SERIES;
6990 case PCI_DEVICE_ID_LSI_INVADER:
6991 case PCI_DEVICE_ID_LSI_INTRUDER:
6992 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6993 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6994 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6995 case PCI_DEVICE_ID_LSI_FURY:
6996 instance->adapter_type = INVADER_SERIES;
6998 default: /* For all other supported controllers */
6999 instance->adapter_type = MFI_SERIES;
7005 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
7007 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
7008 sizeof(u32), &instance->producer_h, GFP_KERNEL);
7009 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
7010 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
7012 if (!instance->producer || !instance->consumer) {
7013 dev_err(&instance->pdev->dev,
7014 "Failed to allocate memory for producer, consumer\n");
7018 *instance->producer = 0;
7019 *instance->consumer = 0;
7024 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
7025 * structures which are not common across MFI
7026 * adapters and fusion adapters.
7027 * For MFI based adapters, allocate producer and
7028 * consumer buffers. For fusion adapters, allocate
7029 * memory for fusion context.
7030 * @instance: Adapter soft state
7031 * return: 0 for SUCCESS
7033 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
7035 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
7037 if (!instance->reply_map)
7040 switch (instance->adapter_type) {
7042 if (megasas_alloc_mfi_ctrl_mem(instance))
7046 case VENTURA_SERIES:
7047 case THUNDERBOLT_SERIES:
7048 case INVADER_SERIES:
7049 if (megasas_alloc_fusion_context(instance))
7056 kfree(instance->reply_map);
7057 instance->reply_map = NULL;
7062 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
7063 * producer, consumer buffers for MFI adapters
7065 * @instance - Adapter soft instance
7068 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
7070 kfree(instance->reply_map);
7071 if (instance->adapter_type == MFI_SERIES) {
7072 if (instance->producer)
7073 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7075 instance->producer_h);
7076 if (instance->consumer)
7077 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7079 instance->consumer_h);
7081 megasas_free_fusion_context(instance);
7086 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
7089 * @instance: Adapter soft instance
7091 * @return: O for SUCCESS
7094 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
7096 struct pci_dev *pdev = instance->pdev;
7097 struct fusion_context *fusion = instance->ctrl_context;
7099 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
7100 sizeof(struct megasas_evt_detail),
7101 &instance->evt_detail_h, GFP_KERNEL);
7103 if (!instance->evt_detail) {
7104 dev_err(&instance->pdev->dev,
7105 "Failed to allocate event detail buffer\n");
7110 fusion->ioc_init_request =
7111 dma_alloc_coherent(&pdev->dev,
7112 sizeof(struct MPI2_IOC_INIT_REQUEST),
7113 &fusion->ioc_init_request_phys,
7116 if (!fusion->ioc_init_request) {
7118 "Failed to allocate PD list buffer\n");
7122 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
7123 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7124 &instance->snapdump_prop_h, GFP_KERNEL);
7126 if (!instance->snapdump_prop)
7128 "Failed to allocate snapdump properties buffer\n");
7130 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
7131 HOST_DEVICE_LIST_SZ,
7132 &instance->host_device_list_buf_h,
7135 if (!instance->host_device_list_buf) {
7137 "Failed to allocate targetid list buffer\n");
7143 instance->pd_list_buf =
7144 dma_alloc_coherent(&pdev->dev,
7145 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7146 &instance->pd_list_buf_h, GFP_KERNEL);
7148 if (!instance->pd_list_buf) {
7149 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
7153 instance->ctrl_info_buf =
7154 dma_alloc_coherent(&pdev->dev,
7155 sizeof(struct megasas_ctrl_info),
7156 &instance->ctrl_info_buf_h, GFP_KERNEL);
7158 if (!instance->ctrl_info_buf) {
7160 "Failed to allocate controller info buffer\n");
7164 instance->ld_list_buf =
7165 dma_alloc_coherent(&pdev->dev,
7166 sizeof(struct MR_LD_LIST),
7167 &instance->ld_list_buf_h, GFP_KERNEL);
7169 if (!instance->ld_list_buf) {
7170 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7174 instance->ld_targetid_list_buf =
7175 dma_alloc_coherent(&pdev->dev,
7176 sizeof(struct MR_LD_TARGETID_LIST),
7177 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
7179 if (!instance->ld_targetid_list_buf) {
7181 "Failed to allocate LD targetid list buffer\n");
7185 if (!reset_devices) {
7186 instance->system_info_buf =
7187 dma_alloc_coherent(&pdev->dev,
7188 sizeof(struct MR_DRV_SYSTEM_INFO),
7189 &instance->system_info_h, GFP_KERNEL);
7191 dma_alloc_coherent(&pdev->dev,
7192 sizeof(struct MR_PD_INFO),
7193 &instance->pd_info_h, GFP_KERNEL);
7194 instance->tgt_prop =
7195 dma_alloc_coherent(&pdev->dev,
7196 sizeof(struct MR_TARGET_PROPERTIES),
7197 &instance->tgt_prop_h, GFP_KERNEL);
7198 instance->crash_dump_buf =
7199 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7200 &instance->crash_dump_h, GFP_KERNEL);
7202 if (!instance->system_info_buf)
7203 dev_err(&instance->pdev->dev,
7204 "Failed to allocate system info buffer\n");
7206 if (!instance->pd_info)
7207 dev_err(&instance->pdev->dev,
7208 "Failed to allocate pd_info buffer\n");
7210 if (!instance->tgt_prop)
7211 dev_err(&instance->pdev->dev,
7212 "Failed to allocate tgt_prop buffer\n");
7214 if (!instance->crash_dump_buf)
7215 dev_err(&instance->pdev->dev,
7216 "Failed to allocate crash dump buffer\n");
7223 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
7224 * during driver load time
7226 * @instance- Adapter soft instance
7230 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7232 struct pci_dev *pdev = instance->pdev;
7233 struct fusion_context *fusion = instance->ctrl_context;
7235 if (instance->evt_detail)
7236 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
7237 instance->evt_detail,
7238 instance->evt_detail_h);
7240 if (fusion && fusion->ioc_init_request)
7241 dma_free_coherent(&pdev->dev,
7242 sizeof(struct MPI2_IOC_INIT_REQUEST),
7243 fusion->ioc_init_request,
7244 fusion->ioc_init_request_phys);
7246 if (instance->pd_list_buf)
7247 dma_free_coherent(&pdev->dev,
7248 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7249 instance->pd_list_buf,
7250 instance->pd_list_buf_h);
7252 if (instance->ld_list_buf)
7253 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
7254 instance->ld_list_buf,
7255 instance->ld_list_buf_h);
7257 if (instance->ld_targetid_list_buf)
7258 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
7259 instance->ld_targetid_list_buf,
7260 instance->ld_targetid_list_buf_h);
7262 if (instance->ctrl_info_buf)
7263 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
7264 instance->ctrl_info_buf,
7265 instance->ctrl_info_buf_h);
7267 if (instance->system_info_buf)
7268 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
7269 instance->system_info_buf,
7270 instance->system_info_h);
7272 if (instance->pd_info)
7273 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
7274 instance->pd_info, instance->pd_info_h);
7276 if (instance->tgt_prop)
7277 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
7278 instance->tgt_prop, instance->tgt_prop_h);
7280 if (instance->crash_dump_buf)
7281 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7282 instance->crash_dump_buf,
7283 instance->crash_dump_h);
7285 if (instance->snapdump_prop)
7286 dma_free_coherent(&pdev->dev,
7287 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7288 instance->snapdump_prop,
7289 instance->snapdump_prop_h);
7291 if (instance->host_device_list_buf)
7292 dma_free_coherent(&pdev->dev,
7293 HOST_DEVICE_LIST_SZ,
7294 instance->host_device_list_buf,
7295 instance->host_device_list_buf_h);
7300 * megasas_init_ctrl_params - Initialize controller's instance
7301 * parameters before FW init
7302 * @instance - Adapter soft instance
7305 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7307 instance->fw_crash_state = UNAVAILABLE;
7309 megasas_poll_wait_aen = 0;
7310 instance->issuepend_done = 1;
7311 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7314 * Initialize locks and queues
7316 INIT_LIST_HEAD(&instance->cmd_pool);
7317 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7319 atomic_set(&instance->fw_outstanding, 0);
7320 atomic64_set(&instance->total_io_count, 0);
7322 init_waitqueue_head(&instance->int_cmd_wait_q);
7323 init_waitqueue_head(&instance->abort_cmd_wait_q);
7325 mutex_init(&instance->crashdump_lock);
7326 spin_lock_init(&instance->mfi_pool_lock);
7327 spin_lock_init(&instance->hba_lock);
7328 spin_lock_init(&instance->stream_lock);
7329 spin_lock_init(&instance->completion_lock);
7331 mutex_init(&instance->reset_mutex);
7333 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7334 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7335 instance->flag_ieee = 1;
7337 megasas_dbg_lvl = 0;
7339 instance->unload = 1;
7340 instance->last_time = 0;
7341 instance->disableOnlineCtrlReset = 1;
7342 instance->UnevenSpanSupport = 0;
7343 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7344 instance->msix_load_balance = false;
7346 if (instance->adapter_type != MFI_SERIES)
7347 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7349 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7353 * megasas_probe_one - PCI hotplug entry point
7354 * @pdev: PCI device structure
7355 * @id: PCI ids of supported hotplugged adapter
7357 static int megasas_probe_one(struct pci_dev *pdev,
7358 const struct pci_device_id *id)
7361 struct Scsi_Host *host;
7362 struct megasas_instance *instance;
7365 switch (pdev->device) {
7366 case PCI_DEVICE_ID_LSI_AERO_10E0:
7367 case PCI_DEVICE_ID_LSI_AERO_10E3:
7368 case PCI_DEVICE_ID_LSI_AERO_10E4:
7369 case PCI_DEVICE_ID_LSI_AERO_10E7:
7370 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7372 case PCI_DEVICE_ID_LSI_AERO_10E1:
7373 case PCI_DEVICE_ID_LSI_AERO_10E5:
7374 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7378 /* Reset MSI-X in the kdump kernel */
7379 if (reset_devices) {
7380 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7382 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7384 if (control & PCI_MSIX_FLAGS_ENABLE) {
7385 dev_info(&pdev->dev, "resetting MSI-X\n");
7386 pci_write_config_word(pdev,
7387 pos + PCI_MSIX_FLAGS,
7389 ~PCI_MSIX_FLAGS_ENABLE);
7395 * PCI prepping: enable device set bus mastering and dma mask
7397 rval = pci_enable_device_mem(pdev);
7403 pci_set_master(pdev);
7405 host = scsi_host_alloc(&megasas_template,
7406 sizeof(struct megasas_instance));
7409 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7410 goto fail_alloc_instance;
7413 instance = (struct megasas_instance *)host->hostdata;
7414 memset(instance, 0, sizeof(*instance));
7415 atomic_set(&instance->fw_reset_no_pci_access, 0);
7418 * Initialize PCI related and misc parameters
7420 instance->pdev = pdev;
7421 instance->host = host;
7422 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7423 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7425 megasas_set_adapter_type(instance);
7428 * Initialize MFI Firmware
7430 if (megasas_init_fw(instance))
7433 if (instance->requestorId) {
7434 if (instance->PlasmaFW111) {
7435 instance->vf_affiliation_111 =
7436 dma_alloc_coherent(&pdev->dev,
7437 sizeof(struct MR_LD_VF_AFFILIATION_111),
7438 &instance->vf_affiliation_111_h,
7440 if (!instance->vf_affiliation_111)
7441 dev_warn(&pdev->dev, "Can't allocate "
7442 "memory for VF affiliation buffer\n");
7444 instance->vf_affiliation =
7445 dma_alloc_coherent(&pdev->dev,
7446 (MAX_LOGICAL_DRIVES + 1) *
7447 sizeof(struct MR_LD_VF_AFFILIATION),
7448 &instance->vf_affiliation_h,
7450 if (!instance->vf_affiliation)
7451 dev_warn(&pdev->dev, "Can't allocate "
7452 "memory for VF affiliation buffer\n");
7457 * Store instance in PCI softstate
7459 pci_set_drvdata(pdev, instance);
7462 * Add this controller to megasas_mgmt_info structure so that it
7463 * can be exported to management applications
7465 megasas_mgmt_info.count++;
7466 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7467 megasas_mgmt_info.max_index++;
7470 * Register with SCSI mid-layer
7472 if (megasas_io_attach(instance))
7473 goto fail_io_attach;
7475 instance->unload = 0;
7477 * Trigger SCSI to scan our drives
7479 if (!instance->enable_fw_dev_list ||
7480 (instance->host_device_list_buf->count > 0))
7481 scsi_scan_host(host);
7484 * Initiate AEN (Asynchronous Event Notification)
7486 if (megasas_start_aen(instance)) {
7487 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7488 goto fail_start_aen;
7491 megasas_setup_debugfs(instance);
7493 /* Get current SR-IOV LD/VF affiliation */
7494 if (instance->requestorId)
7495 megasas_get_ld_vf_affiliation(instance, 1);
7500 instance->unload = 1;
7501 scsi_remove_host(instance->host);
7503 megasas_mgmt_info.count--;
7504 megasas_mgmt_info.max_index--;
7505 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7507 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7508 del_timer_sync(&instance->sriov_heartbeat_timer);
7510 instance->instancet->disable_intr(instance);
7511 megasas_destroy_irqs(instance);
7513 if (instance->adapter_type != MFI_SERIES)
7514 megasas_release_fusion(instance);
7516 megasas_release_mfi(instance);
7518 if (instance->msix_vectors)
7519 pci_free_irq_vectors(instance->pdev);
7520 instance->msix_vectors = 0;
7522 if (instance->fw_crash_state != UNAVAILABLE)
7523 megasas_free_host_crash_buffer(instance);
7525 if (instance->adapter_type != MFI_SERIES)
7526 megasas_fusion_stop_watchdog(instance);
7528 scsi_host_put(host);
7529 fail_alloc_instance:
7530 pci_disable_device(pdev);
7536 * megasas_flush_cache - Requests FW to flush all its caches
7537 * @instance: Adapter soft state
7539 static void megasas_flush_cache(struct megasas_instance *instance)
7541 struct megasas_cmd *cmd;
7542 struct megasas_dcmd_frame *dcmd;
7544 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7547 cmd = megasas_get_cmd(instance);
7552 dcmd = &cmd->frame->dcmd;
7554 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7556 dcmd->cmd = MFI_CMD_DCMD;
7557 dcmd->cmd_status = 0x0;
7558 dcmd->sge_count = 0;
7559 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7562 dcmd->data_xfer_len = 0;
7563 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7564 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7566 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7568 dev_err(&instance->pdev->dev,
7569 "return from %s %d\n", __func__, __LINE__);
7573 megasas_return_cmd(instance, cmd);
7577 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7578 * @instance: Adapter soft state
7579 * @opcode: Shutdown/Hibernate
7581 static void megasas_shutdown_controller(struct megasas_instance *instance,
7584 struct megasas_cmd *cmd;
7585 struct megasas_dcmd_frame *dcmd;
7587 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7590 cmd = megasas_get_cmd(instance);
7595 if (instance->aen_cmd)
7596 megasas_issue_blocked_abort_cmd(instance,
7597 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7598 if (instance->map_update_cmd)
7599 megasas_issue_blocked_abort_cmd(instance,
7600 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7601 if (instance->jbod_seq_cmd)
7602 megasas_issue_blocked_abort_cmd(instance,
7603 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7605 dcmd = &cmd->frame->dcmd;
7607 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7609 dcmd->cmd = MFI_CMD_DCMD;
7610 dcmd->cmd_status = 0x0;
7611 dcmd->sge_count = 0;
7612 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7615 dcmd->data_xfer_len = 0;
7616 dcmd->opcode = cpu_to_le32(opcode);
7618 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7620 dev_err(&instance->pdev->dev,
7621 "return from %s %d\n", __func__, __LINE__);
7625 megasas_return_cmd(instance, cmd);
7630 * megasas_suspend - driver suspend entry point
7631 * @pdev: PCI device structure
7632 * @state: PCI power state to suspend routine
7635 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7637 struct megasas_instance *instance;
7639 instance = pci_get_drvdata(pdev);
7644 instance->unload = 1;
7646 dev_info(&pdev->dev, "%s is called\n", __func__);
7648 /* Shutdown SR-IOV heartbeat timer */
7649 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7650 del_timer_sync(&instance->sriov_heartbeat_timer);
7652 /* Stop the FW fault detection watchdog */
7653 if (instance->adapter_type != MFI_SERIES)
7654 megasas_fusion_stop_watchdog(instance);
7656 megasas_flush_cache(instance);
7657 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7659 /* cancel the delayed work if this work still in queue */
7660 if (instance->ev != NULL) {
7661 struct megasas_aen_event *ev = instance->ev;
7662 cancel_delayed_work_sync(&ev->hotplug_work);
7663 instance->ev = NULL;
7666 tasklet_kill(&instance->isr_tasklet);
7668 pci_set_drvdata(instance->pdev, instance);
7669 instance->instancet->disable_intr(instance);
7671 megasas_destroy_irqs(instance);
7673 if (instance->msix_vectors)
7674 pci_free_irq_vectors(instance->pdev);
7676 pci_save_state(pdev);
7677 pci_disable_device(pdev);
7679 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7685 * megasas_resume- driver resume entry point
7686 * @pdev: PCI device structure
7689 megasas_resume(struct pci_dev *pdev)
7692 struct Scsi_Host *host;
7693 struct megasas_instance *instance;
7696 instance = pci_get_drvdata(pdev);
7701 host = instance->host;
7702 pci_set_power_state(pdev, PCI_D0);
7703 pci_enable_wake(pdev, PCI_D0, 0);
7704 pci_restore_state(pdev);
7706 dev_info(&pdev->dev, "%s is called\n", __func__);
7708 * PCI prepping: enable device set bus mastering and dma mask
7710 rval = pci_enable_device_mem(pdev);
7713 dev_err(&pdev->dev, "Enable device failed\n");
7717 pci_set_master(pdev);
7720 * We expect the FW state to be READY
7723 if (megasas_transition_to_ready(instance, 0)) {
7724 dev_info(&instance->pdev->dev,
7725 "Failed to transition controller to ready from %s!\n",
7727 if (instance->adapter_type != MFI_SERIES) {
7729 instance->instancet->read_fw_status_reg(instance);
7730 if (!(status_reg & MFI_RESET_ADAPTER) ||
7731 ((megasas_adp_reset_wait_for_ready
7732 (instance, true, 0)) == FAILED))
7733 goto fail_ready_state;
7735 atomic_set(&instance->fw_reset_no_pci_access, 1);
7736 instance->instancet->adp_reset
7737 (instance, instance->reg_set);
7738 atomic_set(&instance->fw_reset_no_pci_access, 0);
7740 /* waiting for about 30 seconds before retry */
7743 if (megasas_transition_to_ready(instance, 0))
7744 goto fail_ready_state;
7747 dev_info(&instance->pdev->dev,
7748 "FW restarted successfully from %s!\n",
7751 if (megasas_set_dma_mask(instance))
7752 goto fail_set_dma_mask;
7755 * Initialize MFI Firmware
7758 atomic_set(&instance->fw_outstanding, 0);
7759 atomic_set(&instance->ldio_outstanding, 0);
7761 /* Now re-enable MSI-X */
7762 if (instance->msix_vectors)
7763 megasas_alloc_irq_vectors(instance);
7765 if (!instance->msix_vectors) {
7766 rval = pci_alloc_irq_vectors(instance->pdev, 1, 1,
7769 goto fail_reenable_msix;
7772 megasas_setup_reply_map(instance);
7774 if (instance->adapter_type != MFI_SERIES) {
7775 megasas_reset_reply_desc(instance);
7776 if (megasas_ioc_init_fusion(instance)) {
7777 megasas_free_cmds(instance);
7778 megasas_free_cmds_fusion(instance);
7781 if (!megasas_get_map_info(instance))
7782 megasas_sync_map_info(instance);
7784 *instance->producer = 0;
7785 *instance->consumer = 0;
7786 if (megasas_issue_init_mfi(instance))
7790 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7793 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7794 (unsigned long)instance);
7796 if (instance->msix_vectors ?
7797 megasas_setup_irqs_msix(instance, 0) :
7798 megasas_setup_irqs_ioapic(instance))
7801 if (instance->adapter_type != MFI_SERIES)
7802 megasas_setup_irq_poll(instance);
7804 /* Re-launch SR-IOV heartbeat timer */
7805 if (instance->requestorId) {
7806 if (!megasas_sriov_start_heartbeat(instance, 0))
7807 megasas_start_timer(instance);
7809 instance->skip_heartbeat_timer_del = 1;
7814 instance->instancet->enable_intr(instance);
7815 megasas_setup_jbod_map(instance);
7816 instance->unload = 0;
7819 * Initiate AEN (Asynchronous Event Notification)
7821 if (megasas_start_aen(instance))
7822 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7824 /* Re-launch FW fault watchdog */
7825 if (instance->adapter_type != MFI_SERIES)
7826 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7827 goto fail_start_watchdog;
7831 fail_start_watchdog:
7832 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7833 del_timer_sync(&instance->sriov_heartbeat_timer);
7835 megasas_free_ctrl_dma_buffers(instance);
7836 megasas_free_ctrl_mem(instance);
7837 scsi_host_put(host);
7843 pci_disable_device(pdev);
7848 #define megasas_suspend NULL
7849 #define megasas_resume NULL
7853 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7855 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7859 for (i = 0; i < wait_time; i++) {
7860 adp_state = atomic_read(&instance->adprecovery);
7861 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7862 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7865 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7866 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7871 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7872 dev_info(&instance->pdev->dev,
7873 "%s HBA failed to become operational, adp_state %d\n",
7874 __func__, adp_state);
7882 * megasas_detach_one - PCI hot"un"plug entry point
7883 * @pdev: PCI device structure
7885 static void megasas_detach_one(struct pci_dev *pdev)
7888 struct Scsi_Host *host;
7889 struct megasas_instance *instance;
7890 struct fusion_context *fusion;
7893 instance = pci_get_drvdata(pdev);
7898 host = instance->host;
7899 fusion = instance->ctrl_context;
7901 /* Shutdown SR-IOV heartbeat timer */
7902 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7903 del_timer_sync(&instance->sriov_heartbeat_timer);
7905 /* Stop the FW fault detection watchdog */
7906 if (instance->adapter_type != MFI_SERIES)
7907 megasas_fusion_stop_watchdog(instance);
7909 if (instance->fw_crash_state != UNAVAILABLE)
7910 megasas_free_host_crash_buffer(instance);
7911 scsi_remove_host(instance->host);
7912 instance->unload = 1;
7914 if (megasas_wait_for_adapter_operational(instance))
7915 goto skip_firing_dcmds;
7917 megasas_flush_cache(instance);
7918 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7921 /* cancel the delayed work if this work still in queue*/
7922 if (instance->ev != NULL) {
7923 struct megasas_aen_event *ev = instance->ev;
7924 cancel_delayed_work_sync(&ev->hotplug_work);
7925 instance->ev = NULL;
7928 /* cancel all wait events */
7929 wake_up_all(&instance->int_cmd_wait_q);
7931 tasklet_kill(&instance->isr_tasklet);
7934 * Take the instance off the instance array. Note that we will not
7935 * decrement the max_index. We let this array be sparse array
7937 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7938 if (megasas_mgmt_info.instance[i] == instance) {
7939 megasas_mgmt_info.count--;
7940 megasas_mgmt_info.instance[i] = NULL;
7946 instance->instancet->disable_intr(instance);
7948 megasas_destroy_irqs(instance);
7950 if (instance->msix_vectors)
7951 pci_free_irq_vectors(instance->pdev);
7953 if (instance->adapter_type >= VENTURA_SERIES) {
7954 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7955 kfree(fusion->stream_detect_by_ld[i]);
7956 kfree(fusion->stream_detect_by_ld);
7957 fusion->stream_detect_by_ld = NULL;
7961 if (instance->adapter_type != MFI_SERIES) {
7962 megasas_release_fusion(instance);
7963 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7964 (sizeof(struct MR_PD_CFG_SEQ) *
7965 (MAX_PHYSICAL_DEVICES - 1));
7966 for (i = 0; i < 2 ; i++) {
7967 if (fusion->ld_map[i])
7968 dma_free_coherent(&instance->pdev->dev,
7971 fusion->ld_map_phys[i]);
7972 if (fusion->ld_drv_map[i]) {
7973 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7974 vfree(fusion->ld_drv_map[i]);
7976 free_pages((ulong)fusion->ld_drv_map[i],
7977 fusion->drv_map_pages);
7980 if (fusion->pd_seq_sync[i])
7981 dma_free_coherent(&instance->pdev->dev,
7983 fusion->pd_seq_sync[i],
7984 fusion->pd_seq_phys[i]);
7987 megasas_release_mfi(instance);
7990 if (instance->vf_affiliation)
7991 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7992 sizeof(struct MR_LD_VF_AFFILIATION),
7993 instance->vf_affiliation,
7994 instance->vf_affiliation_h);
7996 if (instance->vf_affiliation_111)
7997 dma_free_coherent(&pdev->dev,
7998 sizeof(struct MR_LD_VF_AFFILIATION_111),
7999 instance->vf_affiliation_111,
8000 instance->vf_affiliation_111_h);
8002 if (instance->hb_host_mem)
8003 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
8004 instance->hb_host_mem,
8005 instance->hb_host_mem_h);
8007 megasas_free_ctrl_dma_buffers(instance);
8009 megasas_free_ctrl_mem(instance);
8011 megasas_destroy_debugfs(instance);
8013 scsi_host_put(host);
8015 pci_disable_device(pdev);
8019 * megasas_shutdown - Shutdown entry point
8020 * @pdev: Generic device structure
8022 static void megasas_shutdown(struct pci_dev *pdev)
8024 struct megasas_instance *instance = pci_get_drvdata(pdev);
8029 instance->unload = 1;
8031 if (megasas_wait_for_adapter_operational(instance))
8032 goto skip_firing_dcmds;
8034 megasas_flush_cache(instance);
8035 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
8038 instance->instancet->disable_intr(instance);
8039 megasas_destroy_irqs(instance);
8041 if (instance->msix_vectors)
8042 pci_free_irq_vectors(instance->pdev);
8046 * megasas_mgmt_open - char node "open" entry point
8047 * @inode: char node inode
8048 * @filep: char node file
8050 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
8053 * Allow only those users with admin rights
8055 if (!capable(CAP_SYS_ADMIN))
8062 * megasas_mgmt_fasync - Async notifier registration from applications
8063 * @fd: char node file descriptor number
8064 * @filep: char node file
8065 * @mode: notifier on/off
8067 * This function adds the calling process to a driver global queue. When an
8068 * event occurs, SIGIO will be sent to all processes in this queue.
8070 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
8074 mutex_lock(&megasas_async_queue_mutex);
8076 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
8078 mutex_unlock(&megasas_async_queue_mutex);
8081 /* For sanity check when we get ioctl */
8082 filep->private_data = filep;
8086 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
8092 * megasas_mgmt_poll - char node "poll" entry point
8093 * @filep: char node file
8094 * @wait: Events to poll for
8096 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
8099 unsigned long flags;
8101 poll_wait(file, &megasas_poll_wait, wait);
8102 spin_lock_irqsave(&poll_aen_lock, flags);
8103 if (megasas_poll_wait_aen)
8104 mask = (EPOLLIN | EPOLLRDNORM);
8107 megasas_poll_wait_aen = 0;
8108 spin_unlock_irqrestore(&poll_aen_lock, flags);
8113 * megasas_set_crash_dump_params_ioctl:
8114 * Send CRASH_DUMP_MODE DCMD to all controllers
8115 * @cmd: MFI command frame
8118 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
8120 struct megasas_instance *local_instance;
8124 crash_support = cmd->frame->dcmd.mbox.w[0];
8126 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8127 local_instance = megasas_mgmt_info.instance[i];
8128 if (local_instance && local_instance->crash_dump_drv_support) {
8129 if ((atomic_read(&local_instance->adprecovery) ==
8130 MEGASAS_HBA_OPERATIONAL) &&
8131 !megasas_set_crash_dump_params(local_instance,
8133 local_instance->crash_dump_app_support =
8135 dev_info(&local_instance->pdev->dev,
8136 "Application firmware crash "
8137 "dump mode set success\n");
8140 dev_info(&local_instance->pdev->dev,
8141 "Application firmware crash "
8142 "dump mode set failed\n");
8151 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
8152 * @instance: Adapter soft state
8153 * @user_ioc: User's ioctl packet
8154 * @ioc: ioctl packet
8157 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
8158 struct megasas_iocpacket __user * user_ioc,
8159 struct megasas_iocpacket *ioc)
8161 struct megasas_sge64 *kern_sge64 = NULL;
8162 struct megasas_sge32 *kern_sge32 = NULL;
8163 struct megasas_cmd *cmd;
8164 void *kbuff_arr[MAX_IOCTL_SGE];
8165 dma_addr_t buf_handle = 0;
8168 dma_addr_t sense_handle;
8171 int ret = DCMD_SUCCESS;
8173 memset(kbuff_arr, 0, sizeof(kbuff_arr));
8175 if (ioc->sge_count > MAX_IOCTL_SGE) {
8176 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
8177 ioc->sge_count, MAX_IOCTL_SGE);
8181 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
8182 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
8183 !instance->support_nvme_passthru) ||
8184 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
8185 !instance->support_pci_lane_margining)) {
8186 dev_err(&instance->pdev->dev,
8187 "Received invalid ioctl command 0x%x\n",
8188 ioc->frame.hdr.cmd);
8192 cmd = megasas_get_cmd(instance);
8194 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
8199 * User's IOCTL packet has 2 frames (maximum). Copy those two
8200 * frames into our cmd's frames. cmd->frame's context will get
8201 * overwritten when we copy from user's frames. So set that value
8204 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
8205 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
8206 cmd->frame->hdr.pad_0 = 0;
8208 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8210 if (instance->consistent_mask_64bit)
8211 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8212 MFI_FRAME_SENSE64));
8214 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
8215 MFI_FRAME_SENSE64));
8217 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8218 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
8220 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
8221 mutex_lock(&instance->reset_mutex);
8222 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8223 megasas_return_cmd(instance, cmd);
8224 mutex_unlock(&instance->reset_mutex);
8227 mutex_unlock(&instance->reset_mutex);
8230 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
8231 error = megasas_set_crash_dump_params_ioctl(cmd);
8232 megasas_return_cmd(instance, cmd);
8237 * The management interface between applications and the fw uses
8238 * MFI frames. E.g, RAID configuration changes, LD property changes
8239 * etc are accomplishes through different kinds of MFI frames. The
8240 * driver needs to care only about substituting user buffers with
8241 * kernel buffers in SGLs. The location of SGL is embedded in the
8242 * struct iocpacket itself.
8244 if (instance->consistent_mask_64bit)
8245 kern_sge64 = (struct megasas_sge64 *)
8246 ((unsigned long)cmd->frame + ioc->sgl_off);
8248 kern_sge32 = (struct megasas_sge32 *)
8249 ((unsigned long)cmd->frame + ioc->sgl_off);
8252 * For each user buffer, create a mirror buffer and copy in
8254 for (i = 0; i < ioc->sge_count; i++) {
8255 if (!ioc->sgl[i].iov_len)
8258 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
8259 ioc->sgl[i].iov_len,
8260 &buf_handle, GFP_KERNEL);
8261 if (!kbuff_arr[i]) {
8262 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8263 "kernel SGL buffer for IOCTL\n");
8269 * We don't change the dma_coherent_mask, so
8270 * dma_alloc_coherent only returns 32bit addresses
8272 if (instance->consistent_mask_64bit) {
8273 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8274 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8276 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8277 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8281 * We created a kernel buffer corresponding to the
8282 * user buffer. Now copy in from the user buffer
8284 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8285 (u32) (ioc->sgl[i].iov_len))) {
8291 if (ioc->sense_len) {
8292 /* make sure the pointer is part of the frame */
8293 if (ioc->sense_off >
8294 (sizeof(union megasas_frame) - sizeof(__le64))) {
8299 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8300 &sense_handle, GFP_KERNEL);
8306 /* always store 64 bits regardless of addressing */
8307 sense_ptr = (void *)cmd->frame + ioc->sense_off;
8308 put_unaligned_le64(sense_handle, sense_ptr);
8312 * Set the sync_cmd flag so that the ISR knows not to complete this
8313 * cmd to the SCSI mid-layer
8317 ret = megasas_issue_blocked_cmd(instance, cmd, 0);
8322 dev_err(&instance->pdev->dev,
8323 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8324 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8325 cmd->cmd_status_drv);
8332 if (instance->unload == 1) {
8333 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8334 "don't submit data to application\n");
8338 * copy out the kernel buffers to user buffers
8340 for (i = 0; i < ioc->sge_count; i++) {
8341 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8342 ioc->sgl[i].iov_len)) {
8349 * copy out the sense
8351 if (ioc->sense_len) {
8353 * sense_ptr points to the location that has the user
8354 * sense buffer address
8356 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8359 if (copy_to_user((void __user *)((unsigned long)
8360 get_unaligned((unsigned long *)sense_ptr)),
8361 sense, ioc->sense_len)) {
8362 dev_err(&instance->pdev->dev, "Failed to copy out to user "
8370 * copy the status codes returned by the fw
8372 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8373 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
8374 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
8380 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
8381 sense, sense_handle);
8384 for (i = 0; i < ioc->sge_count; i++) {
8386 if (instance->consistent_mask_64bit)
8387 dma_free_coherent(&instance->pdev->dev,
8388 le32_to_cpu(kern_sge64[i].length),
8390 le64_to_cpu(kern_sge64[i].phys_addr));
8392 dma_free_coherent(&instance->pdev->dev,
8393 le32_to_cpu(kern_sge32[i].length),
8395 le32_to_cpu(kern_sge32[i].phys_addr));
8396 kbuff_arr[i] = NULL;
8400 megasas_return_cmd(instance, cmd);
8404 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8406 struct megasas_iocpacket __user *user_ioc =
8407 (struct megasas_iocpacket __user *)arg;
8408 struct megasas_iocpacket *ioc;
8409 struct megasas_instance *instance;
8412 ioc = memdup_user(user_ioc, sizeof(*ioc));
8414 return PTR_ERR(ioc);
8416 instance = megasas_lookup_instance(ioc->host_no);
8422 /* Block ioctls in VF mode */
8423 if (instance->requestorId && !allow_vf_ioctls) {
8428 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8429 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8434 if (instance->unload == 1) {
8439 if (down_interruptible(&instance->ioctl_sem)) {
8440 error = -ERESTARTSYS;
8444 if (megasas_wait_for_adapter_operational(instance)) {
8449 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8451 up(&instance->ioctl_sem);
8458 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8460 struct megasas_instance *instance;
8461 struct megasas_aen aen;
8464 if (file->private_data != file) {
8465 printk(KERN_DEBUG "megasas: fasync_helper was not "
8470 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8473 instance = megasas_lookup_instance(aen.host_no);
8478 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8482 if (instance->unload == 1) {
8486 if (megasas_wait_for_adapter_operational(instance))
8489 mutex_lock(&instance->reset_mutex);
8490 error = megasas_register_aen(instance, aen.seq_num,
8491 aen.class_locale_word);
8492 mutex_unlock(&instance->reset_mutex);
8497 * megasas_mgmt_ioctl - char node ioctl entry point
8498 * @file: char device file pointer
8499 * @cmd: ioctl command
8500 * @arg: ioctl command arguments address
8503 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8506 case MEGASAS_IOC_FIRMWARE:
8507 return megasas_mgmt_ioctl_fw(file, arg);
8509 case MEGASAS_IOC_GET_AEN:
8510 return megasas_mgmt_ioctl_aen(file, arg);
8516 #ifdef CONFIG_COMPAT
8517 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8519 struct compat_megasas_iocpacket __user *cioc =
8520 (struct compat_megasas_iocpacket __user *)arg;
8521 struct megasas_iocpacket __user *ioc =
8522 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8526 u32 local_sense_off;
8527 u32 local_sense_len;
8530 if (clear_user(ioc, sizeof(*ioc)))
8533 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8534 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8535 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8536 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8537 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8538 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8542 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8543 * sense_len is not null, so prepare the 64bit value under
8544 * the same condition.
8546 if (get_user(local_sense_off, &ioc->sense_off) ||
8547 get_user(local_sense_len, &ioc->sense_len) ||
8548 get_user(user_sense_off, &cioc->sense_off))
8551 if (local_sense_off != user_sense_off)
8554 if (local_sense_len) {
8555 void __user **sense_ioc_ptr =
8556 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
8557 compat_uptr_t *sense_cioc_ptr =
8558 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
8559 if (get_user(ptr, sense_cioc_ptr) ||
8560 put_user(compat_ptr(ptr), sense_ioc_ptr))
8564 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8565 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8566 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8567 copy_in_user(&ioc->sgl[i].iov_len,
8568 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8572 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8574 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8575 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8576 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8583 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8587 case MEGASAS_IOC_FIRMWARE32:
8588 return megasas_mgmt_compat_ioctl_fw(file, arg);
8589 case MEGASAS_IOC_GET_AEN:
8590 return megasas_mgmt_ioctl_aen(file, arg);
8598 * File operations structure for management interface
8600 static const struct file_operations megasas_mgmt_fops = {
8601 .owner = THIS_MODULE,
8602 .open = megasas_mgmt_open,
8603 .fasync = megasas_mgmt_fasync,
8604 .unlocked_ioctl = megasas_mgmt_ioctl,
8605 .poll = megasas_mgmt_poll,
8606 #ifdef CONFIG_COMPAT
8607 .compat_ioctl = megasas_mgmt_compat_ioctl,
8609 .llseek = noop_llseek,
8613 * PCI hotplug support registration structure
8615 static struct pci_driver megasas_pci_driver = {
8617 .name = "megaraid_sas",
8618 .id_table = megasas_pci_table,
8619 .probe = megasas_probe_one,
8620 .remove = megasas_detach_one,
8621 .suspend = megasas_suspend,
8622 .resume = megasas_resume,
8623 .shutdown = megasas_shutdown,
8627 * Sysfs driver attributes
8629 static ssize_t version_show(struct device_driver *dd, char *buf)
8631 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8634 static DRIVER_ATTR_RO(version);
8636 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8638 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8641 static DRIVER_ATTR_RO(release_date);
8643 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8645 return sprintf(buf, "%u\n", support_poll_for_event);
8647 static DRIVER_ATTR_RO(support_poll_for_event);
8649 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8651 return sprintf(buf, "%u\n", support_device_change);
8653 static DRIVER_ATTR_RO(support_device_change);
8655 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8657 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8660 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8665 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8666 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8671 static DRIVER_ATTR_RW(dbg_lvl);
8674 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8676 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8679 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8682 support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8684 return sprintf(buf, "%u\n", support_pci_lane_margining);
8687 static DRIVER_ATTR_RO(support_pci_lane_margining);
8689 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8691 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8692 scsi_remove_device(sdev);
8693 scsi_device_put(sdev);
8697 * megasas_update_device_list - Update the PD and LD device list from FW
8698 * after an AEN event notification
8699 * @instance: Adapter soft state
8700 * @event_type: Indicates type of event (PD or LD event)
8702 * @return: Success or failure
8704 * Issue DCMDs to Firmware to update the internal device list in driver.
8705 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8706 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8709 int megasas_update_device_list(struct megasas_instance *instance,
8712 int dcmd_ret = DCMD_SUCCESS;
8714 if (instance->enable_fw_dev_list) {
8715 dcmd_ret = megasas_host_device_list_query(instance, false);
8716 if (dcmd_ret != DCMD_SUCCESS)
8719 if (event_type & SCAN_PD_CHANNEL) {
8720 dcmd_ret = megasas_get_pd_list(instance);
8722 if (dcmd_ret != DCMD_SUCCESS)
8726 if (event_type & SCAN_VD_CHANNEL) {
8727 if (!instance->requestorId ||
8728 (instance->requestorId &&
8729 megasas_get_ld_vf_affiliation(instance, 0))) {
8730 dcmd_ret = megasas_ld_list_query(instance,
8731 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8732 if (dcmd_ret != DCMD_SUCCESS)
8743 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8744 * after an AEN event notification
8745 * @instance: Adapter soft state
8746 * @scan_type: Indicates type of devices (PD/LD) to add
8750 void megasas_add_remove_devices(struct megasas_instance *instance,
8756 u16 channel = 0, id = 0;
8757 struct Scsi_Host *host;
8758 struct scsi_device *sdev1;
8759 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8760 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8762 host = instance->host;
8764 if (instance->enable_fw_dev_list) {
8765 targetid_list = instance->host_device_list_buf;
8766 for (i = 0; i < targetid_list->count; i++) {
8767 targetid_entry = &targetid_list->host_device_list[i];
8768 if (targetid_entry->flags.u.bits.is_sys_pd) {
8769 channel = le16_to_cpu(targetid_entry->target_id) /
8770 MEGASAS_MAX_DEV_PER_CHANNEL;
8771 id = le16_to_cpu(targetid_entry->target_id) %
8772 MEGASAS_MAX_DEV_PER_CHANNEL;
8774 channel = MEGASAS_MAX_PD_CHANNELS +
8775 (le16_to_cpu(targetid_entry->target_id) /
8776 MEGASAS_MAX_DEV_PER_CHANNEL);
8777 id = le16_to_cpu(targetid_entry->target_id) %
8778 MEGASAS_MAX_DEV_PER_CHANNEL;
8780 sdev1 = scsi_device_lookup(host, channel, id, 0);
8782 scsi_add_device(host, channel, id, 0);
8784 scsi_device_put(sdev1);
8789 if (scan_type & SCAN_PD_CHANNEL) {
8790 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8791 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8792 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8793 sdev1 = scsi_device_lookup(host, i, j, 0);
8794 if (instance->pd_list[pd_index].driveState ==
8795 MR_PD_STATE_SYSTEM) {
8797 scsi_add_device(host, i, j, 0);
8799 scsi_device_put(sdev1);
8802 megasas_remove_scsi_device(sdev1);
8808 if (scan_type & SCAN_VD_CHANNEL) {
8809 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8810 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8811 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8812 sdev1 = scsi_device_lookup(host,
8813 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8814 if (instance->ld_ids[ld_index] != 0xff) {
8816 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8818 scsi_device_put(sdev1);
8821 megasas_remove_scsi_device(sdev1);
8830 megasas_aen_polling(struct work_struct *work)
8832 struct megasas_aen_event *ev =
8833 container_of(work, struct megasas_aen_event, hotplug_work.work);
8834 struct megasas_instance *instance = ev->instance;
8835 union megasas_evt_class_locale class_locale;
8840 u8 dcmd_ret = DCMD_SUCCESS;
8841 struct scsi_device *sdev1;
8844 printk(KERN_ERR "invalid instance!\n");
8849 /* Don't run the event workqueue thread if OCR is running */
8850 mutex_lock(&instance->reset_mutex);
8852 instance->ev = NULL;
8853 if (instance->evt_detail) {
8854 megasas_decode_evt(instance);
8856 switch (le32_to_cpu(instance->evt_detail->code)) {
8858 case MR_EVT_PD_INSERTED:
8859 case MR_EVT_PD_REMOVED:
8860 event_type = SCAN_PD_CHANNEL;
8863 case MR_EVT_LD_OFFLINE:
8864 case MR_EVT_LD_DELETED:
8865 ld_target_id = instance->evt_detail->args.ld.target_id;
8866 sdev1 = scsi_device_lookup(instance->host,
8867 MEGASAS_MAX_PD_CHANNELS +
8868 (ld_target_id / MEGASAS_MAX_DEV_PER_CHANNEL),
8869 (ld_target_id - MEGASAS_MAX_DEV_PER_CHANNEL),
8872 megasas_remove_scsi_device(sdev1);
8874 event_type = SCAN_VD_CHANNEL;
8876 case MR_EVT_LD_CREATED:
8877 event_type = SCAN_VD_CHANNEL;
8880 case MR_EVT_CFG_CLEARED:
8881 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8882 case MR_EVT_FOREIGN_CFG_IMPORTED:
8883 case MR_EVT_LD_STATE_CHANGE:
8884 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8885 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8886 instance->host->host_no);
8889 case MR_EVT_CTRL_PROP_CHANGED:
8890 dcmd_ret = megasas_get_ctrl_info(instance);
8891 if (dcmd_ret == DCMD_SUCCESS &&
8892 instance->snapdump_wait_time) {
8893 megasas_get_snapdump_properties(instance);
8894 dev_info(&instance->pdev->dev,
8895 "Snap dump wait time\t: %d\n",
8896 instance->snapdump_wait_time);
8904 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8905 mutex_unlock(&instance->reset_mutex);
8911 dcmd_ret = megasas_update_device_list(instance, event_type);
8913 mutex_unlock(&instance->reset_mutex);
8915 if (event_type && dcmd_ret == DCMD_SUCCESS)
8916 megasas_add_remove_devices(instance, event_type);
8918 if (dcmd_ret == DCMD_SUCCESS)
8919 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8921 seq_num = instance->last_seq_num;
8923 /* Register AEN with FW for latest sequence number plus 1 */
8924 class_locale.members.reserved = 0;
8925 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8926 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8928 if (instance->aen_cmd != NULL) {
8933 mutex_lock(&instance->reset_mutex);
8934 error = megasas_register_aen(instance, seq_num,
8937 dev_err(&instance->pdev->dev,
8938 "register aen failed error %x\n", error);
8940 mutex_unlock(&instance->reset_mutex);
8945 * megasas_init - Driver load entry point
8947 static int __init megasas_init(void)
8952 * Booted in kdump kernel, minimize memory footprints by
8953 * disabling few features
8955 if (reset_devices) {
8958 dual_qdepth_disable = 1;
8962 * Announce driver version and other information
8964 pr_info("megasas: %s\n", MEGASAS_VERSION);
8966 spin_lock_init(&poll_aen_lock);
8968 support_poll_for_event = 2;
8969 support_device_change = 1;
8970 support_nvme_encapsulation = true;
8971 support_pci_lane_margining = true;
8973 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8976 * Register character device node
8978 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8981 printk(KERN_DEBUG "megasas: failed to open device node\n");
8985 megasas_mgmt_majorno = rval;
8987 megasas_init_debugfs();
8990 * Register ourselves as PCI hotplug module
8992 rval = pci_register_driver(&megasas_pci_driver);
8995 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8999 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
9000 (event_log_level > MFI_EVT_CLASS_DEAD)) {
9001 pr_warn("megaraid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
9002 event_log_level = MFI_EVT_CLASS_CRITICAL;
9005 rval = driver_create_file(&megasas_pci_driver.driver,
9006 &driver_attr_version);
9008 goto err_dcf_attr_ver;
9010 rval = driver_create_file(&megasas_pci_driver.driver,
9011 &driver_attr_release_date);
9013 goto err_dcf_rel_date;
9015 rval = driver_create_file(&megasas_pci_driver.driver,
9016 &driver_attr_support_poll_for_event);
9018 goto err_dcf_support_poll_for_event;
9020 rval = driver_create_file(&megasas_pci_driver.driver,
9021 &driver_attr_dbg_lvl);
9023 goto err_dcf_dbg_lvl;
9024 rval = driver_create_file(&megasas_pci_driver.driver,
9025 &driver_attr_support_device_change);
9027 goto err_dcf_support_device_change;
9029 rval = driver_create_file(&megasas_pci_driver.driver,
9030 &driver_attr_support_nvme_encapsulation);
9032 goto err_dcf_support_nvme_encapsulation;
9034 rval = driver_create_file(&megasas_pci_driver.driver,
9035 &driver_attr_support_pci_lane_margining);
9037 goto err_dcf_support_pci_lane_margining;
9041 err_dcf_support_pci_lane_margining:
9042 driver_remove_file(&megasas_pci_driver.driver,
9043 &driver_attr_support_nvme_encapsulation);
9045 err_dcf_support_nvme_encapsulation:
9046 driver_remove_file(&megasas_pci_driver.driver,
9047 &driver_attr_support_device_change);
9049 err_dcf_support_device_change:
9050 driver_remove_file(&megasas_pci_driver.driver,
9051 &driver_attr_dbg_lvl);
9053 driver_remove_file(&megasas_pci_driver.driver,
9054 &driver_attr_support_poll_for_event);
9055 err_dcf_support_poll_for_event:
9056 driver_remove_file(&megasas_pci_driver.driver,
9057 &driver_attr_release_date);
9059 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9061 pci_unregister_driver(&megasas_pci_driver);
9063 megasas_exit_debugfs();
9064 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9069 * megasas_exit - Driver unload entry point
9071 static void __exit megasas_exit(void)
9073 driver_remove_file(&megasas_pci_driver.driver,
9074 &driver_attr_dbg_lvl);
9075 driver_remove_file(&megasas_pci_driver.driver,
9076 &driver_attr_support_poll_for_event);
9077 driver_remove_file(&megasas_pci_driver.driver,
9078 &driver_attr_support_device_change);
9079 driver_remove_file(&megasas_pci_driver.driver,
9080 &driver_attr_release_date);
9081 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9082 driver_remove_file(&megasas_pci_driver.driver,
9083 &driver_attr_support_nvme_encapsulation);
9084 driver_remove_file(&megasas_pci_driver.driver,
9085 &driver_attr_support_pci_lane_margining);
9087 pci_unregister_driver(&megasas_pci_driver);
9088 megasas_exit_debugfs();
9089 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9092 module_init(megasas_init);
9093 module_exit(megasas_exit);