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 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);
132 * PCI ID table for all supported controllers
134 static struct pci_device_id megasas_pci_table[] = {
136 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
138 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
140 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
142 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
144 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
146 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
148 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
150 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
151 /* xscale IOP, vega */
152 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
165 /* Intruder 24 port*/
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
167 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
169 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
171 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
172 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
173 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
174 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
175 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
176 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
177 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
178 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
179 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
180 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
181 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
182 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
186 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
188 static int megasas_mgmt_majorno;
189 struct megasas_mgmt_info megasas_mgmt_info;
190 static struct fasync_struct *megasas_async_queue;
191 static DEFINE_MUTEX(megasas_async_queue_mutex);
193 static int megasas_poll_wait_aen;
194 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
195 static u32 support_poll_for_event;
197 static u32 support_device_change;
198 static bool support_nvme_encapsulation;
199 static bool support_pci_lane_margining;
201 /* define lock for aen poll */
202 static spinlock_t poll_aen_lock;
204 extern struct dentry *megasas_debugfs_root;
207 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
210 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
212 megasas_adp_reset_gen2(struct megasas_instance *instance,
213 struct megasas_register_set __iomem *reg_set);
214 static irqreturn_t megasas_isr(int irq, void *devp);
216 megasas_init_adapter_mfi(struct megasas_instance *instance);
218 megasas_build_and_issue_cmd(struct megasas_instance *instance,
219 struct scsi_cmnd *scmd);
220 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
222 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
224 void megasas_fusion_ocr_wq(struct work_struct *work);
225 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
228 megasas_set_dma_mask(struct megasas_instance *instance);
230 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
232 megasas_free_ctrl_mem(struct megasas_instance *instance);
234 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
236 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
238 megasas_init_ctrl_params(struct megasas_instance *instance);
240 u32 megasas_readl(struct megasas_instance *instance,
241 const volatile void __iomem *addr)
245 * Due to a HW errata in Aero controllers, reads to certain
246 * Fusion registers could intermittently return all zeroes.
247 * This behavior is transient in nature and subsequent reads will
248 * return valid value. As a workaround in driver, retry readl for
249 * upto three times until a non-zero value is read.
251 if (instance->adapter_type == AERO_SERIES) {
253 ret_val = readl(addr);
255 } while (ret_val == 0 && i < 3);
263 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
264 * @instance: Adapter soft state
265 * @dcmd: DCMD frame inside MFI command
266 * @dma_addr: DMA address of buffer to be passed to FW
267 * @dma_len: Length of DMA buffer to be passed to FW
270 void megasas_set_dma_settings(struct megasas_instance *instance,
271 struct megasas_dcmd_frame *dcmd,
272 dma_addr_t dma_addr, u32 dma_len)
274 if (instance->consistent_mask_64bit) {
275 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
276 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
277 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
280 dcmd->sgl.sge32[0].phys_addr =
281 cpu_to_le32(lower_32_bits(dma_addr));
282 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
283 dcmd->flags = cpu_to_le16(dcmd->flags);
288 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
290 instance->instancet->fire_cmd(instance,
291 cmd->frame_phys_addr, 0, instance->reg_set);
296 * megasas_get_cmd - Get a command from the free pool
297 * @instance: Adapter soft state
299 * Returns a free command from the pool
301 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
305 struct megasas_cmd *cmd = NULL;
307 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
309 if (!list_empty(&instance->cmd_pool)) {
310 cmd = list_entry((&instance->cmd_pool)->next,
311 struct megasas_cmd, list);
312 list_del_init(&cmd->list);
314 dev_err(&instance->pdev->dev, "Command pool empty!\n");
317 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
322 * megasas_return_cmd - Return a cmd to free command pool
323 * @instance: Adapter soft state
324 * @cmd: Command packet to be returned to free command pool
327 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
331 struct megasas_cmd_fusion *cmd_fusion;
332 struct fusion_context *fusion = instance->ctrl_context;
334 /* This flag is used only for fusion adapter.
335 * Wait for Interrupt for Polled mode DCMD
337 if (cmd->flags & DRV_DCMD_POLLED_MODE)
340 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
343 blk_tags = instance->max_scsi_cmds + cmd->index;
344 cmd_fusion = fusion->cmd_list[blk_tags];
345 megasas_return_cmd_fusion(instance, cmd_fusion);
348 cmd->frame_count = 0;
350 memset(cmd->frame, 0, instance->mfi_frame_size);
351 cmd->frame->io.context = cpu_to_le32(cmd->index);
352 if (!fusion && reset_devices)
353 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
354 list_add(&cmd->list, (&instance->cmd_pool)->next);
356 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
361 format_timestamp(uint32_t timestamp)
363 static char buffer[32];
365 if ((timestamp & 0xff000000) == 0xff000000)
366 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
369 snprintf(buffer, sizeof(buffer), "%us", timestamp);
374 format_class(int8_t class)
376 static char buffer[6];
379 case MFI_EVT_CLASS_DEBUG:
381 case MFI_EVT_CLASS_PROGRESS:
383 case MFI_EVT_CLASS_INFO:
385 case MFI_EVT_CLASS_WARNING:
387 case MFI_EVT_CLASS_CRITICAL:
389 case MFI_EVT_CLASS_FATAL:
391 case MFI_EVT_CLASS_DEAD:
394 snprintf(buffer, sizeof(buffer), "%d", class);
400 * megasas_decode_evt: Decode FW AEN event and print critical event
402 * @instance: Adapter soft state
405 megasas_decode_evt(struct megasas_instance *instance)
407 struct megasas_evt_detail *evt_detail = instance->evt_detail;
408 union megasas_evt_class_locale class_locale;
409 class_locale.word = le32_to_cpu(evt_detail->cl.word);
411 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
412 (event_log_level > MFI_EVT_CLASS_DEAD)) {
413 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");
414 event_log_level = MFI_EVT_CLASS_CRITICAL;
417 if (class_locale.members.class >= event_log_level)
418 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
419 le32_to_cpu(evt_detail->seq_num),
420 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
421 (class_locale.members.locale),
422 format_class(class_locale.members.class),
423 evt_detail->description);
427 * The following functions are defined for xscale
428 * (deviceid : 1064R, PERC5) controllers
432 * megasas_enable_intr_xscale - Enables interrupts
433 * @instance: Adapter soft state
436 megasas_enable_intr_xscale(struct megasas_instance *instance)
438 struct megasas_register_set __iomem *regs;
440 regs = instance->reg_set;
441 writel(0, &(regs)->outbound_intr_mask);
443 /* Dummy readl to force pci flush */
444 readl(®s->outbound_intr_mask);
448 * megasas_disable_intr_xscale -Disables interrupt
449 * @instance: Adapter soft state
452 megasas_disable_intr_xscale(struct megasas_instance *instance)
454 struct megasas_register_set __iomem *regs;
457 regs = instance->reg_set;
458 writel(mask, ®s->outbound_intr_mask);
459 /* Dummy readl to force pci flush */
460 readl(®s->outbound_intr_mask);
464 * megasas_read_fw_status_reg_xscale - returns the current FW status value
465 * @instance: Adapter soft state
468 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
470 return readl(&instance->reg_set->outbound_msg_0);
473 * megasas_clear_interrupt_xscale - Check & clear interrupt
474 * @instance: Adapter soft state
477 megasas_clear_intr_xscale(struct megasas_instance *instance)
481 struct megasas_register_set __iomem *regs;
482 regs = instance->reg_set;
485 * Check if it is our interrupt
487 status = readl(®s->outbound_intr_status);
489 if (status & MFI_OB_INTR_STATUS_MASK)
490 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
491 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
492 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
495 * Clear the interrupt by writing back the same value
498 writel(status, ®s->outbound_intr_status);
500 /* Dummy readl to force pci flush */
501 readl(®s->outbound_intr_status);
507 * megasas_fire_cmd_xscale - Sends command to the FW
508 * @instance: Adapter soft state
509 * @frame_phys_addr : Physical address of cmd
510 * @frame_count : Number of frames for the command
511 * @regs : MFI register set
514 megasas_fire_cmd_xscale(struct megasas_instance *instance,
515 dma_addr_t frame_phys_addr,
517 struct megasas_register_set __iomem *regs)
521 spin_lock_irqsave(&instance->hba_lock, flags);
522 writel((frame_phys_addr >> 3)|(frame_count),
523 &(regs)->inbound_queue_port);
524 spin_unlock_irqrestore(&instance->hba_lock, flags);
528 * megasas_adp_reset_xscale - For controller reset
529 * @instance: Adapter soft state
530 * @regs: MFI register set
533 megasas_adp_reset_xscale(struct megasas_instance *instance,
534 struct megasas_register_set __iomem *regs)
539 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
541 for (i = 0; i < 3; i++)
542 msleep(1000); /* sleep for 3 secs */
544 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
545 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
547 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
549 pci_write_config_dword(instance->pdev,
550 MFI_1068_PCSR_OFFSET, pcidata);
552 for (i = 0; i < 2; i++)
553 msleep(1000); /* need to wait 2 secs again */
556 pci_read_config_dword(instance->pdev,
557 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
558 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
559 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
560 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
562 pci_write_config_dword(instance->pdev,
563 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
570 * megasas_check_reset_xscale - For controller reset check
571 * @instance: Adapter soft state
572 * @regs: MFI register set
575 megasas_check_reset_xscale(struct megasas_instance *instance,
576 struct megasas_register_set __iomem *regs)
578 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
579 (le32_to_cpu(*instance->consumer) ==
580 MEGASAS_ADPRESET_INPROG_SIGN))
585 static struct megasas_instance_template megasas_instance_template_xscale = {
587 .fire_cmd = megasas_fire_cmd_xscale,
588 .enable_intr = megasas_enable_intr_xscale,
589 .disable_intr = megasas_disable_intr_xscale,
590 .clear_intr = megasas_clear_intr_xscale,
591 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
592 .adp_reset = megasas_adp_reset_xscale,
593 .check_reset = megasas_check_reset_xscale,
594 .service_isr = megasas_isr,
595 .tasklet = megasas_complete_cmd_dpc,
596 .init_adapter = megasas_init_adapter_mfi,
597 .build_and_issue_cmd = megasas_build_and_issue_cmd,
598 .issue_dcmd = megasas_issue_dcmd,
602 * This is the end of set of functions & definitions specific
603 * to xscale (deviceid : 1064R, PERC5) controllers
607 * The following functions are defined for ppc (deviceid : 0x60)
612 * megasas_enable_intr_ppc - Enables interrupts
613 * @instance: Adapter soft state
616 megasas_enable_intr_ppc(struct megasas_instance *instance)
618 struct megasas_register_set __iomem *regs;
620 regs = instance->reg_set;
621 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
623 writel(~0x80000000, &(regs)->outbound_intr_mask);
625 /* Dummy readl to force pci flush */
626 readl(®s->outbound_intr_mask);
630 * megasas_disable_intr_ppc - Disable interrupt
631 * @instance: Adapter soft state
634 megasas_disable_intr_ppc(struct megasas_instance *instance)
636 struct megasas_register_set __iomem *regs;
637 u32 mask = 0xFFFFFFFF;
639 regs = instance->reg_set;
640 writel(mask, ®s->outbound_intr_mask);
641 /* Dummy readl to force pci flush */
642 readl(®s->outbound_intr_mask);
646 * megasas_read_fw_status_reg_ppc - returns the current FW status value
647 * @instance: Adapter soft state
650 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
652 return readl(&instance->reg_set->outbound_scratch_pad_0);
656 * megasas_clear_interrupt_ppc - Check & clear interrupt
657 * @instance: Adapter soft state
660 megasas_clear_intr_ppc(struct megasas_instance *instance)
662 u32 status, mfiStatus = 0;
663 struct megasas_register_set __iomem *regs;
664 regs = instance->reg_set;
667 * Check if it is our interrupt
669 status = readl(®s->outbound_intr_status);
671 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
672 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
674 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
675 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
678 * Clear the interrupt by writing back the same value
680 writel(status, ®s->outbound_doorbell_clear);
682 /* Dummy readl to force pci flush */
683 readl(®s->outbound_doorbell_clear);
689 * megasas_fire_cmd_ppc - Sends command to the FW
690 * @instance: Adapter soft state
691 * @frame_phys_addr: Physical address of cmd
692 * @frame_count: Number of frames for the command
693 * @regs: MFI register set
696 megasas_fire_cmd_ppc(struct megasas_instance *instance,
697 dma_addr_t frame_phys_addr,
699 struct megasas_register_set __iomem *regs)
703 spin_lock_irqsave(&instance->hba_lock, flags);
704 writel((frame_phys_addr | (frame_count<<1))|1,
705 &(regs)->inbound_queue_port);
706 spin_unlock_irqrestore(&instance->hba_lock, flags);
710 * megasas_check_reset_ppc - For controller reset check
711 * @instance: Adapter soft state
712 * @regs: MFI register set
715 megasas_check_reset_ppc(struct megasas_instance *instance,
716 struct megasas_register_set __iomem *regs)
718 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
724 static struct megasas_instance_template megasas_instance_template_ppc = {
726 .fire_cmd = megasas_fire_cmd_ppc,
727 .enable_intr = megasas_enable_intr_ppc,
728 .disable_intr = megasas_disable_intr_ppc,
729 .clear_intr = megasas_clear_intr_ppc,
730 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
731 .adp_reset = megasas_adp_reset_xscale,
732 .check_reset = megasas_check_reset_ppc,
733 .service_isr = megasas_isr,
734 .tasklet = megasas_complete_cmd_dpc,
735 .init_adapter = megasas_init_adapter_mfi,
736 .build_and_issue_cmd = megasas_build_and_issue_cmd,
737 .issue_dcmd = megasas_issue_dcmd,
741 * megasas_enable_intr_skinny - Enables interrupts
742 * @instance: Adapter soft state
745 megasas_enable_intr_skinny(struct megasas_instance *instance)
747 struct megasas_register_set __iomem *regs;
749 regs = instance->reg_set;
750 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
752 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
754 /* Dummy readl to force pci flush */
755 readl(®s->outbound_intr_mask);
759 * megasas_disable_intr_skinny - Disables interrupt
760 * @instance: Adapter soft state
763 megasas_disable_intr_skinny(struct megasas_instance *instance)
765 struct megasas_register_set __iomem *regs;
766 u32 mask = 0xFFFFFFFF;
768 regs = instance->reg_set;
769 writel(mask, ®s->outbound_intr_mask);
770 /* Dummy readl to force pci flush */
771 readl(®s->outbound_intr_mask);
775 * megasas_read_fw_status_reg_skinny - returns the current FW status value
776 * @instance: Adapter soft state
779 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
781 return readl(&instance->reg_set->outbound_scratch_pad_0);
785 * megasas_clear_interrupt_skinny - Check & clear interrupt
786 * @instance: Adapter soft state
789 megasas_clear_intr_skinny(struct megasas_instance *instance)
793 struct megasas_register_set __iomem *regs;
794 regs = instance->reg_set;
797 * Check if it is our interrupt
799 status = readl(®s->outbound_intr_status);
801 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
806 * Check if it is our interrupt
808 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
810 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
812 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
815 * Clear the interrupt by writing back the same value
817 writel(status, ®s->outbound_intr_status);
820 * dummy read to flush PCI
822 readl(®s->outbound_intr_status);
828 * megasas_fire_cmd_skinny - Sends command to the FW
829 * @instance: Adapter soft state
830 * @frame_phys_addr: Physical address of cmd
831 * @frame_count: Number of frames for the command
832 * @regs: MFI register set
835 megasas_fire_cmd_skinny(struct megasas_instance *instance,
836 dma_addr_t frame_phys_addr,
838 struct megasas_register_set __iomem *regs)
842 spin_lock_irqsave(&instance->hba_lock, flags);
843 writel(upper_32_bits(frame_phys_addr),
844 &(regs)->inbound_high_queue_port);
845 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
846 &(regs)->inbound_low_queue_port);
847 spin_unlock_irqrestore(&instance->hba_lock, flags);
851 * megasas_check_reset_skinny - For controller reset check
852 * @instance: Adapter soft state
853 * @regs: MFI register set
856 megasas_check_reset_skinny(struct megasas_instance *instance,
857 struct megasas_register_set __iomem *regs)
859 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
865 static struct megasas_instance_template megasas_instance_template_skinny = {
867 .fire_cmd = megasas_fire_cmd_skinny,
868 .enable_intr = megasas_enable_intr_skinny,
869 .disable_intr = megasas_disable_intr_skinny,
870 .clear_intr = megasas_clear_intr_skinny,
871 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
872 .adp_reset = megasas_adp_reset_gen2,
873 .check_reset = megasas_check_reset_skinny,
874 .service_isr = megasas_isr,
875 .tasklet = megasas_complete_cmd_dpc,
876 .init_adapter = megasas_init_adapter_mfi,
877 .build_and_issue_cmd = megasas_build_and_issue_cmd,
878 .issue_dcmd = megasas_issue_dcmd,
883 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
888 * megasas_enable_intr_gen2 - Enables interrupts
889 * @instance: Adapter soft state
892 megasas_enable_intr_gen2(struct megasas_instance *instance)
894 struct megasas_register_set __iomem *regs;
896 regs = instance->reg_set;
897 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
899 /* write ~0x00000005 (4 & 1) to the intr mask*/
900 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
902 /* Dummy readl to force pci flush */
903 readl(®s->outbound_intr_mask);
907 * megasas_disable_intr_gen2 - Disables interrupt
908 * @instance: Adapter soft state
911 megasas_disable_intr_gen2(struct megasas_instance *instance)
913 struct megasas_register_set __iomem *regs;
914 u32 mask = 0xFFFFFFFF;
916 regs = instance->reg_set;
917 writel(mask, ®s->outbound_intr_mask);
918 /* Dummy readl to force pci flush */
919 readl(®s->outbound_intr_mask);
923 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
924 * @instance: Adapter soft state
927 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
929 return readl(&instance->reg_set->outbound_scratch_pad_0);
933 * megasas_clear_interrupt_gen2 - Check & clear interrupt
934 * @instance: Adapter soft state
937 megasas_clear_intr_gen2(struct megasas_instance *instance)
941 struct megasas_register_set __iomem *regs;
942 regs = instance->reg_set;
945 * Check if it is our interrupt
947 status = readl(®s->outbound_intr_status);
949 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
950 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
952 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
953 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
957 * Clear the interrupt by writing back the same value
960 writel(status, ®s->outbound_doorbell_clear);
962 /* Dummy readl to force pci flush */
963 readl(®s->outbound_intr_status);
969 * megasas_fire_cmd_gen2 - Sends command to the FW
970 * @instance: Adapter soft state
971 * @frame_phys_addr: Physical address of cmd
972 * @frame_count: Number of frames for the command
973 * @regs: MFI register set
976 megasas_fire_cmd_gen2(struct megasas_instance *instance,
977 dma_addr_t frame_phys_addr,
979 struct megasas_register_set __iomem *regs)
983 spin_lock_irqsave(&instance->hba_lock, flags);
984 writel((frame_phys_addr | (frame_count<<1))|1,
985 &(regs)->inbound_queue_port);
986 spin_unlock_irqrestore(&instance->hba_lock, flags);
990 * megasas_adp_reset_gen2 - For controller reset
991 * @instance: Adapter soft state
992 * @reg_set: MFI register set
995 megasas_adp_reset_gen2(struct megasas_instance *instance,
996 struct megasas_register_set __iomem *reg_set)
1000 u32 __iomem *seq_offset = ®_set->seq_offset;
1001 u32 __iomem *hostdiag_offset = ®_set->host_diag;
1003 if (instance->instancet == &megasas_instance_template_skinny) {
1004 seq_offset = ®_set->fusion_seq_offset;
1005 hostdiag_offset = ®_set->fusion_host_diag;
1008 writel(0, seq_offset);
1009 writel(4, seq_offset);
1010 writel(0xb, seq_offset);
1011 writel(2, seq_offset);
1012 writel(7, seq_offset);
1013 writel(0xd, seq_offset);
1017 HostDiag = (u32)readl(hostdiag_offset);
1019 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1021 HostDiag = (u32)readl(hostdiag_offset);
1022 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1030 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1032 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1036 HostDiag = (u32)readl(hostdiag_offset);
1037 while (HostDiag & DIAG_RESET_ADAPTER) {
1039 HostDiag = (u32)readl(hostdiag_offset);
1040 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1043 if (retry++ >= 1000)
1051 * megasas_check_reset_gen2 - For controller reset check
1052 * @instance: Adapter soft state
1053 * @regs: MFI register set
1056 megasas_check_reset_gen2(struct megasas_instance *instance,
1057 struct megasas_register_set __iomem *regs)
1059 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1065 static struct megasas_instance_template megasas_instance_template_gen2 = {
1067 .fire_cmd = megasas_fire_cmd_gen2,
1068 .enable_intr = megasas_enable_intr_gen2,
1069 .disable_intr = megasas_disable_intr_gen2,
1070 .clear_intr = megasas_clear_intr_gen2,
1071 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1072 .adp_reset = megasas_adp_reset_gen2,
1073 .check_reset = megasas_check_reset_gen2,
1074 .service_isr = megasas_isr,
1075 .tasklet = megasas_complete_cmd_dpc,
1076 .init_adapter = megasas_init_adapter_mfi,
1077 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1078 .issue_dcmd = megasas_issue_dcmd,
1082 * This is the end of set of functions & definitions
1083 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1087 * Template added for TB (Fusion)
1089 extern struct megasas_instance_template megasas_instance_template_fusion;
1092 * megasas_issue_polled - Issues a polling command
1093 * @instance: Adapter soft state
1094 * @cmd: Command packet to be issued
1096 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1099 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1101 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1103 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1104 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1106 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1107 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1108 __func__, __LINE__);
1112 instance->instancet->issue_dcmd(instance, cmd);
1114 return wait_and_poll(instance, cmd, instance->requestorId ?
1115 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1119 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1120 * @instance: Adapter soft state
1121 * @cmd: Command to be issued
1122 * @timeout: Timeout in seconds
1124 * This function waits on an event for the command to be returned from ISR.
1125 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1126 * Used to issue ioctl commands.
1129 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1130 struct megasas_cmd *cmd, int timeout)
1133 cmd->cmd_status_drv = DCMD_INIT;
1135 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1136 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1137 __func__, __LINE__);
1141 instance->instancet->issue_dcmd(instance, cmd);
1144 ret = wait_event_timeout(instance->int_cmd_wait_q,
1145 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1147 dev_err(&instance->pdev->dev,
1148 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1149 cmd->frame->dcmd.opcode, __func__);
1150 return DCMD_TIMEOUT;
1153 wait_event(instance->int_cmd_wait_q,
1154 cmd->cmd_status_drv != DCMD_INIT);
1156 return cmd->cmd_status_drv;
1160 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1161 * @instance: Adapter soft state
1162 * @cmd_to_abort: Previously issued cmd to be aborted
1163 * @timeout: Timeout in seconds
1165 * MFI firmware can abort previously issued AEN comamnd (automatic event
1166 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1167 * cmd and waits for return status.
1168 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1171 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1172 struct megasas_cmd *cmd_to_abort, int timeout)
1174 struct megasas_cmd *cmd;
1175 struct megasas_abort_frame *abort_fr;
1179 cmd = megasas_get_cmd(instance);
1184 abort_fr = &cmd->frame->abort;
1187 * Prepare and issue the abort frame
1189 abort_fr->cmd = MFI_CMD_ABORT;
1190 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1191 abort_fr->flags = cpu_to_le16(0);
1192 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1193 abort_fr->abort_mfi_phys_addr_lo =
1194 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1195 abort_fr->abort_mfi_phys_addr_hi =
1196 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1199 cmd->cmd_status_drv = DCMD_INIT;
1201 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1202 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1203 __func__, __LINE__);
1207 instance->instancet->issue_dcmd(instance, cmd);
1210 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1211 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1213 opcode = cmd_to_abort->frame->dcmd.opcode;
1214 dev_err(&instance->pdev->dev,
1215 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1217 return DCMD_TIMEOUT;
1220 wait_event(instance->abort_cmd_wait_q,
1221 cmd->cmd_status_drv != DCMD_INIT);
1225 megasas_return_cmd(instance, cmd);
1226 return cmd->cmd_status_drv;
1230 * megasas_make_sgl32 - Prepares 32-bit SGL
1231 * @instance: Adapter soft state
1232 * @scp: SCSI command from the mid-layer
1233 * @mfi_sgl: SGL to be filled in
1235 * If successful, this function returns the number of SG elements. Otherwise,
1239 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1240 union megasas_sgl *mfi_sgl)
1244 struct scatterlist *os_sgl;
1246 sge_count = scsi_dma_map(scp);
1247 BUG_ON(sge_count < 0);
1250 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1251 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1252 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1259 * megasas_make_sgl64 - Prepares 64-bit SGL
1260 * @instance: Adapter soft state
1261 * @scp: SCSI command from the mid-layer
1262 * @mfi_sgl: SGL to be filled in
1264 * If successful, this function returns the number of SG elements. Otherwise,
1268 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1269 union megasas_sgl *mfi_sgl)
1273 struct scatterlist *os_sgl;
1275 sge_count = scsi_dma_map(scp);
1276 BUG_ON(sge_count < 0);
1279 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1280 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1281 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1288 * megasas_make_sgl_skinny - Prepares IEEE SGL
1289 * @instance: Adapter soft state
1290 * @scp: SCSI command from the mid-layer
1291 * @mfi_sgl: SGL to be filled in
1293 * If successful, this function returns the number of SG elements. Otherwise,
1297 megasas_make_sgl_skinny(struct megasas_instance *instance,
1298 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1302 struct scatterlist *os_sgl;
1304 sge_count = scsi_dma_map(scp);
1307 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1308 mfi_sgl->sge_skinny[i].length =
1309 cpu_to_le32(sg_dma_len(os_sgl));
1310 mfi_sgl->sge_skinny[i].phys_addr =
1311 cpu_to_le64(sg_dma_address(os_sgl));
1312 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1319 * megasas_get_frame_count - Computes the number of frames
1320 * @frame_type : type of frame- io or pthru frame
1321 * @sge_count : number of sg elements
1323 * Returns the number of frames required for numnber of sge's (sge_count)
1326 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1327 u8 sge_count, u8 frame_type)
1332 u32 frame_count = 0;
1334 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1335 sizeof(struct megasas_sge32);
1337 if (instance->flag_ieee) {
1338 sge_sz = sizeof(struct megasas_sge_skinny);
1342 * Main frame can contain 2 SGEs for 64-bit SGLs and
1343 * 3 SGEs for 32-bit SGLs for ldio &
1344 * 1 SGEs for 64-bit SGLs and
1345 * 2 SGEs for 32-bit SGLs for pthru frame
1347 if (unlikely(frame_type == PTHRU_FRAME)) {
1348 if (instance->flag_ieee == 1) {
1349 num_cnt = sge_count - 1;
1350 } else if (IS_DMA64)
1351 num_cnt = sge_count - 1;
1353 num_cnt = sge_count - 2;
1355 if (instance->flag_ieee == 1) {
1356 num_cnt = sge_count - 1;
1357 } else if (IS_DMA64)
1358 num_cnt = sge_count - 2;
1360 num_cnt = sge_count - 3;
1364 sge_bytes = sge_sz * num_cnt;
1366 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1367 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1372 if (frame_count > 7)
1378 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1379 * @instance: Adapter soft state
1380 * @scp: SCSI command
1381 * @cmd: Command to be prepared in
1383 * This function prepares CDB commands. These are typcially pass-through
1384 * commands to the devices.
1387 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1388 struct megasas_cmd *cmd)
1393 struct megasas_pthru_frame *pthru;
1395 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1396 device_id = MEGASAS_DEV_INDEX(scp);
1397 pthru = (struct megasas_pthru_frame *)cmd->frame;
1399 if (scp->sc_data_direction == DMA_TO_DEVICE)
1400 flags = MFI_FRAME_DIR_WRITE;
1401 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1402 flags = MFI_FRAME_DIR_READ;
1403 else if (scp->sc_data_direction == DMA_NONE)
1404 flags = MFI_FRAME_DIR_NONE;
1406 if (instance->flag_ieee == 1) {
1407 flags |= MFI_FRAME_IEEE;
1411 * Prepare the DCDB frame
1413 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1414 pthru->cmd_status = 0x0;
1415 pthru->scsi_status = 0x0;
1416 pthru->target_id = device_id;
1417 pthru->lun = scp->device->lun;
1418 pthru->cdb_len = scp->cmd_len;
1421 pthru->flags = cpu_to_le16(flags);
1422 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1424 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1427 * If the command is for the tape device, set the
1428 * pthru timeout to the os layer timeout value.
1430 if (scp->device->type == TYPE_TAPE) {
1431 if ((scp->request->timeout / HZ) > 0xFFFF)
1432 pthru->timeout = cpu_to_le16(0xFFFF);
1434 pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1440 if (instance->flag_ieee == 1) {
1441 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1442 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1444 } else if (IS_DMA64) {
1445 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1446 pthru->sge_count = megasas_make_sgl64(instance, scp,
1449 pthru->sge_count = megasas_make_sgl32(instance, scp,
1452 if (pthru->sge_count > instance->max_num_sge) {
1453 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1459 * Sense info specific
1461 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1462 pthru->sense_buf_phys_addr_hi =
1463 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1464 pthru->sense_buf_phys_addr_lo =
1465 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1468 * Compute the total number of frames this command consumes. FW uses
1469 * this number to pull sufficient number of frames from host memory.
1471 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1474 return cmd->frame_count;
1478 * megasas_build_ldio - Prepares IOs to logical devices
1479 * @instance: Adapter soft state
1480 * @scp: SCSI command
1481 * @cmd: Command to be prepared
1483 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1486 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1487 struct megasas_cmd *cmd)
1490 u8 sc = scp->cmnd[0];
1492 struct megasas_io_frame *ldio;
1494 device_id = MEGASAS_DEV_INDEX(scp);
1495 ldio = (struct megasas_io_frame *)cmd->frame;
1497 if (scp->sc_data_direction == DMA_TO_DEVICE)
1498 flags = MFI_FRAME_DIR_WRITE;
1499 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1500 flags = MFI_FRAME_DIR_READ;
1502 if (instance->flag_ieee == 1) {
1503 flags |= MFI_FRAME_IEEE;
1507 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1509 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1510 ldio->cmd_status = 0x0;
1511 ldio->scsi_status = 0x0;
1512 ldio->target_id = device_id;
1514 ldio->reserved_0 = 0;
1516 ldio->flags = cpu_to_le16(flags);
1517 ldio->start_lba_hi = 0;
1518 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1521 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1523 if (scp->cmd_len == 6) {
1524 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1525 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1526 ((u32) scp->cmnd[2] << 8) |
1527 (u32) scp->cmnd[3]);
1529 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1533 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1535 else if (scp->cmd_len == 10) {
1536 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1537 ((u32) scp->cmnd[7] << 8));
1538 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1539 ((u32) scp->cmnd[3] << 16) |
1540 ((u32) scp->cmnd[4] << 8) |
1541 (u32) scp->cmnd[5]);
1545 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1547 else if (scp->cmd_len == 12) {
1548 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1549 ((u32) scp->cmnd[7] << 16) |
1550 ((u32) scp->cmnd[8] << 8) |
1551 (u32) scp->cmnd[9]);
1553 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1554 ((u32) scp->cmnd[3] << 16) |
1555 ((u32) scp->cmnd[4] << 8) |
1556 (u32) scp->cmnd[5]);
1560 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1562 else if (scp->cmd_len == 16) {
1563 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1564 ((u32) scp->cmnd[11] << 16) |
1565 ((u32) scp->cmnd[12] << 8) |
1566 (u32) scp->cmnd[13]);
1568 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1569 ((u32) scp->cmnd[7] << 16) |
1570 ((u32) scp->cmnd[8] << 8) |
1571 (u32) scp->cmnd[9]);
1573 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1574 ((u32) scp->cmnd[3] << 16) |
1575 ((u32) scp->cmnd[4] << 8) |
1576 (u32) scp->cmnd[5]);
1583 if (instance->flag_ieee) {
1584 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1585 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1587 } else if (IS_DMA64) {
1588 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1589 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1591 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1593 if (ldio->sge_count > instance->max_num_sge) {
1594 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1600 * Sense info specific
1602 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1603 ldio->sense_buf_phys_addr_hi = 0;
1604 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1607 * Compute the total number of frames this command consumes. FW uses
1608 * this number to pull sufficient number of frames from host memory.
1610 cmd->frame_count = megasas_get_frame_count(instance,
1611 ldio->sge_count, IO_FRAME);
1613 return cmd->frame_count;
1617 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1618 * and whether it's RW or non RW
1619 * @cmd: SCSI command
1622 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1626 switch (cmd->cmnd[0]) {
1635 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1636 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1639 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1640 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1646 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1648 * @instance: Adapter soft state
1651 megasas_dump_pending_frames(struct megasas_instance *instance)
1653 struct megasas_cmd *cmd;
1655 union megasas_sgl *mfi_sgl;
1656 struct megasas_io_frame *ldio;
1657 struct megasas_pthru_frame *pthru;
1659 u16 max_cmd = instance->max_fw_cmds;
1661 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1662 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1664 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1666 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1668 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1669 for (i = 0; i < max_cmd; i++) {
1670 cmd = instance->cmd_list[i];
1673 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1674 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1675 ldio = (struct megasas_io_frame *)cmd->frame;
1676 mfi_sgl = &ldio->sgl;
1677 sgcount = ldio->sge_count;
1678 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1679 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1680 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1681 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1682 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1684 pthru = (struct megasas_pthru_frame *) cmd->frame;
1685 mfi_sgl = &pthru->sgl;
1686 sgcount = pthru->sge_count;
1687 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1688 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1689 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1690 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1691 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1693 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1694 for (n = 0; n < sgcount; n++) {
1696 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1697 le32_to_cpu(mfi_sgl->sge64[n].length),
1698 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1700 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1701 le32_to_cpu(mfi_sgl->sge32[n].length),
1702 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1706 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1707 for (i = 0; i < max_cmd; i++) {
1709 cmd = instance->cmd_list[i];
1711 if (cmd->sync_cmd == 1)
1712 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1714 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1718 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1719 struct scsi_cmnd *scmd)
1721 struct megasas_cmd *cmd;
1724 cmd = megasas_get_cmd(instance);
1726 return SCSI_MLQUEUE_HOST_BUSY;
1729 * Logical drive command
1731 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1732 frame_count = megasas_build_ldio(instance, scmd, cmd);
1734 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1737 goto out_return_cmd;
1740 scmd->SCp.ptr = (char *)cmd;
1743 * Issue the command to the FW
1745 atomic_inc(&instance->fw_outstanding);
1747 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1748 cmd->frame_count-1, instance->reg_set);
1752 megasas_return_cmd(instance, cmd);
1753 return SCSI_MLQUEUE_HOST_BUSY;
1758 * megasas_queue_command - Queue entry point
1759 * @shost: adapter SCSI host
1760 * @scmd: SCSI command to be queued
1763 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1765 struct megasas_instance *instance;
1766 struct MR_PRIV_DEVICE *mr_device_priv_data;
1768 instance = (struct megasas_instance *)
1769 scmd->device->host->hostdata;
1771 if (instance->unload == 1) {
1772 scmd->result = DID_NO_CONNECT << 16;
1773 scmd->scsi_done(scmd);
1777 if (instance->issuepend_done == 0)
1778 return SCSI_MLQUEUE_HOST_BUSY;
1781 /* Check for an mpio path and adjust behavior */
1782 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1783 if (megasas_check_mpio_paths(instance, scmd) ==
1784 (DID_REQUEUE << 16)) {
1785 return SCSI_MLQUEUE_HOST_BUSY;
1787 scmd->result = DID_NO_CONNECT << 16;
1788 scmd->scsi_done(scmd);
1793 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1794 scmd->result = DID_NO_CONNECT << 16;
1795 scmd->scsi_done(scmd);
1799 mr_device_priv_data = scmd->device->hostdata;
1800 if (!mr_device_priv_data) {
1801 scmd->result = DID_NO_CONNECT << 16;
1802 scmd->scsi_done(scmd);
1806 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1807 return SCSI_MLQUEUE_HOST_BUSY;
1809 if (mr_device_priv_data->tm_busy)
1810 return SCSI_MLQUEUE_DEVICE_BUSY;
1815 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1816 (scmd->device->id >= instance->fw_supported_vd_count ||
1817 scmd->device->lun)) {
1818 scmd->result = DID_BAD_TARGET << 16;
1822 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1823 MEGASAS_IS_LOGICAL(scmd->device) &&
1824 (!instance->fw_sync_cache_support)) {
1825 scmd->result = DID_OK << 16;
1829 return instance->instancet->build_and_issue_cmd(instance, scmd);
1832 scmd->scsi_done(scmd);
1836 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1840 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1842 if ((megasas_mgmt_info.instance[i]) &&
1843 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1844 return megasas_mgmt_info.instance[i];
1851 * megasas_set_dynamic_target_properties -
1852 * Device property set by driver may not be static and it is required to be
1856 * set dma alignment (only for eedp protection enable vd).
1858 * @sdev: OS provided scsi device
1862 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1863 bool is_target_prop)
1865 u16 pd_index = 0, ld;
1867 struct megasas_instance *instance;
1868 struct fusion_context *fusion;
1869 struct MR_PRIV_DEVICE *mr_device_priv_data;
1870 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1871 struct MR_LD_RAID *raid;
1872 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1874 instance = megasas_lookup_instance(sdev->host->host_no);
1875 fusion = instance->ctrl_context;
1876 mr_device_priv_data = sdev->hostdata;
1878 if (!fusion || !mr_device_priv_data)
1881 if (MEGASAS_IS_LOGICAL(sdev)) {
1882 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1884 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1885 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1886 if (ld >= instance->fw_supported_vd_count)
1888 raid = MR_LdRaidGet(ld, local_map_ptr);
1890 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1891 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1893 mr_device_priv_data->is_tm_capable =
1894 raid->capability.tmCapable;
1896 if (!raid->flags.isEPD)
1897 sdev->no_write_same = 1;
1899 } else if (instance->use_seqnum_jbod_fp) {
1900 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1902 pd_sync = (void *)fusion->pd_seq_sync
1903 [(instance->pd_seq_map_id - 1) & 1];
1904 mr_device_priv_data->is_tm_capable =
1905 pd_sync->seq[pd_index].capability.tmCapable;
1908 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1910 * If FW provides a target reset timeout value, driver will use
1911 * it. If not set, fallback to default values.
1913 mr_device_priv_data->target_reset_tmo =
1914 min_t(u8, instance->max_reset_tmo,
1915 instance->tgt_prop->reset_tmo);
1916 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1918 mr_device_priv_data->target_reset_tmo =
1919 MEGASAS_DEFAULT_TM_TIMEOUT;
1920 mr_device_priv_data->task_abort_tmo =
1921 MEGASAS_DEFAULT_TM_TIMEOUT;
1926 * megasas_set_nvme_device_properties -
1928 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1929 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1931 * MR firmware provides value in KB. Caller of this function converts
1934 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1935 * MR firmware provides value 128 as (32 * 4K) = 128K.
1937 * @sdev: scsi device
1938 * @max_io_size: maximum io transfer size
1942 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1944 struct megasas_instance *instance;
1945 u32 mr_nvme_pg_size;
1947 instance = (struct megasas_instance *)sdev->host->hostdata;
1948 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1949 MR_DEFAULT_NVME_PAGE_SIZE);
1951 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1953 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1954 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1958 * megasas_set_fw_assisted_qd -
1959 * set device queue depth to can_queue
1960 * set device queue depth to fw assisted qd
1962 * @sdev: scsi device
1963 * @is_target_prop true, if fw provided target properties.
1965 static void megasas_set_fw_assisted_qd(struct scsi_device *sdev,
1966 bool is_target_prop)
1969 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1971 struct megasas_instance *instance;
1972 struct MR_PRIV_DEVICE *mr_device_priv_data;
1974 instance = megasas_lookup_instance(sdev->host->host_no);
1975 mr_device_priv_data = sdev->hostdata;
1976 interface_type = mr_device_priv_data->interface_type;
1978 switch (interface_type) {
1980 device_qd = MEGASAS_SAS_QD;
1983 device_qd = MEGASAS_SATA_QD;
1986 device_qd = MEGASAS_NVME_QD;
1990 if (is_target_prop) {
1991 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1993 device_qd = min(instance->host->can_queue,
1994 (int)tgt_device_qd);
1997 if (instance->enable_sdev_max_qd && interface_type != UNKNOWN_DRIVE)
1998 device_qd = instance->host->can_queue;
2000 scsi_change_queue_depth(sdev, device_qd);
2004 * megasas_set_static_target_properties -
2005 * Device property set by driver are static and it is not required to be
2006 * updated after OCR.
2009 * set device queue depth
2010 * set nvme device properties. see - megasas_set_nvme_device_properties
2012 * @sdev: scsi device
2013 * @is_target_prop true, if fw provided target properties.
2015 static void megasas_set_static_target_properties(struct scsi_device *sdev,
2016 bool is_target_prop)
2018 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
2019 struct megasas_instance *instance;
2021 instance = megasas_lookup_instance(sdev->host->host_no);
2024 * The RAID firmware may require extended timeouts.
2026 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
2028 /* max_io_size_kb will be set to non zero for
2029 * nvme based vd and syspd.
2032 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
2034 if (instance->nvme_page_size && max_io_size_kb)
2035 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2037 megasas_set_fw_assisted_qd(sdev, is_target_prop);
2041 static int megasas_slave_configure(struct scsi_device *sdev)
2044 struct megasas_instance *instance;
2045 int ret_target_prop = DCMD_FAILED;
2046 bool is_target_prop = false;
2048 instance = megasas_lookup_instance(sdev->host->host_no);
2049 if (instance->pd_list_not_supported) {
2050 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2051 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2053 if (instance->pd_list[pd_index].driveState !=
2059 mutex_lock(&instance->reset_mutex);
2060 /* Send DCMD to Firmware and cache the information */
2061 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2062 megasas_get_pd_info(instance, sdev);
2064 /* Some ventura firmware may not have instance->nvme_page_size set.
2065 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2067 if ((instance->tgt_prop) && (instance->nvme_page_size))
2068 ret_target_prop = megasas_get_target_prop(instance, sdev);
2070 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2071 megasas_set_static_target_properties(sdev, is_target_prop);
2073 /* This sdev property may change post OCR */
2074 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2076 mutex_unlock(&instance->reset_mutex);
2081 static int megasas_slave_alloc(struct scsi_device *sdev)
2084 struct megasas_instance *instance ;
2085 struct MR_PRIV_DEVICE *mr_device_priv_data;
2087 instance = megasas_lookup_instance(sdev->host->host_no);
2088 if (!MEGASAS_IS_LOGICAL(sdev)) {
2090 * Open the OS scan to the SYSTEM PD
2093 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2095 if ((instance->pd_list_not_supported ||
2096 instance->pd_list[pd_index].driveState ==
2097 MR_PD_STATE_SYSTEM)) {
2104 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2106 if (!mr_device_priv_data)
2108 sdev->hostdata = mr_device_priv_data;
2110 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2111 instance->r1_ldio_hint_default);
2115 static void megasas_slave_destroy(struct scsi_device *sdev)
2117 kfree(sdev->hostdata);
2118 sdev->hostdata = NULL;
2122 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2124 * @instance: Adapter soft state
2127 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2130 struct megasas_cmd *cmd_mfi;
2131 struct megasas_cmd_fusion *cmd_fusion;
2132 struct fusion_context *fusion = instance->ctrl_context;
2134 /* Find all outstanding ioctls */
2136 for (i = 0; i < instance->max_fw_cmds; i++) {
2137 cmd_fusion = fusion->cmd_list[i];
2138 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2139 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2140 if (cmd_mfi->sync_cmd &&
2141 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2142 cmd_mfi->frame->hdr.cmd_status =
2143 MFI_STAT_WRONG_STATE;
2144 megasas_complete_cmd(instance,
2150 for (i = 0; i < instance->max_fw_cmds; i++) {
2151 cmd_mfi = instance->cmd_list[i];
2152 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2154 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2160 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2162 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2163 dev_warn(&instance->pdev->dev,
2164 "Adapter already dead, skipping kill HBA\n");
2168 /* Set critical error to block I/O & ioctls in case caller didn't */
2169 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2170 /* Wait 1 second to ensure IO or ioctls in build have posted */
2172 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2173 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2174 (instance->adapter_type != MFI_SERIES)) {
2175 if (!instance->requestorId) {
2176 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2178 readl(&instance->reg_set->doorbell);
2180 if (instance->requestorId && instance->peerIsPresent)
2181 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2183 writel(MFI_STOP_ADP,
2184 &instance->reg_set->inbound_doorbell);
2186 /* Complete outstanding ioctls when adapter is killed */
2187 megasas_complete_outstanding_ioctls(instance);
2191 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2192 * restored to max value
2193 * @instance: Adapter soft state
2197 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2199 unsigned long flags;
2201 if (instance->flag & MEGASAS_FW_BUSY
2202 && time_after(jiffies, instance->last_time + 5 * HZ)
2203 && atomic_read(&instance->fw_outstanding) <
2204 instance->throttlequeuedepth + 1) {
2206 spin_lock_irqsave(instance->host->host_lock, flags);
2207 instance->flag &= ~MEGASAS_FW_BUSY;
2209 instance->host->can_queue = instance->cur_can_queue;
2210 spin_unlock_irqrestore(instance->host->host_lock, flags);
2215 * megasas_complete_cmd_dpc - Returns FW's controller structure
2216 * @instance_addr: Address of adapter soft state
2218 * Tasklet to complete cmds
2220 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2225 struct megasas_cmd *cmd;
2226 struct megasas_instance *instance =
2227 (struct megasas_instance *)instance_addr;
2228 unsigned long flags;
2230 /* If we have already declared adapter dead, donot complete cmds */
2231 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2234 spin_lock_irqsave(&instance->completion_lock, flags);
2236 producer = le32_to_cpu(*instance->producer);
2237 consumer = le32_to_cpu(*instance->consumer);
2239 while (consumer != producer) {
2240 context = le32_to_cpu(instance->reply_queue[consumer]);
2241 if (context >= instance->max_fw_cmds) {
2242 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2247 cmd = instance->cmd_list[context];
2249 megasas_complete_cmd(instance, cmd, DID_OK);
2252 if (consumer == (instance->max_fw_cmds + 1)) {
2257 *instance->consumer = cpu_to_le32(producer);
2259 spin_unlock_irqrestore(&instance->completion_lock, flags);
2262 * Check if we can restore can_queue
2264 megasas_check_and_restore_queue_depth(instance);
2267 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2270 * megasas_start_timer - Initializes sriov heartbeat timer object
2271 * @instance: Adapter soft state
2274 void megasas_start_timer(struct megasas_instance *instance)
2276 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2278 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2279 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2284 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2287 process_fw_state_change_wq(struct work_struct *work);
2289 static void megasas_do_ocr(struct megasas_instance *instance)
2291 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2292 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2293 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2294 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2296 instance->instancet->disable_intr(instance);
2297 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2298 instance->issuepend_done = 0;
2300 atomic_set(&instance->fw_outstanding, 0);
2301 megasas_internal_reset_defer_cmds(instance);
2302 process_fw_state_change_wq(&instance->work_init);
2305 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2308 struct megasas_cmd *cmd;
2309 struct megasas_dcmd_frame *dcmd;
2310 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2311 dma_addr_t new_affiliation_111_h;
2315 cmd = megasas_get_cmd(instance);
2318 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2319 "Failed to get cmd for scsi%d\n",
2320 instance->host->host_no);
2324 dcmd = &cmd->frame->dcmd;
2326 if (!instance->vf_affiliation_111) {
2327 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2328 "affiliation for scsi%d\n", instance->host->host_no);
2329 megasas_return_cmd(instance, cmd);
2334 memset(instance->vf_affiliation_111, 0,
2335 sizeof(struct MR_LD_VF_AFFILIATION_111));
2337 new_affiliation_111 =
2338 dma_alloc_coherent(&instance->pdev->dev,
2339 sizeof(struct MR_LD_VF_AFFILIATION_111),
2340 &new_affiliation_111_h, GFP_KERNEL);
2341 if (!new_affiliation_111) {
2342 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2343 "memory for new affiliation for scsi%d\n",
2344 instance->host->host_no);
2345 megasas_return_cmd(instance, cmd);
2350 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2352 dcmd->cmd = MFI_CMD_DCMD;
2353 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2354 dcmd->sge_count = 1;
2355 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2358 dcmd->data_xfer_len =
2359 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2360 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2363 dcmd->sgl.sge32[0].phys_addr =
2364 cpu_to_le32(instance->vf_affiliation_111_h);
2366 dcmd->sgl.sge32[0].phys_addr =
2367 cpu_to_le32(new_affiliation_111_h);
2369 dcmd->sgl.sge32[0].length = cpu_to_le32(
2370 sizeof(struct MR_LD_VF_AFFILIATION_111));
2372 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2373 "scsi%d\n", instance->host->host_no);
2375 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2376 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2377 " failed with status 0x%x for scsi%d\n",
2378 dcmd->cmd_status, instance->host->host_no);
2379 retval = 1; /* Do a scan if we couldn't get affiliation */
2384 thisVf = new_affiliation_111->thisVf;
2385 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2386 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2387 new_affiliation_111->map[ld].policy[thisVf]) {
2388 dev_warn(&instance->pdev->dev, "SR-IOV: "
2389 "Got new LD/VF affiliation for scsi%d\n",
2390 instance->host->host_no);
2391 memcpy(instance->vf_affiliation_111,
2392 new_affiliation_111,
2393 sizeof(struct MR_LD_VF_AFFILIATION_111));
2399 if (new_affiliation_111) {
2400 dma_free_coherent(&instance->pdev->dev,
2401 sizeof(struct MR_LD_VF_AFFILIATION_111),
2402 new_affiliation_111,
2403 new_affiliation_111_h);
2406 megasas_return_cmd(instance, cmd);
2411 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2414 struct megasas_cmd *cmd;
2415 struct megasas_dcmd_frame *dcmd;
2416 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2417 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2418 dma_addr_t new_affiliation_h;
2419 int i, j, retval = 0, found = 0, doscan = 0;
2422 cmd = megasas_get_cmd(instance);
2425 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2426 "Failed to get cmd for scsi%d\n",
2427 instance->host->host_no);
2431 dcmd = &cmd->frame->dcmd;
2433 if (!instance->vf_affiliation) {
2434 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2435 "affiliation for scsi%d\n", instance->host->host_no);
2436 megasas_return_cmd(instance, cmd);
2441 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2442 sizeof(struct MR_LD_VF_AFFILIATION));
2445 dma_alloc_coherent(&instance->pdev->dev,
2446 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2447 &new_affiliation_h, GFP_KERNEL);
2448 if (!new_affiliation) {
2449 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2450 "memory for new affiliation for scsi%d\n",
2451 instance->host->host_no);
2452 megasas_return_cmd(instance, cmd);
2457 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2459 dcmd->cmd = MFI_CMD_DCMD;
2460 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2461 dcmd->sge_count = 1;
2462 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2465 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2466 sizeof(struct MR_LD_VF_AFFILIATION));
2467 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2470 dcmd->sgl.sge32[0].phys_addr =
2471 cpu_to_le32(instance->vf_affiliation_h);
2473 dcmd->sgl.sge32[0].phys_addr =
2474 cpu_to_le32(new_affiliation_h);
2476 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2477 sizeof(struct MR_LD_VF_AFFILIATION));
2479 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2480 "scsi%d\n", instance->host->host_no);
2483 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2484 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2485 " failed with status 0x%x for scsi%d\n",
2486 dcmd->cmd_status, instance->host->host_no);
2487 retval = 1; /* Do a scan if we couldn't get affiliation */
2492 if (!new_affiliation->ldCount) {
2493 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2494 "affiliation for passive path for scsi%d\n",
2495 instance->host->host_no);
2499 newmap = new_affiliation->map;
2500 savedmap = instance->vf_affiliation->map;
2501 thisVf = new_affiliation->thisVf;
2502 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2504 for (j = 0; j < instance->vf_affiliation->ldCount;
2506 if (newmap->ref.targetId ==
2507 savedmap->ref.targetId) {
2509 if (newmap->policy[thisVf] !=
2510 savedmap->policy[thisVf]) {
2515 savedmap = (struct MR_LD_VF_MAP *)
2516 ((unsigned char *)savedmap +
2519 if (!found && newmap->policy[thisVf] !=
2520 MR_LD_ACCESS_HIDDEN) {
2524 newmap = (struct MR_LD_VF_MAP *)
2525 ((unsigned char *)newmap + newmap->size);
2528 newmap = new_affiliation->map;
2529 savedmap = instance->vf_affiliation->map;
2531 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2533 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2534 if (savedmap->ref.targetId ==
2535 newmap->ref.targetId) {
2537 if (savedmap->policy[thisVf] !=
2538 newmap->policy[thisVf]) {
2543 newmap = (struct MR_LD_VF_MAP *)
2544 ((unsigned char *)newmap +
2547 if (!found && savedmap->policy[thisVf] !=
2548 MR_LD_ACCESS_HIDDEN) {
2552 savedmap = (struct MR_LD_VF_MAP *)
2553 ((unsigned char *)savedmap +
2559 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2560 "affiliation for scsi%d\n", instance->host->host_no);
2561 memcpy(instance->vf_affiliation, new_affiliation,
2562 new_affiliation->size);
2566 if (new_affiliation)
2567 dma_free_coherent(&instance->pdev->dev,
2568 (MAX_LOGICAL_DRIVES + 1) *
2569 sizeof(struct MR_LD_VF_AFFILIATION),
2570 new_affiliation, new_affiliation_h);
2571 megasas_return_cmd(instance, cmd);
2576 /* This function will get the current SR-IOV LD/VF affiliation */
2577 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2582 if (instance->PlasmaFW111)
2583 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2585 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2589 /* This function will tell FW to start the SR-IOV heartbeat */
2590 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2593 struct megasas_cmd *cmd;
2594 struct megasas_dcmd_frame *dcmd;
2597 cmd = megasas_get_cmd(instance);
2600 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2601 "Failed to get cmd for scsi%d\n",
2602 instance->host->host_no);
2606 dcmd = &cmd->frame->dcmd;
2609 instance->hb_host_mem =
2610 dma_alloc_coherent(&instance->pdev->dev,
2611 sizeof(struct MR_CTRL_HB_HOST_MEM),
2612 &instance->hb_host_mem_h,
2614 if (!instance->hb_host_mem) {
2615 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2616 " memory for heartbeat host memory for scsi%d\n",
2617 instance->host->host_no);
2623 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2625 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2626 dcmd->cmd = MFI_CMD_DCMD;
2627 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2628 dcmd->sge_count = 1;
2629 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2632 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2633 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2635 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2636 sizeof(struct MR_CTRL_HB_HOST_MEM));
2638 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2639 instance->host->host_no);
2641 if ((instance->adapter_type != MFI_SERIES) &&
2642 !instance->mask_interrupts)
2643 retval = megasas_issue_blocked_cmd(instance, cmd,
2644 MEGASAS_ROUTINE_WAIT_TIME_VF);
2646 retval = megasas_issue_polled(instance, cmd);
2649 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2650 "_MEM_ALLOC DCMD %s for scsi%d\n",
2651 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2652 "timed out" : "failed", instance->host->host_no);
2657 megasas_return_cmd(instance, cmd);
2662 /* Handler for SR-IOV heartbeat */
2663 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2665 struct megasas_instance *instance =
2666 from_timer(instance, t, sriov_heartbeat_timer);
2668 if (instance->hb_host_mem->HB.fwCounter !=
2669 instance->hb_host_mem->HB.driverCounter) {
2670 instance->hb_host_mem->HB.driverCounter =
2671 instance->hb_host_mem->HB.fwCounter;
2672 mod_timer(&instance->sriov_heartbeat_timer,
2673 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2675 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2676 "completed for scsi%d\n", instance->host->host_no);
2677 schedule_work(&instance->work_init);
2682 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2683 * @instance: Adapter soft state
2685 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2686 * complete all its outstanding commands. Returns error if one or more IOs
2687 * are pending after this time period. It also marks the controller dead.
2689 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2691 int i, sl, outstanding;
2693 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2694 unsigned long flags;
2695 struct list_head clist_local;
2696 struct megasas_cmd *reset_cmd;
2699 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2700 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2701 __func__, __LINE__);
2705 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2707 INIT_LIST_HEAD(&clist_local);
2708 spin_lock_irqsave(&instance->hba_lock, flags);
2709 list_splice_init(&instance->internal_reset_pending_q,
2711 spin_unlock_irqrestore(&instance->hba_lock, flags);
2713 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2714 for (i = 0; i < wait_time; i++) {
2716 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2720 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2721 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2722 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2727 while (!list_empty(&clist_local)) {
2728 reset_cmd = list_entry((&clist_local)->next,
2729 struct megasas_cmd, list);
2730 list_del_init(&reset_cmd->list);
2731 if (reset_cmd->scmd) {
2732 reset_cmd->scmd->result = DID_REQUEUE << 16;
2733 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2734 reset_index, reset_cmd,
2735 reset_cmd->scmd->cmnd[0]);
2737 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2738 megasas_return_cmd(instance, reset_cmd);
2739 } else if (reset_cmd->sync_cmd) {
2740 dev_notice(&instance->pdev->dev, "%p synch cmds"
2744 reset_cmd->cmd_status_drv = DCMD_INIT;
2745 instance->instancet->fire_cmd(instance,
2746 reset_cmd->frame_phys_addr,
2747 0, instance->reg_set);
2749 dev_notice(&instance->pdev->dev, "%p unexpected"
2759 for (i = 0; i < resetwaittime; i++) {
2760 outstanding = atomic_read(&instance->fw_outstanding);
2765 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2766 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2767 "commands to complete\n",i,outstanding);
2769 * Call cmd completion routine. Cmd to be
2770 * be completed directly without depending on isr.
2772 megasas_complete_cmd_dpc((unsigned long)instance);
2779 outstanding = atomic_read(&instance->fw_outstanding);
2780 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2782 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2783 goto no_outstanding;
2785 if (instance->disableOnlineCtrlReset)
2786 goto kill_hba_and_failed;
2788 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2789 dev_info(&instance->pdev->dev,
2790 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2791 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2793 goto kill_hba_and_failed;
2794 megasas_do_ocr(instance);
2796 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2797 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2798 __func__, __LINE__);
2801 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2802 __func__, __LINE__);
2804 for (sl = 0; sl < 10; sl++)
2807 outstanding = atomic_read(&instance->fw_outstanding);
2809 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2810 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2811 goto no_outstanding;
2818 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2819 __func__, __LINE__);
2822 kill_hba_and_failed:
2824 /* Reset not supported, kill adapter */
2825 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2826 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2827 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2828 atomic_read(&instance->fw_outstanding));
2829 megasas_dump_pending_frames(instance);
2830 megaraid_sas_kill_hba(instance);
2836 * megasas_generic_reset - Generic reset routine
2837 * @scmd: Mid-layer SCSI command
2839 * This routine implements a generic reset handler for device, bus and host
2840 * reset requests. Device, bus and host specific reset handlers can use this
2841 * function after they do their specific tasks.
2843 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2846 struct megasas_instance *instance;
2848 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2850 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2851 scmd->cmnd[0], scmd->retries);
2853 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2854 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2858 ret_val = megasas_wait_for_outstanding(instance);
2859 if (ret_val == SUCCESS)
2860 dev_notice(&instance->pdev->dev, "reset successful\n");
2862 dev_err(&instance->pdev->dev, "failed to do reset\n");
2868 * megasas_reset_timer - quiesce the adapter if required
2871 * Sets the FW busy flag and reduces the host->can_queue if the
2872 * cmd has not been completed within the timeout period.
2875 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2877 struct megasas_instance *instance;
2878 unsigned long flags;
2880 if (time_after(jiffies, scmd->jiffies_at_alloc +
2881 (scmd_timeout * 2) * HZ)) {
2885 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2886 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2887 /* FW is busy, throttle IO */
2888 spin_lock_irqsave(instance->host->host_lock, flags);
2890 instance->host->can_queue = instance->throttlequeuedepth;
2891 instance->last_time = jiffies;
2892 instance->flag |= MEGASAS_FW_BUSY;
2894 spin_unlock_irqrestore(instance->host->host_lock, flags);
2896 return BLK_EH_RESET_TIMER;
2900 * megasas_dump - This function will print hexdump of provided buffer.
2901 * @buf: Buffer to be dumped
2902 * @sz: Size in bytes
2903 * @format: Different formats of dumping e.g. format=n will
2904 * cause only 'n' 32 bit words to be dumped in a single
2908 megasas_dump(void *buf, int sz, int format)
2911 __le32 *buf_loc = (__le32 *)buf;
2913 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2914 if ((i % format) == 0) {
2916 printk(KERN_CONT "\n");
2917 printk(KERN_CONT "%08x: ", (i * 4));
2919 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2921 printk(KERN_CONT "\n");
2925 * megasas_dump_reg_set - This function will print hexdump of register set
2926 * @reg_set: Register set to be dumped
2929 megasas_dump_reg_set(void __iomem *reg_set)
2931 unsigned int i, sz = 256;
2932 u32 __iomem *reg = (u32 __iomem *)reg_set;
2934 for (i = 0; i < (sz / sizeof(u32)); i++)
2935 printk("%08x: %08x\n", (i * 4), readl(®[i]));
2939 * megasas_dump_fusion_io - This function will print key details
2941 * @scmd: SCSI command pointer of SCSI IO
2944 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2946 struct megasas_cmd_fusion *cmd;
2947 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2948 struct megasas_instance *instance;
2950 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2951 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2953 scmd_printk(KERN_INFO, scmd,
2954 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
2955 scmd, scmd->retries, scmd->allowed);
2956 scsi_print_command(scmd);
2959 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2960 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2961 scmd_printk(KERN_INFO, scmd,
2962 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
2963 req_desc->SCSIIO.RequestFlags,
2964 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2965 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2967 printk(KERN_INFO "IO request frame:\n");
2968 megasas_dump(cmd->io_request,
2969 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
2970 printk(KERN_INFO "Chain frame:\n");
2971 megasas_dump(cmd->sg_frame,
2972 instance->max_chain_frame_sz, 8);
2978 * megasas_dump_sys_regs - This function will dump system registers through
2980 * @reg_set: Pointer to System register set.
2981 * @buf: Buffer to which output is to be written.
2982 * @return: Number of bytes written to buffer.
2984 static inline ssize_t
2985 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2987 unsigned int i, sz = 256;
2988 int bytes_wrote = 0;
2989 char *loc = (char *)buf;
2990 u32 __iomem *reg = (u32 __iomem *)reg_set;
2992 for (i = 0; i < sz / sizeof(u32); i++) {
2993 bytes_wrote += scnprintf(loc + bytes_wrote,
2994 PAGE_SIZE - bytes_wrote,
2995 "%08x: %08x\n", (i * 4),
3002 * megasas_reset_bus_host - Bus & host reset handler entry point
3003 * @scmd: Mid-layer SCSI command
3005 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
3008 struct megasas_instance *instance;
3010 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3012 scmd_printk(KERN_INFO, scmd,
3013 "OCR is requested due to IO timeout!!\n");
3015 scmd_printk(KERN_INFO, scmd,
3016 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
3017 scmd->device->host->shost_state,
3018 scsi_host_busy(scmd->device->host),
3019 atomic_read(&instance->fw_outstanding));
3021 * First wait for all commands to complete
3023 if (instance->adapter_type == MFI_SERIES) {
3024 ret = megasas_generic_reset(scmd);
3026 megasas_dump_fusion_io(scmd);
3027 ret = megasas_reset_fusion(scmd->device->host,
3028 SCSIIO_TIMEOUT_OCR);
3035 * megasas_task_abort - Issues task abort request to firmware
3036 * (supported only for fusion adapters)
3037 * @scmd: SCSI command pointer
3039 static int megasas_task_abort(struct scsi_cmnd *scmd)
3042 struct megasas_instance *instance;
3044 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3046 if (instance->adapter_type != MFI_SERIES)
3047 ret = megasas_task_abort_fusion(scmd);
3049 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3057 * megasas_reset_target: Issues target reset request to firmware
3058 * (supported only for fusion adapters)
3059 * @scmd: SCSI command pointer
3061 static int megasas_reset_target(struct scsi_cmnd *scmd)
3064 struct megasas_instance *instance;
3066 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3068 if (instance->adapter_type != MFI_SERIES)
3069 ret = megasas_reset_target_fusion(scmd);
3071 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3079 * megasas_bios_param - Returns disk geometry for a disk
3080 * @sdev: device handle
3081 * @bdev: block device
3082 * @capacity: drive capacity
3083 * @geom: geometry parameters
3086 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3087 sector_t capacity, int geom[])
3094 /* Default heads (64) & sectors (32) */
3098 tmp = heads * sectors;
3099 cylinders = capacity;
3101 sector_div(cylinders, tmp);
3104 * Handle extended translation size for logical drives > 1Gb
3107 if (capacity >= 0x200000) {
3110 tmp = heads*sectors;
3111 cylinders = capacity;
3112 sector_div(cylinders, tmp);
3117 geom[2] = cylinders;
3122 static void megasas_aen_polling(struct work_struct *work);
3125 * megasas_service_aen - Processes an event notification
3126 * @instance: Adapter soft state
3127 * @cmd: AEN command completed by the ISR
3129 * For AEN, driver sends a command down to FW that is held by the FW till an
3130 * event occurs. When an event of interest occurs, FW completes the command
3131 * that it was previously holding.
3133 * This routines sends SIGIO signal to processes that have registered with the
3137 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3139 unsigned long flags;
3142 * Don't signal app if it is just an aborted previously registered aen
3144 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3145 spin_lock_irqsave(&poll_aen_lock, flags);
3146 megasas_poll_wait_aen = 1;
3147 spin_unlock_irqrestore(&poll_aen_lock, flags);
3148 wake_up(&megasas_poll_wait);
3149 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3154 instance->aen_cmd = NULL;
3156 megasas_return_cmd(instance, cmd);
3158 if ((instance->unload == 0) &&
3159 ((instance->issuepend_done == 1))) {
3160 struct megasas_aen_event *ev;
3162 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3164 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3166 ev->instance = instance;
3168 INIT_DELAYED_WORK(&ev->hotplug_work,
3169 megasas_aen_polling);
3170 schedule_delayed_work(&ev->hotplug_work, 0);
3176 fw_crash_buffer_store(struct device *cdev,
3177 struct device_attribute *attr, const char *buf, size_t count)
3179 struct Scsi_Host *shost = class_to_shost(cdev);
3180 struct megasas_instance *instance =
3181 (struct megasas_instance *) shost->hostdata;
3183 unsigned long flags;
3185 if (kstrtoint(buf, 0, &val) != 0)
3188 spin_lock_irqsave(&instance->crashdump_lock, flags);
3189 instance->fw_crash_buffer_offset = val;
3190 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3195 fw_crash_buffer_show(struct device *cdev,
3196 struct device_attribute *attr, char *buf)
3198 struct Scsi_Host *shost = class_to_shost(cdev);
3199 struct megasas_instance *instance =
3200 (struct megasas_instance *) shost->hostdata;
3202 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3203 unsigned long chunk_left_bytes;
3204 unsigned long src_addr;
3205 unsigned long flags;
3208 spin_lock_irqsave(&instance->crashdump_lock, flags);
3209 buff_offset = instance->fw_crash_buffer_offset;
3210 if (!instance->crash_dump_buf &&
3211 !((instance->fw_crash_state == AVAILABLE) ||
3212 (instance->fw_crash_state == COPYING))) {
3213 dev_err(&instance->pdev->dev,
3214 "Firmware crash dump is not available\n");
3215 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3219 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3220 dev_err(&instance->pdev->dev,
3221 "Firmware crash dump offset is out of range\n");
3222 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3226 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3227 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3228 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3229 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3231 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3232 (buff_offset % dmachunk);
3233 memcpy(buf, (void *)src_addr, size);
3234 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3240 fw_crash_buffer_size_show(struct device *cdev,
3241 struct device_attribute *attr, char *buf)
3243 struct Scsi_Host *shost = class_to_shost(cdev);
3244 struct megasas_instance *instance =
3245 (struct megasas_instance *) shost->hostdata;
3247 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3248 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3252 fw_crash_state_store(struct device *cdev,
3253 struct device_attribute *attr, const char *buf, size_t count)
3255 struct Scsi_Host *shost = class_to_shost(cdev);
3256 struct megasas_instance *instance =
3257 (struct megasas_instance *) shost->hostdata;
3259 unsigned long flags;
3261 if (kstrtoint(buf, 0, &val) != 0)
3264 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3265 dev_err(&instance->pdev->dev, "application updates invalid "
3266 "firmware crash state\n");
3270 instance->fw_crash_state = val;
3272 if ((val == COPIED) || (val == COPY_ERROR)) {
3273 spin_lock_irqsave(&instance->crashdump_lock, flags);
3274 megasas_free_host_crash_buffer(instance);
3275 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3276 if (val == COPY_ERROR)
3277 dev_info(&instance->pdev->dev, "application failed to "
3278 "copy Firmware crash dump\n");
3280 dev_info(&instance->pdev->dev, "Firmware crash dump "
3281 "copied successfully\n");
3287 fw_crash_state_show(struct device *cdev,
3288 struct device_attribute *attr, char *buf)
3290 struct Scsi_Host *shost = class_to_shost(cdev);
3291 struct megasas_instance *instance =
3292 (struct megasas_instance *) shost->hostdata;
3294 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3298 page_size_show(struct device *cdev,
3299 struct device_attribute *attr, char *buf)
3301 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3305 ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3308 struct Scsi_Host *shost = class_to_shost(cdev);
3309 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3311 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3315 fw_cmds_outstanding_show(struct device *cdev,
3316 struct device_attribute *attr, char *buf)
3318 struct Scsi_Host *shost = class_to_shost(cdev);
3319 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3321 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3325 enable_sdev_max_qd_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 = (struct megasas_instance *)shost->hostdata;
3331 return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3335 enable_sdev_max_qd_store(struct device *cdev,
3336 struct device_attribute *attr, const char *buf, size_t count)
3338 struct Scsi_Host *shost = class_to_shost(cdev);
3339 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3341 bool is_target_prop;
3342 int ret_target_prop = DCMD_FAILED;
3343 struct scsi_device *sdev;
3345 if (kstrtou32(buf, 0, &val) != 0) {
3346 pr_err("megasas: could not set enable_sdev_max_qd\n");
3350 mutex_lock(&instance->reset_mutex);
3352 instance->enable_sdev_max_qd = true;
3354 instance->enable_sdev_max_qd = false;
3356 shost_for_each_device(sdev, shost) {
3357 ret_target_prop = megasas_get_target_prop(instance, sdev);
3358 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3359 megasas_set_fw_assisted_qd(sdev, is_target_prop);
3361 mutex_unlock(&instance->reset_mutex);
3367 dump_system_regs_show(struct device *cdev,
3368 struct device_attribute *attr, char *buf)
3370 struct Scsi_Host *shost = class_to_shost(cdev);
3371 struct megasas_instance *instance =
3372 (struct megasas_instance *)shost->hostdata;
3374 return megasas_dump_sys_regs(instance->reg_set, buf);
3378 raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3381 struct Scsi_Host *shost = class_to_shost(cdev);
3382 struct megasas_instance *instance =
3383 (struct megasas_instance *)shost->hostdata;
3385 return snprintf(buf, PAGE_SIZE, "%ld\n",
3386 (unsigned long)instance->map_id);
3389 static DEVICE_ATTR_RW(fw_crash_buffer);
3390 static DEVICE_ATTR_RO(fw_crash_buffer_size);
3391 static DEVICE_ATTR_RW(fw_crash_state);
3392 static DEVICE_ATTR_RO(page_size);
3393 static DEVICE_ATTR_RO(ldio_outstanding);
3394 static DEVICE_ATTR_RO(fw_cmds_outstanding);
3395 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3396 static DEVICE_ATTR_RO(dump_system_regs);
3397 static DEVICE_ATTR_RO(raid_map_id);
3399 static struct device_attribute *megaraid_host_attrs[] = {
3400 &dev_attr_fw_crash_buffer_size,
3401 &dev_attr_fw_crash_buffer,
3402 &dev_attr_fw_crash_state,
3403 &dev_attr_page_size,
3404 &dev_attr_ldio_outstanding,
3405 &dev_attr_fw_cmds_outstanding,
3406 &dev_attr_enable_sdev_max_qd,
3407 &dev_attr_dump_system_regs,
3408 &dev_attr_raid_map_id,
3413 * Scsi host template for megaraid_sas driver
3415 static struct scsi_host_template megasas_template = {
3417 .module = THIS_MODULE,
3418 .name = "Avago SAS based MegaRAID driver",
3419 .proc_name = "megaraid_sas",
3420 .slave_configure = megasas_slave_configure,
3421 .slave_alloc = megasas_slave_alloc,
3422 .slave_destroy = megasas_slave_destroy,
3423 .queuecommand = megasas_queue_command,
3424 .eh_target_reset_handler = megasas_reset_target,
3425 .eh_abort_handler = megasas_task_abort,
3426 .eh_host_reset_handler = megasas_reset_bus_host,
3427 .eh_timed_out = megasas_reset_timer,
3428 .shost_attrs = megaraid_host_attrs,
3429 .bios_param = megasas_bios_param,
3430 .change_queue_depth = scsi_change_queue_depth,
3431 .max_segment_size = 0xffffffff,
3435 * megasas_complete_int_cmd - Completes an internal command
3436 * @instance: Adapter soft state
3437 * @cmd: Command to be completed
3439 * The megasas_issue_blocked_cmd() function waits for a command to complete
3440 * after it issues a command. This function wakes up that waiting routine by
3441 * calling wake_up() on the wait queue.
3444 megasas_complete_int_cmd(struct megasas_instance *instance,
3445 struct megasas_cmd *cmd)
3447 if (cmd->cmd_status_drv == DCMD_INIT)
3448 cmd->cmd_status_drv =
3449 (cmd->frame->io.cmd_status == MFI_STAT_OK) ?
3450 DCMD_SUCCESS : DCMD_FAILED;
3452 wake_up(&instance->int_cmd_wait_q);
3456 * megasas_complete_abort - Completes aborting a command
3457 * @instance: Adapter soft state
3458 * @cmd: Cmd that was issued to abort another cmd
3460 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3461 * after it issues an abort on a previously issued command. This function
3462 * wakes up all functions waiting on the same wait queue.
3465 megasas_complete_abort(struct megasas_instance *instance,
3466 struct megasas_cmd *cmd)
3468 if (cmd->sync_cmd) {
3470 cmd->cmd_status_drv = DCMD_SUCCESS;
3471 wake_up(&instance->abort_cmd_wait_q);
3476 * megasas_complete_cmd - Completes a command
3477 * @instance: Adapter soft state
3478 * @cmd: Command to be completed
3479 * @alt_status: If non-zero, use this value as status to
3480 * SCSI mid-layer instead of the value returned
3481 * by the FW. This should be used if caller wants
3482 * an alternate status (as in the case of aborted
3486 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3490 struct megasas_header *hdr = &cmd->frame->hdr;
3491 unsigned long flags;
3492 struct fusion_context *fusion = instance->ctrl_context;
3495 /* flag for the retry reset */
3496 cmd->retry_for_fw_reset = 0;
3499 cmd->scmd->SCp.ptr = NULL;
3502 case MFI_CMD_INVALID:
3503 /* Some older 1068 controller FW may keep a pended
3504 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3505 when booting the kdump kernel. Ignore this command to
3506 prevent a kernel panic on shutdown of the kdump kernel. */
3507 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3509 dev_warn(&instance->pdev->dev, "If you have a controller "
3510 "other than PERC5, please upgrade your firmware\n");
3512 case MFI_CMD_PD_SCSI_IO:
3513 case MFI_CMD_LD_SCSI_IO:
3516 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3517 * issued either through an IO path or an IOCTL path. If it
3518 * was via IOCTL, we will send it to internal completion.
3520 if (cmd->sync_cmd) {
3522 megasas_complete_int_cmd(instance, cmd);
3527 case MFI_CMD_LD_READ:
3528 case MFI_CMD_LD_WRITE:
3531 cmd->scmd->result = alt_status << 16;
3537 atomic_dec(&instance->fw_outstanding);
3539 scsi_dma_unmap(cmd->scmd);
3540 cmd->scmd->scsi_done(cmd->scmd);
3541 megasas_return_cmd(instance, cmd);
3546 switch (hdr->cmd_status) {
3549 cmd->scmd->result = DID_OK << 16;
3552 case MFI_STAT_SCSI_IO_FAILED:
3553 case MFI_STAT_LD_INIT_IN_PROGRESS:
3555 (DID_ERROR << 16) | hdr->scsi_status;
3558 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3560 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3562 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3563 memset(cmd->scmd->sense_buffer, 0,
3564 SCSI_SENSE_BUFFERSIZE);
3565 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3568 cmd->scmd->result |= DRIVER_SENSE << 24;
3573 case MFI_STAT_LD_OFFLINE:
3574 case MFI_STAT_DEVICE_NOT_FOUND:
3575 cmd->scmd->result = DID_BAD_TARGET << 16;
3579 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3581 cmd->scmd->result = DID_ERROR << 16;
3585 atomic_dec(&instance->fw_outstanding);
3587 scsi_dma_unmap(cmd->scmd);
3588 cmd->scmd->scsi_done(cmd->scmd);
3589 megasas_return_cmd(instance, cmd);
3596 case MFI_CMD_TOOLBOX:
3597 megasas_complete_int_cmd(instance, cmd);
3601 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3602 /* Check for LD map update */
3603 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3604 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3605 fusion->fast_path_io = 0;
3606 spin_lock_irqsave(instance->host->host_lock, flags);
3607 status = cmd->frame->hdr.cmd_status;
3608 instance->map_update_cmd = NULL;
3609 if (status != MFI_STAT_OK) {
3610 if (status != MFI_STAT_NOT_FOUND)
3611 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3612 cmd->frame->hdr.cmd_status);
3614 megasas_return_cmd(instance, cmd);
3615 spin_unlock_irqrestore(
3616 instance->host->host_lock,
3622 megasas_return_cmd(instance, cmd);
3625 * Set fast path IO to ZERO.
3626 * Validate Map will set proper value.
3627 * Meanwhile all IOs will go as LD IO.
3629 if (status == MFI_STAT_OK &&
3630 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3632 fusion->fast_path_io = 1;
3634 fusion->fast_path_io = 0;
3637 megasas_sync_map_info(instance);
3638 spin_unlock_irqrestore(instance->host->host_lock,
3642 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3643 opcode == MR_DCMD_CTRL_EVENT_GET) {
3644 spin_lock_irqsave(&poll_aen_lock, flags);
3645 megasas_poll_wait_aen = 0;
3646 spin_unlock_irqrestore(&poll_aen_lock, flags);
3649 /* FW has an updated PD sequence */
3650 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3651 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3653 spin_lock_irqsave(instance->host->host_lock, flags);
3654 status = cmd->frame->hdr.cmd_status;
3655 instance->jbod_seq_cmd = NULL;
3656 megasas_return_cmd(instance, cmd);
3658 if (status == MFI_STAT_OK) {
3659 instance->pd_seq_map_id++;
3660 /* Re-register a pd sync seq num cmd */
3661 if (megasas_sync_pd_seq_num(instance, true))
3662 instance->use_seqnum_jbod_fp = false;
3664 instance->use_seqnum_jbod_fp = false;
3666 spin_unlock_irqrestore(instance->host->host_lock, flags);
3671 * See if got an event notification
3673 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3674 megasas_service_aen(instance, cmd);
3676 megasas_complete_int_cmd(instance, cmd);
3682 * Cmd issued to abort another cmd returned
3684 megasas_complete_abort(instance, cmd);
3688 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3690 megasas_complete_int_cmd(instance, cmd);
3696 * megasas_issue_pending_cmds_again - issue all pending cmds
3697 * in FW again because of the fw reset
3698 * @instance: Adapter soft state
3701 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3703 struct megasas_cmd *cmd;
3704 struct list_head clist_local;
3705 union megasas_evt_class_locale class_locale;
3706 unsigned long flags;
3709 INIT_LIST_HEAD(&clist_local);
3710 spin_lock_irqsave(&instance->hba_lock, flags);
3711 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3712 spin_unlock_irqrestore(&instance->hba_lock, flags);
3714 while (!list_empty(&clist_local)) {
3715 cmd = list_entry((&clist_local)->next,
3716 struct megasas_cmd, list);
3717 list_del_init(&cmd->list);
3719 if (cmd->sync_cmd || cmd->scmd) {
3720 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3721 "detected to be pending while HBA reset\n",
3722 cmd, cmd->scmd, cmd->sync_cmd);
3724 cmd->retry_for_fw_reset++;
3726 if (cmd->retry_for_fw_reset == 3) {
3727 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3728 "was tried multiple times during reset."
3729 "Shutting down the HBA\n",
3730 cmd, cmd->scmd, cmd->sync_cmd);
3731 instance->instancet->disable_intr(instance);
3732 atomic_set(&instance->fw_reset_no_pci_access, 1);
3733 megaraid_sas_kill_hba(instance);
3738 if (cmd->sync_cmd == 1) {
3740 dev_notice(&instance->pdev->dev, "unexpected"
3741 "cmd attached to internal command!\n");
3743 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3744 "on the internal reset queue,"
3745 "issue it again.\n", cmd);
3746 cmd->cmd_status_drv = DCMD_INIT;
3747 instance->instancet->fire_cmd(instance,
3748 cmd->frame_phys_addr,
3749 0, instance->reg_set);
3750 } else if (cmd->scmd) {
3751 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3752 "detected on the internal queue, issue again.\n",
3753 cmd, cmd->scmd->cmnd[0]);
3755 atomic_inc(&instance->fw_outstanding);
3756 instance->instancet->fire_cmd(instance,
3757 cmd->frame_phys_addr,
3758 cmd->frame_count-1, instance->reg_set);
3760 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3761 "internal reset defer list while re-issue!!\n",
3766 if (instance->aen_cmd) {
3767 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3768 megasas_return_cmd(instance, instance->aen_cmd);
3770 instance->aen_cmd = NULL;
3774 * Initiate AEN (Asynchronous Event Notification)
3776 seq_num = instance->last_seq_num;
3777 class_locale.members.reserved = 0;
3778 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3779 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3781 megasas_register_aen(instance, seq_num, class_locale.word);
3785 * Move the internal reset pending commands to a deferred queue.
3787 * We move the commands pending at internal reset time to a
3788 * pending queue. This queue would be flushed after successful
3789 * completion of the internal reset sequence. if the internal reset
3790 * did not complete in time, the kernel reset handler would flush
3794 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3796 struct megasas_cmd *cmd;
3798 u16 max_cmd = instance->max_fw_cmds;
3800 unsigned long flags;
3803 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3804 for (i = 0; i < max_cmd; i++) {
3805 cmd = instance->cmd_list[i];
3806 if (cmd->sync_cmd == 1 || cmd->scmd) {
3807 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3808 "on the defer queue as internal\n",
3809 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3811 if (!list_empty(&cmd->list)) {
3812 dev_notice(&instance->pdev->dev, "ERROR while"
3813 " moving this cmd:%p, %d %p, it was"
3814 "discovered on some list?\n",
3815 cmd, cmd->sync_cmd, cmd->scmd);
3817 list_del_init(&cmd->list);
3820 list_add_tail(&cmd->list,
3821 &instance->internal_reset_pending_q);
3824 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3829 process_fw_state_change_wq(struct work_struct *work)
3831 struct megasas_instance *instance =
3832 container_of(work, struct megasas_instance, work_init);
3834 unsigned long flags;
3836 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3837 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3838 atomic_read(&instance->adprecovery));
3842 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3843 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3844 "state, restarting it...\n");
3846 instance->instancet->disable_intr(instance);
3847 atomic_set(&instance->fw_outstanding, 0);
3849 atomic_set(&instance->fw_reset_no_pci_access, 1);
3850 instance->instancet->adp_reset(instance, instance->reg_set);
3851 atomic_set(&instance->fw_reset_no_pci_access, 0);
3853 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3854 "initiating next stage...\n");
3856 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3857 "state 2 starting...\n");
3859 /* waiting for about 20 second before start the second init */
3860 for (wait = 0; wait < 30; wait++) {
3864 if (megasas_transition_to_ready(instance, 1)) {
3865 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3867 atomic_set(&instance->fw_reset_no_pci_access, 1);
3868 megaraid_sas_kill_hba(instance);
3872 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3873 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3874 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3876 *instance->consumer = *instance->producer;
3878 *instance->consumer = 0;
3879 *instance->producer = 0;
3882 megasas_issue_init_mfi(instance);
3884 spin_lock_irqsave(&instance->hba_lock, flags);
3885 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3886 spin_unlock_irqrestore(&instance->hba_lock, flags);
3887 instance->instancet->enable_intr(instance);
3889 megasas_issue_pending_cmds_again(instance);
3890 instance->issuepend_done = 1;
3895 * megasas_deplete_reply_queue - Processes all completed commands
3896 * @instance: Adapter soft state
3897 * @alt_status: Alternate status to be returned to
3898 * SCSI mid-layer instead of the status
3899 * returned by the FW
3900 * Note: this must be called with hba lock held
3903 megasas_deplete_reply_queue(struct megasas_instance *instance,
3909 if ((mfiStatus = instance->instancet->check_reset(instance,
3910 instance->reg_set)) == 1) {
3914 mfiStatus = instance->instancet->clear_intr(instance);
3915 if (mfiStatus == 0) {
3916 /* Hardware may not set outbound_intr_status in MSI-X mode */
3917 if (!instance->msix_vectors)
3921 instance->mfiStatus = mfiStatus;
3923 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3924 fw_state = instance->instancet->read_fw_status_reg(
3925 instance) & MFI_STATE_MASK;
3927 if (fw_state != MFI_STATE_FAULT) {
3928 dev_notice(&instance->pdev->dev, "fw state:%x\n",
3932 if ((fw_state == MFI_STATE_FAULT) &&
3933 (instance->disableOnlineCtrlReset == 0)) {
3934 dev_notice(&instance->pdev->dev, "wait adp restart\n");
3936 if ((instance->pdev->device ==
3937 PCI_DEVICE_ID_LSI_SAS1064R) ||
3938 (instance->pdev->device ==
3939 PCI_DEVICE_ID_DELL_PERC5) ||
3940 (instance->pdev->device ==
3941 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3943 *instance->consumer =
3944 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3948 instance->instancet->disable_intr(instance);
3949 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3950 instance->issuepend_done = 0;
3952 atomic_set(&instance->fw_outstanding, 0);
3953 megasas_internal_reset_defer_cmds(instance);
3955 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3956 fw_state, atomic_read(&instance->adprecovery));
3958 schedule_work(&instance->work_init);
3962 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3963 fw_state, instance->disableOnlineCtrlReset);
3967 tasklet_schedule(&instance->isr_tasklet);
3972 * megasas_isr - isr entry point
3974 * @devp: IRQ context address
3976 static irqreturn_t megasas_isr(int irq, void *devp)
3978 struct megasas_irq_context *irq_context = devp;
3979 struct megasas_instance *instance = irq_context->instance;
3980 unsigned long flags;
3983 if (atomic_read(&instance->fw_reset_no_pci_access))
3986 spin_lock_irqsave(&instance->hba_lock, flags);
3987 rc = megasas_deplete_reply_queue(instance, DID_OK);
3988 spin_unlock_irqrestore(&instance->hba_lock, flags);
3994 * megasas_transition_to_ready - Move the FW to READY state
3995 * @instance: Adapter soft state
3996 * @ocr: Adapter reset state
3998 * During the initialization, FW passes can potentially be in any one of
3999 * several possible states. If the FW in operational, waiting-for-handshake
4000 * states, driver must take steps to bring it to ready state. Otherwise, it
4001 * has to wait for the ready state.
4004 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
4009 u32 abs_state, curr_abs_state;
4011 abs_state = instance->instancet->read_fw_status_reg(instance);
4012 fw_state = abs_state & MFI_STATE_MASK;
4014 if (fw_state != MFI_STATE_READY)
4015 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
4018 while (fw_state != MFI_STATE_READY) {
4022 case MFI_STATE_FAULT:
4023 dev_printk(KERN_ERR, &instance->pdev->dev,
4024 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
4025 abs_state & MFI_STATE_FAULT_CODE,
4026 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4028 max_wait = MEGASAS_RESET_WAIT_TIME;
4031 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4032 megasas_dump_reg_set(instance->reg_set);
4036 case MFI_STATE_WAIT_HANDSHAKE:
4038 * Set the CLR bit in inbound doorbell
4040 if ((instance->pdev->device ==
4041 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4042 (instance->pdev->device ==
4043 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4044 (instance->adapter_type != MFI_SERIES))
4046 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4047 &instance->reg_set->doorbell);
4050 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4051 &instance->reg_set->inbound_doorbell);
4053 max_wait = MEGASAS_RESET_WAIT_TIME;
4056 case MFI_STATE_BOOT_MESSAGE_PENDING:
4057 if ((instance->pdev->device ==
4058 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4059 (instance->pdev->device ==
4060 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4061 (instance->adapter_type != MFI_SERIES))
4062 writel(MFI_INIT_HOTPLUG,
4063 &instance->reg_set->doorbell);
4065 writel(MFI_INIT_HOTPLUG,
4066 &instance->reg_set->inbound_doorbell);
4068 max_wait = MEGASAS_RESET_WAIT_TIME;
4071 case MFI_STATE_OPERATIONAL:
4073 * Bring it to READY state; assuming max wait 10 secs
4075 instance->instancet->disable_intr(instance);
4076 if ((instance->pdev->device ==
4077 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4078 (instance->pdev->device ==
4079 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4080 (instance->adapter_type != MFI_SERIES)) {
4081 writel(MFI_RESET_FLAGS,
4082 &instance->reg_set->doorbell);
4084 if (instance->adapter_type != MFI_SERIES) {
4085 for (i = 0; i < (10 * 1000); i += 20) {
4097 writel(MFI_RESET_FLAGS,
4098 &instance->reg_set->inbound_doorbell);
4100 max_wait = MEGASAS_RESET_WAIT_TIME;
4103 case MFI_STATE_UNDEFINED:
4105 * This state should not last for more than 2 seconds
4107 max_wait = MEGASAS_RESET_WAIT_TIME;
4110 case MFI_STATE_BB_INIT:
4111 max_wait = MEGASAS_RESET_WAIT_TIME;
4114 case MFI_STATE_FW_INIT:
4115 max_wait = MEGASAS_RESET_WAIT_TIME;
4118 case MFI_STATE_FW_INIT_2:
4119 max_wait = MEGASAS_RESET_WAIT_TIME;
4122 case MFI_STATE_DEVICE_SCAN:
4123 max_wait = MEGASAS_RESET_WAIT_TIME;
4126 case MFI_STATE_FLUSH_CACHE:
4127 max_wait = MEGASAS_RESET_WAIT_TIME;
4131 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4133 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4134 megasas_dump_reg_set(instance->reg_set);
4139 * The cur_state should not last for more than max_wait secs
4141 for (i = 0; i < max_wait * 50; i++) {
4142 curr_abs_state = instance->instancet->
4143 read_fw_status_reg(instance);
4145 if (abs_state == curr_abs_state) {
4152 * Return error if fw_state hasn't changed after max_wait
4154 if (curr_abs_state == abs_state) {
4155 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4156 "in %d secs\n", fw_state, max_wait);
4157 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4158 megasas_dump_reg_set(instance->reg_set);
4162 abs_state = curr_abs_state;
4163 fw_state = curr_abs_state & MFI_STATE_MASK;
4165 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4171 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4172 * @instance: Adapter soft state
4174 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4177 u16 max_cmd = instance->max_mfi_cmds;
4178 struct megasas_cmd *cmd;
4180 if (!instance->frame_dma_pool)
4184 * Return all frames to pool
4186 for (i = 0; i < max_cmd; i++) {
4188 cmd = instance->cmd_list[i];
4191 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4192 cmd->frame_phys_addr);
4195 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4196 cmd->sense_phys_addr);
4200 * Now destroy the pool itself
4202 dma_pool_destroy(instance->frame_dma_pool);
4203 dma_pool_destroy(instance->sense_dma_pool);
4205 instance->frame_dma_pool = NULL;
4206 instance->sense_dma_pool = NULL;
4210 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4211 * @instance: Adapter soft state
4213 * Each command packet has an embedded DMA memory buffer that is used for
4214 * filling MFI frame and the SG list that immediately follows the frame. This
4215 * function creates those DMA memory buffers for each command packet by using
4216 * PCI pool facility.
4218 static int megasas_create_frame_pool(struct megasas_instance *instance)
4223 struct megasas_cmd *cmd;
4225 max_cmd = instance->max_mfi_cmds;
4228 * For MFI controllers.
4230 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4231 * Total 960 byte (15 MFI frame of 64 byte)
4233 * Fusion adapter require only 3 extra frame.
4234 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4235 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4236 * Total 192 byte (3 MFI frame of 64 byte)
4238 frame_count = (instance->adapter_type == MFI_SERIES) ?
4240 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4242 * Use DMA pool facility provided by PCI layer
4244 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4245 &instance->pdev->dev,
4246 instance->mfi_frame_size, 256, 0);
4248 if (!instance->frame_dma_pool) {
4249 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4253 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4254 &instance->pdev->dev, 128,
4257 if (!instance->sense_dma_pool) {
4258 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4260 dma_pool_destroy(instance->frame_dma_pool);
4261 instance->frame_dma_pool = NULL;
4267 * Allocate and attach a frame to each of the commands in cmd_list.
4268 * By making cmd->index as the context instead of the &cmd, we can
4269 * always use 32bit context regardless of the architecture
4271 for (i = 0; i < max_cmd; i++) {
4273 cmd = instance->cmd_list[i];
4275 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4276 GFP_KERNEL, &cmd->frame_phys_addr);
4278 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4279 GFP_KERNEL, &cmd->sense_phys_addr);
4282 * megasas_teardown_frame_pool() takes care of freeing
4283 * whatever has been allocated
4285 if (!cmd->frame || !cmd->sense) {
4286 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4287 megasas_teardown_frame_pool(instance);
4291 cmd->frame->io.context = cpu_to_le32(cmd->index);
4292 cmd->frame->io.pad_0 = 0;
4293 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4294 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4301 * megasas_free_cmds - Free all the cmds in the free cmd pool
4302 * @instance: Adapter soft state
4304 void megasas_free_cmds(struct megasas_instance *instance)
4308 /* First free the MFI frame pool */
4309 megasas_teardown_frame_pool(instance);
4311 /* Free all the commands in the cmd_list */
4312 for (i = 0; i < instance->max_mfi_cmds; i++)
4314 kfree(instance->cmd_list[i]);
4316 /* Free the cmd_list buffer itself */
4317 kfree(instance->cmd_list);
4318 instance->cmd_list = NULL;
4320 INIT_LIST_HEAD(&instance->cmd_pool);
4324 * megasas_alloc_cmds - Allocates the command packets
4325 * @instance: Adapter soft state
4327 * Each command that is issued to the FW, whether IO commands from the OS or
4328 * internal commands like IOCTLs, are wrapped in local data structure called
4329 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4332 * Each frame has a 32-bit field called context (tag). This context is used
4333 * to get back the megasas_cmd from the frame when a frame gets completed in
4334 * the ISR. Typically the address of the megasas_cmd itself would be used as
4335 * the context. But we wanted to keep the differences between 32 and 64 bit
4336 * systems to the mininum. We always use 32 bit integers for the context. In
4337 * this driver, the 32 bit values are the indices into an array cmd_list.
4338 * This array is used only to look up the megasas_cmd given the context. The
4339 * free commands themselves are maintained in a linked list called cmd_pool.
4341 int megasas_alloc_cmds(struct megasas_instance *instance)
4346 struct megasas_cmd *cmd;
4348 max_cmd = instance->max_mfi_cmds;
4351 * instance->cmd_list is an array of struct megasas_cmd pointers.
4352 * Allocate the dynamic array first and then allocate individual
4355 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4357 if (!instance->cmd_list) {
4358 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4362 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4364 for (i = 0; i < max_cmd; i++) {
4365 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4368 if (!instance->cmd_list[i]) {
4370 for (j = 0; j < i; j++)
4371 kfree(instance->cmd_list[j]);
4373 kfree(instance->cmd_list);
4374 instance->cmd_list = NULL;
4380 for (i = 0; i < max_cmd; i++) {
4381 cmd = instance->cmd_list[i];
4382 memset(cmd, 0, sizeof(struct megasas_cmd));
4385 cmd->instance = instance;
4387 list_add_tail(&cmd->list, &instance->cmd_pool);
4391 * Create a frame pool and assign one frame to each cmd
4393 if (megasas_create_frame_pool(instance)) {
4394 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4395 megasas_free_cmds(instance);
4403 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4404 * @instance: Adapter soft state
4406 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4407 * or FW is not under OCR.
4410 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4412 if (instance->adapter_type == MFI_SERIES)
4413 return KILL_ADAPTER;
4414 else if (instance->unload ||
4415 test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4416 &instance->reset_flags))
4417 return IGNORE_TIMEOUT;
4419 return INITIATE_OCR;
4423 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4426 struct megasas_cmd *cmd;
4427 struct megasas_dcmd_frame *dcmd;
4429 struct MR_PRIV_DEVICE *mr_device_priv_data;
4432 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4433 cmd = megasas_get_cmd(instance);
4436 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4440 dcmd = &cmd->frame->dcmd;
4442 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4443 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4445 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4446 dcmd->cmd = MFI_CMD_DCMD;
4447 dcmd->cmd_status = 0xFF;
4448 dcmd->sge_count = 1;
4449 dcmd->flags = MFI_FRAME_DIR_READ;
4452 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4453 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4455 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4456 sizeof(struct MR_PD_INFO));
4458 if ((instance->adapter_type != MFI_SERIES) &&
4459 !instance->mask_interrupts)
4460 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4462 ret = megasas_issue_polled(instance, cmd);
4466 mr_device_priv_data = sdev->hostdata;
4467 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4468 mr_device_priv_data->interface_type =
4469 instance->pd_info->state.ddf.pdType.intf;
4474 switch (dcmd_timeout_ocr_possible(instance)) {
4476 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4477 mutex_unlock(&instance->reset_mutex);
4478 megasas_reset_fusion(instance->host,
4479 MFI_IO_TIMEOUT_OCR);
4480 mutex_lock(&instance->reset_mutex);
4483 megaraid_sas_kill_hba(instance);
4485 case IGNORE_TIMEOUT:
4486 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4487 __func__, __LINE__);
4494 if (ret != DCMD_TIMEOUT)
4495 megasas_return_cmd(instance, cmd);
4500 * megasas_get_pd_list_info - Returns FW's pd_list structure
4501 * @instance: Adapter soft state
4502 * @pd_list: pd_list structure
4504 * Issues an internal command (DCMD) to get the FW's controller PD
4505 * list structure. This information is mainly used to find out SYSTEM
4506 * supported by the FW.
4509 megasas_get_pd_list(struct megasas_instance *instance)
4511 int ret = 0, pd_index = 0;
4512 struct megasas_cmd *cmd;
4513 struct megasas_dcmd_frame *dcmd;
4514 struct MR_PD_LIST *ci;
4515 struct MR_PD_ADDRESS *pd_addr;
4517 if (instance->pd_list_not_supported) {
4518 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4519 "not supported by firmware\n");
4523 ci = instance->pd_list_buf;
4525 cmd = megasas_get_cmd(instance);
4528 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4532 dcmd = &cmd->frame->dcmd;
4534 memset(ci, 0, sizeof(*ci));
4535 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4537 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4538 dcmd->mbox.b[1] = 0;
4539 dcmd->cmd = MFI_CMD_DCMD;
4540 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4541 dcmd->sge_count = 1;
4542 dcmd->flags = MFI_FRAME_DIR_READ;
4545 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4546 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4548 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4549 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4551 if ((instance->adapter_type != MFI_SERIES) &&
4552 !instance->mask_interrupts)
4553 ret = megasas_issue_blocked_cmd(instance, cmd,
4554 MFI_IO_TIMEOUT_SECS);
4556 ret = megasas_issue_polled(instance, cmd);
4560 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4561 "failed/not supported by firmware\n");
4563 if (instance->adapter_type != MFI_SERIES)
4564 megaraid_sas_kill_hba(instance);
4566 instance->pd_list_not_supported = 1;
4570 switch (dcmd_timeout_ocr_possible(instance)) {
4572 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4574 * DCMD failed from AEN path.
4575 * AEN path already hold reset_mutex to avoid PCI access
4576 * while OCR is in progress.
4578 mutex_unlock(&instance->reset_mutex);
4579 megasas_reset_fusion(instance->host,
4580 MFI_IO_TIMEOUT_OCR);
4581 mutex_lock(&instance->reset_mutex);
4584 megaraid_sas_kill_hba(instance);
4586 case IGNORE_TIMEOUT:
4587 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4588 __func__, __LINE__);
4596 if (megasas_dbg_lvl & LD_PD_DEBUG)
4597 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4598 __func__, le32_to_cpu(ci->count));
4600 if ((le32_to_cpu(ci->count) >
4601 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4604 memset(instance->local_pd_list, 0,
4605 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4607 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4608 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4609 le16_to_cpu(pd_addr->deviceId);
4610 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4611 pd_addr->scsiDevType;
4612 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4614 if (megasas_dbg_lvl & LD_PD_DEBUG)
4615 dev_info(&instance->pdev->dev,
4616 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4617 pd_index, le16_to_cpu(pd_addr->deviceId),
4618 pd_addr->scsiDevType);
4622 memcpy(instance->pd_list, instance->local_pd_list,
4623 sizeof(instance->pd_list));
4628 if (ret != DCMD_TIMEOUT)
4629 megasas_return_cmd(instance, cmd);
4635 * megasas_get_ld_list_info - Returns FW's ld_list structure
4636 * @instance: Adapter soft state
4637 * @ld_list: ld_list structure
4639 * Issues an internal command (DCMD) to get the FW's controller PD
4640 * list structure. This information is mainly used to find out SYSTEM
4641 * supported by the FW.
4644 megasas_get_ld_list(struct megasas_instance *instance)
4646 int ret = 0, ld_index = 0, ids = 0;
4647 struct megasas_cmd *cmd;
4648 struct megasas_dcmd_frame *dcmd;
4649 struct MR_LD_LIST *ci;
4650 dma_addr_t ci_h = 0;
4653 ci = instance->ld_list_buf;
4654 ci_h = instance->ld_list_buf_h;
4656 cmd = megasas_get_cmd(instance);
4659 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4663 dcmd = &cmd->frame->dcmd;
4665 memset(ci, 0, sizeof(*ci));
4666 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4668 if (instance->supportmax256vd)
4669 dcmd->mbox.b[0] = 1;
4670 dcmd->cmd = MFI_CMD_DCMD;
4671 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4672 dcmd->sge_count = 1;
4673 dcmd->flags = MFI_FRAME_DIR_READ;
4675 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4676 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4679 megasas_set_dma_settings(instance, dcmd, ci_h,
4680 sizeof(struct MR_LD_LIST));
4682 if ((instance->adapter_type != MFI_SERIES) &&
4683 !instance->mask_interrupts)
4684 ret = megasas_issue_blocked_cmd(instance, cmd,
4685 MFI_IO_TIMEOUT_SECS);
4687 ret = megasas_issue_polled(instance, cmd);
4689 ld_count = le32_to_cpu(ci->ldCount);
4693 megaraid_sas_kill_hba(instance);
4697 switch (dcmd_timeout_ocr_possible(instance)) {
4699 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4701 * DCMD failed from AEN path.
4702 * AEN path already hold reset_mutex to avoid PCI access
4703 * while OCR is in progress.
4705 mutex_unlock(&instance->reset_mutex);
4706 megasas_reset_fusion(instance->host,
4707 MFI_IO_TIMEOUT_OCR);
4708 mutex_lock(&instance->reset_mutex);
4711 megaraid_sas_kill_hba(instance);
4713 case IGNORE_TIMEOUT:
4714 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4715 __func__, __LINE__);
4722 if (megasas_dbg_lvl & LD_PD_DEBUG)
4723 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4724 __func__, ld_count);
4726 if (ld_count > instance->fw_supported_vd_count)
4729 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4731 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4732 if (ci->ldList[ld_index].state != 0) {
4733 ids = ci->ldList[ld_index].ref.targetId;
4734 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4735 if (megasas_dbg_lvl & LD_PD_DEBUG)
4736 dev_info(&instance->pdev->dev,
4737 "LD%d: targetID: 0x%03x\n",
4745 if (ret != DCMD_TIMEOUT)
4746 megasas_return_cmd(instance, cmd);
4752 * megasas_ld_list_query - Returns FW's ld_list structure
4753 * @instance: Adapter soft state
4754 * @query_type: ld_list structure type
4756 * Issues an internal command (DCMD) to get the FW's controller PD
4757 * list structure. This information is mainly used to find out SYSTEM
4758 * supported by the FW.
4761 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4763 int ret = 0, ld_index = 0, ids = 0;
4764 struct megasas_cmd *cmd;
4765 struct megasas_dcmd_frame *dcmd;
4766 struct MR_LD_TARGETID_LIST *ci;
4767 dma_addr_t ci_h = 0;
4770 ci = instance->ld_targetid_list_buf;
4771 ci_h = instance->ld_targetid_list_buf_h;
4773 cmd = megasas_get_cmd(instance);
4776 dev_warn(&instance->pdev->dev,
4777 "megasas_ld_list_query: Failed to get cmd\n");
4781 dcmd = &cmd->frame->dcmd;
4783 memset(ci, 0, sizeof(*ci));
4784 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4786 dcmd->mbox.b[0] = query_type;
4787 if (instance->supportmax256vd)
4788 dcmd->mbox.b[2] = 1;
4790 dcmd->cmd = MFI_CMD_DCMD;
4791 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4792 dcmd->sge_count = 1;
4793 dcmd->flags = MFI_FRAME_DIR_READ;
4795 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4796 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4799 megasas_set_dma_settings(instance, dcmd, ci_h,
4800 sizeof(struct MR_LD_TARGETID_LIST));
4802 if ((instance->adapter_type != MFI_SERIES) &&
4803 !instance->mask_interrupts)
4804 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4806 ret = megasas_issue_polled(instance, cmd);
4810 dev_info(&instance->pdev->dev,
4811 "DCMD not supported by firmware - %s %d\n",
4812 __func__, __LINE__);
4813 ret = megasas_get_ld_list(instance);
4816 switch (dcmd_timeout_ocr_possible(instance)) {
4818 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4820 * DCMD failed from AEN path.
4821 * AEN path already hold reset_mutex to avoid PCI access
4822 * while OCR is in progress.
4824 mutex_unlock(&instance->reset_mutex);
4825 megasas_reset_fusion(instance->host,
4826 MFI_IO_TIMEOUT_OCR);
4827 mutex_lock(&instance->reset_mutex);
4830 megaraid_sas_kill_hba(instance);
4832 case IGNORE_TIMEOUT:
4833 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4834 __func__, __LINE__);
4840 tgtid_count = le32_to_cpu(ci->count);
4842 if (megasas_dbg_lvl & LD_PD_DEBUG)
4843 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4844 __func__, tgtid_count);
4846 if ((tgtid_count > (instance->fw_supported_vd_count)))
4849 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4850 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4851 ids = ci->targetId[ld_index];
4852 instance->ld_ids[ids] = ci->targetId[ld_index];
4853 if (megasas_dbg_lvl & LD_PD_DEBUG)
4854 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4855 ld_index, ci->targetId[ld_index]);
4861 if (ret != DCMD_TIMEOUT)
4862 megasas_return_cmd(instance, cmd);
4868 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4869 * dcmd.mbox - reserved
4870 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4871 * Desc: This DCMD will return the combined device list
4872 * Status: MFI_STAT_OK - List returned successfully
4873 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4875 * @instance: Adapter soft state
4876 * @is_probe: Driver probe check
4877 * Return: 0 if DCMD succeeded
4878 * non-zero if failed
4881 megasas_host_device_list_query(struct megasas_instance *instance,
4884 int ret, i, target_id;
4885 struct megasas_cmd *cmd;
4886 struct megasas_dcmd_frame *dcmd;
4887 struct MR_HOST_DEVICE_LIST *ci;
4891 ci = instance->host_device_list_buf;
4892 ci_h = instance->host_device_list_buf_h;
4894 cmd = megasas_get_cmd(instance);
4897 dev_warn(&instance->pdev->dev,
4898 "%s: failed to get cmd\n",
4903 dcmd = &cmd->frame->dcmd;
4905 memset(ci, 0, sizeof(*ci));
4906 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4908 dcmd->mbox.b[0] = is_probe ? 0 : 1;
4909 dcmd->cmd = MFI_CMD_DCMD;
4910 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4911 dcmd->sge_count = 1;
4912 dcmd->flags = MFI_FRAME_DIR_READ;
4915 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4916 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4918 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4920 if (!instance->mask_interrupts) {
4921 ret = megasas_issue_blocked_cmd(instance, cmd,
4922 MFI_IO_TIMEOUT_SECS);
4924 ret = megasas_issue_polled(instance, cmd);
4925 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4930 /* Fill the internal pd_list and ld_ids array based on
4931 * targetIds returned by FW
4933 count = le32_to_cpu(ci->count);
4935 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
4938 if (megasas_dbg_lvl & LD_PD_DEBUG)
4939 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
4942 memset(instance->local_pd_list, 0,
4943 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4944 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4945 for (i = 0; i < count; i++) {
4946 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4947 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4948 instance->local_pd_list[target_id].tid = target_id;
4949 instance->local_pd_list[target_id].driveType =
4950 ci->host_device_list[i].scsi_type;
4951 instance->local_pd_list[target_id].driveState =
4953 if (megasas_dbg_lvl & LD_PD_DEBUG)
4954 dev_info(&instance->pdev->dev,
4955 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
4956 i, target_id, ci->host_device_list[i].scsi_type);
4958 instance->ld_ids[target_id] = target_id;
4959 if (megasas_dbg_lvl & LD_PD_DEBUG)
4960 dev_info(&instance->pdev->dev,
4961 "Device %d: LD targetID: 0x%03x\n",
4966 memcpy(instance->pd_list, instance->local_pd_list,
4967 sizeof(instance->pd_list));
4971 switch (dcmd_timeout_ocr_possible(instance)) {
4973 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4974 mutex_unlock(&instance->reset_mutex);
4975 megasas_reset_fusion(instance->host,
4976 MFI_IO_TIMEOUT_OCR);
4977 mutex_lock(&instance->reset_mutex);
4980 megaraid_sas_kill_hba(instance);
4982 case IGNORE_TIMEOUT:
4983 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4984 __func__, __LINE__);
4989 dev_err(&instance->pdev->dev,
4990 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4995 if (ret != DCMD_TIMEOUT)
4996 megasas_return_cmd(instance, cmd);
5002 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
5003 * instance : Controller's instance
5005 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
5007 struct fusion_context *fusion;
5008 u32 ventura_map_sz = 0;
5010 fusion = instance->ctrl_context;
5011 /* For MFI based controllers return dummy success */
5015 instance->supportmax256vd =
5016 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
5017 /* Below is additional check to address future FW enhancement */
5018 if (instance->ctrl_info_buf->max_lds > 64)
5019 instance->supportmax256vd = 1;
5021 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
5022 * MEGASAS_MAX_DEV_PER_CHANNEL;
5023 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
5024 * MEGASAS_MAX_DEV_PER_CHANNEL;
5025 if (instance->supportmax256vd) {
5026 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
5027 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5029 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5030 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5033 dev_info(&instance->pdev->dev,
5034 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
5035 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
5036 instance->ctrl_info_buf->max_lds);
5038 if (instance->max_raid_mapsize) {
5039 ventura_map_sz = instance->max_raid_mapsize *
5040 MR_MIN_MAP_SIZE; /* 64k */
5041 fusion->current_map_sz = ventura_map_sz;
5042 fusion->max_map_sz = ventura_map_sz;
5044 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
5045 (sizeof(struct MR_LD_SPAN_MAP) *
5046 (instance->fw_supported_vd_count - 1));
5047 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
5049 fusion->max_map_sz =
5050 max(fusion->old_map_sz, fusion->new_map_sz);
5052 if (instance->supportmax256vd)
5053 fusion->current_map_sz = fusion->new_map_sz;
5055 fusion->current_map_sz = fusion->old_map_sz;
5057 /* irrespective of FW raid maps, driver raid map is constant */
5058 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
5062 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
5063 * dcmd.hdr.length - number of bytes to read
5064 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
5065 * Desc: Fill in snapdump properties
5066 * Status: MFI_STAT_OK- Command successful
5068 void megasas_get_snapdump_properties(struct megasas_instance *instance)
5071 struct megasas_cmd *cmd;
5072 struct megasas_dcmd_frame *dcmd;
5073 struct MR_SNAPDUMP_PROPERTIES *ci;
5074 dma_addr_t ci_h = 0;
5076 ci = instance->snapdump_prop;
5077 ci_h = instance->snapdump_prop_h;
5082 cmd = megasas_get_cmd(instance);
5085 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
5089 dcmd = &cmd->frame->dcmd;
5091 memset(ci, 0, sizeof(*ci));
5092 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5094 dcmd->cmd = MFI_CMD_DCMD;
5095 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5096 dcmd->sge_count = 1;
5097 dcmd->flags = MFI_FRAME_DIR_READ;
5100 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5101 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5103 megasas_set_dma_settings(instance, dcmd, ci_h,
5104 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5106 if (!instance->mask_interrupts) {
5107 ret = megasas_issue_blocked_cmd(instance, cmd,
5108 MFI_IO_TIMEOUT_SECS);
5110 ret = megasas_issue_polled(instance, cmd);
5111 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5116 instance->snapdump_wait_time =
5117 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5118 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5122 switch (dcmd_timeout_ocr_possible(instance)) {
5124 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5125 mutex_unlock(&instance->reset_mutex);
5126 megasas_reset_fusion(instance->host,
5127 MFI_IO_TIMEOUT_OCR);
5128 mutex_lock(&instance->reset_mutex);
5131 megaraid_sas_kill_hba(instance);
5133 case IGNORE_TIMEOUT:
5134 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5135 __func__, __LINE__);
5140 if (ret != DCMD_TIMEOUT)
5141 megasas_return_cmd(instance, cmd);
5145 * megasas_get_controller_info - Returns FW's controller structure
5146 * @instance: Adapter soft state
5148 * Issues an internal command (DCMD) to get the FW's controller structure.
5149 * This information is mainly used to find out the maximum IO transfer per
5150 * command supported by the FW.
5153 megasas_get_ctrl_info(struct megasas_instance *instance)
5156 struct megasas_cmd *cmd;
5157 struct megasas_dcmd_frame *dcmd;
5158 struct megasas_ctrl_info *ci;
5159 dma_addr_t ci_h = 0;
5161 ci = instance->ctrl_info_buf;
5162 ci_h = instance->ctrl_info_buf_h;
5164 cmd = megasas_get_cmd(instance);
5167 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5171 dcmd = &cmd->frame->dcmd;
5173 memset(ci, 0, sizeof(*ci));
5174 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5176 dcmd->cmd = MFI_CMD_DCMD;
5177 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5178 dcmd->sge_count = 1;
5179 dcmd->flags = MFI_FRAME_DIR_READ;
5182 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5183 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5184 dcmd->mbox.b[0] = 1;
5186 megasas_set_dma_settings(instance, dcmd, ci_h,
5187 sizeof(struct megasas_ctrl_info));
5189 if ((instance->adapter_type != MFI_SERIES) &&
5190 !instance->mask_interrupts) {
5191 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5193 ret = megasas_issue_polled(instance, cmd);
5194 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5199 /* Save required controller information in
5200 * CPU endianness format.
5202 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5203 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5204 le32_to_cpus((u32 *)&ci->adapterOperations2);
5205 le32_to_cpus((u32 *)&ci->adapterOperations3);
5206 le16_to_cpus((u16 *)&ci->adapter_operations4);
5207 le32_to_cpus((u32 *)&ci->adapter_operations5);
5209 /* Update the latest Ext VD info.
5210 * From Init path, store current firmware details.
5211 * From OCR path, detect any firmware properties changes.
5212 * in case of Firmware upgrade without system reboot.
5214 megasas_update_ext_vd_details(instance);
5215 instance->support_seqnum_jbod_fp =
5216 ci->adapterOperations3.useSeqNumJbodFP;
5217 instance->support_morethan256jbod =
5218 ci->adapter_operations4.support_pd_map_target_id;
5219 instance->support_nvme_passthru =
5220 ci->adapter_operations4.support_nvme_passthru;
5221 instance->support_pci_lane_margining =
5222 ci->adapter_operations5.support_pci_lane_margining;
5223 instance->task_abort_tmo = ci->TaskAbortTO;
5224 instance->max_reset_tmo = ci->MaxResetTO;
5226 /*Check whether controller is iMR or MR */
5227 instance->is_imr = (ci->memory_size ? 0 : 1);
5229 instance->snapdump_wait_time =
5230 (ci->properties.on_off_properties2.enable_snap_dump ?
5231 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5233 instance->enable_fw_dev_list =
5234 ci->properties.on_off_properties2.enable_fw_dev_list;
5236 dev_info(&instance->pdev->dev,
5237 "controller type\t: %s(%dMB)\n",
5238 instance->is_imr ? "iMR" : "MR",
5239 le16_to_cpu(ci->memory_size));
5241 instance->disableOnlineCtrlReset =
5242 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5243 instance->secure_jbod_support =
5244 ci->adapterOperations3.supportSecurityonJBOD;
5245 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5246 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5247 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5248 instance->secure_jbod_support ? "Yes" : "No");
5249 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5250 instance->support_nvme_passthru ? "Yes" : "No");
5251 dev_info(&instance->pdev->dev,
5252 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5253 instance->task_abort_tmo, instance->max_reset_tmo);
5254 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5255 instance->support_seqnum_jbod_fp ? "Yes" : "No");
5256 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5257 instance->support_pci_lane_margining ? "Yes" : "No");
5262 switch (dcmd_timeout_ocr_possible(instance)) {
5264 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5265 mutex_unlock(&instance->reset_mutex);
5266 megasas_reset_fusion(instance->host,
5267 MFI_IO_TIMEOUT_OCR);
5268 mutex_lock(&instance->reset_mutex);
5271 megaraid_sas_kill_hba(instance);
5273 case IGNORE_TIMEOUT:
5274 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5275 __func__, __LINE__);
5280 megaraid_sas_kill_hba(instance);
5285 if (ret != DCMD_TIMEOUT)
5286 megasas_return_cmd(instance, cmd);
5292 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5295 * @instance: Adapter soft state
5296 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5297 MR_CRASH_BUF_TURN_OFF = 0
5298 MR_CRASH_BUF_TURN_ON = 1
5299 * @return 0 on success non-zero on failure.
5300 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5301 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5302 * that driver supports crash dump feature. This DCMD will be sent only if
5303 * crash dump feature is supported by the FW.
5306 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5310 struct megasas_cmd *cmd;
5311 struct megasas_dcmd_frame *dcmd;
5313 cmd = megasas_get_cmd(instance);
5316 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5321 dcmd = &cmd->frame->dcmd;
5323 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5324 dcmd->mbox.b[0] = crash_buf_state;
5325 dcmd->cmd = MFI_CMD_DCMD;
5326 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5327 dcmd->sge_count = 1;
5328 dcmd->flags = MFI_FRAME_DIR_NONE;
5331 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5332 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5334 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5335 CRASH_DMA_BUF_SIZE);
5337 if ((instance->adapter_type != MFI_SERIES) &&
5338 !instance->mask_interrupts)
5339 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5341 ret = megasas_issue_polled(instance, cmd);
5343 if (ret == DCMD_TIMEOUT) {
5344 switch (dcmd_timeout_ocr_possible(instance)) {
5346 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5347 megasas_reset_fusion(instance->host,
5348 MFI_IO_TIMEOUT_OCR);
5351 megaraid_sas_kill_hba(instance);
5353 case IGNORE_TIMEOUT:
5354 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5355 __func__, __LINE__);
5359 megasas_return_cmd(instance, cmd);
5365 * megasas_issue_init_mfi - Initializes the FW
5366 * @instance: Adapter soft state
5368 * Issues the INIT MFI cmd
5371 megasas_issue_init_mfi(struct megasas_instance *instance)
5374 struct megasas_cmd *cmd;
5375 struct megasas_init_frame *init_frame;
5376 struct megasas_init_queue_info *initq_info;
5377 dma_addr_t init_frame_h;
5378 dma_addr_t initq_info_h;
5381 * Prepare a init frame. Note the init frame points to queue info
5382 * structure. Each frame has SGL allocated after first 64 bytes. For
5383 * this frame - since we don't need any SGL - we use SGL's space as
5384 * queue info structure
5386 * We will not get a NULL command below. We just created the pool.
5388 cmd = megasas_get_cmd(instance);
5390 init_frame = (struct megasas_init_frame *)cmd->frame;
5391 initq_info = (struct megasas_init_queue_info *)
5392 ((unsigned long)init_frame + 64);
5394 init_frame_h = cmd->frame_phys_addr;
5395 initq_info_h = init_frame_h + 64;
5397 context = init_frame->context;
5398 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5399 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5400 init_frame->context = context;
5402 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5403 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5405 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5406 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5408 init_frame->cmd = MFI_CMD_INIT;
5409 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5410 init_frame->queue_info_new_phys_addr_lo =
5411 cpu_to_le32(lower_32_bits(initq_info_h));
5412 init_frame->queue_info_new_phys_addr_hi =
5413 cpu_to_le32(upper_32_bits(initq_info_h));
5415 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5418 * disable the intr before firing the init frame to FW
5420 instance->instancet->disable_intr(instance);
5423 * Issue the init frame in polled mode
5426 if (megasas_issue_polled(instance, cmd)) {
5427 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5428 megasas_return_cmd(instance, cmd);
5432 megasas_return_cmd(instance, cmd);
5441 megasas_init_adapter_mfi(struct megasas_instance *instance)
5447 * Get various operational parameters from status register
5449 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5451 * Reduce the max supported cmds by 1. This is to ensure that the
5452 * reply_q_sz (1 more than the max cmd that driver may send)
5453 * does not exceed max cmds that the FW can support
5455 instance->max_fw_cmds = instance->max_fw_cmds-1;
5456 instance->max_mfi_cmds = instance->max_fw_cmds;
5457 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5460 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5461 * are reserved for IOCTL + driver's internal DCMDs.
5463 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5464 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5465 instance->max_scsi_cmds = (instance->max_fw_cmds -
5466 MEGASAS_SKINNY_INT_CMDS);
5467 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5469 instance->max_scsi_cmds = (instance->max_fw_cmds -
5471 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5474 instance->cur_can_queue = instance->max_scsi_cmds;
5476 * Create a pool of commands
5478 if (megasas_alloc_cmds(instance))
5479 goto fail_alloc_cmds;
5482 * Allocate memory for reply queue. Length of reply queue should
5483 * be _one_ more than the maximum commands handled by the firmware.
5485 * Note: When FW completes commands, it places corresponding contex
5486 * values in this circular reply queue. This circular queue is a fairly
5487 * typical producer-consumer queue. FW is the producer (of completed
5488 * commands) and the driver is the consumer.
5490 context_sz = sizeof(u32);
5491 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5493 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5494 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5496 if (!instance->reply_queue) {
5497 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5498 goto fail_reply_queue;
5501 if (megasas_issue_init_mfi(instance))
5504 if (megasas_get_ctrl_info(instance)) {
5505 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5506 "Fail from %s %d\n", instance->unique_id,
5507 __func__, __LINE__);
5511 instance->fw_support_ieee = 0;
5512 instance->fw_support_ieee =
5513 (instance->instancet->read_fw_status_reg(instance) &
5516 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5517 instance->fw_support_ieee);
5519 if (instance->fw_support_ieee)
5520 instance->flag_ieee = 1;
5526 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5527 instance->reply_queue, instance->reply_queue_h);
5529 megasas_free_cmds(instance);
5536 void megasas_setup_irq_poll(struct megasas_instance *instance)
5538 struct megasas_irq_context *irq_ctx;
5541 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5543 /* Initialize IRQ poll */
5544 for (i = 0; i < count; i++) {
5545 irq_ctx = &instance->irq_context[i];
5546 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5547 irq_ctx->irq_poll_scheduled = false;
5548 irq_poll_init(&irq_ctx->irqpoll,
5549 instance->threshold_reply_count,
5555 * megasas_setup_irqs_ioapic - register legacy interrupts.
5556 * @instance: Adapter soft state
5558 * Do not enable interrupt, only setup ISRs.
5560 * Return 0 on success.
5563 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5565 struct pci_dev *pdev;
5567 pdev = instance->pdev;
5568 instance->irq_context[0].instance = instance;
5569 instance->irq_context[0].MSIxIndex = 0;
5570 snprintf(instance->irq_context->name, MEGASAS_MSIX_NAME_LEN, "%s%u",
5571 "megasas", instance->host->host_no);
5572 if (request_irq(pci_irq_vector(pdev, 0),
5573 instance->instancet->service_isr, IRQF_SHARED,
5574 instance->irq_context->name, &instance->irq_context[0])) {
5575 dev_err(&instance->pdev->dev,
5576 "Failed to register IRQ from %s %d\n",
5577 __func__, __LINE__);
5580 instance->perf_mode = MR_LATENCY_PERF_MODE;
5581 instance->low_latency_index_start = 0;
5586 * megasas_setup_irqs_msix - register MSI-x interrupts.
5587 * @instance: Adapter soft state
5588 * @is_probe: Driver probe check
5590 * Do not enable interrupt, only setup ISRs.
5592 * Return 0 on success.
5595 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5598 struct pci_dev *pdev;
5600 pdev = instance->pdev;
5603 for (i = 0; i < instance->msix_vectors; i++) {
5604 instance->irq_context[i].instance = instance;
5605 instance->irq_context[i].MSIxIndex = i;
5606 snprintf(instance->irq_context[i].name, MEGASAS_MSIX_NAME_LEN, "%s%u-msix%u",
5607 "megasas", instance->host->host_no, i);
5608 if (request_irq(pci_irq_vector(pdev, i),
5609 instance->instancet->service_isr, 0, instance->irq_context[i].name,
5610 &instance->irq_context[i])) {
5611 dev_err(&instance->pdev->dev,
5612 "Failed to register IRQ for vector %d.\n", i);
5613 for (j = 0; j < i; j++) {
5614 if (j < instance->low_latency_index_start)
5615 irq_set_affinity_hint(
5616 pci_irq_vector(pdev, j), NULL);
5617 free_irq(pci_irq_vector(pdev, j),
5618 &instance->irq_context[j]);
5620 /* Retry irq register for IO_APIC*/
5621 instance->msix_vectors = 0;
5622 instance->msix_load_balance = false;
5624 pci_free_irq_vectors(instance->pdev);
5625 return megasas_setup_irqs_ioapic(instance);
5636 * megasas_destroy_irqs- unregister interrupts.
5637 * @instance: Adapter soft state
5641 megasas_destroy_irqs(struct megasas_instance *instance) {
5645 struct megasas_irq_context *irq_ctx;
5647 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5648 if (instance->adapter_type != MFI_SERIES) {
5649 for (i = 0; i < count; i++) {
5650 irq_ctx = &instance->irq_context[i];
5651 irq_poll_disable(&irq_ctx->irqpoll);
5655 if (instance->msix_vectors)
5656 for (i = 0; i < instance->msix_vectors; i++) {
5657 if (i < instance->low_latency_index_start)
5658 irq_set_affinity_hint(
5659 pci_irq_vector(instance->pdev, i), NULL);
5660 free_irq(pci_irq_vector(instance->pdev, i),
5661 &instance->irq_context[i]);
5664 free_irq(pci_irq_vector(instance->pdev, 0),
5665 &instance->irq_context[0]);
5669 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5670 * @instance: Adapter soft state
5672 * Return 0 on success.
5675 megasas_setup_jbod_map(struct megasas_instance *instance)
5678 struct fusion_context *fusion = instance->ctrl_context;
5681 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5682 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5684 instance->use_seqnum_jbod_fp =
5685 instance->support_seqnum_jbod_fp;
5686 if (reset_devices || !fusion ||
5687 !instance->support_seqnum_jbod_fp) {
5688 dev_info(&instance->pdev->dev,
5689 "JBOD sequence map is disabled %s %d\n",
5690 __func__, __LINE__);
5691 instance->use_seqnum_jbod_fp = false;
5695 if (fusion->pd_seq_sync[0])
5698 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5699 fusion->pd_seq_sync[i] = dma_alloc_coherent
5700 (&instance->pdev->dev, pd_seq_map_sz,
5701 &fusion->pd_seq_phys[i], GFP_KERNEL);
5702 if (!fusion->pd_seq_sync[i]) {
5703 dev_err(&instance->pdev->dev,
5704 "Failed to allocate memory from %s %d\n",
5705 __func__, __LINE__);
5707 dma_free_coherent(&instance->pdev->dev,
5708 pd_seq_map_sz, fusion->pd_seq_sync[0],
5709 fusion->pd_seq_phys[0]);
5710 fusion->pd_seq_sync[0] = NULL;
5712 instance->use_seqnum_jbod_fp = false;
5718 if (!megasas_sync_pd_seq_num(instance, false) &&
5719 !megasas_sync_pd_seq_num(instance, true))
5720 instance->use_seqnum_jbod_fp = true;
5722 instance->use_seqnum_jbod_fp = false;
5725 static void megasas_setup_reply_map(struct megasas_instance *instance)
5727 const struct cpumask *mask;
5728 unsigned int queue, cpu, low_latency_index_start;
5730 low_latency_index_start = instance->low_latency_index_start;
5732 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
5733 mask = pci_irq_get_affinity(instance->pdev, queue);
5737 for_each_cpu(cpu, mask)
5738 instance->reply_map[cpu] = queue;
5743 queue = low_latency_index_start;
5744 for_each_possible_cpu(cpu) {
5745 instance->reply_map[cpu] = queue;
5746 if (queue == (instance->msix_vectors - 1))
5747 queue = low_latency_index_start;
5754 * megasas_get_device_list - Get the PD and LD device list from FW.
5755 * @instance: Adapter soft state
5756 * @return: Success or failure
5758 * Issue DCMDs to Firmware to get the PD and LD list.
5759 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5760 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5763 int megasas_get_device_list(struct megasas_instance *instance)
5765 memset(instance->pd_list, 0,
5766 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5767 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5769 if (instance->enable_fw_dev_list) {
5770 if (megasas_host_device_list_query(instance, true))
5773 if (megasas_get_pd_list(instance) < 0) {
5774 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5778 if (megasas_ld_list_query(instance,
5779 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5780 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5789 * megasas_set_high_iops_queue_affinity_hint - Set affinity hint for high IOPS queues
5790 * @instance: Adapter soft state
5794 megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5797 int local_numa_node;
5799 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
5800 local_numa_node = dev_to_node(&instance->pdev->dev);
5802 for (i = 0; i < instance->low_latency_index_start; i++)
5803 irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5804 cpumask_of_node(local_numa_node));
5809 __megasas_alloc_irq_vectors(struct megasas_instance *instance)
5812 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5813 struct irq_affinity *descp = &desc;
5815 irq_flags = PCI_IRQ_MSIX;
5817 if (instance->smp_affinity_enable)
5818 irq_flags |= PCI_IRQ_AFFINITY;
5822 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5823 instance->low_latency_index_start,
5824 instance->msix_vectors, irq_flags, descp);
5830 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5831 * @instance: Adapter soft state
5835 megasas_alloc_irq_vectors(struct megasas_instance *instance)
5838 unsigned int num_msix_req;
5840 i = __megasas_alloc_irq_vectors(instance);
5842 if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
5843 (i != instance->msix_vectors)) {
5844 if (instance->msix_vectors)
5845 pci_free_irq_vectors(instance->pdev);
5846 /* Disable Balanced IOPS mode and try realloc vectors */
5847 instance->perf_mode = MR_LATENCY_PERF_MODE;
5848 instance->low_latency_index_start = 1;
5849 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5851 instance->msix_vectors = min(num_msix_req,
5852 instance->msix_vectors);
5854 i = __megasas_alloc_irq_vectors(instance);
5858 dev_info(&instance->pdev->dev,
5859 "requested/available msix %d/%d\n", instance->msix_vectors, i);
5862 instance->msix_vectors = i;
5864 instance->msix_vectors = 0;
5866 if (instance->smp_affinity_enable)
5867 megasas_set_high_iops_queue_affinity_hint(instance);
5871 * megasas_init_fw - Initializes the FW
5872 * @instance: Adapter soft state
5874 * This is the main function for initializing firmware
5877 static int megasas_init_fw(struct megasas_instance *instance)
5880 u32 max_sectors_2, tmp_sectors, msix_enable;
5881 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5882 resource_size_t base_addr;
5883 void *base_addr_phys;
5884 struct megasas_ctrl_info *ctrl_info = NULL;
5885 unsigned long bar_list;
5887 struct IOV_111 *iovPtr;
5888 struct fusion_context *fusion;
5889 bool intr_coalescing;
5890 unsigned int num_msix_req;
5893 fusion = instance->ctrl_context;
5895 /* Find first memory bar */
5896 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5897 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5898 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5900 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5904 base_addr = pci_resource_start(instance->pdev, instance->bar);
5905 instance->reg_set = ioremap(base_addr, 8192);
5907 if (!instance->reg_set) {
5908 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5912 base_addr_phys = &base_addr;
5913 dev_printk(KERN_DEBUG, &instance->pdev->dev,
5914 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
5915 instance->bar, base_addr_phys, instance->reg_set);
5917 if (instance->adapter_type != MFI_SERIES)
5918 instance->instancet = &megasas_instance_template_fusion;
5920 switch (instance->pdev->device) {
5921 case PCI_DEVICE_ID_LSI_SAS1078R:
5922 case PCI_DEVICE_ID_LSI_SAS1078DE:
5923 instance->instancet = &megasas_instance_template_ppc;
5925 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5926 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5927 instance->instancet = &megasas_instance_template_gen2;
5929 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5930 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5931 instance->instancet = &megasas_instance_template_skinny;
5933 case PCI_DEVICE_ID_LSI_SAS1064R:
5934 case PCI_DEVICE_ID_DELL_PERC5:
5936 instance->instancet = &megasas_instance_template_xscale;
5937 instance->pd_list_not_supported = 1;
5942 if (megasas_transition_to_ready(instance, 0)) {
5943 dev_info(&instance->pdev->dev,
5944 "Failed to transition controller to ready from %s!\n",
5946 if (instance->adapter_type != MFI_SERIES) {
5947 status_reg = instance->instancet->read_fw_status_reg(
5949 if (status_reg & MFI_RESET_ADAPTER) {
5950 if (megasas_adp_reset_wait_for_ready
5951 (instance, true, 0) == FAILED)
5952 goto fail_ready_state;
5954 goto fail_ready_state;
5957 atomic_set(&instance->fw_reset_no_pci_access, 1);
5958 instance->instancet->adp_reset
5959 (instance, instance->reg_set);
5960 atomic_set(&instance->fw_reset_no_pci_access, 0);
5962 /*waiting for about 30 second before retry*/
5965 if (megasas_transition_to_ready(instance, 0))
5966 goto fail_ready_state;
5969 dev_info(&instance->pdev->dev,
5970 "FW restarted successfully from %s!\n",
5974 megasas_init_ctrl_params(instance);
5976 if (megasas_set_dma_mask(instance))
5977 goto fail_ready_state;
5979 if (megasas_alloc_ctrl_mem(instance))
5980 goto fail_alloc_dma_buf;
5982 if (megasas_alloc_ctrl_dma_buffers(instance))
5983 goto fail_alloc_dma_buf;
5985 fusion = instance->ctrl_context;
5987 if (instance->adapter_type >= VENTURA_SERIES) {
5989 megasas_readl(instance,
5990 &instance->reg_set->outbound_scratch_pad_2);
5991 instance->max_raid_mapsize = ((scratch_pad_2 >>
5992 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5993 MR_MAX_RAID_MAP_SIZE_MASK);
5996 instance->enable_sdev_max_qd = enable_sdev_max_qd;
5998 switch (instance->adapter_type) {
5999 case VENTURA_SERIES:
6000 fusion->pcie_bw_limitation = true;
6003 fusion->r56_div_offload = true;
6009 /* Check if MSI-X is supported while in ready state */
6010 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
6012 if (msix_enable && !msix_disable) {
6014 scratch_pad_1 = megasas_readl
6015 (instance, &instance->reg_set->outbound_scratch_pad_1);
6016 /* Check max MSI-X vectors */
6018 if (instance->adapter_type == THUNDERBOLT_SERIES) {
6019 /* Thunderbolt Series*/
6020 instance->msix_vectors = (scratch_pad_1
6021 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
6023 instance->msix_vectors = ((scratch_pad_1
6024 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
6025 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
6028 * For Invader series, > 8 MSI-x vectors
6029 * supported by FW/HW implies combined
6030 * reply queue mode is enabled.
6031 * For Ventura series, > 16 MSI-x vectors
6032 * supported by FW/HW implies combined
6033 * reply queue mode is enabled.
6035 switch (instance->adapter_type) {
6036 case INVADER_SERIES:
6037 if (instance->msix_vectors > 8)
6038 instance->msix_combined = true;
6041 case VENTURA_SERIES:
6042 if (instance->msix_vectors > 16)
6043 instance->msix_combined = true;
6048 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
6051 if (instance->adapter_type >= INVADER_SERIES &&
6052 !instance->msix_combined) {
6053 instance->msix_load_balance = true;
6054 instance->smp_affinity_enable = false;
6057 /* Save 1-15 reply post index address to local memory
6058 * Index 0 is already saved from reg offset
6059 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
6061 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
6062 instance->reply_post_host_index_addr[loop] =
6064 ((u8 __iomem *)instance->reg_set +
6065 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
6070 dev_info(&instance->pdev->dev,
6071 "firmware supports msix\t: (%d)",
6072 instance->msix_vectors);
6074 instance->msix_vectors = min(msix_vectors,
6075 instance->msix_vectors);
6076 } else /* MFI adapters */
6077 instance->msix_vectors = 1;
6081 * For Aero (if some conditions are met), driver will configure a
6082 * few additional reply queues with interrupt coalescing enabled.
6083 * These queues with interrupt coalescing enabled are called
6084 * High IOPS queues and rest of reply queues (based on number of
6085 * logical CPUs) are termed as Low latency queues.
6087 * Total Number of reply queues = High IOPS queues + low latency queues
6089 * For rest of fusion adapters, 1 additional reply queue will be
6090 * reserved for management commands, rest of reply queues
6091 * (based on number of logical CPUs) will be used for IOs and
6092 * referenced as IO queues.
6093 * Total Number of reply queues = 1 + IO queues
6095 * MFI adapters supports single MSI-x so single reply queue
6096 * will be used for IO and management commands.
6099 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
6101 if (intr_coalescing &&
6102 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6103 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
6104 instance->perf_mode = MR_BALANCED_PERF_MODE;
6106 instance->perf_mode = MR_LATENCY_PERF_MODE;
6109 if (instance->adapter_type == AERO_SERIES) {
6110 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6111 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6114 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6115 * in latency perf mode and enable R1 PCI bandwidth algorithm
6118 instance->perf_mode = MR_LATENCY_PERF_MODE;
6119 fusion->pcie_bw_limitation = true;
6123 * Performance mode settings provided through module parameter-perf_mode will
6124 * take affect only for:
6125 * 1. Aero family of adapters.
6126 * 2. When user sets module parameter- perf_mode in range of 0-2.
6128 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6129 (perf_mode <= MR_LATENCY_PERF_MODE))
6130 instance->perf_mode = perf_mode;
6132 * If intr coalescing is not supported by controller FW, then IOPS
6133 * and Balanced modes are not feasible.
6135 if (!intr_coalescing)
6136 instance->perf_mode = MR_LATENCY_PERF_MODE;
6140 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
6141 instance->low_latency_index_start =
6142 MR_HIGH_IOPS_QUEUE_COUNT;
6144 instance->low_latency_index_start = 1;
6146 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6148 instance->msix_vectors = min(num_msix_req,
6149 instance->msix_vectors);
6151 megasas_alloc_irq_vectors(instance);
6152 if (!instance->msix_vectors)
6153 instance->msix_load_balance = false;
6156 * MSI-X host index 0 is common for all adapter.
6157 * It is used for all MPT based Adapters.
6159 if (instance->msix_combined) {
6160 instance->reply_post_host_index_addr[0] =
6161 (u32 *)((u8 *)instance->reg_set +
6162 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6164 instance->reply_post_host_index_addr[0] =
6165 (u32 *)((u8 *)instance->reg_set +
6166 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6169 if (!instance->msix_vectors) {
6170 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6172 goto fail_init_adapter;
6175 megasas_setup_reply_map(instance);
6177 dev_info(&instance->pdev->dev,
6178 "current msix/online cpus\t: (%d/%d)\n",
6179 instance->msix_vectors, (unsigned int)num_online_cpus());
6180 dev_info(&instance->pdev->dev,
6181 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
6183 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6184 (unsigned long)instance);
6187 * Below are default value for legacy Firmware.
6188 * non-fusion based controllers
6190 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6191 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
6192 /* Get operational params, sge flags, send init cmd to controller */
6193 if (instance->instancet->init_adapter(instance))
6194 goto fail_init_adapter;
6196 if (instance->adapter_type >= VENTURA_SERIES) {
6198 megasas_readl(instance,
6199 &instance->reg_set->outbound_scratch_pad_3);
6200 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
6201 MR_DEFAULT_NVME_PAGE_SHIFT)
6202 instance->nvme_page_size =
6203 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
6205 dev_info(&instance->pdev->dev,
6206 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6209 if (instance->msix_vectors ?
6210 megasas_setup_irqs_msix(instance, 1) :
6211 megasas_setup_irqs_ioapic(instance))
6212 goto fail_init_adapter;
6214 if (instance->adapter_type != MFI_SERIES)
6215 megasas_setup_irq_poll(instance);
6217 instance->instancet->enable_intr(instance);
6219 dev_info(&instance->pdev->dev, "INIT adapter done\n");
6221 megasas_setup_jbod_map(instance);
6223 if (megasas_get_device_list(instance) != SUCCESS) {
6224 dev_err(&instance->pdev->dev,
6225 "%s: megasas_get_device_list failed\n",
6227 goto fail_get_ld_pd_list;
6230 /* stream detection initialization */
6231 if (instance->adapter_type >= VENTURA_SERIES) {
6232 fusion->stream_detect_by_ld =
6233 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6234 sizeof(struct LD_STREAM_DETECT *),
6236 if (!fusion->stream_detect_by_ld) {
6237 dev_err(&instance->pdev->dev,
6238 "unable to allocate stream detection for pool of LDs\n");
6239 goto fail_get_ld_pd_list;
6241 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6242 fusion->stream_detect_by_ld[i] =
6243 kzalloc(sizeof(struct LD_STREAM_DETECT),
6245 if (!fusion->stream_detect_by_ld[i]) {
6246 dev_err(&instance->pdev->dev,
6247 "unable to allocate stream detect by LD\n ");
6248 for (j = 0; j < i; ++j)
6249 kfree(fusion->stream_detect_by_ld[j]);
6250 kfree(fusion->stream_detect_by_ld);
6251 fusion->stream_detect_by_ld = NULL;
6252 goto fail_get_ld_pd_list;
6254 fusion->stream_detect_by_ld[i]->mru_bit_map
6260 * Compute the max allowed sectors per IO: The controller info has two
6261 * limits on max sectors. Driver should use the minimum of these two.
6263 * 1 << stripe_sz_ops.min = max sectors per strip
6265 * Note that older firmwares ( < FW ver 30) didn't report information
6266 * to calculate max_sectors_1. So the number ended up as zero always.
6269 ctrl_info = instance->ctrl_info_buf;
6271 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6272 le16_to_cpu(ctrl_info->max_strips_per_io);
6273 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
6275 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
6277 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6278 instance->passive = ctrl_info->cluster.passive;
6279 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
6280 instance->UnevenSpanSupport =
6281 ctrl_info->adapterOperations2.supportUnevenSpans;
6282 if (instance->UnevenSpanSupport) {
6283 struct fusion_context *fusion = instance->ctrl_context;
6284 if (MR_ValidateMapInfo(instance, instance->map_id))
6285 fusion->fast_path_io = 1;
6287 fusion->fast_path_io = 0;
6290 if (ctrl_info->host_interface.SRIOV) {
6291 instance->requestorId = ctrl_info->iov.requestorId;
6292 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6293 if (!ctrl_info->adapterOperations2.activePassive)
6294 instance->PlasmaFW111 = 1;
6296 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6297 instance->PlasmaFW111 ? "1.11" : "new");
6299 if (instance->PlasmaFW111) {
6300 iovPtr = (struct IOV_111 *)
6301 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6302 instance->requestorId = iovPtr->requestorId;
6305 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6306 instance->requestorId);
6309 instance->crash_dump_fw_support =
6310 ctrl_info->adapterOperations3.supportCrashDump;
6311 instance->crash_dump_drv_support =
6312 (instance->crash_dump_fw_support &&
6313 instance->crash_dump_buf);
6314 if (instance->crash_dump_drv_support)
6315 megasas_set_crash_dump_params(instance,
6316 MR_CRASH_BUF_TURN_OFF);
6319 if (instance->crash_dump_buf)
6320 dma_free_coherent(&instance->pdev->dev,
6322 instance->crash_dump_buf,
6323 instance->crash_dump_h);
6324 instance->crash_dump_buf = NULL;
6327 if (instance->snapdump_wait_time) {
6328 megasas_get_snapdump_properties(instance);
6329 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6330 instance->snapdump_wait_time);
6333 dev_info(&instance->pdev->dev,
6334 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6335 le16_to_cpu(ctrl_info->pci.vendor_id),
6336 le16_to_cpu(ctrl_info->pci.device_id),
6337 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6338 le16_to_cpu(ctrl_info->pci.sub_device_id));
6339 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6340 instance->UnevenSpanSupport ? "yes" : "no");
6341 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6342 instance->crash_dump_drv_support ? "yes" : "no");
6343 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6344 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
6346 instance->max_sectors_per_req = instance->max_num_sge *
6347 SGE_BUFFER_SIZE / 512;
6348 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6349 instance->max_sectors_per_req = tmp_sectors;
6351 /* Check for valid throttlequeuedepth module parameter */
6352 if (throttlequeuedepth &&
6353 throttlequeuedepth <= instance->max_scsi_cmds)
6354 instance->throttlequeuedepth = throttlequeuedepth;
6356 instance->throttlequeuedepth =
6357 MEGASAS_THROTTLE_QUEUE_DEPTH;
6359 if ((resetwaittime < 1) ||
6360 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6361 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6363 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6364 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6366 /* Launch SR-IOV heartbeat timer */
6367 if (instance->requestorId) {
6368 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6369 megasas_start_timer(instance);
6371 instance->skip_heartbeat_timer_del = 1;
6372 goto fail_get_ld_pd_list;
6377 * Create and start watchdog thread which will monitor
6378 * controller state every 1 sec and trigger OCR when
6379 * it enters fault state
6381 if (instance->adapter_type != MFI_SERIES)
6382 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6383 goto fail_start_watchdog;
6387 fail_start_watchdog:
6388 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6389 del_timer_sync(&instance->sriov_heartbeat_timer);
6390 fail_get_ld_pd_list:
6391 instance->instancet->disable_intr(instance);
6392 megasas_destroy_irqs(instance);
6394 if (instance->msix_vectors)
6395 pci_free_irq_vectors(instance->pdev);
6396 instance->msix_vectors = 0;
6398 megasas_free_ctrl_dma_buffers(instance);
6399 megasas_free_ctrl_mem(instance);
6401 iounmap(instance->reg_set);
6404 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6406 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6407 __func__, __LINE__);
6412 * megasas_release_mfi - Reverses the FW initialization
6413 * @instance: Adapter soft state
6415 static void megasas_release_mfi(struct megasas_instance *instance)
6417 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6419 if (instance->reply_queue)
6420 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6421 instance->reply_queue, instance->reply_queue_h);
6423 megasas_free_cmds(instance);
6425 iounmap(instance->reg_set);
6427 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6431 * megasas_get_seq_num - Gets latest event sequence numbers
6432 * @instance: Adapter soft state
6433 * @eli: FW event log sequence numbers information
6435 * FW maintains a log of all events in a non-volatile area. Upper layers would
6436 * usually find out the latest sequence number of the events, the seq number at
6437 * the boot etc. They would "read" all the events below the latest seq number
6438 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6439 * number), they would subsribe to AEN (asynchronous event notification) and
6440 * wait for the events to happen.
6443 megasas_get_seq_num(struct megasas_instance *instance,
6444 struct megasas_evt_log_info *eli)
6446 struct megasas_cmd *cmd;
6447 struct megasas_dcmd_frame *dcmd;
6448 struct megasas_evt_log_info *el_info;
6449 dma_addr_t el_info_h = 0;
6452 cmd = megasas_get_cmd(instance);
6458 dcmd = &cmd->frame->dcmd;
6459 el_info = dma_alloc_coherent(&instance->pdev->dev,
6460 sizeof(struct megasas_evt_log_info),
6461 &el_info_h, GFP_KERNEL);
6463 megasas_return_cmd(instance, cmd);
6467 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6469 dcmd->cmd = MFI_CMD_DCMD;
6470 dcmd->cmd_status = 0x0;
6471 dcmd->sge_count = 1;
6472 dcmd->flags = MFI_FRAME_DIR_READ;
6475 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6476 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6478 megasas_set_dma_settings(instance, dcmd, el_info_h,
6479 sizeof(struct megasas_evt_log_info));
6481 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6482 if (ret != DCMD_SUCCESS) {
6483 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6484 __func__, __LINE__);
6489 * Copy the data back into callers buffer
6491 eli->newest_seq_num = el_info->newest_seq_num;
6492 eli->oldest_seq_num = el_info->oldest_seq_num;
6493 eli->clear_seq_num = el_info->clear_seq_num;
6494 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6495 eli->boot_seq_num = el_info->boot_seq_num;
6498 dma_free_coherent(&instance->pdev->dev,
6499 sizeof(struct megasas_evt_log_info),
6500 el_info, el_info_h);
6502 megasas_return_cmd(instance, cmd);
6508 * megasas_register_aen - Registers for asynchronous event notification
6509 * @instance: Adapter soft state
6510 * @seq_num: The starting sequence number
6511 * @class_locale_word: Class of the event
6513 * This function subscribes for AEN for events beyond the @seq_num. It requests
6514 * to be notified if and only if the event is of type @class_locale
6517 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6518 u32 class_locale_word)
6521 struct megasas_cmd *cmd;
6522 struct megasas_dcmd_frame *dcmd;
6523 union megasas_evt_class_locale curr_aen;
6524 union megasas_evt_class_locale prev_aen;
6527 * If there an AEN pending already (aen_cmd), check if the
6528 * class_locale of that pending AEN is inclusive of the new
6529 * AEN request we currently have. If it is, then we don't have
6530 * to do anything. In other words, whichever events the current
6531 * AEN request is subscribing to, have already been subscribed
6534 * If the old_cmd is _not_ inclusive, then we have to abort
6535 * that command, form a class_locale that is superset of both
6536 * old and current and re-issue to the FW
6539 curr_aen.word = class_locale_word;
6541 if (instance->aen_cmd) {
6544 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6546 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6547 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6548 dev_info(&instance->pdev->dev,
6549 "%s %d out of range class %d send by application\n",
6550 __func__, __LINE__, curr_aen.members.class);
6555 * A class whose enum value is smaller is inclusive of all
6556 * higher values. If a PROGRESS (= -1) was previously
6557 * registered, then a new registration requests for higher
6558 * classes need not be sent to FW. They are automatically
6561 * Locale numbers don't have such hierarchy. They are bitmap
6564 if ((prev_aen.members.class <= curr_aen.members.class) &&
6565 !((prev_aen.members.locale & curr_aen.members.locale) ^
6566 curr_aen.members.locale)) {
6568 * Previously issued event registration includes
6569 * current request. Nothing to do.
6573 curr_aen.members.locale |= prev_aen.members.locale;
6575 if (prev_aen.members.class < curr_aen.members.class)
6576 curr_aen.members.class = prev_aen.members.class;
6578 instance->aen_cmd->abort_aen = 1;
6579 ret_val = megasas_issue_blocked_abort_cmd(instance,
6584 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6585 "previous AEN command\n");
6591 cmd = megasas_get_cmd(instance);
6596 dcmd = &cmd->frame->dcmd;
6598 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6601 * Prepare DCMD for aen registration
6603 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6605 dcmd->cmd = MFI_CMD_DCMD;
6606 dcmd->cmd_status = 0x0;
6607 dcmd->sge_count = 1;
6608 dcmd->flags = MFI_FRAME_DIR_READ;
6611 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6612 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6613 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6614 instance->last_seq_num = seq_num;
6615 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6617 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6618 sizeof(struct megasas_evt_detail));
6620 if (instance->aen_cmd != NULL) {
6621 megasas_return_cmd(instance, cmd);
6626 * Store reference to the cmd used to register for AEN. When an
6627 * application wants us to register for AEN, we have to abort this
6628 * cmd and re-register with a new EVENT LOCALE supplied by that app
6630 instance->aen_cmd = cmd;
6633 * Issue the aen registration frame
6635 instance->instancet->issue_dcmd(instance, cmd);
6640 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6642 * This DCMD will fetch few properties of LD/system PD defined
6643 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6645 * DCMD send by drivers whenever new target is added to the OS.
6647 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6648 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6649 * 0 = system PD, 1 = LD.
6650 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6651 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6653 * @instance: Adapter soft state
6654 * @sdev: OS provided scsi device
6656 * Returns 0 on success non-zero on failure.
6659 megasas_get_target_prop(struct megasas_instance *instance,
6660 struct scsi_device *sdev)
6663 struct megasas_cmd *cmd;
6664 struct megasas_dcmd_frame *dcmd;
6665 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6668 cmd = megasas_get_cmd(instance);
6671 dev_err(&instance->pdev->dev,
6672 "Failed to get cmd %s\n", __func__);
6676 dcmd = &cmd->frame->dcmd;
6678 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6679 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6680 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6682 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6683 dcmd->cmd = MFI_CMD_DCMD;
6684 dcmd->cmd_status = 0xFF;
6685 dcmd->sge_count = 1;
6686 dcmd->flags = MFI_FRAME_DIR_READ;
6689 dcmd->data_xfer_len =
6690 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6691 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6693 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6694 sizeof(struct MR_TARGET_PROPERTIES));
6696 if ((instance->adapter_type != MFI_SERIES) &&
6697 !instance->mask_interrupts)
6698 ret = megasas_issue_blocked_cmd(instance,
6699 cmd, MFI_IO_TIMEOUT_SECS);
6701 ret = megasas_issue_polled(instance, cmd);
6705 switch (dcmd_timeout_ocr_possible(instance)) {
6707 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6708 mutex_unlock(&instance->reset_mutex);
6709 megasas_reset_fusion(instance->host,
6710 MFI_IO_TIMEOUT_OCR);
6711 mutex_lock(&instance->reset_mutex);
6714 megaraid_sas_kill_hba(instance);
6716 case IGNORE_TIMEOUT:
6717 dev_info(&instance->pdev->dev,
6718 "Ignore DCMD timeout: %s %d\n",
6719 __func__, __LINE__);
6725 megasas_return_cmd(instance, cmd);
6727 if (ret != DCMD_SUCCESS)
6728 dev_err(&instance->pdev->dev,
6729 "return from %s %d return value %d\n",
6730 __func__, __LINE__, ret);
6736 * megasas_start_aen - Subscribes to AEN during driver load time
6737 * @instance: Adapter soft state
6739 static int megasas_start_aen(struct megasas_instance *instance)
6741 struct megasas_evt_log_info eli;
6742 union megasas_evt_class_locale class_locale;
6745 * Get the latest sequence number from FW
6747 memset(&eli, 0, sizeof(eli));
6749 if (megasas_get_seq_num(instance, &eli))
6753 * Register AEN with FW for latest sequence number plus 1
6755 class_locale.members.reserved = 0;
6756 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6757 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6759 return megasas_register_aen(instance,
6760 le32_to_cpu(eli.newest_seq_num) + 1,
6765 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6766 * @instance: Adapter soft state
6768 static int megasas_io_attach(struct megasas_instance *instance)
6770 struct Scsi_Host *host = instance->host;
6773 * Export parameters required by SCSI mid-layer
6775 host->unique_id = instance->unique_id;
6776 host->can_queue = instance->max_scsi_cmds;
6777 host->this_id = instance->init_id;
6778 host->sg_tablesize = instance->max_num_sge;
6780 if (instance->fw_support_ieee)
6781 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6784 * Check if the module parameter value for max_sectors can be used
6786 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6787 instance->max_sectors_per_req = max_sectors;
6790 if (((instance->pdev->device ==
6791 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6792 (instance->pdev->device ==
6793 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6794 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6795 instance->max_sectors_per_req = max_sectors;
6797 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6798 "and <= %d (or < 1MB for GEN2 controller)\n",
6799 instance->max_sectors_per_req);
6804 host->max_sectors = instance->max_sectors_per_req;
6805 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6806 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6807 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6808 host->max_lun = MEGASAS_MAX_LUN;
6809 host->max_cmd_len = 16;
6812 * Notify the mid-layer about the new controller
6814 if (scsi_add_host(host, &instance->pdev->dev)) {
6815 dev_err(&instance->pdev->dev,
6816 "Failed to add host from %s %d\n",
6817 __func__, __LINE__);
6825 * megasas_set_dma_mask - Set DMA mask for supported controllers
6827 * @instance: Adapter soft state
6830 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6833 * By default, driver/FW will operate in 32bit DMA addresses
6834 * for consistent DMA mapping but if 32 bit consistent
6835 * DMA mask fails, driver will try with 63 bit consistent
6836 * mask provided FW is true 63bit DMA capable
6838 * For older controllers(Thunderbolt and MFI based adapters)-
6839 * driver/FW will operate in 32 bit consistent DMA addresses.
6842 megasas_set_dma_mask(struct megasas_instance *instance)
6844 u64 consistent_mask;
6845 struct pci_dev *pdev;
6848 pdev = instance->pdev;
6849 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6850 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6853 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6854 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6855 goto fail_set_dma_mask;
6857 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6858 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6859 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6861 * If 32 bit DMA mask fails, then try for 64 bit mask
6862 * for FW capable of handling 64 bit DMA.
6864 scratch_pad_1 = megasas_readl
6865 (instance, &instance->reg_set->outbound_scratch_pad_1);
6867 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6868 goto fail_set_dma_mask;
6869 else if (dma_set_mask_and_coherent(&pdev->dev,
6871 goto fail_set_dma_mask;
6873 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6874 goto fail_set_dma_mask;
6876 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6877 instance->consistent_mask_64bit = false;
6879 instance->consistent_mask_64bit = true;
6881 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6882 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6883 (instance->consistent_mask_64bit ? "63" : "32"));
6888 dev_err(&pdev->dev, "Failed to set DMA mask\n");
6894 * megasas_set_adapter_type - Set adapter type.
6895 * Supported controllers can be divided in
6896 * different categories-
6897 * enum MR_ADAPTER_TYPE {
6899 * THUNDERBOLT_SERIES = 2,
6900 * INVADER_SERIES = 3,
6901 * VENTURA_SERIES = 4,
6904 * @instance: Adapter soft state
6907 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6909 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6910 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6911 instance->adapter_type = MFI_SERIES;
6913 switch (instance->pdev->device) {
6914 case PCI_DEVICE_ID_LSI_AERO_10E1:
6915 case PCI_DEVICE_ID_LSI_AERO_10E2:
6916 case PCI_DEVICE_ID_LSI_AERO_10E5:
6917 case PCI_DEVICE_ID_LSI_AERO_10E6:
6918 instance->adapter_type = AERO_SERIES;
6920 case PCI_DEVICE_ID_LSI_VENTURA:
6921 case PCI_DEVICE_ID_LSI_CRUSADER:
6922 case PCI_DEVICE_ID_LSI_HARPOON:
6923 case PCI_DEVICE_ID_LSI_TOMCAT:
6924 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6925 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6926 instance->adapter_type = VENTURA_SERIES;
6928 case PCI_DEVICE_ID_LSI_FUSION:
6929 case PCI_DEVICE_ID_LSI_PLASMA:
6930 instance->adapter_type = THUNDERBOLT_SERIES;
6932 case PCI_DEVICE_ID_LSI_INVADER:
6933 case PCI_DEVICE_ID_LSI_INTRUDER:
6934 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6935 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6936 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6937 case PCI_DEVICE_ID_LSI_FURY:
6938 instance->adapter_type = INVADER_SERIES;
6940 default: /* For all other supported controllers */
6941 instance->adapter_type = MFI_SERIES;
6947 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6949 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6950 sizeof(u32), &instance->producer_h, GFP_KERNEL);
6951 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6952 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
6954 if (!instance->producer || !instance->consumer) {
6955 dev_err(&instance->pdev->dev,
6956 "Failed to allocate memory for producer, consumer\n");
6960 *instance->producer = 0;
6961 *instance->consumer = 0;
6966 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
6967 * structures which are not common across MFI
6968 * adapters and fusion adapters.
6969 * For MFI based adapters, allocate producer and
6970 * consumer buffers. For fusion adapters, allocate
6971 * memory for fusion context.
6972 * @instance: Adapter soft state
6973 * return: 0 for SUCCESS
6975 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6977 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
6979 if (!instance->reply_map)
6982 switch (instance->adapter_type) {
6984 if (megasas_alloc_mfi_ctrl_mem(instance))
6988 case VENTURA_SERIES:
6989 case THUNDERBOLT_SERIES:
6990 case INVADER_SERIES:
6991 if (megasas_alloc_fusion_context(instance))
6998 kfree(instance->reply_map);
6999 instance->reply_map = NULL;
7004 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
7005 * producer, consumer buffers for MFI adapters
7007 * @instance - Adapter soft instance
7010 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
7012 kfree(instance->reply_map);
7013 if (instance->adapter_type == MFI_SERIES) {
7014 if (instance->producer)
7015 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7017 instance->producer_h);
7018 if (instance->consumer)
7019 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7021 instance->consumer_h);
7023 megasas_free_fusion_context(instance);
7028 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
7031 * @instance: Adapter soft instance
7033 * @return: O for SUCCESS
7036 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
7038 struct pci_dev *pdev = instance->pdev;
7039 struct fusion_context *fusion = instance->ctrl_context;
7041 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
7042 sizeof(struct megasas_evt_detail),
7043 &instance->evt_detail_h, GFP_KERNEL);
7045 if (!instance->evt_detail) {
7046 dev_err(&instance->pdev->dev,
7047 "Failed to allocate event detail buffer\n");
7052 fusion->ioc_init_request =
7053 dma_alloc_coherent(&pdev->dev,
7054 sizeof(struct MPI2_IOC_INIT_REQUEST),
7055 &fusion->ioc_init_request_phys,
7058 if (!fusion->ioc_init_request) {
7060 "Failed to allocate PD list buffer\n");
7064 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
7065 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7066 &instance->snapdump_prop_h, GFP_KERNEL);
7068 if (!instance->snapdump_prop)
7070 "Failed to allocate snapdump properties buffer\n");
7072 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
7073 HOST_DEVICE_LIST_SZ,
7074 &instance->host_device_list_buf_h,
7077 if (!instance->host_device_list_buf) {
7079 "Failed to allocate targetid list buffer\n");
7085 instance->pd_list_buf =
7086 dma_alloc_coherent(&pdev->dev,
7087 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7088 &instance->pd_list_buf_h, GFP_KERNEL);
7090 if (!instance->pd_list_buf) {
7091 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
7095 instance->ctrl_info_buf =
7096 dma_alloc_coherent(&pdev->dev,
7097 sizeof(struct megasas_ctrl_info),
7098 &instance->ctrl_info_buf_h, GFP_KERNEL);
7100 if (!instance->ctrl_info_buf) {
7102 "Failed to allocate controller info buffer\n");
7106 instance->ld_list_buf =
7107 dma_alloc_coherent(&pdev->dev,
7108 sizeof(struct MR_LD_LIST),
7109 &instance->ld_list_buf_h, GFP_KERNEL);
7111 if (!instance->ld_list_buf) {
7112 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7116 instance->ld_targetid_list_buf =
7117 dma_alloc_coherent(&pdev->dev,
7118 sizeof(struct MR_LD_TARGETID_LIST),
7119 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
7121 if (!instance->ld_targetid_list_buf) {
7123 "Failed to allocate LD targetid list buffer\n");
7127 if (!reset_devices) {
7128 instance->system_info_buf =
7129 dma_alloc_coherent(&pdev->dev,
7130 sizeof(struct MR_DRV_SYSTEM_INFO),
7131 &instance->system_info_h, GFP_KERNEL);
7133 dma_alloc_coherent(&pdev->dev,
7134 sizeof(struct MR_PD_INFO),
7135 &instance->pd_info_h, GFP_KERNEL);
7136 instance->tgt_prop =
7137 dma_alloc_coherent(&pdev->dev,
7138 sizeof(struct MR_TARGET_PROPERTIES),
7139 &instance->tgt_prop_h, GFP_KERNEL);
7140 instance->crash_dump_buf =
7141 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7142 &instance->crash_dump_h, GFP_KERNEL);
7144 if (!instance->system_info_buf)
7145 dev_err(&instance->pdev->dev,
7146 "Failed to allocate system info buffer\n");
7148 if (!instance->pd_info)
7149 dev_err(&instance->pdev->dev,
7150 "Failed to allocate pd_info buffer\n");
7152 if (!instance->tgt_prop)
7153 dev_err(&instance->pdev->dev,
7154 "Failed to allocate tgt_prop buffer\n");
7156 if (!instance->crash_dump_buf)
7157 dev_err(&instance->pdev->dev,
7158 "Failed to allocate crash dump buffer\n");
7165 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
7166 * during driver load time
7168 * @instance- Adapter soft instance
7172 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7174 struct pci_dev *pdev = instance->pdev;
7175 struct fusion_context *fusion = instance->ctrl_context;
7177 if (instance->evt_detail)
7178 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
7179 instance->evt_detail,
7180 instance->evt_detail_h);
7182 if (fusion && fusion->ioc_init_request)
7183 dma_free_coherent(&pdev->dev,
7184 sizeof(struct MPI2_IOC_INIT_REQUEST),
7185 fusion->ioc_init_request,
7186 fusion->ioc_init_request_phys);
7188 if (instance->pd_list_buf)
7189 dma_free_coherent(&pdev->dev,
7190 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7191 instance->pd_list_buf,
7192 instance->pd_list_buf_h);
7194 if (instance->ld_list_buf)
7195 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
7196 instance->ld_list_buf,
7197 instance->ld_list_buf_h);
7199 if (instance->ld_targetid_list_buf)
7200 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
7201 instance->ld_targetid_list_buf,
7202 instance->ld_targetid_list_buf_h);
7204 if (instance->ctrl_info_buf)
7205 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
7206 instance->ctrl_info_buf,
7207 instance->ctrl_info_buf_h);
7209 if (instance->system_info_buf)
7210 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
7211 instance->system_info_buf,
7212 instance->system_info_h);
7214 if (instance->pd_info)
7215 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
7216 instance->pd_info, instance->pd_info_h);
7218 if (instance->tgt_prop)
7219 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
7220 instance->tgt_prop, instance->tgt_prop_h);
7222 if (instance->crash_dump_buf)
7223 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7224 instance->crash_dump_buf,
7225 instance->crash_dump_h);
7227 if (instance->snapdump_prop)
7228 dma_free_coherent(&pdev->dev,
7229 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7230 instance->snapdump_prop,
7231 instance->snapdump_prop_h);
7233 if (instance->host_device_list_buf)
7234 dma_free_coherent(&pdev->dev,
7235 HOST_DEVICE_LIST_SZ,
7236 instance->host_device_list_buf,
7237 instance->host_device_list_buf_h);
7242 * megasas_init_ctrl_params - Initialize controller's instance
7243 * parameters before FW init
7244 * @instance - Adapter soft instance
7247 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7249 instance->fw_crash_state = UNAVAILABLE;
7251 megasas_poll_wait_aen = 0;
7252 instance->issuepend_done = 1;
7253 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7256 * Initialize locks and queues
7258 INIT_LIST_HEAD(&instance->cmd_pool);
7259 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7261 atomic_set(&instance->fw_outstanding, 0);
7262 atomic64_set(&instance->total_io_count, 0);
7264 init_waitqueue_head(&instance->int_cmd_wait_q);
7265 init_waitqueue_head(&instance->abort_cmd_wait_q);
7267 spin_lock_init(&instance->crashdump_lock);
7268 spin_lock_init(&instance->mfi_pool_lock);
7269 spin_lock_init(&instance->hba_lock);
7270 spin_lock_init(&instance->stream_lock);
7271 spin_lock_init(&instance->completion_lock);
7273 mutex_init(&instance->reset_mutex);
7275 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7276 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7277 instance->flag_ieee = 1;
7279 megasas_dbg_lvl = 0;
7281 instance->unload = 1;
7282 instance->last_time = 0;
7283 instance->disableOnlineCtrlReset = 1;
7284 instance->UnevenSpanSupport = 0;
7285 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7286 instance->msix_load_balance = false;
7288 if (instance->adapter_type != MFI_SERIES)
7289 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7291 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7295 * megasas_probe_one - PCI hotplug entry point
7296 * @pdev: PCI device structure
7297 * @id: PCI ids of supported hotplugged adapter
7299 static int megasas_probe_one(struct pci_dev *pdev,
7300 const struct pci_device_id *id)
7303 struct Scsi_Host *host;
7304 struct megasas_instance *instance;
7307 switch (pdev->device) {
7308 case PCI_DEVICE_ID_LSI_AERO_10E0:
7309 case PCI_DEVICE_ID_LSI_AERO_10E3:
7310 case PCI_DEVICE_ID_LSI_AERO_10E4:
7311 case PCI_DEVICE_ID_LSI_AERO_10E7:
7312 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7314 case PCI_DEVICE_ID_LSI_AERO_10E1:
7315 case PCI_DEVICE_ID_LSI_AERO_10E5:
7316 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7320 /* Reset MSI-X in the kdump kernel */
7321 if (reset_devices) {
7322 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7324 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7326 if (control & PCI_MSIX_FLAGS_ENABLE) {
7327 dev_info(&pdev->dev, "resetting MSI-X\n");
7328 pci_write_config_word(pdev,
7329 pos + PCI_MSIX_FLAGS,
7331 ~PCI_MSIX_FLAGS_ENABLE);
7337 * PCI prepping: enable device set bus mastering and dma mask
7339 rval = pci_enable_device_mem(pdev);
7345 pci_set_master(pdev);
7347 host = scsi_host_alloc(&megasas_template,
7348 sizeof(struct megasas_instance));
7351 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7352 goto fail_alloc_instance;
7355 instance = (struct megasas_instance *)host->hostdata;
7356 memset(instance, 0, sizeof(*instance));
7357 atomic_set(&instance->fw_reset_no_pci_access, 0);
7360 * Initialize PCI related and misc parameters
7362 instance->pdev = pdev;
7363 instance->host = host;
7364 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7365 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7367 megasas_set_adapter_type(instance);
7370 * Initialize MFI Firmware
7372 if (megasas_init_fw(instance))
7375 if (instance->requestorId) {
7376 if (instance->PlasmaFW111) {
7377 instance->vf_affiliation_111 =
7378 dma_alloc_coherent(&pdev->dev,
7379 sizeof(struct MR_LD_VF_AFFILIATION_111),
7380 &instance->vf_affiliation_111_h,
7382 if (!instance->vf_affiliation_111)
7383 dev_warn(&pdev->dev, "Can't allocate "
7384 "memory for VF affiliation buffer\n");
7386 instance->vf_affiliation =
7387 dma_alloc_coherent(&pdev->dev,
7388 (MAX_LOGICAL_DRIVES + 1) *
7389 sizeof(struct MR_LD_VF_AFFILIATION),
7390 &instance->vf_affiliation_h,
7392 if (!instance->vf_affiliation)
7393 dev_warn(&pdev->dev, "Can't allocate "
7394 "memory for VF affiliation buffer\n");
7399 * Store instance in PCI softstate
7401 pci_set_drvdata(pdev, instance);
7404 * Add this controller to megasas_mgmt_info structure so that it
7405 * can be exported to management applications
7407 megasas_mgmt_info.count++;
7408 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7409 megasas_mgmt_info.max_index++;
7412 * Register with SCSI mid-layer
7414 if (megasas_io_attach(instance))
7415 goto fail_io_attach;
7417 instance->unload = 0;
7419 * Trigger SCSI to scan our drives
7421 if (!instance->enable_fw_dev_list ||
7422 (instance->host_device_list_buf->count > 0))
7423 scsi_scan_host(host);
7426 * Initiate AEN (Asynchronous Event Notification)
7428 if (megasas_start_aen(instance)) {
7429 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7430 goto fail_start_aen;
7433 megasas_setup_debugfs(instance);
7435 /* Get current SR-IOV LD/VF affiliation */
7436 if (instance->requestorId)
7437 megasas_get_ld_vf_affiliation(instance, 1);
7443 megasas_mgmt_info.count--;
7444 megasas_mgmt_info.max_index--;
7445 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7447 instance->instancet->disable_intr(instance);
7448 megasas_destroy_irqs(instance);
7450 if (instance->adapter_type != MFI_SERIES)
7451 megasas_release_fusion(instance);
7453 megasas_release_mfi(instance);
7454 if (instance->msix_vectors)
7455 pci_free_irq_vectors(instance->pdev);
7457 scsi_host_put(host);
7458 fail_alloc_instance:
7459 pci_disable_device(pdev);
7465 * megasas_flush_cache - Requests FW to flush all its caches
7466 * @instance: Adapter soft state
7468 static void megasas_flush_cache(struct megasas_instance *instance)
7470 struct megasas_cmd *cmd;
7471 struct megasas_dcmd_frame *dcmd;
7473 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7476 cmd = megasas_get_cmd(instance);
7481 dcmd = &cmd->frame->dcmd;
7483 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7485 dcmd->cmd = MFI_CMD_DCMD;
7486 dcmd->cmd_status = 0x0;
7487 dcmd->sge_count = 0;
7488 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7491 dcmd->data_xfer_len = 0;
7492 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7493 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7495 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7497 dev_err(&instance->pdev->dev,
7498 "return from %s %d\n", __func__, __LINE__);
7502 megasas_return_cmd(instance, cmd);
7506 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7507 * @instance: Adapter soft state
7508 * @opcode: Shutdown/Hibernate
7510 static void megasas_shutdown_controller(struct megasas_instance *instance,
7513 struct megasas_cmd *cmd;
7514 struct megasas_dcmd_frame *dcmd;
7516 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7519 cmd = megasas_get_cmd(instance);
7524 if (instance->aen_cmd)
7525 megasas_issue_blocked_abort_cmd(instance,
7526 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7527 if (instance->map_update_cmd)
7528 megasas_issue_blocked_abort_cmd(instance,
7529 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7530 if (instance->jbod_seq_cmd)
7531 megasas_issue_blocked_abort_cmd(instance,
7532 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7534 dcmd = &cmd->frame->dcmd;
7536 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7538 dcmd->cmd = MFI_CMD_DCMD;
7539 dcmd->cmd_status = 0x0;
7540 dcmd->sge_count = 0;
7541 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7544 dcmd->data_xfer_len = 0;
7545 dcmd->opcode = cpu_to_le32(opcode);
7547 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7549 dev_err(&instance->pdev->dev,
7550 "return from %s %d\n", __func__, __LINE__);
7554 megasas_return_cmd(instance, cmd);
7559 * megasas_suspend - driver suspend entry point
7560 * @pdev: PCI device structure
7561 * @state: PCI power state to suspend routine
7564 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7566 struct megasas_instance *instance;
7568 instance = pci_get_drvdata(pdev);
7573 instance->unload = 1;
7575 dev_info(&pdev->dev, "%s is called\n", __func__);
7577 /* Shutdown SR-IOV heartbeat timer */
7578 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7579 del_timer_sync(&instance->sriov_heartbeat_timer);
7581 /* Stop the FW fault detection watchdog */
7582 if (instance->adapter_type != MFI_SERIES)
7583 megasas_fusion_stop_watchdog(instance);
7585 megasas_flush_cache(instance);
7586 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7588 /* cancel the delayed work if this work still in queue */
7589 if (instance->ev != NULL) {
7590 struct megasas_aen_event *ev = instance->ev;
7591 cancel_delayed_work_sync(&ev->hotplug_work);
7592 instance->ev = NULL;
7595 tasklet_kill(&instance->isr_tasklet);
7597 pci_set_drvdata(instance->pdev, instance);
7598 instance->instancet->disable_intr(instance);
7600 megasas_destroy_irqs(instance);
7602 if (instance->msix_vectors)
7603 pci_free_irq_vectors(instance->pdev);
7605 pci_save_state(pdev);
7606 pci_disable_device(pdev);
7608 pci_set_power_state(pdev, pci_choose_state(pdev, state));
7614 * megasas_resume- driver resume entry point
7615 * @pdev: PCI device structure
7618 megasas_resume(struct pci_dev *pdev)
7621 struct Scsi_Host *host;
7622 struct megasas_instance *instance;
7625 instance = pci_get_drvdata(pdev);
7630 host = instance->host;
7631 pci_set_power_state(pdev, PCI_D0);
7632 pci_enable_wake(pdev, PCI_D0, 0);
7633 pci_restore_state(pdev);
7635 dev_info(&pdev->dev, "%s is called\n", __func__);
7637 * PCI prepping: enable device set bus mastering and dma mask
7639 rval = pci_enable_device_mem(pdev);
7642 dev_err(&pdev->dev, "Enable device failed\n");
7646 pci_set_master(pdev);
7649 * We expect the FW state to be READY
7652 if (megasas_transition_to_ready(instance, 0)) {
7653 dev_info(&instance->pdev->dev,
7654 "Failed to transition controller to ready from %s!\n",
7656 if (instance->adapter_type != MFI_SERIES) {
7658 instance->instancet->read_fw_status_reg(instance);
7659 if (!(status_reg & MFI_RESET_ADAPTER) ||
7660 ((megasas_adp_reset_wait_for_ready
7661 (instance, true, 0)) == FAILED))
7662 goto fail_ready_state;
7664 atomic_set(&instance->fw_reset_no_pci_access, 1);
7665 instance->instancet->adp_reset
7666 (instance, instance->reg_set);
7667 atomic_set(&instance->fw_reset_no_pci_access, 0);
7669 /* waiting for about 30 seconds before retry */
7672 if (megasas_transition_to_ready(instance, 0))
7673 goto fail_ready_state;
7676 dev_info(&instance->pdev->dev,
7677 "FW restarted successfully from %s!\n",
7680 if (megasas_set_dma_mask(instance))
7681 goto fail_set_dma_mask;
7684 * Initialize MFI Firmware
7687 atomic_set(&instance->fw_outstanding, 0);
7688 atomic_set(&instance->ldio_outstanding, 0);
7690 /* Now re-enable MSI-X */
7691 if (instance->msix_vectors)
7692 megasas_alloc_irq_vectors(instance);
7694 if (!instance->msix_vectors) {
7695 rval = pci_alloc_irq_vectors(instance->pdev, 1, 1,
7698 goto fail_reenable_msix;
7701 megasas_setup_reply_map(instance);
7703 if (instance->adapter_type != MFI_SERIES) {
7704 megasas_reset_reply_desc(instance);
7705 if (megasas_ioc_init_fusion(instance)) {
7706 megasas_free_cmds(instance);
7707 megasas_free_cmds_fusion(instance);
7710 if (!megasas_get_map_info(instance))
7711 megasas_sync_map_info(instance);
7713 *instance->producer = 0;
7714 *instance->consumer = 0;
7715 if (megasas_issue_init_mfi(instance))
7719 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7722 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7723 (unsigned long)instance);
7725 if (instance->msix_vectors ?
7726 megasas_setup_irqs_msix(instance, 0) :
7727 megasas_setup_irqs_ioapic(instance))
7730 if (instance->adapter_type != MFI_SERIES)
7731 megasas_setup_irq_poll(instance);
7733 /* Re-launch SR-IOV heartbeat timer */
7734 if (instance->requestorId) {
7735 if (!megasas_sriov_start_heartbeat(instance, 0))
7736 megasas_start_timer(instance);
7738 instance->skip_heartbeat_timer_del = 1;
7743 instance->instancet->enable_intr(instance);
7744 megasas_setup_jbod_map(instance);
7745 instance->unload = 0;
7748 * Initiate AEN (Asynchronous Event Notification)
7750 if (megasas_start_aen(instance))
7751 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7753 /* Re-launch FW fault watchdog */
7754 if (instance->adapter_type != MFI_SERIES)
7755 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7756 goto fail_start_watchdog;
7760 fail_start_watchdog:
7761 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7762 del_timer_sync(&instance->sriov_heartbeat_timer);
7764 megasas_free_ctrl_dma_buffers(instance);
7765 megasas_free_ctrl_mem(instance);
7766 scsi_host_put(host);
7772 pci_disable_device(pdev);
7777 #define megasas_suspend NULL
7778 #define megasas_resume NULL
7782 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7784 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7788 for (i = 0; i < wait_time; i++) {
7789 adp_state = atomic_read(&instance->adprecovery);
7790 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7791 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7794 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7795 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7800 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7801 dev_info(&instance->pdev->dev,
7802 "%s HBA failed to become operational, adp_state %d\n",
7803 __func__, adp_state);
7811 * megasas_detach_one - PCI hot"un"plug entry point
7812 * @pdev: PCI device structure
7814 static void megasas_detach_one(struct pci_dev *pdev)
7817 struct Scsi_Host *host;
7818 struct megasas_instance *instance;
7819 struct fusion_context *fusion;
7822 instance = pci_get_drvdata(pdev);
7827 host = instance->host;
7828 fusion = instance->ctrl_context;
7830 /* Shutdown SR-IOV heartbeat timer */
7831 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7832 del_timer_sync(&instance->sriov_heartbeat_timer);
7834 /* Stop the FW fault detection watchdog */
7835 if (instance->adapter_type != MFI_SERIES)
7836 megasas_fusion_stop_watchdog(instance);
7838 if (instance->fw_crash_state != UNAVAILABLE)
7839 megasas_free_host_crash_buffer(instance);
7840 scsi_remove_host(instance->host);
7841 instance->unload = 1;
7843 if (megasas_wait_for_adapter_operational(instance))
7844 goto skip_firing_dcmds;
7846 megasas_flush_cache(instance);
7847 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7850 /* cancel the delayed work if this work still in queue*/
7851 if (instance->ev != NULL) {
7852 struct megasas_aen_event *ev = instance->ev;
7853 cancel_delayed_work_sync(&ev->hotplug_work);
7854 instance->ev = NULL;
7857 /* cancel all wait events */
7858 wake_up_all(&instance->int_cmd_wait_q);
7860 tasklet_kill(&instance->isr_tasklet);
7863 * Take the instance off the instance array. Note that we will not
7864 * decrement the max_index. We let this array be sparse array
7866 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7867 if (megasas_mgmt_info.instance[i] == instance) {
7868 megasas_mgmt_info.count--;
7869 megasas_mgmt_info.instance[i] = NULL;
7875 instance->instancet->disable_intr(instance);
7877 megasas_destroy_irqs(instance);
7879 if (instance->msix_vectors)
7880 pci_free_irq_vectors(instance->pdev);
7882 if (instance->adapter_type >= VENTURA_SERIES) {
7883 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7884 kfree(fusion->stream_detect_by_ld[i]);
7885 kfree(fusion->stream_detect_by_ld);
7886 fusion->stream_detect_by_ld = NULL;
7890 if (instance->adapter_type != MFI_SERIES) {
7891 megasas_release_fusion(instance);
7892 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7893 (sizeof(struct MR_PD_CFG_SEQ) *
7894 (MAX_PHYSICAL_DEVICES - 1));
7895 for (i = 0; i < 2 ; i++) {
7896 if (fusion->ld_map[i])
7897 dma_free_coherent(&instance->pdev->dev,
7900 fusion->ld_map_phys[i]);
7901 if (fusion->ld_drv_map[i]) {
7902 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7903 vfree(fusion->ld_drv_map[i]);
7905 free_pages((ulong)fusion->ld_drv_map[i],
7906 fusion->drv_map_pages);
7909 if (fusion->pd_seq_sync[i])
7910 dma_free_coherent(&instance->pdev->dev,
7912 fusion->pd_seq_sync[i],
7913 fusion->pd_seq_phys[i]);
7916 megasas_release_mfi(instance);
7919 if (instance->vf_affiliation)
7920 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7921 sizeof(struct MR_LD_VF_AFFILIATION),
7922 instance->vf_affiliation,
7923 instance->vf_affiliation_h);
7925 if (instance->vf_affiliation_111)
7926 dma_free_coherent(&pdev->dev,
7927 sizeof(struct MR_LD_VF_AFFILIATION_111),
7928 instance->vf_affiliation_111,
7929 instance->vf_affiliation_111_h);
7931 if (instance->hb_host_mem)
7932 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
7933 instance->hb_host_mem,
7934 instance->hb_host_mem_h);
7936 megasas_free_ctrl_dma_buffers(instance);
7938 megasas_free_ctrl_mem(instance);
7940 megasas_destroy_debugfs(instance);
7942 scsi_host_put(host);
7944 pci_disable_device(pdev);
7948 * megasas_shutdown - Shutdown entry point
7949 * @pdev: Generic device structure
7951 static void megasas_shutdown(struct pci_dev *pdev)
7953 struct megasas_instance *instance = pci_get_drvdata(pdev);
7958 instance->unload = 1;
7960 if (megasas_wait_for_adapter_operational(instance))
7961 goto skip_firing_dcmds;
7963 megasas_flush_cache(instance);
7964 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7967 instance->instancet->disable_intr(instance);
7968 megasas_destroy_irqs(instance);
7970 if (instance->msix_vectors)
7971 pci_free_irq_vectors(instance->pdev);
7975 * megasas_mgmt_open - char node "open" entry point
7976 * @inode: char node inode
7977 * @filep: char node file
7979 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7982 * Allow only those users with admin rights
7984 if (!capable(CAP_SYS_ADMIN))
7991 * megasas_mgmt_fasync - Async notifier registration from applications
7992 * @fd: char node file descriptor number
7993 * @filep: char node file
7994 * @mode: notifier on/off
7996 * This function adds the calling process to a driver global queue. When an
7997 * event occurs, SIGIO will be sent to all processes in this queue.
7999 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
8003 mutex_lock(&megasas_async_queue_mutex);
8005 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
8007 mutex_unlock(&megasas_async_queue_mutex);
8010 /* For sanity check when we get ioctl */
8011 filep->private_data = filep;
8015 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
8021 * megasas_mgmt_poll - char node "poll" entry point
8022 * @filep: char node file
8023 * @wait: Events to poll for
8025 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
8028 unsigned long flags;
8030 poll_wait(file, &megasas_poll_wait, wait);
8031 spin_lock_irqsave(&poll_aen_lock, flags);
8032 if (megasas_poll_wait_aen)
8033 mask = (EPOLLIN | EPOLLRDNORM);
8036 megasas_poll_wait_aen = 0;
8037 spin_unlock_irqrestore(&poll_aen_lock, flags);
8042 * megasas_set_crash_dump_params_ioctl:
8043 * Send CRASH_DUMP_MODE DCMD to all controllers
8044 * @cmd: MFI command frame
8047 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
8049 struct megasas_instance *local_instance;
8053 crash_support = cmd->frame->dcmd.mbox.w[0];
8055 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8056 local_instance = megasas_mgmt_info.instance[i];
8057 if (local_instance && local_instance->crash_dump_drv_support) {
8058 if ((atomic_read(&local_instance->adprecovery) ==
8059 MEGASAS_HBA_OPERATIONAL) &&
8060 !megasas_set_crash_dump_params(local_instance,
8062 local_instance->crash_dump_app_support =
8064 dev_info(&local_instance->pdev->dev,
8065 "Application firmware crash "
8066 "dump mode set success\n");
8069 dev_info(&local_instance->pdev->dev,
8070 "Application firmware crash "
8071 "dump mode set failed\n");
8080 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
8081 * @instance: Adapter soft state
8082 * @user_ioc: User's ioctl packet
8083 * @ioc: ioctl packet
8086 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
8087 struct megasas_iocpacket __user * user_ioc,
8088 struct megasas_iocpacket *ioc)
8090 struct megasas_sge64 *kern_sge64 = NULL;
8091 struct megasas_sge32 *kern_sge32 = NULL;
8092 struct megasas_cmd *cmd;
8093 void *kbuff_arr[MAX_IOCTL_SGE];
8094 dma_addr_t buf_handle = 0;
8097 dma_addr_t sense_handle;
8098 unsigned long *sense_ptr;
8100 int ret = DCMD_SUCCESS;
8102 memset(kbuff_arr, 0, sizeof(kbuff_arr));
8104 if (ioc->sge_count > MAX_IOCTL_SGE) {
8105 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
8106 ioc->sge_count, MAX_IOCTL_SGE);
8110 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
8111 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
8112 !instance->support_nvme_passthru) ||
8113 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
8114 !instance->support_pci_lane_margining)) {
8115 dev_err(&instance->pdev->dev,
8116 "Received invalid ioctl command 0x%x\n",
8117 ioc->frame.hdr.cmd);
8121 cmd = megasas_get_cmd(instance);
8123 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
8128 * User's IOCTL packet has 2 frames (maximum). Copy those two
8129 * frames into our cmd's frames. cmd->frame's context will get
8130 * overwritten when we copy from user's frames. So set that value
8133 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
8134 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
8135 cmd->frame->hdr.pad_0 = 0;
8137 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8139 if (instance->consistent_mask_64bit)
8140 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8141 MFI_FRAME_SENSE64));
8143 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
8144 MFI_FRAME_SENSE64));
8146 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8147 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
8149 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
8150 mutex_lock(&instance->reset_mutex);
8151 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8152 megasas_return_cmd(instance, cmd);
8153 mutex_unlock(&instance->reset_mutex);
8156 mutex_unlock(&instance->reset_mutex);
8159 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
8160 error = megasas_set_crash_dump_params_ioctl(cmd);
8161 megasas_return_cmd(instance, cmd);
8166 * The management interface between applications and the fw uses
8167 * MFI frames. E.g, RAID configuration changes, LD property changes
8168 * etc are accomplishes through different kinds of MFI frames. The
8169 * driver needs to care only about substituting user buffers with
8170 * kernel buffers in SGLs. The location of SGL is embedded in the
8171 * struct iocpacket itself.
8173 if (instance->consistent_mask_64bit)
8174 kern_sge64 = (struct megasas_sge64 *)
8175 ((unsigned long)cmd->frame + ioc->sgl_off);
8177 kern_sge32 = (struct megasas_sge32 *)
8178 ((unsigned long)cmd->frame + ioc->sgl_off);
8181 * For each user buffer, create a mirror buffer and copy in
8183 for (i = 0; i < ioc->sge_count; i++) {
8184 if (!ioc->sgl[i].iov_len)
8187 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
8188 ioc->sgl[i].iov_len,
8189 &buf_handle, GFP_KERNEL);
8190 if (!kbuff_arr[i]) {
8191 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8192 "kernel SGL buffer for IOCTL\n");
8198 * We don't change the dma_coherent_mask, so
8199 * dma_alloc_coherent only returns 32bit addresses
8201 if (instance->consistent_mask_64bit) {
8202 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8203 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8205 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8206 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8210 * We created a kernel buffer corresponding to the
8211 * user buffer. Now copy in from the user buffer
8213 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8214 (u32) (ioc->sgl[i].iov_len))) {
8220 if (ioc->sense_len) {
8221 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8222 &sense_handle, GFP_KERNEL);
8229 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
8230 if (instance->consistent_mask_64bit)
8231 *sense_ptr = cpu_to_le64(sense_handle);
8233 *sense_ptr = cpu_to_le32(sense_handle);
8237 * Set the sync_cmd flag so that the ISR knows not to complete this
8238 * cmd to the SCSI mid-layer
8242 ret = megasas_issue_blocked_cmd(instance, cmd, 0);
8247 dev_err(&instance->pdev->dev,
8248 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8249 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8250 cmd->cmd_status_drv);
8257 if (instance->unload == 1) {
8258 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8259 "don't submit data to application\n");
8263 * copy out the kernel buffers to user buffers
8265 for (i = 0; i < ioc->sge_count; i++) {
8266 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8267 ioc->sgl[i].iov_len)) {
8274 * copy out the sense
8276 if (ioc->sense_len) {
8278 * sense_ptr points to the location that has the user
8279 * sense buffer address
8281 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8284 if (copy_to_user((void __user *)((unsigned long)
8285 get_unaligned((unsigned long *)sense_ptr)),
8286 sense, ioc->sense_len)) {
8287 dev_err(&instance->pdev->dev, "Failed to copy out to user "
8295 * copy the status codes returned by the fw
8297 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8298 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
8299 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
8305 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
8306 sense, sense_handle);
8309 for (i = 0; i < ioc->sge_count; i++) {
8311 if (instance->consistent_mask_64bit)
8312 dma_free_coherent(&instance->pdev->dev,
8313 le32_to_cpu(kern_sge64[i].length),
8315 le64_to_cpu(kern_sge64[i].phys_addr));
8317 dma_free_coherent(&instance->pdev->dev,
8318 le32_to_cpu(kern_sge32[i].length),
8320 le32_to_cpu(kern_sge32[i].phys_addr));
8321 kbuff_arr[i] = NULL;
8325 megasas_return_cmd(instance, cmd);
8329 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8331 struct megasas_iocpacket __user *user_ioc =
8332 (struct megasas_iocpacket __user *)arg;
8333 struct megasas_iocpacket *ioc;
8334 struct megasas_instance *instance;
8337 ioc = memdup_user(user_ioc, sizeof(*ioc));
8339 return PTR_ERR(ioc);
8341 instance = megasas_lookup_instance(ioc->host_no);
8347 /* Block ioctls in VF mode */
8348 if (instance->requestorId && !allow_vf_ioctls) {
8353 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8354 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8359 if (instance->unload == 1) {
8364 if (down_interruptible(&instance->ioctl_sem)) {
8365 error = -ERESTARTSYS;
8369 if (megasas_wait_for_adapter_operational(instance)) {
8374 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8376 up(&instance->ioctl_sem);
8383 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8385 struct megasas_instance *instance;
8386 struct megasas_aen aen;
8389 if (file->private_data != file) {
8390 printk(KERN_DEBUG "megasas: fasync_helper was not "
8395 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8398 instance = megasas_lookup_instance(aen.host_no);
8403 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8407 if (instance->unload == 1) {
8411 if (megasas_wait_for_adapter_operational(instance))
8414 mutex_lock(&instance->reset_mutex);
8415 error = megasas_register_aen(instance, aen.seq_num,
8416 aen.class_locale_word);
8417 mutex_unlock(&instance->reset_mutex);
8422 * megasas_mgmt_ioctl - char node ioctl entry point
8423 * @file: char device file pointer
8424 * @cmd: ioctl command
8425 * @arg: ioctl command arguments address
8428 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8431 case MEGASAS_IOC_FIRMWARE:
8432 return megasas_mgmt_ioctl_fw(file, arg);
8434 case MEGASAS_IOC_GET_AEN:
8435 return megasas_mgmt_ioctl_aen(file, arg);
8441 #ifdef CONFIG_COMPAT
8442 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8444 struct compat_megasas_iocpacket __user *cioc =
8445 (struct compat_megasas_iocpacket __user *)arg;
8446 struct megasas_iocpacket __user *ioc =
8447 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8451 u32 local_sense_off;
8452 u32 local_sense_len;
8455 if (clear_user(ioc, sizeof(*ioc)))
8458 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8459 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8460 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8461 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8462 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8463 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8467 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8468 * sense_len is not null, so prepare the 64bit value under
8469 * the same condition.
8471 if (get_user(local_sense_off, &ioc->sense_off) ||
8472 get_user(local_sense_len, &ioc->sense_len) ||
8473 get_user(user_sense_off, &cioc->sense_off))
8476 if (local_sense_off != user_sense_off)
8479 if (local_sense_len) {
8480 void __user **sense_ioc_ptr =
8481 (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
8482 compat_uptr_t *sense_cioc_ptr =
8483 (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
8484 if (get_user(ptr, sense_cioc_ptr) ||
8485 put_user(compat_ptr(ptr), sense_ioc_ptr))
8489 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8490 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8491 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8492 copy_in_user(&ioc->sgl[i].iov_len,
8493 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8497 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8499 if (copy_in_user(&cioc->frame.hdr.cmd_status,
8500 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8501 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8508 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8512 case MEGASAS_IOC_FIRMWARE32:
8513 return megasas_mgmt_compat_ioctl_fw(file, arg);
8514 case MEGASAS_IOC_GET_AEN:
8515 return megasas_mgmt_ioctl_aen(file, arg);
8523 * File operations structure for management interface
8525 static const struct file_operations megasas_mgmt_fops = {
8526 .owner = THIS_MODULE,
8527 .open = megasas_mgmt_open,
8528 .fasync = megasas_mgmt_fasync,
8529 .unlocked_ioctl = megasas_mgmt_ioctl,
8530 .poll = megasas_mgmt_poll,
8531 #ifdef CONFIG_COMPAT
8532 .compat_ioctl = megasas_mgmt_compat_ioctl,
8534 .llseek = noop_llseek,
8538 * PCI hotplug support registration structure
8540 static struct pci_driver megasas_pci_driver = {
8542 .name = "megaraid_sas",
8543 .id_table = megasas_pci_table,
8544 .probe = megasas_probe_one,
8545 .remove = megasas_detach_one,
8546 .suspend = megasas_suspend,
8547 .resume = megasas_resume,
8548 .shutdown = megasas_shutdown,
8552 * Sysfs driver attributes
8554 static ssize_t version_show(struct device_driver *dd, char *buf)
8556 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8559 static DRIVER_ATTR_RO(version);
8561 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8563 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8566 static DRIVER_ATTR_RO(release_date);
8568 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8570 return sprintf(buf, "%u\n", support_poll_for_event);
8572 static DRIVER_ATTR_RO(support_poll_for_event);
8574 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8576 return sprintf(buf, "%u\n", support_device_change);
8578 static DRIVER_ATTR_RO(support_device_change);
8580 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8582 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8585 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8590 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8591 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8596 static DRIVER_ATTR_RW(dbg_lvl);
8599 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8601 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8604 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8607 support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8609 return sprintf(buf, "%u\n", support_pci_lane_margining);
8612 static DRIVER_ATTR_RO(support_pci_lane_margining);
8614 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8616 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8617 scsi_remove_device(sdev);
8618 scsi_device_put(sdev);
8622 * megasas_update_device_list - Update the PD and LD device list from FW
8623 * after an AEN event notification
8624 * @instance: Adapter soft state
8625 * @event_type: Indicates type of event (PD or LD event)
8627 * @return: Success or failure
8629 * Issue DCMDs to Firmware to update the internal device list in driver.
8630 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8631 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8634 int megasas_update_device_list(struct megasas_instance *instance,
8637 int dcmd_ret = DCMD_SUCCESS;
8639 if (instance->enable_fw_dev_list) {
8640 dcmd_ret = megasas_host_device_list_query(instance, false);
8641 if (dcmd_ret != DCMD_SUCCESS)
8644 if (event_type & SCAN_PD_CHANNEL) {
8645 dcmd_ret = megasas_get_pd_list(instance);
8647 if (dcmd_ret != DCMD_SUCCESS)
8651 if (event_type & SCAN_VD_CHANNEL) {
8652 if (!instance->requestorId ||
8653 (instance->requestorId &&
8654 megasas_get_ld_vf_affiliation(instance, 0))) {
8655 dcmd_ret = megasas_ld_list_query(instance,
8656 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8657 if (dcmd_ret != DCMD_SUCCESS)
8668 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8669 * after an AEN event notification
8670 * @instance: Adapter soft state
8671 * @scan_type: Indicates type of devices (PD/LD) to add
8675 void megasas_add_remove_devices(struct megasas_instance *instance,
8681 u16 channel = 0, id = 0;
8682 struct Scsi_Host *host;
8683 struct scsi_device *sdev1;
8684 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8685 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8687 host = instance->host;
8689 if (instance->enable_fw_dev_list) {
8690 targetid_list = instance->host_device_list_buf;
8691 for (i = 0; i < targetid_list->count; i++) {
8692 targetid_entry = &targetid_list->host_device_list[i];
8693 if (targetid_entry->flags.u.bits.is_sys_pd) {
8694 channel = le16_to_cpu(targetid_entry->target_id) /
8695 MEGASAS_MAX_DEV_PER_CHANNEL;
8696 id = le16_to_cpu(targetid_entry->target_id) %
8697 MEGASAS_MAX_DEV_PER_CHANNEL;
8699 channel = MEGASAS_MAX_PD_CHANNELS +
8700 (le16_to_cpu(targetid_entry->target_id) /
8701 MEGASAS_MAX_DEV_PER_CHANNEL);
8702 id = le16_to_cpu(targetid_entry->target_id) %
8703 MEGASAS_MAX_DEV_PER_CHANNEL;
8705 sdev1 = scsi_device_lookup(host, channel, id, 0);
8707 scsi_add_device(host, channel, id, 0);
8709 scsi_device_put(sdev1);
8714 if (scan_type & SCAN_PD_CHANNEL) {
8715 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8716 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8717 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8718 sdev1 = scsi_device_lookup(host, i, j, 0);
8719 if (instance->pd_list[pd_index].driveState ==
8720 MR_PD_STATE_SYSTEM) {
8722 scsi_add_device(host, i, j, 0);
8724 scsi_device_put(sdev1);
8727 megasas_remove_scsi_device(sdev1);
8733 if (scan_type & SCAN_VD_CHANNEL) {
8734 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8735 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8736 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8737 sdev1 = scsi_device_lookup(host,
8738 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8739 if (instance->ld_ids[ld_index] != 0xff) {
8741 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8743 scsi_device_put(sdev1);
8746 megasas_remove_scsi_device(sdev1);
8755 megasas_aen_polling(struct work_struct *work)
8757 struct megasas_aen_event *ev =
8758 container_of(work, struct megasas_aen_event, hotplug_work.work);
8759 struct megasas_instance *instance = ev->instance;
8760 union megasas_evt_class_locale class_locale;
8764 u8 dcmd_ret = DCMD_SUCCESS;
8767 printk(KERN_ERR "invalid instance!\n");
8772 /* Don't run the event workqueue thread if OCR is running */
8773 mutex_lock(&instance->reset_mutex);
8775 instance->ev = NULL;
8776 if (instance->evt_detail) {
8777 megasas_decode_evt(instance);
8779 switch (le32_to_cpu(instance->evt_detail->code)) {
8781 case MR_EVT_PD_INSERTED:
8782 case MR_EVT_PD_REMOVED:
8783 event_type = SCAN_PD_CHANNEL;
8786 case MR_EVT_LD_OFFLINE:
8787 case MR_EVT_CFG_CLEARED:
8788 case MR_EVT_LD_DELETED:
8789 case MR_EVT_LD_CREATED:
8790 event_type = SCAN_VD_CHANNEL;
8793 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8794 case MR_EVT_FOREIGN_CFG_IMPORTED:
8795 case MR_EVT_LD_STATE_CHANGE:
8796 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8797 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8798 instance->host->host_no);
8801 case MR_EVT_CTRL_PROP_CHANGED:
8802 dcmd_ret = megasas_get_ctrl_info(instance);
8803 if (dcmd_ret == DCMD_SUCCESS &&
8804 instance->snapdump_wait_time) {
8805 megasas_get_snapdump_properties(instance);
8806 dev_info(&instance->pdev->dev,
8807 "Snap dump wait time\t: %d\n",
8808 instance->snapdump_wait_time);
8816 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8817 mutex_unlock(&instance->reset_mutex);
8823 dcmd_ret = megasas_update_device_list(instance, event_type);
8825 mutex_unlock(&instance->reset_mutex);
8827 if (event_type && dcmd_ret == DCMD_SUCCESS)
8828 megasas_add_remove_devices(instance, event_type);
8830 if (dcmd_ret == DCMD_SUCCESS)
8831 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8833 seq_num = instance->last_seq_num;
8835 /* Register AEN with FW for latest sequence number plus 1 */
8836 class_locale.members.reserved = 0;
8837 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8838 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8840 if (instance->aen_cmd != NULL) {
8845 mutex_lock(&instance->reset_mutex);
8846 error = megasas_register_aen(instance, seq_num,
8849 dev_err(&instance->pdev->dev,
8850 "register aen failed error %x\n", error);
8852 mutex_unlock(&instance->reset_mutex);
8857 * megasas_init - Driver load entry point
8859 static int __init megasas_init(void)
8864 * Booted in kdump kernel, minimize memory footprints by
8865 * disabling few features
8867 if (reset_devices) {
8870 dual_qdepth_disable = 1;
8874 * Announce driver version and other information
8876 pr_info("megasas: %s\n", MEGASAS_VERSION);
8878 spin_lock_init(&poll_aen_lock);
8880 support_poll_for_event = 2;
8881 support_device_change = 1;
8882 support_nvme_encapsulation = true;
8883 support_pci_lane_margining = true;
8885 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8888 * Register character device node
8890 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8893 printk(KERN_DEBUG "megasas: failed to open device node\n");
8897 megasas_mgmt_majorno = rval;
8899 megasas_init_debugfs();
8902 * Register ourselves as PCI hotplug module
8904 rval = pci_register_driver(&megasas_pci_driver);
8907 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8911 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
8912 (event_log_level > MFI_EVT_CLASS_DEAD)) {
8913 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");
8914 event_log_level = MFI_EVT_CLASS_CRITICAL;
8917 rval = driver_create_file(&megasas_pci_driver.driver,
8918 &driver_attr_version);
8920 goto err_dcf_attr_ver;
8922 rval = driver_create_file(&megasas_pci_driver.driver,
8923 &driver_attr_release_date);
8925 goto err_dcf_rel_date;
8927 rval = driver_create_file(&megasas_pci_driver.driver,
8928 &driver_attr_support_poll_for_event);
8930 goto err_dcf_support_poll_for_event;
8932 rval = driver_create_file(&megasas_pci_driver.driver,
8933 &driver_attr_dbg_lvl);
8935 goto err_dcf_dbg_lvl;
8936 rval = driver_create_file(&megasas_pci_driver.driver,
8937 &driver_attr_support_device_change);
8939 goto err_dcf_support_device_change;
8941 rval = driver_create_file(&megasas_pci_driver.driver,
8942 &driver_attr_support_nvme_encapsulation);
8944 goto err_dcf_support_nvme_encapsulation;
8946 rval = driver_create_file(&megasas_pci_driver.driver,
8947 &driver_attr_support_pci_lane_margining);
8949 goto err_dcf_support_pci_lane_margining;
8953 err_dcf_support_pci_lane_margining:
8954 driver_remove_file(&megasas_pci_driver.driver,
8955 &driver_attr_support_nvme_encapsulation);
8957 err_dcf_support_nvme_encapsulation:
8958 driver_remove_file(&megasas_pci_driver.driver,
8959 &driver_attr_support_device_change);
8961 err_dcf_support_device_change:
8962 driver_remove_file(&megasas_pci_driver.driver,
8963 &driver_attr_dbg_lvl);
8965 driver_remove_file(&megasas_pci_driver.driver,
8966 &driver_attr_support_poll_for_event);
8967 err_dcf_support_poll_for_event:
8968 driver_remove_file(&megasas_pci_driver.driver,
8969 &driver_attr_release_date);
8971 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8973 pci_unregister_driver(&megasas_pci_driver);
8975 megasas_exit_debugfs();
8976 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8981 * megasas_exit - Driver unload entry point
8983 static void __exit megasas_exit(void)
8985 driver_remove_file(&megasas_pci_driver.driver,
8986 &driver_attr_dbg_lvl);
8987 driver_remove_file(&megasas_pci_driver.driver,
8988 &driver_attr_support_poll_for_event);
8989 driver_remove_file(&megasas_pci_driver.driver,
8990 &driver_attr_support_device_change);
8991 driver_remove_file(&megasas_pci_driver.driver,
8992 &driver_attr_release_date);
8993 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8994 driver_remove_file(&megasas_pci_driver.driver,
8995 &driver_attr_support_nvme_encapsulation);
8996 driver_remove_file(&megasas_pci_driver.driver,
8997 &driver_attr_support_pci_lane_margining);
8999 pci_unregister_driver(&megasas_pci_driver);
9000 megasas_exit_debugfs();
9001 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9004 module_init(megasas_init);
9005 module_exit(megasas_exit);