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.
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>
40 #include <linux/blk-mq-pci.h>
42 #include <scsi/scsi.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
46 #include <scsi/scsi_tcq.h>
47 #include <scsi/scsi_dbg.h>
48 #include "megaraid_sas_fusion.h"
49 #include "megaraid_sas.h"
52 * Number of sectors per IO command
53 * Will be set in megasas_init_mfi if user does not provide
55 static unsigned int max_sectors;
56 module_param_named(max_sectors, max_sectors, int, 0444);
57 MODULE_PARM_DESC(max_sectors,
58 "Maximum number of sectors per IO command");
60 static int msix_disable;
61 module_param(msix_disable, int, 0444);
62 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
64 static unsigned int msix_vectors;
65 module_param(msix_vectors, int, 0444);
66 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
68 static int allow_vf_ioctls;
69 module_param(allow_vf_ioctls, int, 0444);
70 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
72 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
73 module_param(throttlequeuedepth, int, 0444);
74 MODULE_PARM_DESC(throttlequeuedepth,
75 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
77 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
78 module_param(resetwaittime, int, 0444);
79 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
81 static int smp_affinity_enable = 1;
82 module_param(smp_affinity_enable, int, 0444);
83 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
85 static int rdpq_enable = 1;
86 module_param(rdpq_enable, int, 0444);
87 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
89 unsigned int dual_qdepth_disable;
90 module_param(dual_qdepth_disable, int, 0444);
91 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
93 static unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
94 module_param(scmd_timeout, int, 0444);
95 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
98 module_param(perf_mode, int, 0444);
99 MODULE_PARM_DESC(perf_mode, "Performance mode (only for Aero adapters), options:\n\t\t"
100 "0 - balanced: High iops and low latency queues are allocated &\n\t\t"
101 "interrupt coalescing is enabled only on high iops queues\n\t\t"
102 "1 - iops: High iops queues are not allocated &\n\t\t"
103 "interrupt coalescing is enabled on all queues\n\t\t"
104 "2 - latency: High iops queues are not allocated &\n\t\t"
105 "interrupt coalescing is disabled on all queues\n\t\t"
106 "default mode is 'balanced'"
109 int event_log_level = MFI_EVT_CLASS_CRITICAL;
110 module_param(event_log_level, int, 0644);
111 MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
113 unsigned int enable_sdev_max_qd;
114 module_param(enable_sdev_max_qd, int, 0444);
115 MODULE_PARM_DESC(enable_sdev_max_qd, "Enable sdev max qd as can_queue. Default: 0");
118 module_param(poll_queues, int, 0444);
119 MODULE_PARM_DESC(poll_queues, "Number of queues to be use for io_uring poll mode.\n\t\t"
120 "This parameter is effective only if host_tagset_enable=1 &\n\t\t"
121 "It is not applicable for MFI_SERIES. &\n\t\t"
122 "Driver will work in latency mode. &\n\t\t"
123 "High iops queues are not allocated &\n\t\t"
126 int host_tagset_enable = 1;
127 module_param(host_tagset_enable, int, 0444);
128 MODULE_PARM_DESC(host_tagset_enable, "Shared host tagset enable/disable Default: enable(1)");
130 MODULE_LICENSE("GPL");
131 MODULE_VERSION(MEGASAS_VERSION);
133 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
135 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
136 static int megasas_get_pd_list(struct megasas_instance *instance);
137 static int megasas_ld_list_query(struct megasas_instance *instance,
139 static int megasas_issue_init_mfi(struct megasas_instance *instance);
140 static int megasas_register_aen(struct megasas_instance *instance,
141 u32 seq_num, u32 class_locale_word);
142 static void megasas_get_pd_info(struct megasas_instance *instance,
143 struct scsi_device *sdev);
145 megasas_set_ld_removed_by_fw(struct megasas_instance *instance);
148 * PCI ID table for all supported controllers
150 static struct pci_device_id megasas_pci_table[] = {
152 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
154 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
156 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
158 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
160 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
162 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
164 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
166 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
167 /* xscale IOP, vega */
168 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
170 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
172 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
174 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
176 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
178 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
180 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
181 /* Intruder 24 port*/
182 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
183 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
185 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
186 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
187 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
188 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
189 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
190 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
191 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
192 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
193 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
194 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
195 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
196 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
197 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
198 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
202 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
204 static int megasas_mgmt_majorno;
205 struct megasas_mgmt_info megasas_mgmt_info;
206 static struct fasync_struct *megasas_async_queue;
207 static DEFINE_MUTEX(megasas_async_queue_mutex);
209 static int megasas_poll_wait_aen;
210 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
211 static u32 support_poll_for_event;
213 static u32 support_device_change;
214 static bool support_nvme_encapsulation;
215 static bool support_pci_lane_margining;
217 /* define lock for aen poll */
218 static DEFINE_SPINLOCK(poll_aen_lock);
220 extern struct dentry *megasas_debugfs_root;
221 extern int megasas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num);
224 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
227 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
229 megasas_adp_reset_gen2(struct megasas_instance *instance,
230 struct megasas_register_set __iomem *reg_set);
231 static irqreturn_t megasas_isr(int irq, void *devp);
233 megasas_init_adapter_mfi(struct megasas_instance *instance);
235 megasas_build_and_issue_cmd(struct megasas_instance *instance,
236 struct scsi_cmnd *scmd);
237 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
239 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
241 void megasas_fusion_ocr_wq(struct work_struct *work);
242 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
245 megasas_set_dma_mask(struct megasas_instance *instance);
247 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
249 megasas_free_ctrl_mem(struct megasas_instance *instance);
251 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
253 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
255 megasas_init_ctrl_params(struct megasas_instance *instance);
257 u32 megasas_readl(struct megasas_instance *instance,
258 const volatile void __iomem *addr)
262 * Due to a HW errata in Aero controllers, reads to certain
263 * Fusion registers could intermittently return all zeroes.
264 * This behavior is transient in nature and subsequent reads will
265 * return valid value. As a workaround in driver, retry readl for
266 * upto three times until a non-zero value is read.
268 if (instance->adapter_type == AERO_SERIES) {
270 ret_val = readl(addr);
272 } while (ret_val == 0 && i < 3);
280 * megasas_set_dma_settings - Populate DMA address, length and flags for DCMDs
281 * @instance: Adapter soft state
282 * @dcmd: DCMD frame inside MFI command
283 * @dma_addr: DMA address of buffer to be passed to FW
284 * @dma_len: Length of DMA buffer to be passed to FW
287 void megasas_set_dma_settings(struct megasas_instance *instance,
288 struct megasas_dcmd_frame *dcmd,
289 dma_addr_t dma_addr, u32 dma_len)
291 if (instance->consistent_mask_64bit) {
292 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
293 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
294 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
297 dcmd->sgl.sge32[0].phys_addr =
298 cpu_to_le32(lower_32_bits(dma_addr));
299 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
300 dcmd->flags = cpu_to_le16(dcmd->flags);
305 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
307 instance->instancet->fire_cmd(instance,
308 cmd->frame_phys_addr, 0, instance->reg_set);
313 * megasas_get_cmd - Get a command from the free pool
314 * @instance: Adapter soft state
316 * Returns a free command from the pool
318 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
322 struct megasas_cmd *cmd = NULL;
324 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
326 if (!list_empty(&instance->cmd_pool)) {
327 cmd = list_entry((&instance->cmd_pool)->next,
328 struct megasas_cmd, list);
329 list_del_init(&cmd->list);
331 dev_err(&instance->pdev->dev, "Command pool empty!\n");
334 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
339 * megasas_return_cmd - Return a cmd to free command pool
340 * @instance: Adapter soft state
341 * @cmd: Command packet to be returned to free command pool
344 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
348 struct megasas_cmd_fusion *cmd_fusion;
349 struct fusion_context *fusion = instance->ctrl_context;
351 /* This flag is used only for fusion adapter.
352 * Wait for Interrupt for Polled mode DCMD
354 if (cmd->flags & DRV_DCMD_POLLED_MODE)
357 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
360 blk_tags = instance->max_scsi_cmds + cmd->index;
361 cmd_fusion = fusion->cmd_list[blk_tags];
362 megasas_return_cmd_fusion(instance, cmd_fusion);
365 cmd->frame_count = 0;
367 memset(cmd->frame, 0, instance->mfi_frame_size);
368 cmd->frame->io.context = cpu_to_le32(cmd->index);
369 if (!fusion && reset_devices)
370 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
371 list_add(&cmd->list, (&instance->cmd_pool)->next);
373 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
378 format_timestamp(uint32_t timestamp)
380 static char buffer[32];
382 if ((timestamp & 0xff000000) == 0xff000000)
383 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
386 snprintf(buffer, sizeof(buffer), "%us", timestamp);
391 format_class(int8_t class)
393 static char buffer[6];
396 case MFI_EVT_CLASS_DEBUG:
398 case MFI_EVT_CLASS_PROGRESS:
400 case MFI_EVT_CLASS_INFO:
402 case MFI_EVT_CLASS_WARNING:
404 case MFI_EVT_CLASS_CRITICAL:
406 case MFI_EVT_CLASS_FATAL:
408 case MFI_EVT_CLASS_DEAD:
411 snprintf(buffer, sizeof(buffer), "%d", class);
417 * megasas_decode_evt: Decode FW AEN event and print critical event
419 * @instance: Adapter soft state
422 megasas_decode_evt(struct megasas_instance *instance)
424 struct megasas_evt_detail *evt_detail = instance->evt_detail;
425 union megasas_evt_class_locale class_locale;
426 class_locale.word = le32_to_cpu(evt_detail->cl.word);
428 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
429 (event_log_level > MFI_EVT_CLASS_DEAD)) {
430 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");
431 event_log_level = MFI_EVT_CLASS_CRITICAL;
434 if (class_locale.members.class >= event_log_level)
435 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
436 le32_to_cpu(evt_detail->seq_num),
437 format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
438 (class_locale.members.locale),
439 format_class(class_locale.members.class),
440 evt_detail->description);
442 if (megasas_dbg_lvl & LD_PD_DEBUG)
443 dev_info(&instance->pdev->dev,
444 "evt_detail.args.ld.target_id/index %d/%d\n",
445 evt_detail->args.ld.target_id, evt_detail->args.ld.ld_index);
450 * The following functions are defined for xscale
451 * (deviceid : 1064R, PERC5) controllers
455 * megasas_enable_intr_xscale - Enables interrupts
456 * @instance: Adapter soft state
459 megasas_enable_intr_xscale(struct megasas_instance *instance)
461 struct megasas_register_set __iomem *regs;
463 regs = instance->reg_set;
464 writel(0, &(regs)->outbound_intr_mask);
466 /* Dummy readl to force pci flush */
467 readl(®s->outbound_intr_mask);
471 * megasas_disable_intr_xscale -Disables interrupt
472 * @instance: Adapter soft state
475 megasas_disable_intr_xscale(struct megasas_instance *instance)
477 struct megasas_register_set __iomem *regs;
480 regs = instance->reg_set;
481 writel(mask, ®s->outbound_intr_mask);
482 /* Dummy readl to force pci flush */
483 readl(®s->outbound_intr_mask);
487 * megasas_read_fw_status_reg_xscale - returns the current FW status value
488 * @instance: Adapter soft state
491 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
493 return readl(&instance->reg_set->outbound_msg_0);
496 * megasas_clear_intr_xscale - Check & clear interrupt
497 * @instance: Adapter soft state
500 megasas_clear_intr_xscale(struct megasas_instance *instance)
504 struct megasas_register_set __iomem *regs;
505 regs = instance->reg_set;
508 * Check if it is our interrupt
510 status = readl(®s->outbound_intr_status);
512 if (status & MFI_OB_INTR_STATUS_MASK)
513 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
514 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
515 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
518 * Clear the interrupt by writing back the same value
521 writel(status, ®s->outbound_intr_status);
523 /* Dummy readl to force pci flush */
524 readl(®s->outbound_intr_status);
530 * megasas_fire_cmd_xscale - Sends command to the FW
531 * @instance: Adapter soft state
532 * @frame_phys_addr : Physical address of cmd
533 * @frame_count : Number of frames for the command
534 * @regs : MFI register set
537 megasas_fire_cmd_xscale(struct megasas_instance *instance,
538 dma_addr_t frame_phys_addr,
540 struct megasas_register_set __iomem *regs)
544 spin_lock_irqsave(&instance->hba_lock, flags);
545 writel((frame_phys_addr >> 3)|(frame_count),
546 &(regs)->inbound_queue_port);
547 spin_unlock_irqrestore(&instance->hba_lock, flags);
551 * megasas_adp_reset_xscale - For controller reset
552 * @instance: Adapter soft state
553 * @regs: MFI register set
556 megasas_adp_reset_xscale(struct megasas_instance *instance,
557 struct megasas_register_set __iomem *regs)
562 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
564 for (i = 0; i < 3; i++)
565 msleep(1000); /* sleep for 3 secs */
567 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
568 dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
570 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
572 pci_write_config_dword(instance->pdev,
573 MFI_1068_PCSR_OFFSET, pcidata);
575 for (i = 0; i < 2; i++)
576 msleep(1000); /* need to wait 2 secs again */
579 pci_read_config_dword(instance->pdev,
580 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
581 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
582 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
583 dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
585 pci_write_config_dword(instance->pdev,
586 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
593 * megasas_check_reset_xscale - For controller reset check
594 * @instance: Adapter soft state
595 * @regs: MFI register set
598 megasas_check_reset_xscale(struct megasas_instance *instance,
599 struct megasas_register_set __iomem *regs)
601 if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
602 (le32_to_cpu(*instance->consumer) ==
603 MEGASAS_ADPRESET_INPROG_SIGN))
608 static struct megasas_instance_template megasas_instance_template_xscale = {
610 .fire_cmd = megasas_fire_cmd_xscale,
611 .enable_intr = megasas_enable_intr_xscale,
612 .disable_intr = megasas_disable_intr_xscale,
613 .clear_intr = megasas_clear_intr_xscale,
614 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
615 .adp_reset = megasas_adp_reset_xscale,
616 .check_reset = megasas_check_reset_xscale,
617 .service_isr = megasas_isr,
618 .tasklet = megasas_complete_cmd_dpc,
619 .init_adapter = megasas_init_adapter_mfi,
620 .build_and_issue_cmd = megasas_build_and_issue_cmd,
621 .issue_dcmd = megasas_issue_dcmd,
625 * This is the end of set of functions & definitions specific
626 * to xscale (deviceid : 1064R, PERC5) controllers
630 * The following functions are defined for ppc (deviceid : 0x60)
635 * megasas_enable_intr_ppc - Enables interrupts
636 * @instance: Adapter soft state
639 megasas_enable_intr_ppc(struct megasas_instance *instance)
641 struct megasas_register_set __iomem *regs;
643 regs = instance->reg_set;
644 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
646 writel(~0x80000000, &(regs)->outbound_intr_mask);
648 /* Dummy readl to force pci flush */
649 readl(®s->outbound_intr_mask);
653 * megasas_disable_intr_ppc - Disable interrupt
654 * @instance: Adapter soft state
657 megasas_disable_intr_ppc(struct megasas_instance *instance)
659 struct megasas_register_set __iomem *regs;
660 u32 mask = 0xFFFFFFFF;
662 regs = instance->reg_set;
663 writel(mask, ®s->outbound_intr_mask);
664 /* Dummy readl to force pci flush */
665 readl(®s->outbound_intr_mask);
669 * megasas_read_fw_status_reg_ppc - returns the current FW status value
670 * @instance: Adapter soft state
673 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
675 return readl(&instance->reg_set->outbound_scratch_pad_0);
679 * megasas_clear_intr_ppc - Check & clear interrupt
680 * @instance: Adapter soft state
683 megasas_clear_intr_ppc(struct megasas_instance *instance)
685 u32 status, mfiStatus = 0;
686 struct megasas_register_set __iomem *regs;
687 regs = instance->reg_set;
690 * Check if it is our interrupt
692 status = readl(®s->outbound_intr_status);
694 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
695 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
697 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
698 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
701 * Clear the interrupt by writing back the same value
703 writel(status, ®s->outbound_doorbell_clear);
705 /* Dummy readl to force pci flush */
706 readl(®s->outbound_doorbell_clear);
712 * megasas_fire_cmd_ppc - Sends command to the FW
713 * @instance: Adapter soft state
714 * @frame_phys_addr: Physical address of cmd
715 * @frame_count: Number of frames for the command
716 * @regs: MFI register set
719 megasas_fire_cmd_ppc(struct megasas_instance *instance,
720 dma_addr_t frame_phys_addr,
722 struct megasas_register_set __iomem *regs)
726 spin_lock_irqsave(&instance->hba_lock, flags);
727 writel((frame_phys_addr | (frame_count<<1))|1,
728 &(regs)->inbound_queue_port);
729 spin_unlock_irqrestore(&instance->hba_lock, flags);
733 * megasas_check_reset_ppc - For controller reset check
734 * @instance: Adapter soft state
735 * @regs: MFI register set
738 megasas_check_reset_ppc(struct megasas_instance *instance,
739 struct megasas_register_set __iomem *regs)
741 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
747 static struct megasas_instance_template megasas_instance_template_ppc = {
749 .fire_cmd = megasas_fire_cmd_ppc,
750 .enable_intr = megasas_enable_intr_ppc,
751 .disable_intr = megasas_disable_intr_ppc,
752 .clear_intr = megasas_clear_intr_ppc,
753 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
754 .adp_reset = megasas_adp_reset_xscale,
755 .check_reset = megasas_check_reset_ppc,
756 .service_isr = megasas_isr,
757 .tasklet = megasas_complete_cmd_dpc,
758 .init_adapter = megasas_init_adapter_mfi,
759 .build_and_issue_cmd = megasas_build_and_issue_cmd,
760 .issue_dcmd = megasas_issue_dcmd,
764 * megasas_enable_intr_skinny - Enables interrupts
765 * @instance: Adapter soft state
768 megasas_enable_intr_skinny(struct megasas_instance *instance)
770 struct megasas_register_set __iomem *regs;
772 regs = instance->reg_set;
773 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
775 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
777 /* Dummy readl to force pci flush */
778 readl(®s->outbound_intr_mask);
782 * megasas_disable_intr_skinny - Disables interrupt
783 * @instance: Adapter soft state
786 megasas_disable_intr_skinny(struct megasas_instance *instance)
788 struct megasas_register_set __iomem *regs;
789 u32 mask = 0xFFFFFFFF;
791 regs = instance->reg_set;
792 writel(mask, ®s->outbound_intr_mask);
793 /* Dummy readl to force pci flush */
794 readl(®s->outbound_intr_mask);
798 * megasas_read_fw_status_reg_skinny - returns the current FW status value
799 * @instance: Adapter soft state
802 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
804 return readl(&instance->reg_set->outbound_scratch_pad_0);
808 * megasas_clear_intr_skinny - Check & clear interrupt
809 * @instance: Adapter soft state
812 megasas_clear_intr_skinny(struct megasas_instance *instance)
816 struct megasas_register_set __iomem *regs;
817 regs = instance->reg_set;
820 * Check if it is our interrupt
822 status = readl(®s->outbound_intr_status);
824 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
829 * Check if it is our interrupt
831 if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
833 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
835 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
838 * Clear the interrupt by writing back the same value
840 writel(status, ®s->outbound_intr_status);
843 * dummy read to flush PCI
845 readl(®s->outbound_intr_status);
851 * megasas_fire_cmd_skinny - Sends command to the FW
852 * @instance: Adapter soft state
853 * @frame_phys_addr: Physical address of cmd
854 * @frame_count: Number of frames for the command
855 * @regs: MFI register set
858 megasas_fire_cmd_skinny(struct megasas_instance *instance,
859 dma_addr_t frame_phys_addr,
861 struct megasas_register_set __iomem *regs)
865 spin_lock_irqsave(&instance->hba_lock, flags);
866 writel(upper_32_bits(frame_phys_addr),
867 &(regs)->inbound_high_queue_port);
868 writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
869 &(regs)->inbound_low_queue_port);
870 spin_unlock_irqrestore(&instance->hba_lock, flags);
874 * megasas_check_reset_skinny - For controller reset check
875 * @instance: Adapter soft state
876 * @regs: MFI register set
879 megasas_check_reset_skinny(struct megasas_instance *instance,
880 struct megasas_register_set __iomem *regs)
882 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
888 static struct megasas_instance_template megasas_instance_template_skinny = {
890 .fire_cmd = megasas_fire_cmd_skinny,
891 .enable_intr = megasas_enable_intr_skinny,
892 .disable_intr = megasas_disable_intr_skinny,
893 .clear_intr = megasas_clear_intr_skinny,
894 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
895 .adp_reset = megasas_adp_reset_gen2,
896 .check_reset = megasas_check_reset_skinny,
897 .service_isr = megasas_isr,
898 .tasklet = megasas_complete_cmd_dpc,
899 .init_adapter = megasas_init_adapter_mfi,
900 .build_and_issue_cmd = megasas_build_and_issue_cmd,
901 .issue_dcmd = megasas_issue_dcmd,
906 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
911 * megasas_enable_intr_gen2 - Enables interrupts
912 * @instance: Adapter soft state
915 megasas_enable_intr_gen2(struct megasas_instance *instance)
917 struct megasas_register_set __iomem *regs;
919 regs = instance->reg_set;
920 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
922 /* write ~0x00000005 (4 & 1) to the intr mask*/
923 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
925 /* Dummy readl to force pci flush */
926 readl(®s->outbound_intr_mask);
930 * megasas_disable_intr_gen2 - Disables interrupt
931 * @instance: Adapter soft state
934 megasas_disable_intr_gen2(struct megasas_instance *instance)
936 struct megasas_register_set __iomem *regs;
937 u32 mask = 0xFFFFFFFF;
939 regs = instance->reg_set;
940 writel(mask, ®s->outbound_intr_mask);
941 /* Dummy readl to force pci flush */
942 readl(®s->outbound_intr_mask);
946 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
947 * @instance: Adapter soft state
950 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
952 return readl(&instance->reg_set->outbound_scratch_pad_0);
956 * megasas_clear_intr_gen2 - Check & clear interrupt
957 * @instance: Adapter soft state
960 megasas_clear_intr_gen2(struct megasas_instance *instance)
964 struct megasas_register_set __iomem *regs;
965 regs = instance->reg_set;
968 * Check if it is our interrupt
970 status = readl(®s->outbound_intr_status);
972 if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
973 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
975 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
976 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
980 * Clear the interrupt by writing back the same value
983 writel(status, ®s->outbound_doorbell_clear);
985 /* Dummy readl to force pci flush */
986 readl(®s->outbound_intr_status);
992 * megasas_fire_cmd_gen2 - Sends command to the FW
993 * @instance: Adapter soft state
994 * @frame_phys_addr: Physical address of cmd
995 * @frame_count: Number of frames for the command
996 * @regs: MFI register set
999 megasas_fire_cmd_gen2(struct megasas_instance *instance,
1000 dma_addr_t frame_phys_addr,
1002 struct megasas_register_set __iomem *regs)
1004 unsigned long flags;
1006 spin_lock_irqsave(&instance->hba_lock, flags);
1007 writel((frame_phys_addr | (frame_count<<1))|1,
1008 &(regs)->inbound_queue_port);
1009 spin_unlock_irqrestore(&instance->hba_lock, flags);
1013 * megasas_adp_reset_gen2 - For controller reset
1014 * @instance: Adapter soft state
1015 * @reg_set: MFI register set
1018 megasas_adp_reset_gen2(struct megasas_instance *instance,
1019 struct megasas_register_set __iomem *reg_set)
1023 u32 __iomem *seq_offset = ®_set->seq_offset;
1024 u32 __iomem *hostdiag_offset = ®_set->host_diag;
1026 if (instance->instancet == &megasas_instance_template_skinny) {
1027 seq_offset = ®_set->fusion_seq_offset;
1028 hostdiag_offset = ®_set->fusion_host_diag;
1031 writel(0, seq_offset);
1032 writel(4, seq_offset);
1033 writel(0xb, seq_offset);
1034 writel(2, seq_offset);
1035 writel(7, seq_offset);
1036 writel(0xd, seq_offset);
1040 HostDiag = (u32)readl(hostdiag_offset);
1042 while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1044 HostDiag = (u32)readl(hostdiag_offset);
1045 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1053 dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1055 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1059 HostDiag = (u32)readl(hostdiag_offset);
1060 while (HostDiag & DIAG_RESET_ADAPTER) {
1062 HostDiag = (u32)readl(hostdiag_offset);
1063 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1066 if (retry++ >= 1000)
1074 * megasas_check_reset_gen2 - For controller reset check
1075 * @instance: Adapter soft state
1076 * @regs: MFI register set
1079 megasas_check_reset_gen2(struct megasas_instance *instance,
1080 struct megasas_register_set __iomem *regs)
1082 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1088 static struct megasas_instance_template megasas_instance_template_gen2 = {
1090 .fire_cmd = megasas_fire_cmd_gen2,
1091 .enable_intr = megasas_enable_intr_gen2,
1092 .disable_intr = megasas_disable_intr_gen2,
1093 .clear_intr = megasas_clear_intr_gen2,
1094 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1095 .adp_reset = megasas_adp_reset_gen2,
1096 .check_reset = megasas_check_reset_gen2,
1097 .service_isr = megasas_isr,
1098 .tasklet = megasas_complete_cmd_dpc,
1099 .init_adapter = megasas_init_adapter_mfi,
1100 .build_and_issue_cmd = megasas_build_and_issue_cmd,
1101 .issue_dcmd = megasas_issue_dcmd,
1105 * This is the end of set of functions & definitions
1106 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1110 * Template added for TB (Fusion)
1112 extern struct megasas_instance_template megasas_instance_template_fusion;
1115 * megasas_issue_polled - Issues a polling command
1116 * @instance: Adapter soft state
1117 * @cmd: Command packet to be issued
1119 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1122 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1124 struct megasas_header *frame_hdr = &cmd->frame->hdr;
1126 frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1127 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1129 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1130 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1131 __func__, __LINE__);
1135 instance->instancet->issue_dcmd(instance, cmd);
1137 return wait_and_poll(instance, cmd, instance->requestorId ?
1138 MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1142 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
1143 * @instance: Adapter soft state
1144 * @cmd: Command to be issued
1145 * @timeout: Timeout in seconds
1147 * This function waits on an event for the command to be returned from ISR.
1148 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1149 * Used to issue ioctl commands.
1152 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1153 struct megasas_cmd *cmd, int timeout)
1156 cmd->cmd_status_drv = DCMD_INIT;
1158 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1159 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1160 __func__, __LINE__);
1164 instance->instancet->issue_dcmd(instance, cmd);
1167 ret = wait_event_timeout(instance->int_cmd_wait_q,
1168 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1170 dev_err(&instance->pdev->dev,
1171 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1172 cmd->frame->dcmd.opcode, __func__);
1173 return DCMD_TIMEOUT;
1176 wait_event(instance->int_cmd_wait_q,
1177 cmd->cmd_status_drv != DCMD_INIT);
1179 return cmd->cmd_status_drv;
1183 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
1184 * @instance: Adapter soft state
1185 * @cmd_to_abort: Previously issued cmd to be aborted
1186 * @timeout: Timeout in seconds
1188 * MFI firmware can abort previously issued AEN comamnd (automatic event
1189 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1190 * cmd and waits for return status.
1191 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1194 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1195 struct megasas_cmd *cmd_to_abort, int timeout)
1197 struct megasas_cmd *cmd;
1198 struct megasas_abort_frame *abort_fr;
1202 cmd = megasas_get_cmd(instance);
1207 abort_fr = &cmd->frame->abort;
1210 * Prepare and issue the abort frame
1212 abort_fr->cmd = MFI_CMD_ABORT;
1213 abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1214 abort_fr->flags = cpu_to_le16(0);
1215 abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1216 abort_fr->abort_mfi_phys_addr_lo =
1217 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1218 abort_fr->abort_mfi_phys_addr_hi =
1219 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1222 cmd->cmd_status_drv = DCMD_INIT;
1224 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1225 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1226 __func__, __LINE__);
1230 instance->instancet->issue_dcmd(instance, cmd);
1233 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1234 cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1236 opcode = cmd_to_abort->frame->dcmd.opcode;
1237 dev_err(&instance->pdev->dev,
1238 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1240 return DCMD_TIMEOUT;
1243 wait_event(instance->abort_cmd_wait_q,
1244 cmd->cmd_status_drv != DCMD_INIT);
1248 megasas_return_cmd(instance, cmd);
1249 return cmd->cmd_status_drv;
1253 * megasas_make_sgl32 - Prepares 32-bit SGL
1254 * @instance: Adapter soft state
1255 * @scp: SCSI command from the mid-layer
1256 * @mfi_sgl: SGL to be filled in
1258 * If successful, this function returns the number of SG elements. Otherwise,
1262 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1263 union megasas_sgl *mfi_sgl)
1267 struct scatterlist *os_sgl;
1269 sge_count = scsi_dma_map(scp);
1270 BUG_ON(sge_count < 0);
1273 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1274 mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1275 mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1282 * megasas_make_sgl64 - Prepares 64-bit SGL
1283 * @instance: Adapter soft state
1284 * @scp: SCSI command from the mid-layer
1285 * @mfi_sgl: SGL to be filled in
1287 * If successful, this function returns the number of SG elements. Otherwise,
1291 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1292 union megasas_sgl *mfi_sgl)
1296 struct scatterlist *os_sgl;
1298 sge_count = scsi_dma_map(scp);
1299 BUG_ON(sge_count < 0);
1302 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1303 mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1304 mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1311 * megasas_make_sgl_skinny - Prepares IEEE SGL
1312 * @instance: Adapter soft state
1313 * @scp: SCSI command from the mid-layer
1314 * @mfi_sgl: SGL to be filled in
1316 * If successful, this function returns the number of SG elements. Otherwise,
1320 megasas_make_sgl_skinny(struct megasas_instance *instance,
1321 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1325 struct scatterlist *os_sgl;
1327 sge_count = scsi_dma_map(scp);
1330 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1331 mfi_sgl->sge_skinny[i].length =
1332 cpu_to_le32(sg_dma_len(os_sgl));
1333 mfi_sgl->sge_skinny[i].phys_addr =
1334 cpu_to_le64(sg_dma_address(os_sgl));
1335 mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1342 * megasas_get_frame_count - Computes the number of frames
1343 * @frame_type : type of frame- io or pthru frame
1344 * @sge_count : number of sg elements
1346 * Returns the number of frames required for numnber of sge's (sge_count)
1349 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1350 u8 sge_count, u8 frame_type)
1355 u32 frame_count = 0;
1357 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1358 sizeof(struct megasas_sge32);
1360 if (instance->flag_ieee) {
1361 sge_sz = sizeof(struct megasas_sge_skinny);
1365 * Main frame can contain 2 SGEs for 64-bit SGLs and
1366 * 3 SGEs for 32-bit SGLs for ldio &
1367 * 1 SGEs for 64-bit SGLs and
1368 * 2 SGEs for 32-bit SGLs for pthru frame
1370 if (unlikely(frame_type == PTHRU_FRAME)) {
1371 if (instance->flag_ieee == 1) {
1372 num_cnt = sge_count - 1;
1373 } else if (IS_DMA64)
1374 num_cnt = sge_count - 1;
1376 num_cnt = sge_count - 2;
1378 if (instance->flag_ieee == 1) {
1379 num_cnt = sge_count - 1;
1380 } else if (IS_DMA64)
1381 num_cnt = sge_count - 2;
1383 num_cnt = sge_count - 3;
1387 sge_bytes = sge_sz * num_cnt;
1389 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1390 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1395 if (frame_count > 7)
1401 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1402 * @instance: Adapter soft state
1403 * @scp: SCSI command
1404 * @cmd: Command to be prepared in
1406 * This function prepares CDB commands. These are typcially pass-through
1407 * commands to the devices.
1410 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1411 struct megasas_cmd *cmd)
1416 struct megasas_pthru_frame *pthru;
1418 is_logical = MEGASAS_IS_LOGICAL(scp->device);
1419 device_id = MEGASAS_DEV_INDEX(scp);
1420 pthru = (struct megasas_pthru_frame *)cmd->frame;
1422 if (scp->sc_data_direction == DMA_TO_DEVICE)
1423 flags = MFI_FRAME_DIR_WRITE;
1424 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1425 flags = MFI_FRAME_DIR_READ;
1426 else if (scp->sc_data_direction == DMA_NONE)
1427 flags = MFI_FRAME_DIR_NONE;
1429 if (instance->flag_ieee == 1) {
1430 flags |= MFI_FRAME_IEEE;
1434 * Prepare the DCDB frame
1436 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1437 pthru->cmd_status = 0x0;
1438 pthru->scsi_status = 0x0;
1439 pthru->target_id = device_id;
1440 pthru->lun = scp->device->lun;
1441 pthru->cdb_len = scp->cmd_len;
1444 pthru->flags = cpu_to_le16(flags);
1445 pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1447 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1450 * If the command is for the tape device, set the
1451 * pthru timeout to the os layer timeout value.
1453 if (scp->device->type == TYPE_TAPE) {
1454 if (scsi_cmd_to_rq(scp)->timeout / HZ > 0xFFFF)
1455 pthru->timeout = cpu_to_le16(0xFFFF);
1457 pthru->timeout = cpu_to_le16(scsi_cmd_to_rq(scp)->timeout / HZ);
1463 if (instance->flag_ieee == 1) {
1464 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1465 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1467 } else if (IS_DMA64) {
1468 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1469 pthru->sge_count = megasas_make_sgl64(instance, scp,
1472 pthru->sge_count = megasas_make_sgl32(instance, scp,
1475 if (pthru->sge_count > instance->max_num_sge) {
1476 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1482 * Sense info specific
1484 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1485 pthru->sense_buf_phys_addr_hi =
1486 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1487 pthru->sense_buf_phys_addr_lo =
1488 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1491 * Compute the total number of frames this command consumes. FW uses
1492 * this number to pull sufficient number of frames from host memory.
1494 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1497 return cmd->frame_count;
1501 * megasas_build_ldio - Prepares IOs to logical devices
1502 * @instance: Adapter soft state
1503 * @scp: SCSI command
1504 * @cmd: Command to be prepared
1506 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1509 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1510 struct megasas_cmd *cmd)
1513 u8 sc = scp->cmnd[0];
1515 struct megasas_io_frame *ldio;
1517 device_id = MEGASAS_DEV_INDEX(scp);
1518 ldio = (struct megasas_io_frame *)cmd->frame;
1520 if (scp->sc_data_direction == DMA_TO_DEVICE)
1521 flags = MFI_FRAME_DIR_WRITE;
1522 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1523 flags = MFI_FRAME_DIR_READ;
1525 if (instance->flag_ieee == 1) {
1526 flags |= MFI_FRAME_IEEE;
1530 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1532 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1533 ldio->cmd_status = 0x0;
1534 ldio->scsi_status = 0x0;
1535 ldio->target_id = device_id;
1537 ldio->reserved_0 = 0;
1539 ldio->flags = cpu_to_le16(flags);
1540 ldio->start_lba_hi = 0;
1541 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1544 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1546 if (scp->cmd_len == 6) {
1547 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1548 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1549 ((u32) scp->cmnd[2] << 8) |
1550 (u32) scp->cmnd[3]);
1552 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1556 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1558 else if (scp->cmd_len == 10) {
1559 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1560 ((u32) scp->cmnd[7] << 8));
1561 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1562 ((u32) scp->cmnd[3] << 16) |
1563 ((u32) scp->cmnd[4] << 8) |
1564 (u32) scp->cmnd[5]);
1568 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1570 else if (scp->cmd_len == 12) {
1571 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1572 ((u32) scp->cmnd[7] << 16) |
1573 ((u32) scp->cmnd[8] << 8) |
1574 (u32) scp->cmnd[9]);
1576 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1577 ((u32) scp->cmnd[3] << 16) |
1578 ((u32) scp->cmnd[4] << 8) |
1579 (u32) scp->cmnd[5]);
1583 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1585 else if (scp->cmd_len == 16) {
1586 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1587 ((u32) scp->cmnd[11] << 16) |
1588 ((u32) scp->cmnd[12] << 8) |
1589 (u32) scp->cmnd[13]);
1591 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1592 ((u32) scp->cmnd[7] << 16) |
1593 ((u32) scp->cmnd[8] << 8) |
1594 (u32) scp->cmnd[9]);
1596 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1597 ((u32) scp->cmnd[3] << 16) |
1598 ((u32) scp->cmnd[4] << 8) |
1599 (u32) scp->cmnd[5]);
1606 if (instance->flag_ieee) {
1607 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1608 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1610 } else if (IS_DMA64) {
1611 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1612 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1614 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1616 if (ldio->sge_count > instance->max_num_sge) {
1617 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1623 * Sense info specific
1625 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1626 ldio->sense_buf_phys_addr_hi = 0;
1627 ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1630 * Compute the total number of frames this command consumes. FW uses
1631 * this number to pull sufficient number of frames from host memory.
1633 cmd->frame_count = megasas_get_frame_count(instance,
1634 ldio->sge_count, IO_FRAME);
1636 return cmd->frame_count;
1640 * megasas_cmd_type - Checks if the cmd is for logical drive/sysPD
1641 * and whether it's RW or non RW
1642 * @cmd: SCSI command
1645 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1649 switch (cmd->cmnd[0]) {
1658 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1659 READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1662 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1663 NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1669 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds
1671 * @instance: Adapter soft state
1674 megasas_dump_pending_frames(struct megasas_instance *instance)
1676 struct megasas_cmd *cmd;
1678 union megasas_sgl *mfi_sgl;
1679 struct megasas_io_frame *ldio;
1680 struct megasas_pthru_frame *pthru;
1682 u16 max_cmd = instance->max_fw_cmds;
1684 dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1685 dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1687 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1689 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1691 dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1692 for (i = 0; i < max_cmd; i++) {
1693 cmd = instance->cmd_list[i];
1696 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1697 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1698 ldio = (struct megasas_io_frame *)cmd->frame;
1699 mfi_sgl = &ldio->sgl;
1700 sgcount = ldio->sge_count;
1701 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1702 " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1703 instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1704 le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1705 le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1707 pthru = (struct megasas_pthru_frame *) cmd->frame;
1708 mfi_sgl = &pthru->sgl;
1709 sgcount = pthru->sge_count;
1710 dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1711 "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1712 instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1713 pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1714 le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1716 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1717 for (n = 0; n < sgcount; n++) {
1719 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1720 le32_to_cpu(mfi_sgl->sge64[n].length),
1721 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1723 dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1724 le32_to_cpu(mfi_sgl->sge32[n].length),
1725 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1729 dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1730 for (i = 0; i < max_cmd; i++) {
1732 cmd = instance->cmd_list[i];
1734 if (cmd->sync_cmd == 1)
1735 dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1737 dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1741 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1742 struct scsi_cmnd *scmd)
1744 struct megasas_cmd *cmd;
1747 cmd = megasas_get_cmd(instance);
1749 return SCSI_MLQUEUE_HOST_BUSY;
1752 * Logical drive command
1754 if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1755 frame_count = megasas_build_ldio(instance, scmd, cmd);
1757 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1760 goto out_return_cmd;
1763 megasas_priv(scmd)->cmd_priv = cmd;
1766 * Issue the command to the FW
1768 atomic_inc(&instance->fw_outstanding);
1770 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1771 cmd->frame_count-1, instance->reg_set);
1775 megasas_return_cmd(instance, cmd);
1776 return SCSI_MLQUEUE_HOST_BUSY;
1781 * megasas_queue_command - Queue entry point
1782 * @shost: adapter SCSI host
1783 * @scmd: SCSI command to be queued
1786 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1788 struct megasas_instance *instance;
1789 struct MR_PRIV_DEVICE *mr_device_priv_data;
1792 instance = (struct megasas_instance *)
1793 scmd->device->host->hostdata;
1795 if (instance->unload == 1) {
1796 scmd->result = DID_NO_CONNECT << 16;
1801 if (instance->issuepend_done == 0)
1802 return SCSI_MLQUEUE_HOST_BUSY;
1805 /* Check for an mpio path and adjust behavior */
1806 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1807 if (megasas_check_mpio_paths(instance, scmd) ==
1808 (DID_REQUEUE << 16)) {
1809 return SCSI_MLQUEUE_HOST_BUSY;
1811 scmd->result = DID_NO_CONNECT << 16;
1817 mr_device_priv_data = scmd->device->hostdata;
1818 if (!mr_device_priv_data ||
1819 (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)) {
1820 scmd->result = DID_NO_CONNECT << 16;
1825 if (MEGASAS_IS_LOGICAL(scmd->device)) {
1826 ld_tgt_id = MEGASAS_TARGET_ID(scmd->device);
1827 if (instance->ld_tgtid_status[ld_tgt_id] == LD_TARGET_ID_DELETED) {
1828 scmd->result = DID_NO_CONNECT << 16;
1834 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1835 return SCSI_MLQUEUE_HOST_BUSY;
1837 if (mr_device_priv_data->tm_busy)
1838 return SCSI_MLQUEUE_DEVICE_BUSY;
1843 if (MEGASAS_IS_LOGICAL(scmd->device) &&
1844 (scmd->device->id >= instance->fw_supported_vd_count ||
1845 scmd->device->lun)) {
1846 scmd->result = DID_BAD_TARGET << 16;
1850 if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1851 MEGASAS_IS_LOGICAL(scmd->device) &&
1852 (!instance->fw_sync_cache_support)) {
1853 scmd->result = DID_OK << 16;
1857 return instance->instancet->build_and_issue_cmd(instance, scmd);
1864 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1868 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1870 if ((megasas_mgmt_info.instance[i]) &&
1871 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1872 return megasas_mgmt_info.instance[i];
1879 * megasas_set_dynamic_target_properties -
1880 * Device property set by driver may not be static and it is required to be
1884 * set dma alignment (only for eedp protection enable vd).
1886 * @sdev: OS provided scsi device
1890 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1891 bool is_target_prop)
1893 u16 pd_index = 0, ld;
1895 struct megasas_instance *instance;
1896 struct fusion_context *fusion;
1897 struct MR_PRIV_DEVICE *mr_device_priv_data;
1898 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1899 struct MR_LD_RAID *raid;
1900 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1902 instance = megasas_lookup_instance(sdev->host->host_no);
1903 fusion = instance->ctrl_context;
1904 mr_device_priv_data = sdev->hostdata;
1906 if (!fusion || !mr_device_priv_data)
1909 if (MEGASAS_IS_LOGICAL(sdev)) {
1910 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1912 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1913 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1914 if (ld >= instance->fw_supported_vd_count)
1916 raid = MR_LdRaidGet(ld, local_map_ptr);
1918 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1919 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1921 mr_device_priv_data->is_tm_capable =
1922 raid->capability.tmCapable;
1924 if (!raid->flags.isEPD)
1925 sdev->no_write_same = 1;
1927 } else if (instance->use_seqnum_jbod_fp) {
1928 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1930 pd_sync = (void *)fusion->pd_seq_sync
1931 [(instance->pd_seq_map_id - 1) & 1];
1932 mr_device_priv_data->is_tm_capable =
1933 pd_sync->seq[pd_index].capability.tmCapable;
1936 if (is_target_prop && instance->tgt_prop->reset_tmo) {
1938 * If FW provides a target reset timeout value, driver will use
1939 * it. If not set, fallback to default values.
1941 mr_device_priv_data->target_reset_tmo =
1942 min_t(u8, instance->max_reset_tmo,
1943 instance->tgt_prop->reset_tmo);
1944 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1946 mr_device_priv_data->target_reset_tmo =
1947 MEGASAS_DEFAULT_TM_TIMEOUT;
1948 mr_device_priv_data->task_abort_tmo =
1949 MEGASAS_DEFAULT_TM_TIMEOUT;
1954 * megasas_set_nvme_device_properties -
1956 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1957 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1959 * MR firmware provides value in KB. Caller of this function converts
1962 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1963 * MR firmware provides value 128 as (32 * 4K) = 128K.
1965 * @sdev: scsi device
1966 * @max_io_size: maximum io transfer size
1970 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1972 struct megasas_instance *instance;
1973 u32 mr_nvme_pg_size;
1975 instance = (struct megasas_instance *)sdev->host->hostdata;
1976 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1977 MR_DEFAULT_NVME_PAGE_SIZE);
1979 blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1981 blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1982 blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1986 * megasas_set_fw_assisted_qd -
1987 * set device queue depth to can_queue
1988 * set device queue depth to fw assisted qd
1990 * @sdev: scsi device
1991 * @is_target_prop true, if fw provided target properties.
1993 static void megasas_set_fw_assisted_qd(struct scsi_device *sdev,
1994 bool is_target_prop)
1997 u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1999 struct megasas_instance *instance;
2000 struct MR_PRIV_DEVICE *mr_device_priv_data;
2002 instance = megasas_lookup_instance(sdev->host->host_no);
2003 mr_device_priv_data = sdev->hostdata;
2004 interface_type = mr_device_priv_data->interface_type;
2006 switch (interface_type) {
2008 device_qd = MEGASAS_SAS_QD;
2011 device_qd = MEGASAS_SATA_QD;
2014 device_qd = MEGASAS_NVME_QD;
2018 if (is_target_prop) {
2019 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
2021 device_qd = min(instance->host->can_queue,
2022 (int)tgt_device_qd);
2025 if (instance->enable_sdev_max_qd && interface_type != UNKNOWN_DRIVE)
2026 device_qd = instance->host->can_queue;
2028 scsi_change_queue_depth(sdev, device_qd);
2032 * megasas_set_static_target_properties -
2033 * Device property set by driver are static and it is not required to be
2034 * updated after OCR.
2037 * set device queue depth
2038 * set nvme device properties. see - megasas_set_nvme_device_properties
2040 * @sdev: scsi device
2041 * @is_target_prop true, if fw provided target properties.
2043 static void megasas_set_static_target_properties(struct scsi_device *sdev,
2044 bool is_target_prop)
2046 u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
2047 struct megasas_instance *instance;
2049 instance = megasas_lookup_instance(sdev->host->host_no);
2052 * The RAID firmware may require extended timeouts.
2054 blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
2056 /* max_io_size_kb will be set to non zero for
2057 * nvme based vd and syspd.
2060 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
2062 if (instance->nvme_page_size && max_io_size_kb)
2063 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2065 megasas_set_fw_assisted_qd(sdev, is_target_prop);
2069 static int megasas_slave_configure(struct scsi_device *sdev)
2072 struct megasas_instance *instance;
2073 int ret_target_prop = DCMD_FAILED;
2074 bool is_target_prop = false;
2076 instance = megasas_lookup_instance(sdev->host->host_no);
2077 if (instance->pd_list_not_supported) {
2078 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2079 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2081 if (instance->pd_list[pd_index].driveState !=
2087 mutex_lock(&instance->reset_mutex);
2088 /* Send DCMD to Firmware and cache the information */
2089 if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2090 megasas_get_pd_info(instance, sdev);
2092 /* Some ventura firmware may not have instance->nvme_page_size set.
2093 * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2095 if ((instance->tgt_prop) && (instance->nvme_page_size))
2096 ret_target_prop = megasas_get_target_prop(instance, sdev);
2098 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2099 megasas_set_static_target_properties(sdev, is_target_prop);
2101 /* This sdev property may change post OCR */
2102 megasas_set_dynamic_target_properties(sdev, is_target_prop);
2104 mutex_unlock(&instance->reset_mutex);
2109 static int megasas_slave_alloc(struct scsi_device *sdev)
2111 u16 pd_index = 0, ld_tgt_id;
2112 struct megasas_instance *instance ;
2113 struct MR_PRIV_DEVICE *mr_device_priv_data;
2115 instance = megasas_lookup_instance(sdev->host->host_no);
2116 if (!MEGASAS_IS_LOGICAL(sdev)) {
2118 * Open the OS scan to the SYSTEM PD
2121 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2123 if ((instance->pd_list_not_supported ||
2124 instance->pd_list[pd_index].driveState ==
2125 MR_PD_STATE_SYSTEM)) {
2129 } else if (!MEGASAS_IS_LUN_VALID(sdev)) {
2130 sdev_printk(KERN_INFO, sdev, "%s: invalid LUN\n", __func__);
2135 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2137 if (!mr_device_priv_data)
2140 if (MEGASAS_IS_LOGICAL(sdev)) {
2141 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2142 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_ACTIVE;
2143 if (megasas_dbg_lvl & LD_PD_DEBUG)
2144 sdev_printk(KERN_INFO, sdev, "LD target ID %d created.\n", ld_tgt_id);
2147 sdev->hostdata = mr_device_priv_data;
2149 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2150 instance->r1_ldio_hint_default);
2154 static void megasas_slave_destroy(struct scsi_device *sdev)
2157 struct megasas_instance *instance;
2159 instance = megasas_lookup_instance(sdev->host->host_no);
2161 if (MEGASAS_IS_LOGICAL(sdev)) {
2162 if (!MEGASAS_IS_LUN_VALID(sdev)) {
2163 sdev_printk(KERN_INFO, sdev, "%s: invalid LUN\n", __func__);
2166 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2167 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_DELETED;
2168 if (megasas_dbg_lvl & LD_PD_DEBUG)
2169 sdev_printk(KERN_INFO, sdev,
2170 "LD target ID %d removed from OS stack\n", ld_tgt_id);
2173 kfree(sdev->hostdata);
2174 sdev->hostdata = NULL;
2178 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2180 * @instance: Adapter soft state
2183 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2186 struct megasas_cmd *cmd_mfi;
2187 struct megasas_cmd_fusion *cmd_fusion;
2188 struct fusion_context *fusion = instance->ctrl_context;
2190 /* Find all outstanding ioctls */
2192 for (i = 0; i < instance->max_fw_cmds; i++) {
2193 cmd_fusion = fusion->cmd_list[i];
2194 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2195 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2196 if (cmd_mfi->sync_cmd &&
2197 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2198 cmd_mfi->frame->hdr.cmd_status =
2199 MFI_STAT_WRONG_STATE;
2200 megasas_complete_cmd(instance,
2206 for (i = 0; i < instance->max_fw_cmds; i++) {
2207 cmd_mfi = instance->cmd_list[i];
2208 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2210 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2216 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2218 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2219 dev_warn(&instance->pdev->dev,
2220 "Adapter already dead, skipping kill HBA\n");
2224 /* Set critical error to block I/O & ioctls in case caller didn't */
2225 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2226 /* Wait 1 second to ensure IO or ioctls in build have posted */
2228 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2229 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2230 (instance->adapter_type != MFI_SERIES)) {
2231 if (!instance->requestorId) {
2232 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2234 readl(&instance->reg_set->doorbell);
2236 if (instance->requestorId && instance->peerIsPresent)
2237 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2239 writel(MFI_STOP_ADP,
2240 &instance->reg_set->inbound_doorbell);
2242 /* Complete outstanding ioctls when adapter is killed */
2243 megasas_complete_outstanding_ioctls(instance);
2247 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2248 * restored to max value
2249 * @instance: Adapter soft state
2253 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2255 unsigned long flags;
2257 if (instance->flag & MEGASAS_FW_BUSY
2258 && time_after(jiffies, instance->last_time + 5 * HZ)
2259 && atomic_read(&instance->fw_outstanding) <
2260 instance->throttlequeuedepth + 1) {
2262 spin_lock_irqsave(instance->host->host_lock, flags);
2263 instance->flag &= ~MEGASAS_FW_BUSY;
2265 instance->host->can_queue = instance->cur_can_queue;
2266 spin_unlock_irqrestore(instance->host->host_lock, flags);
2271 * megasas_complete_cmd_dpc - Returns FW's controller structure
2272 * @instance_addr: Address of adapter soft state
2274 * Tasklet to complete cmds
2276 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2281 struct megasas_cmd *cmd;
2282 struct megasas_instance *instance =
2283 (struct megasas_instance *)instance_addr;
2284 unsigned long flags;
2286 /* If we have already declared adapter dead, donot complete cmds */
2287 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2290 spin_lock_irqsave(&instance->completion_lock, flags);
2292 producer = le32_to_cpu(*instance->producer);
2293 consumer = le32_to_cpu(*instance->consumer);
2295 while (consumer != producer) {
2296 context = le32_to_cpu(instance->reply_queue[consumer]);
2297 if (context >= instance->max_fw_cmds) {
2298 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2303 cmd = instance->cmd_list[context];
2305 megasas_complete_cmd(instance, cmd, DID_OK);
2308 if (consumer == (instance->max_fw_cmds + 1)) {
2313 *instance->consumer = cpu_to_le32(producer);
2315 spin_unlock_irqrestore(&instance->completion_lock, flags);
2318 * Check if we can restore can_queue
2320 megasas_check_and_restore_queue_depth(instance);
2323 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2326 * megasas_start_timer - Initializes sriov heartbeat timer object
2327 * @instance: Adapter soft state
2330 void megasas_start_timer(struct megasas_instance *instance)
2332 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2334 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2335 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2340 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2343 process_fw_state_change_wq(struct work_struct *work);
2345 static void megasas_do_ocr(struct megasas_instance *instance)
2347 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2348 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2349 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2350 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2352 instance->instancet->disable_intr(instance);
2353 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2354 instance->issuepend_done = 0;
2356 atomic_set(&instance->fw_outstanding, 0);
2357 megasas_internal_reset_defer_cmds(instance);
2358 process_fw_state_change_wq(&instance->work_init);
2361 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2364 struct megasas_cmd *cmd;
2365 struct megasas_dcmd_frame *dcmd;
2366 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2367 dma_addr_t new_affiliation_111_h;
2371 cmd = megasas_get_cmd(instance);
2374 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2375 "Failed to get cmd for scsi%d\n",
2376 instance->host->host_no);
2380 dcmd = &cmd->frame->dcmd;
2382 if (!instance->vf_affiliation_111) {
2383 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2384 "affiliation for scsi%d\n", instance->host->host_no);
2385 megasas_return_cmd(instance, cmd);
2390 memset(instance->vf_affiliation_111, 0,
2391 sizeof(struct MR_LD_VF_AFFILIATION_111));
2393 new_affiliation_111 =
2394 dma_alloc_coherent(&instance->pdev->dev,
2395 sizeof(struct MR_LD_VF_AFFILIATION_111),
2396 &new_affiliation_111_h, GFP_KERNEL);
2397 if (!new_affiliation_111) {
2398 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2399 "memory for new affiliation for scsi%d\n",
2400 instance->host->host_no);
2401 megasas_return_cmd(instance, cmd);
2406 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2408 dcmd->cmd = MFI_CMD_DCMD;
2409 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2410 dcmd->sge_count = 1;
2411 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2414 dcmd->data_xfer_len =
2415 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2416 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2419 dcmd->sgl.sge32[0].phys_addr =
2420 cpu_to_le32(instance->vf_affiliation_111_h);
2422 dcmd->sgl.sge32[0].phys_addr =
2423 cpu_to_le32(new_affiliation_111_h);
2425 dcmd->sgl.sge32[0].length = cpu_to_le32(
2426 sizeof(struct MR_LD_VF_AFFILIATION_111));
2428 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2429 "scsi%d\n", instance->host->host_no);
2431 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2432 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2433 " failed with status 0x%x for scsi%d\n",
2434 dcmd->cmd_status, instance->host->host_no);
2435 retval = 1; /* Do a scan if we couldn't get affiliation */
2440 thisVf = new_affiliation_111->thisVf;
2441 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2442 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2443 new_affiliation_111->map[ld].policy[thisVf]) {
2444 dev_warn(&instance->pdev->dev, "SR-IOV: "
2445 "Got new LD/VF affiliation for scsi%d\n",
2446 instance->host->host_no);
2447 memcpy(instance->vf_affiliation_111,
2448 new_affiliation_111,
2449 sizeof(struct MR_LD_VF_AFFILIATION_111));
2455 if (new_affiliation_111) {
2456 dma_free_coherent(&instance->pdev->dev,
2457 sizeof(struct MR_LD_VF_AFFILIATION_111),
2458 new_affiliation_111,
2459 new_affiliation_111_h);
2462 megasas_return_cmd(instance, cmd);
2467 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2470 struct megasas_cmd *cmd;
2471 struct megasas_dcmd_frame *dcmd;
2472 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2473 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2474 dma_addr_t new_affiliation_h;
2475 int i, j, retval = 0, found = 0, doscan = 0;
2478 cmd = megasas_get_cmd(instance);
2481 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2482 "Failed to get cmd for scsi%d\n",
2483 instance->host->host_no);
2487 dcmd = &cmd->frame->dcmd;
2489 if (!instance->vf_affiliation) {
2490 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2491 "affiliation for scsi%d\n", instance->host->host_no);
2492 megasas_return_cmd(instance, cmd);
2497 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2498 sizeof(struct MR_LD_VF_AFFILIATION));
2501 dma_alloc_coherent(&instance->pdev->dev,
2502 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2503 &new_affiliation_h, GFP_KERNEL);
2504 if (!new_affiliation) {
2505 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2506 "memory for new affiliation for scsi%d\n",
2507 instance->host->host_no);
2508 megasas_return_cmd(instance, cmd);
2513 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2515 dcmd->cmd = MFI_CMD_DCMD;
2516 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2517 dcmd->sge_count = 1;
2518 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2521 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2522 sizeof(struct MR_LD_VF_AFFILIATION));
2523 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2526 dcmd->sgl.sge32[0].phys_addr =
2527 cpu_to_le32(instance->vf_affiliation_h);
2529 dcmd->sgl.sge32[0].phys_addr =
2530 cpu_to_le32(new_affiliation_h);
2532 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2533 sizeof(struct MR_LD_VF_AFFILIATION));
2535 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2536 "scsi%d\n", instance->host->host_no);
2539 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2540 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2541 " failed with status 0x%x for scsi%d\n",
2542 dcmd->cmd_status, instance->host->host_no);
2543 retval = 1; /* Do a scan if we couldn't get affiliation */
2548 if (!new_affiliation->ldCount) {
2549 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2550 "affiliation for passive path for scsi%d\n",
2551 instance->host->host_no);
2555 newmap = new_affiliation->map;
2556 savedmap = instance->vf_affiliation->map;
2557 thisVf = new_affiliation->thisVf;
2558 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2560 for (j = 0; j < instance->vf_affiliation->ldCount;
2562 if (newmap->ref.targetId ==
2563 savedmap->ref.targetId) {
2565 if (newmap->policy[thisVf] !=
2566 savedmap->policy[thisVf]) {
2571 savedmap = (struct MR_LD_VF_MAP *)
2572 ((unsigned char *)savedmap +
2575 if (!found && newmap->policy[thisVf] !=
2576 MR_LD_ACCESS_HIDDEN) {
2580 newmap = (struct MR_LD_VF_MAP *)
2581 ((unsigned char *)newmap + newmap->size);
2584 newmap = new_affiliation->map;
2585 savedmap = instance->vf_affiliation->map;
2587 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2589 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2590 if (savedmap->ref.targetId ==
2591 newmap->ref.targetId) {
2593 if (savedmap->policy[thisVf] !=
2594 newmap->policy[thisVf]) {
2599 newmap = (struct MR_LD_VF_MAP *)
2600 ((unsigned char *)newmap +
2603 if (!found && savedmap->policy[thisVf] !=
2604 MR_LD_ACCESS_HIDDEN) {
2608 savedmap = (struct MR_LD_VF_MAP *)
2609 ((unsigned char *)savedmap +
2615 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2616 "affiliation for scsi%d\n", instance->host->host_no);
2617 memcpy(instance->vf_affiliation, new_affiliation,
2618 new_affiliation->size);
2622 if (new_affiliation)
2623 dma_free_coherent(&instance->pdev->dev,
2624 (MAX_LOGICAL_DRIVES + 1) *
2625 sizeof(struct MR_LD_VF_AFFILIATION),
2626 new_affiliation, new_affiliation_h);
2627 megasas_return_cmd(instance, cmd);
2632 /* This function will get the current SR-IOV LD/VF affiliation */
2633 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2638 if (instance->PlasmaFW111)
2639 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2641 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2645 /* This function will tell FW to start the SR-IOV heartbeat */
2646 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2649 struct megasas_cmd *cmd;
2650 struct megasas_dcmd_frame *dcmd;
2653 cmd = megasas_get_cmd(instance);
2656 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2657 "Failed to get cmd for scsi%d\n",
2658 instance->host->host_no);
2662 dcmd = &cmd->frame->dcmd;
2665 instance->hb_host_mem =
2666 dma_alloc_coherent(&instance->pdev->dev,
2667 sizeof(struct MR_CTRL_HB_HOST_MEM),
2668 &instance->hb_host_mem_h,
2670 if (!instance->hb_host_mem) {
2671 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2672 " memory for heartbeat host memory for scsi%d\n",
2673 instance->host->host_no);
2679 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2681 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2682 dcmd->cmd = MFI_CMD_DCMD;
2683 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2684 dcmd->sge_count = 1;
2685 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2688 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2689 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2691 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2692 sizeof(struct MR_CTRL_HB_HOST_MEM));
2694 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2695 instance->host->host_no);
2697 if ((instance->adapter_type != MFI_SERIES) &&
2698 !instance->mask_interrupts)
2699 retval = megasas_issue_blocked_cmd(instance, cmd,
2700 MEGASAS_ROUTINE_WAIT_TIME_VF);
2702 retval = megasas_issue_polled(instance, cmd);
2705 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2706 "_MEM_ALLOC DCMD %s for scsi%d\n",
2707 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2708 "timed out" : "failed", instance->host->host_no);
2713 megasas_return_cmd(instance, cmd);
2718 /* Handler for SR-IOV heartbeat */
2719 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2721 struct megasas_instance *instance =
2722 from_timer(instance, t, sriov_heartbeat_timer);
2724 if (instance->hb_host_mem->HB.fwCounter !=
2725 instance->hb_host_mem->HB.driverCounter) {
2726 instance->hb_host_mem->HB.driverCounter =
2727 instance->hb_host_mem->HB.fwCounter;
2728 mod_timer(&instance->sriov_heartbeat_timer,
2729 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2731 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2732 "completed for scsi%d\n", instance->host->host_no);
2733 schedule_work(&instance->work_init);
2738 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2739 * @instance: Adapter soft state
2741 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2742 * complete all its outstanding commands. Returns error if one or more IOs
2743 * are pending after this time period. It also marks the controller dead.
2745 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2747 int i, sl, outstanding;
2749 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2750 unsigned long flags;
2751 struct list_head clist_local;
2752 struct megasas_cmd *reset_cmd;
2755 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2756 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2757 __func__, __LINE__);
2761 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2763 INIT_LIST_HEAD(&clist_local);
2764 spin_lock_irqsave(&instance->hba_lock, flags);
2765 list_splice_init(&instance->internal_reset_pending_q,
2767 spin_unlock_irqrestore(&instance->hba_lock, flags);
2769 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2770 for (i = 0; i < wait_time; i++) {
2772 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2776 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2777 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2778 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2783 while (!list_empty(&clist_local)) {
2784 reset_cmd = list_entry((&clist_local)->next,
2785 struct megasas_cmd, list);
2786 list_del_init(&reset_cmd->list);
2787 if (reset_cmd->scmd) {
2788 reset_cmd->scmd->result = DID_REQUEUE << 16;
2789 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2790 reset_index, reset_cmd,
2791 reset_cmd->scmd->cmnd[0]);
2793 scsi_done(reset_cmd->scmd);
2794 megasas_return_cmd(instance, reset_cmd);
2795 } else if (reset_cmd->sync_cmd) {
2796 dev_notice(&instance->pdev->dev, "%p synch cmds"
2800 reset_cmd->cmd_status_drv = DCMD_INIT;
2801 instance->instancet->fire_cmd(instance,
2802 reset_cmd->frame_phys_addr,
2803 0, instance->reg_set);
2805 dev_notice(&instance->pdev->dev, "%p unexpected"
2815 for (i = 0; i < resetwaittime; i++) {
2816 outstanding = atomic_read(&instance->fw_outstanding);
2821 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2822 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2823 "commands to complete\n",i,outstanding);
2825 * Call cmd completion routine. Cmd to be
2826 * be completed directly without depending on isr.
2828 megasas_complete_cmd_dpc((unsigned long)instance);
2835 outstanding = atomic_read(&instance->fw_outstanding);
2836 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2838 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2839 goto no_outstanding;
2841 if (instance->disableOnlineCtrlReset)
2842 goto kill_hba_and_failed;
2844 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2845 dev_info(&instance->pdev->dev,
2846 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2847 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2849 goto kill_hba_and_failed;
2850 megasas_do_ocr(instance);
2852 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2853 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2854 __func__, __LINE__);
2857 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2858 __func__, __LINE__);
2860 for (sl = 0; sl < 10; sl++)
2863 outstanding = atomic_read(&instance->fw_outstanding);
2865 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2866 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2867 goto no_outstanding;
2874 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2875 __func__, __LINE__);
2878 kill_hba_and_failed:
2880 /* Reset not supported, kill adapter */
2881 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2882 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2883 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2884 atomic_read(&instance->fw_outstanding));
2885 megasas_dump_pending_frames(instance);
2886 megaraid_sas_kill_hba(instance);
2892 * megasas_generic_reset - Generic reset routine
2893 * @scmd: Mid-layer SCSI command
2895 * This routine implements a generic reset handler for device, bus and host
2896 * reset requests. Device, bus and host specific reset handlers can use this
2897 * function after they do their specific tasks.
2899 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2902 struct megasas_instance *instance;
2904 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2906 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2907 scmd->cmnd[0], scmd->retries);
2909 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2910 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2914 ret_val = megasas_wait_for_outstanding(instance);
2915 if (ret_val == SUCCESS)
2916 dev_notice(&instance->pdev->dev, "reset successful\n");
2918 dev_err(&instance->pdev->dev, "failed to do reset\n");
2924 * megasas_reset_timer - quiesce the adapter if required
2927 * Sets the FW busy flag and reduces the host->can_queue if the
2928 * cmd has not been completed within the timeout period.
2930 static enum scsi_timeout_action megasas_reset_timer(struct scsi_cmnd *scmd)
2932 struct megasas_instance *instance;
2933 unsigned long flags;
2935 if (time_after(jiffies, scmd->jiffies_at_alloc +
2936 (scmd_timeout * 2) * HZ)) {
2937 return SCSI_EH_NOT_HANDLED;
2940 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2941 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2942 /* FW is busy, throttle IO */
2943 spin_lock_irqsave(instance->host->host_lock, flags);
2945 instance->host->can_queue = instance->throttlequeuedepth;
2946 instance->last_time = jiffies;
2947 instance->flag |= MEGASAS_FW_BUSY;
2949 spin_unlock_irqrestore(instance->host->host_lock, flags);
2951 return SCSI_EH_RESET_TIMER;
2955 * megasas_dump - This function will print hexdump of provided buffer.
2956 * @buf: Buffer to be dumped
2957 * @sz: Size in bytes
2958 * @format: Different formats of dumping e.g. format=n will
2959 * cause only 'n' 32 bit words to be dumped in a single
2963 megasas_dump(void *buf, int sz, int format)
2966 __le32 *buf_loc = (__le32 *)buf;
2968 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2969 if ((i % format) == 0) {
2971 printk(KERN_CONT "\n");
2972 printk(KERN_CONT "%08x: ", (i * 4));
2974 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2976 printk(KERN_CONT "\n");
2980 * megasas_dump_reg_set - This function will print hexdump of register set
2981 * @reg_set: Register set to be dumped
2984 megasas_dump_reg_set(void __iomem *reg_set)
2986 unsigned int i, sz = 256;
2987 u32 __iomem *reg = (u32 __iomem *)reg_set;
2989 for (i = 0; i < (sz / sizeof(u32)); i++)
2990 printk("%08x: %08x\n", (i * 4), readl(®[i]));
2994 * megasas_dump_fusion_io - This function will print key details
2996 * @scmd: SCSI command pointer of SCSI IO
2999 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
3001 struct megasas_cmd_fusion *cmd = megasas_priv(scmd)->cmd_priv;
3002 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3003 struct megasas_instance *instance;
3005 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3007 scmd_printk(KERN_INFO, scmd,
3008 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
3009 scmd, scmd->retries, scmd->allowed);
3010 scsi_print_command(scmd);
3013 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
3014 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
3015 scmd_printk(KERN_INFO, scmd,
3016 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
3017 req_desc->SCSIIO.RequestFlags,
3018 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
3019 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
3021 printk(KERN_INFO "IO request frame:\n");
3022 megasas_dump(cmd->io_request,
3023 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
3024 printk(KERN_INFO "Chain frame:\n");
3025 megasas_dump(cmd->sg_frame,
3026 instance->max_chain_frame_sz, 8);
3032 * megasas_dump_sys_regs - This function will dump system registers through
3034 * @reg_set: Pointer to System register set.
3035 * @buf: Buffer to which output is to be written.
3036 * @return: Number of bytes written to buffer.
3038 static inline ssize_t
3039 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
3041 unsigned int i, sz = 256;
3042 int bytes_wrote = 0;
3043 char *loc = (char *)buf;
3044 u32 __iomem *reg = (u32 __iomem *)reg_set;
3046 for (i = 0; i < sz / sizeof(u32); i++) {
3047 bytes_wrote += scnprintf(loc + bytes_wrote,
3048 PAGE_SIZE - bytes_wrote,
3049 "%08x: %08x\n", (i * 4),
3056 * megasas_reset_bus_host - Bus & host reset handler entry point
3057 * @scmd: Mid-layer SCSI command
3059 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
3062 struct megasas_instance *instance;
3064 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3066 scmd_printk(KERN_INFO, scmd,
3067 "OCR is requested due to IO timeout!!\n");
3069 scmd_printk(KERN_INFO, scmd,
3070 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
3071 scmd->device->host->shost_state,
3072 scsi_host_busy(scmd->device->host),
3073 atomic_read(&instance->fw_outstanding));
3075 * First wait for all commands to complete
3077 if (instance->adapter_type == MFI_SERIES) {
3078 ret = megasas_generic_reset(scmd);
3080 megasas_dump_fusion_io(scmd);
3081 ret = megasas_reset_fusion(scmd->device->host,
3082 SCSIIO_TIMEOUT_OCR);
3089 * megasas_task_abort - Issues task abort request to firmware
3090 * (supported only for fusion adapters)
3091 * @scmd: SCSI command pointer
3093 static int megasas_task_abort(struct scsi_cmnd *scmd)
3096 struct megasas_instance *instance;
3098 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3100 if (instance->adapter_type != MFI_SERIES)
3101 ret = megasas_task_abort_fusion(scmd);
3103 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3111 * megasas_reset_target: Issues target reset request to firmware
3112 * (supported only for fusion adapters)
3113 * @scmd: SCSI command pointer
3115 static int megasas_reset_target(struct scsi_cmnd *scmd)
3118 struct megasas_instance *instance;
3120 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3122 if (instance->adapter_type != MFI_SERIES)
3123 ret = megasas_reset_target_fusion(scmd);
3125 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3133 * megasas_bios_param - Returns disk geometry for a disk
3134 * @sdev: device handle
3135 * @bdev: block device
3136 * @capacity: drive capacity
3137 * @geom: geometry parameters
3140 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3141 sector_t capacity, int geom[])
3148 /* Default heads (64) & sectors (32) */
3152 tmp = heads * sectors;
3153 cylinders = capacity;
3155 sector_div(cylinders, tmp);
3158 * Handle extended translation size for logical drives > 1Gb
3161 if (capacity >= 0x200000) {
3164 tmp = heads*sectors;
3165 cylinders = capacity;
3166 sector_div(cylinders, tmp);
3171 geom[2] = cylinders;
3176 static void megasas_map_queues(struct Scsi_Host *shost)
3178 struct megasas_instance *instance;
3179 int qoff = 0, offset;
3180 struct blk_mq_queue_map *map;
3182 instance = (struct megasas_instance *)shost->hostdata;
3184 if (shost->nr_hw_queues == 1)
3187 offset = instance->low_latency_index_start;
3189 /* Setup Default hctx */
3190 map = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
3191 map->nr_queues = instance->msix_vectors - offset;
3192 map->queue_offset = 0;
3193 blk_mq_pci_map_queues(map, instance->pdev, offset);
3194 qoff += map->nr_queues;
3195 offset += map->nr_queues;
3197 /* we never use READ queue, so can't cheat blk-mq */
3198 shost->tag_set.map[HCTX_TYPE_READ].nr_queues = 0;
3200 /* Setup Poll hctx */
3201 map = &shost->tag_set.map[HCTX_TYPE_POLL];
3202 map->nr_queues = instance->iopoll_q_count;
3203 if (map->nr_queues) {
3205 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3206 * affinity), so use the regular blk-mq cpu mapping
3208 map->queue_offset = qoff;
3209 blk_mq_map_queues(map);
3213 static void megasas_aen_polling(struct work_struct *work);
3216 * megasas_service_aen - Processes an event notification
3217 * @instance: Adapter soft state
3218 * @cmd: AEN command completed by the ISR
3220 * For AEN, driver sends a command down to FW that is held by the FW till an
3221 * event occurs. When an event of interest occurs, FW completes the command
3222 * that it was previously holding.
3224 * This routines sends SIGIO signal to processes that have registered with the
3228 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3230 unsigned long flags;
3233 * Don't signal app if it is just an aborted previously registered aen
3235 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3236 spin_lock_irqsave(&poll_aen_lock, flags);
3237 megasas_poll_wait_aen = 1;
3238 spin_unlock_irqrestore(&poll_aen_lock, flags);
3239 wake_up(&megasas_poll_wait);
3240 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3245 instance->aen_cmd = NULL;
3247 megasas_return_cmd(instance, cmd);
3249 if ((instance->unload == 0) &&
3250 ((instance->issuepend_done == 1))) {
3251 struct megasas_aen_event *ev;
3253 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3255 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3257 ev->instance = instance;
3259 INIT_DELAYED_WORK(&ev->hotplug_work,
3260 megasas_aen_polling);
3261 schedule_delayed_work(&ev->hotplug_work, 0);
3267 fw_crash_buffer_store(struct device *cdev,
3268 struct device_attribute *attr, const char *buf, size_t count)
3270 struct Scsi_Host *shost = class_to_shost(cdev);
3271 struct megasas_instance *instance =
3272 (struct megasas_instance *) shost->hostdata;
3274 unsigned long flags;
3276 if (kstrtoint(buf, 0, &val) != 0)
3279 spin_lock_irqsave(&instance->crashdump_lock, flags);
3280 instance->fw_crash_buffer_offset = val;
3281 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3286 fw_crash_buffer_show(struct device *cdev,
3287 struct device_attribute *attr, char *buf)
3289 struct Scsi_Host *shost = class_to_shost(cdev);
3290 struct megasas_instance *instance =
3291 (struct megasas_instance *) shost->hostdata;
3293 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3294 unsigned long chunk_left_bytes;
3295 unsigned long src_addr;
3296 unsigned long flags;
3299 spin_lock_irqsave(&instance->crashdump_lock, flags);
3300 buff_offset = instance->fw_crash_buffer_offset;
3301 if (!instance->crash_dump_buf ||
3302 !((instance->fw_crash_state == AVAILABLE) ||
3303 (instance->fw_crash_state == COPYING))) {
3304 dev_err(&instance->pdev->dev,
3305 "Firmware crash dump is not available\n");
3306 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3310 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3311 dev_err(&instance->pdev->dev,
3312 "Firmware crash dump offset is out of range\n");
3313 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3317 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3318 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3319 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3320 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3322 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3323 (buff_offset % dmachunk);
3324 memcpy(buf, (void *)src_addr, size);
3325 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3331 fw_crash_buffer_size_show(struct device *cdev,
3332 struct device_attribute *attr, char *buf)
3334 struct Scsi_Host *shost = class_to_shost(cdev);
3335 struct megasas_instance *instance =
3336 (struct megasas_instance *) shost->hostdata;
3338 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3339 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3343 fw_crash_state_store(struct device *cdev,
3344 struct device_attribute *attr, const char *buf, size_t count)
3346 struct Scsi_Host *shost = class_to_shost(cdev);
3347 struct megasas_instance *instance =
3348 (struct megasas_instance *) shost->hostdata;
3350 unsigned long flags;
3352 if (kstrtoint(buf, 0, &val) != 0)
3355 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3356 dev_err(&instance->pdev->dev, "application updates invalid "
3357 "firmware crash state\n");
3361 instance->fw_crash_state = val;
3363 if ((val == COPIED) || (val == COPY_ERROR)) {
3364 spin_lock_irqsave(&instance->crashdump_lock, flags);
3365 megasas_free_host_crash_buffer(instance);
3366 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3367 if (val == COPY_ERROR)
3368 dev_info(&instance->pdev->dev, "application failed to "
3369 "copy Firmware crash dump\n");
3371 dev_info(&instance->pdev->dev, "Firmware crash dump "
3372 "copied successfully\n");
3378 fw_crash_state_show(struct device *cdev,
3379 struct device_attribute *attr, char *buf)
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, "%d\n", instance->fw_crash_state);
3389 page_size_show(struct device *cdev,
3390 struct device_attribute *attr, char *buf)
3392 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3396 ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3399 struct Scsi_Host *shost = class_to_shost(cdev);
3400 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3402 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3406 fw_cmds_outstanding_show(struct device *cdev,
3407 struct device_attribute *attr, char *buf)
3409 struct Scsi_Host *shost = class_to_shost(cdev);
3410 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3412 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3416 enable_sdev_max_qd_show(struct device *cdev,
3417 struct device_attribute *attr, char *buf)
3419 struct Scsi_Host *shost = class_to_shost(cdev);
3420 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3422 return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3426 enable_sdev_max_qd_store(struct device *cdev,
3427 struct device_attribute *attr, const char *buf, size_t count)
3429 struct Scsi_Host *shost = class_to_shost(cdev);
3430 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3432 bool is_target_prop;
3433 int ret_target_prop = DCMD_FAILED;
3434 struct scsi_device *sdev;
3436 if (kstrtou32(buf, 0, &val) != 0) {
3437 pr_err("megasas: could not set enable_sdev_max_qd\n");
3441 mutex_lock(&instance->reset_mutex);
3443 instance->enable_sdev_max_qd = true;
3445 instance->enable_sdev_max_qd = false;
3447 shost_for_each_device(sdev, shost) {
3448 ret_target_prop = megasas_get_target_prop(instance, sdev);
3449 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3450 megasas_set_fw_assisted_qd(sdev, is_target_prop);
3452 mutex_unlock(&instance->reset_mutex);
3458 dump_system_regs_show(struct device *cdev,
3459 struct device_attribute *attr, char *buf)
3461 struct Scsi_Host *shost = class_to_shost(cdev);
3462 struct megasas_instance *instance =
3463 (struct megasas_instance *)shost->hostdata;
3465 return megasas_dump_sys_regs(instance->reg_set, buf);
3469 raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3472 struct Scsi_Host *shost = class_to_shost(cdev);
3473 struct megasas_instance *instance =
3474 (struct megasas_instance *)shost->hostdata;
3476 return snprintf(buf, PAGE_SIZE, "%ld\n",
3477 (unsigned long)instance->map_id);
3480 static DEVICE_ATTR_RW(fw_crash_buffer);
3481 static DEVICE_ATTR_RO(fw_crash_buffer_size);
3482 static DEVICE_ATTR_RW(fw_crash_state);
3483 static DEVICE_ATTR_RO(page_size);
3484 static DEVICE_ATTR_RO(ldio_outstanding);
3485 static DEVICE_ATTR_RO(fw_cmds_outstanding);
3486 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3487 static DEVICE_ATTR_RO(dump_system_regs);
3488 static DEVICE_ATTR_RO(raid_map_id);
3490 static struct attribute *megaraid_host_attrs[] = {
3491 &dev_attr_fw_crash_buffer_size.attr,
3492 &dev_attr_fw_crash_buffer.attr,
3493 &dev_attr_fw_crash_state.attr,
3494 &dev_attr_page_size.attr,
3495 &dev_attr_ldio_outstanding.attr,
3496 &dev_attr_fw_cmds_outstanding.attr,
3497 &dev_attr_enable_sdev_max_qd.attr,
3498 &dev_attr_dump_system_regs.attr,
3499 &dev_attr_raid_map_id.attr,
3503 ATTRIBUTE_GROUPS(megaraid_host);
3506 * Scsi host template for megaraid_sas driver
3508 static const struct scsi_host_template megasas_template = {
3510 .module = THIS_MODULE,
3511 .name = "Avago SAS based MegaRAID driver",
3512 .proc_name = "megaraid_sas",
3513 .slave_configure = megasas_slave_configure,
3514 .slave_alloc = megasas_slave_alloc,
3515 .slave_destroy = megasas_slave_destroy,
3516 .queuecommand = megasas_queue_command,
3517 .eh_target_reset_handler = megasas_reset_target,
3518 .eh_abort_handler = megasas_task_abort,
3519 .eh_host_reset_handler = megasas_reset_bus_host,
3520 .eh_timed_out = megasas_reset_timer,
3521 .shost_groups = megaraid_host_groups,
3522 .bios_param = megasas_bios_param,
3523 .map_queues = megasas_map_queues,
3524 .mq_poll = megasas_blk_mq_poll,
3525 .change_queue_depth = scsi_change_queue_depth,
3526 .max_segment_size = 0xffffffff,
3527 .cmd_size = sizeof(struct megasas_cmd_priv),
3531 * megasas_complete_int_cmd - Completes an internal command
3532 * @instance: Adapter soft state
3533 * @cmd: Command to be completed
3535 * The megasas_issue_blocked_cmd() function waits for a command to complete
3536 * after it issues a command. This function wakes up that waiting routine by
3537 * calling wake_up() on the wait queue.
3540 megasas_complete_int_cmd(struct megasas_instance *instance,
3541 struct megasas_cmd *cmd)
3543 if (cmd->cmd_status_drv == DCMD_INIT)
3544 cmd->cmd_status_drv =
3545 (cmd->frame->io.cmd_status == MFI_STAT_OK) ?
3546 DCMD_SUCCESS : DCMD_FAILED;
3548 wake_up(&instance->int_cmd_wait_q);
3552 * megasas_complete_abort - Completes aborting a command
3553 * @instance: Adapter soft state
3554 * @cmd: Cmd that was issued to abort another cmd
3556 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3557 * after it issues an abort on a previously issued command. This function
3558 * wakes up all functions waiting on the same wait queue.
3561 megasas_complete_abort(struct megasas_instance *instance,
3562 struct megasas_cmd *cmd)
3564 if (cmd->sync_cmd) {
3566 cmd->cmd_status_drv = DCMD_SUCCESS;
3567 wake_up(&instance->abort_cmd_wait_q);
3572 megasas_set_ld_removed_by_fw(struct megasas_instance *instance)
3576 for (i = 0; (i < MEGASAS_MAX_LD_IDS); i++) {
3577 if (instance->ld_ids_prev[i] != 0xff &&
3578 instance->ld_ids_from_raidmap[i] == 0xff) {
3579 if (megasas_dbg_lvl & LD_PD_DEBUG)
3580 dev_info(&instance->pdev->dev,
3581 "LD target ID %d removed from RAID map\n", i);
3582 instance->ld_tgtid_status[i] = LD_TARGET_ID_DELETED;
3588 * megasas_complete_cmd - Completes a command
3589 * @instance: Adapter soft state
3590 * @cmd: Command to be completed
3591 * @alt_status: If non-zero, use this value as status to
3592 * SCSI mid-layer instead of the value returned
3593 * by the FW. This should be used if caller wants
3594 * an alternate status (as in the case of aborted
3598 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3602 struct megasas_header *hdr = &cmd->frame->hdr;
3603 unsigned long flags;
3604 struct fusion_context *fusion = instance->ctrl_context;
3607 /* flag for the retry reset */
3608 cmd->retry_for_fw_reset = 0;
3611 megasas_priv(cmd->scmd)->cmd_priv = NULL;
3614 case MFI_CMD_INVALID:
3615 /* Some older 1068 controller FW may keep a pended
3616 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3617 when booting the kdump kernel. Ignore this command to
3618 prevent a kernel panic on shutdown of the kdump kernel. */
3619 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3621 dev_warn(&instance->pdev->dev, "If you have a controller "
3622 "other than PERC5, please upgrade your firmware\n");
3624 case MFI_CMD_PD_SCSI_IO:
3625 case MFI_CMD_LD_SCSI_IO:
3628 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3629 * issued either through an IO path or an IOCTL path. If it
3630 * was via IOCTL, we will send it to internal completion.
3632 if (cmd->sync_cmd) {
3634 megasas_complete_int_cmd(instance, cmd);
3639 case MFI_CMD_LD_READ:
3640 case MFI_CMD_LD_WRITE:
3643 cmd->scmd->result = alt_status << 16;
3649 atomic_dec(&instance->fw_outstanding);
3651 scsi_dma_unmap(cmd->scmd);
3652 scsi_done(cmd->scmd);
3653 megasas_return_cmd(instance, cmd);
3658 switch (hdr->cmd_status) {
3661 cmd->scmd->result = DID_OK << 16;
3664 case MFI_STAT_SCSI_IO_FAILED:
3665 case MFI_STAT_LD_INIT_IN_PROGRESS:
3667 (DID_ERROR << 16) | hdr->scsi_status;
3670 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3672 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3674 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3675 memset(cmd->scmd->sense_buffer, 0,
3676 SCSI_SENSE_BUFFERSIZE);
3677 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3683 case MFI_STAT_LD_OFFLINE:
3684 case MFI_STAT_DEVICE_NOT_FOUND:
3685 cmd->scmd->result = DID_BAD_TARGET << 16;
3689 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3691 cmd->scmd->result = DID_ERROR << 16;
3695 atomic_dec(&instance->fw_outstanding);
3697 scsi_dma_unmap(cmd->scmd);
3698 scsi_done(cmd->scmd);
3699 megasas_return_cmd(instance, cmd);
3706 case MFI_CMD_TOOLBOX:
3707 megasas_complete_int_cmd(instance, cmd);
3711 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3712 /* Check for LD map update */
3713 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3714 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3715 fusion->fast_path_io = 0;
3716 spin_lock_irqsave(instance->host->host_lock, flags);
3717 status = cmd->frame->hdr.cmd_status;
3718 instance->map_update_cmd = NULL;
3719 if (status != MFI_STAT_OK) {
3720 if (status != MFI_STAT_NOT_FOUND)
3721 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3722 cmd->frame->hdr.cmd_status);
3724 megasas_return_cmd(instance, cmd);
3725 spin_unlock_irqrestore(
3726 instance->host->host_lock,
3732 megasas_return_cmd(instance, cmd);
3735 * Set fast path IO to ZERO.
3736 * Validate Map will set proper value.
3737 * Meanwhile all IOs will go as LD IO.
3739 if (status == MFI_STAT_OK &&
3740 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3742 fusion->fast_path_io = 1;
3744 fusion->fast_path_io = 0;
3747 if (instance->adapter_type >= INVADER_SERIES)
3748 megasas_set_ld_removed_by_fw(instance);
3750 megasas_sync_map_info(instance);
3751 spin_unlock_irqrestore(instance->host->host_lock,
3756 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3757 opcode == MR_DCMD_CTRL_EVENT_GET) {
3758 spin_lock_irqsave(&poll_aen_lock, flags);
3759 megasas_poll_wait_aen = 0;
3760 spin_unlock_irqrestore(&poll_aen_lock, flags);
3763 /* FW has an updated PD sequence */
3764 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3765 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3767 spin_lock_irqsave(instance->host->host_lock, flags);
3768 status = cmd->frame->hdr.cmd_status;
3769 instance->jbod_seq_cmd = NULL;
3770 megasas_return_cmd(instance, cmd);
3772 if (status == MFI_STAT_OK) {
3773 instance->pd_seq_map_id++;
3774 /* Re-register a pd sync seq num cmd */
3775 if (megasas_sync_pd_seq_num(instance, true))
3776 instance->use_seqnum_jbod_fp = false;
3778 instance->use_seqnum_jbod_fp = false;
3780 spin_unlock_irqrestore(instance->host->host_lock, flags);
3785 * See if got an event notification
3787 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3788 megasas_service_aen(instance, cmd);
3790 megasas_complete_int_cmd(instance, cmd);
3796 * Cmd issued to abort another cmd returned
3798 megasas_complete_abort(instance, cmd);
3802 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3804 megasas_complete_int_cmd(instance, cmd);
3810 * megasas_issue_pending_cmds_again - issue all pending cmds
3811 * in FW again because of the fw reset
3812 * @instance: Adapter soft state
3815 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3817 struct megasas_cmd *cmd;
3818 struct list_head clist_local;
3819 union megasas_evt_class_locale class_locale;
3820 unsigned long flags;
3823 INIT_LIST_HEAD(&clist_local);
3824 spin_lock_irqsave(&instance->hba_lock, flags);
3825 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3826 spin_unlock_irqrestore(&instance->hba_lock, flags);
3828 while (!list_empty(&clist_local)) {
3829 cmd = list_entry((&clist_local)->next,
3830 struct megasas_cmd, list);
3831 list_del_init(&cmd->list);
3833 if (cmd->sync_cmd || cmd->scmd) {
3834 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3835 "detected to be pending while HBA reset\n",
3836 cmd, cmd->scmd, cmd->sync_cmd);
3838 cmd->retry_for_fw_reset++;
3840 if (cmd->retry_for_fw_reset == 3) {
3841 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3842 "was tried multiple times during reset."
3843 "Shutting down the HBA\n",
3844 cmd, cmd->scmd, cmd->sync_cmd);
3845 instance->instancet->disable_intr(instance);
3846 atomic_set(&instance->fw_reset_no_pci_access, 1);
3847 megaraid_sas_kill_hba(instance);
3852 if (cmd->sync_cmd == 1) {
3854 dev_notice(&instance->pdev->dev, "unexpected"
3855 "cmd attached to internal command!\n");
3857 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3858 "on the internal reset queue,"
3859 "issue it again.\n", cmd);
3860 cmd->cmd_status_drv = DCMD_INIT;
3861 instance->instancet->fire_cmd(instance,
3862 cmd->frame_phys_addr,
3863 0, instance->reg_set);
3864 } else if (cmd->scmd) {
3865 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3866 "detected on the internal queue, issue again.\n",
3867 cmd, cmd->scmd->cmnd[0]);
3869 atomic_inc(&instance->fw_outstanding);
3870 instance->instancet->fire_cmd(instance,
3871 cmd->frame_phys_addr,
3872 cmd->frame_count-1, instance->reg_set);
3874 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3875 "internal reset defer list while re-issue!!\n",
3880 if (instance->aen_cmd) {
3881 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3882 megasas_return_cmd(instance, instance->aen_cmd);
3884 instance->aen_cmd = NULL;
3888 * Initiate AEN (Asynchronous Event Notification)
3890 seq_num = instance->last_seq_num;
3891 class_locale.members.reserved = 0;
3892 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3893 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3895 megasas_register_aen(instance, seq_num, class_locale.word);
3899 * Move the internal reset pending commands to a deferred queue.
3901 * We move the commands pending at internal reset time to a
3902 * pending queue. This queue would be flushed after successful
3903 * completion of the internal reset sequence. if the internal reset
3904 * did not complete in time, the kernel reset handler would flush
3908 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3910 struct megasas_cmd *cmd;
3912 u16 max_cmd = instance->max_fw_cmds;
3914 unsigned long flags;
3917 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3918 for (i = 0; i < max_cmd; i++) {
3919 cmd = instance->cmd_list[i];
3920 if (cmd->sync_cmd == 1 || cmd->scmd) {
3921 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3922 "on the defer queue as internal\n",
3923 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3925 if (!list_empty(&cmd->list)) {
3926 dev_notice(&instance->pdev->dev, "ERROR while"
3927 " moving this cmd:%p, %d %p, it was"
3928 "discovered on some list?\n",
3929 cmd, cmd->sync_cmd, cmd->scmd);
3931 list_del_init(&cmd->list);
3934 list_add_tail(&cmd->list,
3935 &instance->internal_reset_pending_q);
3938 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3943 process_fw_state_change_wq(struct work_struct *work)
3945 struct megasas_instance *instance =
3946 container_of(work, struct megasas_instance, work_init);
3948 unsigned long flags;
3950 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3951 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3952 atomic_read(&instance->adprecovery));
3956 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3957 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3958 "state, restarting it...\n");
3960 instance->instancet->disable_intr(instance);
3961 atomic_set(&instance->fw_outstanding, 0);
3963 atomic_set(&instance->fw_reset_no_pci_access, 1);
3964 instance->instancet->adp_reset(instance, instance->reg_set);
3965 atomic_set(&instance->fw_reset_no_pci_access, 0);
3967 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3968 "initiating next stage...\n");
3970 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3971 "state 2 starting...\n");
3973 /* waiting for about 20 second before start the second init */
3974 for (wait = 0; wait < 30; wait++) {
3978 if (megasas_transition_to_ready(instance, 1)) {
3979 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3981 atomic_set(&instance->fw_reset_no_pci_access, 1);
3982 megaraid_sas_kill_hba(instance);
3986 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3987 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3988 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3990 *instance->consumer = *instance->producer;
3992 *instance->consumer = 0;
3993 *instance->producer = 0;
3996 megasas_issue_init_mfi(instance);
3998 spin_lock_irqsave(&instance->hba_lock, flags);
3999 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
4000 spin_unlock_irqrestore(&instance->hba_lock, flags);
4001 instance->instancet->enable_intr(instance);
4003 megasas_issue_pending_cmds_again(instance);
4004 instance->issuepend_done = 1;
4009 * megasas_deplete_reply_queue - Processes all completed commands
4010 * @instance: Adapter soft state
4011 * @alt_status: Alternate status to be returned to
4012 * SCSI mid-layer instead of the status
4013 * returned by the FW
4014 * Note: this must be called with hba lock held
4017 megasas_deplete_reply_queue(struct megasas_instance *instance,
4023 if (instance->instancet->check_reset(instance, instance->reg_set) == 1)
4026 mfiStatus = instance->instancet->clear_intr(instance);
4027 if (mfiStatus == 0) {
4028 /* Hardware may not set outbound_intr_status in MSI-X mode */
4029 if (!instance->msix_vectors)
4033 instance->mfiStatus = mfiStatus;
4035 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
4036 fw_state = instance->instancet->read_fw_status_reg(
4037 instance) & MFI_STATE_MASK;
4039 if (fw_state != MFI_STATE_FAULT) {
4040 dev_notice(&instance->pdev->dev, "fw state:%x\n",
4044 if ((fw_state == MFI_STATE_FAULT) &&
4045 (instance->disableOnlineCtrlReset == 0)) {
4046 dev_notice(&instance->pdev->dev, "wait adp restart\n");
4048 if ((instance->pdev->device ==
4049 PCI_DEVICE_ID_LSI_SAS1064R) ||
4050 (instance->pdev->device ==
4051 PCI_DEVICE_ID_DELL_PERC5) ||
4052 (instance->pdev->device ==
4053 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
4055 *instance->consumer =
4056 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
4060 instance->instancet->disable_intr(instance);
4061 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4062 instance->issuepend_done = 0;
4064 atomic_set(&instance->fw_outstanding, 0);
4065 megasas_internal_reset_defer_cmds(instance);
4067 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
4068 fw_state, atomic_read(&instance->adprecovery));
4070 schedule_work(&instance->work_init);
4074 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
4075 fw_state, instance->disableOnlineCtrlReset);
4079 tasklet_schedule(&instance->isr_tasklet);
4084 * megasas_isr - isr entry point
4086 * @devp: IRQ context address
4088 static irqreturn_t megasas_isr(int irq, void *devp)
4090 struct megasas_irq_context *irq_context = devp;
4091 struct megasas_instance *instance = irq_context->instance;
4092 unsigned long flags;
4095 if (atomic_read(&instance->fw_reset_no_pci_access))
4098 spin_lock_irqsave(&instance->hba_lock, flags);
4099 rc = megasas_deplete_reply_queue(instance, DID_OK);
4100 spin_unlock_irqrestore(&instance->hba_lock, flags);
4106 * megasas_transition_to_ready - Move the FW to READY state
4107 * @instance: Adapter soft state
4108 * @ocr: Adapter reset state
4110 * During the initialization, FW passes can potentially be in any one of
4111 * several possible states. If the FW in operational, waiting-for-handshake
4112 * states, driver must take steps to bring it to ready state. Otherwise, it
4113 * has to wait for the ready state.
4116 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
4121 u32 abs_state, curr_abs_state;
4123 abs_state = instance->instancet->read_fw_status_reg(instance);
4124 fw_state = abs_state & MFI_STATE_MASK;
4126 if (fw_state != MFI_STATE_READY)
4127 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
4130 while (fw_state != MFI_STATE_READY) {
4134 case MFI_STATE_FAULT:
4135 dev_printk(KERN_ERR, &instance->pdev->dev,
4136 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
4137 abs_state & MFI_STATE_FAULT_CODE,
4138 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4140 max_wait = MEGASAS_RESET_WAIT_TIME;
4143 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4144 megasas_dump_reg_set(instance->reg_set);
4148 case MFI_STATE_WAIT_HANDSHAKE:
4150 * Set the CLR bit in inbound doorbell
4152 if ((instance->pdev->device ==
4153 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4154 (instance->pdev->device ==
4155 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4156 (instance->adapter_type != MFI_SERIES))
4158 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4159 &instance->reg_set->doorbell);
4162 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4163 &instance->reg_set->inbound_doorbell);
4165 max_wait = MEGASAS_RESET_WAIT_TIME;
4168 case MFI_STATE_BOOT_MESSAGE_PENDING:
4169 if ((instance->pdev->device ==
4170 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4171 (instance->pdev->device ==
4172 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4173 (instance->adapter_type != MFI_SERIES))
4174 writel(MFI_INIT_HOTPLUG,
4175 &instance->reg_set->doorbell);
4177 writel(MFI_INIT_HOTPLUG,
4178 &instance->reg_set->inbound_doorbell);
4180 max_wait = MEGASAS_RESET_WAIT_TIME;
4183 case MFI_STATE_OPERATIONAL:
4185 * Bring it to READY state; assuming max wait 10 secs
4187 instance->instancet->disable_intr(instance);
4188 if ((instance->pdev->device ==
4189 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4190 (instance->pdev->device ==
4191 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4192 (instance->adapter_type != MFI_SERIES)) {
4193 writel(MFI_RESET_FLAGS,
4194 &instance->reg_set->doorbell);
4196 if (instance->adapter_type != MFI_SERIES) {
4197 for (i = 0; i < (10 * 1000); i += 20) {
4209 writel(MFI_RESET_FLAGS,
4210 &instance->reg_set->inbound_doorbell);
4212 max_wait = MEGASAS_RESET_WAIT_TIME;
4215 case MFI_STATE_UNDEFINED:
4217 * This state should not last for more than 2 seconds
4219 max_wait = MEGASAS_RESET_WAIT_TIME;
4222 case MFI_STATE_BB_INIT:
4223 max_wait = MEGASAS_RESET_WAIT_TIME;
4226 case MFI_STATE_FW_INIT:
4227 max_wait = MEGASAS_RESET_WAIT_TIME;
4230 case MFI_STATE_FW_INIT_2:
4231 max_wait = MEGASAS_RESET_WAIT_TIME;
4234 case MFI_STATE_DEVICE_SCAN:
4235 max_wait = MEGASAS_RESET_WAIT_TIME;
4238 case MFI_STATE_FLUSH_CACHE:
4239 max_wait = MEGASAS_RESET_WAIT_TIME;
4243 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4245 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4246 megasas_dump_reg_set(instance->reg_set);
4251 * The cur_state should not last for more than max_wait secs
4253 for (i = 0; i < max_wait * 50; i++) {
4254 curr_abs_state = instance->instancet->
4255 read_fw_status_reg(instance);
4257 if (abs_state == curr_abs_state) {
4264 * Return error if fw_state hasn't changed after max_wait
4266 if (curr_abs_state == abs_state) {
4267 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4268 "in %d secs\n", fw_state, max_wait);
4269 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4270 megasas_dump_reg_set(instance->reg_set);
4274 abs_state = curr_abs_state;
4275 fw_state = curr_abs_state & MFI_STATE_MASK;
4277 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4283 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4284 * @instance: Adapter soft state
4286 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4289 u16 max_cmd = instance->max_mfi_cmds;
4290 struct megasas_cmd *cmd;
4292 if (!instance->frame_dma_pool)
4296 * Return all frames to pool
4298 for (i = 0; i < max_cmd; i++) {
4300 cmd = instance->cmd_list[i];
4303 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4304 cmd->frame_phys_addr);
4307 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4308 cmd->sense_phys_addr);
4312 * Now destroy the pool itself
4314 dma_pool_destroy(instance->frame_dma_pool);
4315 dma_pool_destroy(instance->sense_dma_pool);
4317 instance->frame_dma_pool = NULL;
4318 instance->sense_dma_pool = NULL;
4322 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4323 * @instance: Adapter soft state
4325 * Each command packet has an embedded DMA memory buffer that is used for
4326 * filling MFI frame and the SG list that immediately follows the frame. This
4327 * function creates those DMA memory buffers for each command packet by using
4328 * PCI pool facility.
4330 static int megasas_create_frame_pool(struct megasas_instance *instance)
4335 struct megasas_cmd *cmd;
4337 max_cmd = instance->max_mfi_cmds;
4340 * For MFI controllers.
4342 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4343 * Total 960 byte (15 MFI frame of 64 byte)
4345 * Fusion adapter require only 3 extra frame.
4346 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4347 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4348 * Total 192 byte (3 MFI frame of 64 byte)
4350 frame_count = (instance->adapter_type == MFI_SERIES) ?
4352 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4354 * Use DMA pool facility provided by PCI layer
4356 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4357 &instance->pdev->dev,
4358 instance->mfi_frame_size, 256, 0);
4360 if (!instance->frame_dma_pool) {
4361 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4365 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4366 &instance->pdev->dev, 128,
4369 if (!instance->sense_dma_pool) {
4370 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4372 dma_pool_destroy(instance->frame_dma_pool);
4373 instance->frame_dma_pool = NULL;
4379 * Allocate and attach a frame to each of the commands in cmd_list.
4380 * By making cmd->index as the context instead of the &cmd, we can
4381 * always use 32bit context regardless of the architecture
4383 for (i = 0; i < max_cmd; i++) {
4385 cmd = instance->cmd_list[i];
4387 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4388 GFP_KERNEL, &cmd->frame_phys_addr);
4390 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4391 GFP_KERNEL, &cmd->sense_phys_addr);
4394 * megasas_teardown_frame_pool() takes care of freeing
4395 * whatever has been allocated
4397 if (!cmd->frame || !cmd->sense) {
4398 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4399 megasas_teardown_frame_pool(instance);
4403 cmd->frame->io.context = cpu_to_le32(cmd->index);
4404 cmd->frame->io.pad_0 = 0;
4405 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4406 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4413 * megasas_free_cmds - Free all the cmds in the free cmd pool
4414 * @instance: Adapter soft state
4416 void megasas_free_cmds(struct megasas_instance *instance)
4420 /* First free the MFI frame pool */
4421 megasas_teardown_frame_pool(instance);
4423 /* Free all the commands in the cmd_list */
4424 for (i = 0; i < instance->max_mfi_cmds; i++)
4426 kfree(instance->cmd_list[i]);
4428 /* Free the cmd_list buffer itself */
4429 kfree(instance->cmd_list);
4430 instance->cmd_list = NULL;
4432 INIT_LIST_HEAD(&instance->cmd_pool);
4436 * megasas_alloc_cmds - Allocates the command packets
4437 * @instance: Adapter soft state
4439 * Each command that is issued to the FW, whether IO commands from the OS or
4440 * internal commands like IOCTLs, are wrapped in local data structure called
4441 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4444 * Each frame has a 32-bit field called context (tag). This context is used
4445 * to get back the megasas_cmd from the frame when a frame gets completed in
4446 * the ISR. Typically the address of the megasas_cmd itself would be used as
4447 * the context. But we wanted to keep the differences between 32 and 64 bit
4448 * systems to the mininum. We always use 32 bit integers for the context. In
4449 * this driver, the 32 bit values are the indices into an array cmd_list.
4450 * This array is used only to look up the megasas_cmd given the context. The
4451 * free commands themselves are maintained in a linked list called cmd_pool.
4453 int megasas_alloc_cmds(struct megasas_instance *instance)
4458 struct megasas_cmd *cmd;
4460 max_cmd = instance->max_mfi_cmds;
4463 * instance->cmd_list is an array of struct megasas_cmd pointers.
4464 * Allocate the dynamic array first and then allocate individual
4467 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4469 if (!instance->cmd_list) {
4470 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4474 for (i = 0; i < max_cmd; i++) {
4475 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4478 if (!instance->cmd_list[i]) {
4480 for (j = 0; j < i; j++)
4481 kfree(instance->cmd_list[j]);
4483 kfree(instance->cmd_list);
4484 instance->cmd_list = NULL;
4490 for (i = 0; i < max_cmd; i++) {
4491 cmd = instance->cmd_list[i];
4492 memset(cmd, 0, sizeof(struct megasas_cmd));
4495 cmd->instance = instance;
4497 list_add_tail(&cmd->list, &instance->cmd_pool);
4501 * Create a frame pool and assign one frame to each cmd
4503 if (megasas_create_frame_pool(instance)) {
4504 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4505 megasas_free_cmds(instance);
4513 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4514 * @instance: Adapter soft state
4516 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4517 * or FW is not under OCR.
4520 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4522 if (instance->adapter_type == MFI_SERIES)
4523 return KILL_ADAPTER;
4524 else if (instance->unload ||
4525 test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4526 &instance->reset_flags))
4527 return IGNORE_TIMEOUT;
4529 return INITIATE_OCR;
4533 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4536 struct megasas_cmd *cmd;
4537 struct megasas_dcmd_frame *dcmd;
4539 struct MR_PRIV_DEVICE *mr_device_priv_data;
4542 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4543 cmd = megasas_get_cmd(instance);
4546 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4550 dcmd = &cmd->frame->dcmd;
4552 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4553 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4555 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4556 dcmd->cmd = MFI_CMD_DCMD;
4557 dcmd->cmd_status = 0xFF;
4558 dcmd->sge_count = 1;
4559 dcmd->flags = MFI_FRAME_DIR_READ;
4562 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4563 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4565 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4566 sizeof(struct MR_PD_INFO));
4568 if ((instance->adapter_type != MFI_SERIES) &&
4569 !instance->mask_interrupts)
4570 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4572 ret = megasas_issue_polled(instance, cmd);
4576 mr_device_priv_data = sdev->hostdata;
4577 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4578 mr_device_priv_data->interface_type =
4579 instance->pd_info->state.ddf.pdType.intf;
4584 switch (dcmd_timeout_ocr_possible(instance)) {
4586 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4587 mutex_unlock(&instance->reset_mutex);
4588 megasas_reset_fusion(instance->host,
4589 MFI_IO_TIMEOUT_OCR);
4590 mutex_lock(&instance->reset_mutex);
4593 megaraid_sas_kill_hba(instance);
4595 case IGNORE_TIMEOUT:
4596 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4597 __func__, __LINE__);
4604 if (ret != DCMD_TIMEOUT)
4605 megasas_return_cmd(instance, cmd);
4610 * megasas_get_pd_list_info - Returns FW's pd_list structure
4611 * @instance: Adapter soft state
4612 * @pd_list: pd_list structure
4614 * Issues an internal command (DCMD) to get the FW's controller PD
4615 * list structure. This information is mainly used to find out SYSTEM
4616 * supported by the FW.
4619 megasas_get_pd_list(struct megasas_instance *instance)
4621 int ret = 0, pd_index = 0;
4622 struct megasas_cmd *cmd;
4623 struct megasas_dcmd_frame *dcmd;
4624 struct MR_PD_LIST *ci;
4625 struct MR_PD_ADDRESS *pd_addr;
4627 if (instance->pd_list_not_supported) {
4628 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4629 "not supported by firmware\n");
4633 ci = instance->pd_list_buf;
4635 cmd = megasas_get_cmd(instance);
4638 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4642 dcmd = &cmd->frame->dcmd;
4644 memset(ci, 0, sizeof(*ci));
4645 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4647 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4648 dcmd->mbox.b[1] = 0;
4649 dcmd->cmd = MFI_CMD_DCMD;
4650 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4651 dcmd->sge_count = 1;
4652 dcmd->flags = MFI_FRAME_DIR_READ;
4655 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4656 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4658 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4659 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4661 if ((instance->adapter_type != MFI_SERIES) &&
4662 !instance->mask_interrupts)
4663 ret = megasas_issue_blocked_cmd(instance, cmd,
4664 MFI_IO_TIMEOUT_SECS);
4666 ret = megasas_issue_polled(instance, cmd);
4670 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4671 "failed/not supported by firmware\n");
4673 if (instance->adapter_type != MFI_SERIES)
4674 megaraid_sas_kill_hba(instance);
4676 instance->pd_list_not_supported = 1;
4680 switch (dcmd_timeout_ocr_possible(instance)) {
4682 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4684 * DCMD failed from AEN path.
4685 * AEN path already hold reset_mutex to avoid PCI access
4686 * while OCR is in progress.
4688 mutex_unlock(&instance->reset_mutex);
4689 megasas_reset_fusion(instance->host,
4690 MFI_IO_TIMEOUT_OCR);
4691 mutex_lock(&instance->reset_mutex);
4694 megaraid_sas_kill_hba(instance);
4696 case IGNORE_TIMEOUT:
4697 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4698 __func__, __LINE__);
4706 if (megasas_dbg_lvl & LD_PD_DEBUG)
4707 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4708 __func__, le32_to_cpu(ci->count));
4710 if ((le32_to_cpu(ci->count) >
4711 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4714 memset(instance->local_pd_list, 0,
4715 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4717 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4718 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4719 le16_to_cpu(pd_addr->deviceId);
4720 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4721 pd_addr->scsiDevType;
4722 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4724 if (megasas_dbg_lvl & LD_PD_DEBUG)
4725 dev_info(&instance->pdev->dev,
4726 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4727 pd_index, le16_to_cpu(pd_addr->deviceId),
4728 pd_addr->scsiDevType);
4732 memcpy(instance->pd_list, instance->local_pd_list,
4733 sizeof(instance->pd_list));
4738 if (ret != DCMD_TIMEOUT)
4739 megasas_return_cmd(instance, cmd);
4745 * megasas_get_ld_list_info - Returns FW's ld_list structure
4746 * @instance: Adapter soft state
4747 * @ld_list: ld_list structure
4749 * Issues an internal command (DCMD) to get the FW's controller PD
4750 * list structure. This information is mainly used to find out SYSTEM
4751 * supported by the FW.
4754 megasas_get_ld_list(struct megasas_instance *instance)
4756 int ret = 0, ld_index = 0, ids = 0;
4757 struct megasas_cmd *cmd;
4758 struct megasas_dcmd_frame *dcmd;
4759 struct MR_LD_LIST *ci;
4760 dma_addr_t ci_h = 0;
4763 ci = instance->ld_list_buf;
4764 ci_h = instance->ld_list_buf_h;
4766 cmd = megasas_get_cmd(instance);
4769 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4773 dcmd = &cmd->frame->dcmd;
4775 memset(ci, 0, sizeof(*ci));
4776 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4778 if (instance->supportmax256vd)
4779 dcmd->mbox.b[0] = 1;
4780 dcmd->cmd = MFI_CMD_DCMD;
4781 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4782 dcmd->sge_count = 1;
4783 dcmd->flags = MFI_FRAME_DIR_READ;
4785 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4786 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4789 megasas_set_dma_settings(instance, dcmd, ci_h,
4790 sizeof(struct MR_LD_LIST));
4792 if ((instance->adapter_type != MFI_SERIES) &&
4793 !instance->mask_interrupts)
4794 ret = megasas_issue_blocked_cmd(instance, cmd,
4795 MFI_IO_TIMEOUT_SECS);
4797 ret = megasas_issue_polled(instance, cmd);
4799 ld_count = le32_to_cpu(ci->ldCount);
4803 megaraid_sas_kill_hba(instance);
4807 switch (dcmd_timeout_ocr_possible(instance)) {
4809 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4811 * DCMD failed from AEN path.
4812 * AEN path already hold reset_mutex to avoid PCI access
4813 * while OCR is in progress.
4815 mutex_unlock(&instance->reset_mutex);
4816 megasas_reset_fusion(instance->host,
4817 MFI_IO_TIMEOUT_OCR);
4818 mutex_lock(&instance->reset_mutex);
4821 megaraid_sas_kill_hba(instance);
4823 case IGNORE_TIMEOUT:
4824 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4825 __func__, __LINE__);
4832 if (megasas_dbg_lvl & LD_PD_DEBUG)
4833 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4834 __func__, ld_count);
4836 if (ld_count > instance->fw_supported_vd_count)
4839 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4841 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4842 if (ci->ldList[ld_index].state != 0) {
4843 ids = ci->ldList[ld_index].ref.targetId;
4844 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4845 if (megasas_dbg_lvl & LD_PD_DEBUG)
4846 dev_info(&instance->pdev->dev,
4847 "LD%d: targetID: 0x%03x\n",
4855 if (ret != DCMD_TIMEOUT)
4856 megasas_return_cmd(instance, cmd);
4862 * megasas_ld_list_query - Returns FW's ld_list structure
4863 * @instance: Adapter soft state
4864 * @query_type: ld_list structure type
4866 * Issues an internal command (DCMD) to get the FW's controller PD
4867 * list structure. This information is mainly used to find out SYSTEM
4868 * supported by the FW.
4871 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4873 int ret = 0, ld_index = 0, ids = 0;
4874 struct megasas_cmd *cmd;
4875 struct megasas_dcmd_frame *dcmd;
4876 struct MR_LD_TARGETID_LIST *ci;
4877 dma_addr_t ci_h = 0;
4880 ci = instance->ld_targetid_list_buf;
4881 ci_h = instance->ld_targetid_list_buf_h;
4883 cmd = megasas_get_cmd(instance);
4886 dev_warn(&instance->pdev->dev,
4887 "megasas_ld_list_query: Failed to get cmd\n");
4891 dcmd = &cmd->frame->dcmd;
4893 memset(ci, 0, sizeof(*ci));
4894 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4896 dcmd->mbox.b[0] = query_type;
4897 if (instance->supportmax256vd)
4898 dcmd->mbox.b[2] = 1;
4900 dcmd->cmd = MFI_CMD_DCMD;
4901 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4902 dcmd->sge_count = 1;
4903 dcmd->flags = MFI_FRAME_DIR_READ;
4905 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4906 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4909 megasas_set_dma_settings(instance, dcmd, ci_h,
4910 sizeof(struct MR_LD_TARGETID_LIST));
4912 if ((instance->adapter_type != MFI_SERIES) &&
4913 !instance->mask_interrupts)
4914 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4916 ret = megasas_issue_polled(instance, cmd);
4920 dev_info(&instance->pdev->dev,
4921 "DCMD not supported by firmware - %s %d\n",
4922 __func__, __LINE__);
4923 ret = megasas_get_ld_list(instance);
4926 switch (dcmd_timeout_ocr_possible(instance)) {
4928 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4930 * DCMD failed from AEN path.
4931 * AEN path already hold reset_mutex to avoid PCI access
4932 * while OCR is in progress.
4934 mutex_unlock(&instance->reset_mutex);
4935 megasas_reset_fusion(instance->host,
4936 MFI_IO_TIMEOUT_OCR);
4937 mutex_lock(&instance->reset_mutex);
4940 megaraid_sas_kill_hba(instance);
4942 case IGNORE_TIMEOUT:
4943 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4944 __func__, __LINE__);
4950 tgtid_count = le32_to_cpu(ci->count);
4952 if (megasas_dbg_lvl & LD_PD_DEBUG)
4953 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4954 __func__, tgtid_count);
4956 if ((tgtid_count > (instance->fw_supported_vd_count)))
4959 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4960 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4961 ids = ci->targetId[ld_index];
4962 instance->ld_ids[ids] = ci->targetId[ld_index];
4963 if (megasas_dbg_lvl & LD_PD_DEBUG)
4964 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4965 ld_index, ci->targetId[ld_index]);
4971 if (ret != DCMD_TIMEOUT)
4972 megasas_return_cmd(instance, cmd);
4978 * megasas_host_device_list_query
4979 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4980 * dcmd.mbox - reserved
4981 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4982 * Desc: This DCMD will return the combined device list
4983 * Status: MFI_STAT_OK - List returned successfully
4984 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4986 * @instance: Adapter soft state
4987 * @is_probe: Driver probe check
4988 * Return: 0 if DCMD succeeded
4989 * non-zero if failed
4992 megasas_host_device_list_query(struct megasas_instance *instance,
4995 int ret, i, target_id;
4996 struct megasas_cmd *cmd;
4997 struct megasas_dcmd_frame *dcmd;
4998 struct MR_HOST_DEVICE_LIST *ci;
5002 ci = instance->host_device_list_buf;
5003 ci_h = instance->host_device_list_buf_h;
5005 cmd = megasas_get_cmd(instance);
5008 dev_warn(&instance->pdev->dev,
5009 "%s: failed to get cmd\n",
5014 dcmd = &cmd->frame->dcmd;
5016 memset(ci, 0, sizeof(*ci));
5017 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5019 dcmd->mbox.b[0] = is_probe ? 0 : 1;
5020 dcmd->cmd = MFI_CMD_DCMD;
5021 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5022 dcmd->sge_count = 1;
5023 dcmd->flags = MFI_FRAME_DIR_READ;
5026 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
5027 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
5029 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
5031 if (!instance->mask_interrupts) {
5032 ret = megasas_issue_blocked_cmd(instance, cmd,
5033 MFI_IO_TIMEOUT_SECS);
5035 ret = megasas_issue_polled(instance, cmd);
5036 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5041 /* Fill the internal pd_list and ld_ids array based on
5042 * targetIds returned by FW
5044 count = le32_to_cpu(ci->count);
5046 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
5049 if (megasas_dbg_lvl & LD_PD_DEBUG)
5050 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
5053 memset(instance->local_pd_list, 0,
5054 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
5055 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
5056 for (i = 0; i < count; i++) {
5057 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
5058 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
5059 instance->local_pd_list[target_id].tid = target_id;
5060 instance->local_pd_list[target_id].driveType =
5061 ci->host_device_list[i].scsi_type;
5062 instance->local_pd_list[target_id].driveState =
5064 if (megasas_dbg_lvl & LD_PD_DEBUG)
5065 dev_info(&instance->pdev->dev,
5066 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
5067 i, target_id, ci->host_device_list[i].scsi_type);
5069 instance->ld_ids[target_id] = target_id;
5070 if (megasas_dbg_lvl & LD_PD_DEBUG)
5071 dev_info(&instance->pdev->dev,
5072 "Device %d: LD targetID: 0x%03x\n",
5077 memcpy(instance->pd_list, instance->local_pd_list,
5078 sizeof(instance->pd_list));
5082 switch (dcmd_timeout_ocr_possible(instance)) {
5084 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5085 mutex_unlock(&instance->reset_mutex);
5086 megasas_reset_fusion(instance->host,
5087 MFI_IO_TIMEOUT_OCR);
5088 mutex_lock(&instance->reset_mutex);
5091 megaraid_sas_kill_hba(instance);
5093 case IGNORE_TIMEOUT:
5094 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5095 __func__, __LINE__);
5100 dev_err(&instance->pdev->dev,
5101 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
5106 if (ret != DCMD_TIMEOUT)
5107 megasas_return_cmd(instance, cmd);
5113 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
5114 * instance : Controller's instance
5116 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
5118 struct fusion_context *fusion;
5119 u32 ventura_map_sz = 0;
5121 fusion = instance->ctrl_context;
5122 /* For MFI based controllers return dummy success */
5126 instance->supportmax256vd =
5127 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
5128 /* Below is additional check to address future FW enhancement */
5129 if (instance->ctrl_info_buf->max_lds > 64)
5130 instance->supportmax256vd = 1;
5132 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
5133 * MEGASAS_MAX_DEV_PER_CHANNEL;
5134 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
5135 * MEGASAS_MAX_DEV_PER_CHANNEL;
5136 if (instance->supportmax256vd) {
5137 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
5138 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5140 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5141 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5144 dev_info(&instance->pdev->dev,
5145 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
5146 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
5147 instance->ctrl_info_buf->max_lds);
5149 if (instance->max_raid_mapsize) {
5150 ventura_map_sz = instance->max_raid_mapsize *
5151 MR_MIN_MAP_SIZE; /* 64k */
5152 fusion->current_map_sz = ventura_map_sz;
5153 fusion->max_map_sz = ventura_map_sz;
5155 fusion->old_map_sz =
5156 struct_size((struct MR_FW_RAID_MAP *)0, ldSpanMap,
5157 instance->fw_supported_vd_count);
5158 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
5160 fusion->max_map_sz =
5161 max(fusion->old_map_sz, fusion->new_map_sz);
5163 if (instance->supportmax256vd)
5164 fusion->current_map_sz = fusion->new_map_sz;
5166 fusion->current_map_sz = fusion->old_map_sz;
5168 /* irrespective of FW raid maps, driver raid map is constant */
5169 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
5173 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
5174 * dcmd.hdr.length - number of bytes to read
5175 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
5176 * Desc: Fill in snapdump properties
5177 * Status: MFI_STAT_OK- Command successful
5179 void megasas_get_snapdump_properties(struct megasas_instance *instance)
5182 struct megasas_cmd *cmd;
5183 struct megasas_dcmd_frame *dcmd;
5184 struct MR_SNAPDUMP_PROPERTIES *ci;
5185 dma_addr_t ci_h = 0;
5187 ci = instance->snapdump_prop;
5188 ci_h = instance->snapdump_prop_h;
5193 cmd = megasas_get_cmd(instance);
5196 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
5200 dcmd = &cmd->frame->dcmd;
5202 memset(ci, 0, sizeof(*ci));
5203 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5205 dcmd->cmd = MFI_CMD_DCMD;
5206 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5207 dcmd->sge_count = 1;
5208 dcmd->flags = MFI_FRAME_DIR_READ;
5211 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5212 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5214 megasas_set_dma_settings(instance, dcmd, ci_h,
5215 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5217 if (!instance->mask_interrupts) {
5218 ret = megasas_issue_blocked_cmd(instance, cmd,
5219 MFI_IO_TIMEOUT_SECS);
5221 ret = megasas_issue_polled(instance, cmd);
5222 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5227 instance->snapdump_wait_time =
5228 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5229 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5233 switch (dcmd_timeout_ocr_possible(instance)) {
5235 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5236 mutex_unlock(&instance->reset_mutex);
5237 megasas_reset_fusion(instance->host,
5238 MFI_IO_TIMEOUT_OCR);
5239 mutex_lock(&instance->reset_mutex);
5242 megaraid_sas_kill_hba(instance);
5244 case IGNORE_TIMEOUT:
5245 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5246 __func__, __LINE__);
5251 if (ret != DCMD_TIMEOUT)
5252 megasas_return_cmd(instance, cmd);
5256 * megasas_get_ctrl_info - Returns FW's controller structure
5257 * @instance: Adapter soft state
5259 * Issues an internal command (DCMD) to get the FW's controller structure.
5260 * This information is mainly used to find out the maximum IO transfer per
5261 * command supported by the FW.
5264 megasas_get_ctrl_info(struct megasas_instance *instance)
5267 struct megasas_cmd *cmd;
5268 struct megasas_dcmd_frame *dcmd;
5269 struct megasas_ctrl_info *ci;
5270 dma_addr_t ci_h = 0;
5272 ci = instance->ctrl_info_buf;
5273 ci_h = instance->ctrl_info_buf_h;
5275 cmd = megasas_get_cmd(instance);
5278 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5282 dcmd = &cmd->frame->dcmd;
5284 memset(ci, 0, sizeof(*ci));
5285 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5287 dcmd->cmd = MFI_CMD_DCMD;
5288 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5289 dcmd->sge_count = 1;
5290 dcmd->flags = MFI_FRAME_DIR_READ;
5293 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5294 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5295 dcmd->mbox.b[0] = 1;
5297 megasas_set_dma_settings(instance, dcmd, ci_h,
5298 sizeof(struct megasas_ctrl_info));
5300 if ((instance->adapter_type != MFI_SERIES) &&
5301 !instance->mask_interrupts) {
5302 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5304 ret = megasas_issue_polled(instance, cmd);
5305 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5310 /* Save required controller information in
5311 * CPU endianness format.
5313 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5314 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5315 le32_to_cpus((u32 *)&ci->adapterOperations2);
5316 le32_to_cpus((u32 *)&ci->adapterOperations3);
5317 le16_to_cpus((u16 *)&ci->adapter_operations4);
5318 le32_to_cpus((u32 *)&ci->adapter_operations5);
5320 /* Update the latest Ext VD info.
5321 * From Init path, store current firmware details.
5322 * From OCR path, detect any firmware properties changes.
5323 * in case of Firmware upgrade without system reboot.
5325 megasas_update_ext_vd_details(instance);
5326 instance->support_seqnum_jbod_fp =
5327 ci->adapterOperations3.useSeqNumJbodFP;
5328 instance->support_morethan256jbod =
5329 ci->adapter_operations4.support_pd_map_target_id;
5330 instance->support_nvme_passthru =
5331 ci->adapter_operations4.support_nvme_passthru;
5332 instance->support_pci_lane_margining =
5333 ci->adapter_operations5.support_pci_lane_margining;
5334 instance->task_abort_tmo = ci->TaskAbortTO;
5335 instance->max_reset_tmo = ci->MaxResetTO;
5337 /*Check whether controller is iMR or MR */
5338 instance->is_imr = (ci->memory_size ? 0 : 1);
5340 instance->snapdump_wait_time =
5341 (ci->properties.on_off_properties2.enable_snap_dump ?
5342 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5344 instance->enable_fw_dev_list =
5345 ci->properties.on_off_properties2.enable_fw_dev_list;
5347 dev_info(&instance->pdev->dev,
5348 "controller type\t: %s(%dMB)\n",
5349 instance->is_imr ? "iMR" : "MR",
5350 le16_to_cpu(ci->memory_size));
5352 instance->disableOnlineCtrlReset =
5353 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5354 instance->secure_jbod_support =
5355 ci->adapterOperations3.supportSecurityonJBOD;
5356 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5357 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5358 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5359 instance->secure_jbod_support ? "Yes" : "No");
5360 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5361 instance->support_nvme_passthru ? "Yes" : "No");
5362 dev_info(&instance->pdev->dev,
5363 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5364 instance->task_abort_tmo, instance->max_reset_tmo);
5365 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5366 instance->support_seqnum_jbod_fp ? "Yes" : "No");
5367 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5368 instance->support_pci_lane_margining ? "Yes" : "No");
5373 switch (dcmd_timeout_ocr_possible(instance)) {
5375 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5376 mutex_unlock(&instance->reset_mutex);
5377 megasas_reset_fusion(instance->host,
5378 MFI_IO_TIMEOUT_OCR);
5379 mutex_lock(&instance->reset_mutex);
5382 megaraid_sas_kill_hba(instance);
5384 case IGNORE_TIMEOUT:
5385 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5386 __func__, __LINE__);
5391 megaraid_sas_kill_hba(instance);
5396 if (ret != DCMD_TIMEOUT)
5397 megasas_return_cmd(instance, cmd);
5403 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5406 * @instance: Adapter soft state
5407 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5408 MR_CRASH_BUF_TURN_OFF = 0
5409 MR_CRASH_BUF_TURN_ON = 1
5410 * @return 0 on success non-zero on failure.
5411 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5412 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5413 * that driver supports crash dump feature. This DCMD will be sent only if
5414 * crash dump feature is supported by the FW.
5417 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5421 struct megasas_cmd *cmd;
5422 struct megasas_dcmd_frame *dcmd;
5424 cmd = megasas_get_cmd(instance);
5427 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5432 dcmd = &cmd->frame->dcmd;
5434 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5435 dcmd->mbox.b[0] = crash_buf_state;
5436 dcmd->cmd = MFI_CMD_DCMD;
5437 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5438 dcmd->sge_count = 1;
5439 dcmd->flags = MFI_FRAME_DIR_NONE;
5442 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5443 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5445 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5446 CRASH_DMA_BUF_SIZE);
5448 if ((instance->adapter_type != MFI_SERIES) &&
5449 !instance->mask_interrupts)
5450 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5452 ret = megasas_issue_polled(instance, cmd);
5454 if (ret == DCMD_TIMEOUT) {
5455 switch (dcmd_timeout_ocr_possible(instance)) {
5457 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5458 megasas_reset_fusion(instance->host,
5459 MFI_IO_TIMEOUT_OCR);
5462 megaraid_sas_kill_hba(instance);
5464 case IGNORE_TIMEOUT:
5465 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5466 __func__, __LINE__);
5470 megasas_return_cmd(instance, cmd);
5476 * megasas_issue_init_mfi - Initializes the FW
5477 * @instance: Adapter soft state
5479 * Issues the INIT MFI cmd
5482 megasas_issue_init_mfi(struct megasas_instance *instance)
5485 struct megasas_cmd *cmd;
5486 struct megasas_init_frame *init_frame;
5487 struct megasas_init_queue_info *initq_info;
5488 dma_addr_t init_frame_h;
5489 dma_addr_t initq_info_h;
5492 * Prepare a init frame. Note the init frame points to queue info
5493 * structure. Each frame has SGL allocated after first 64 bytes. For
5494 * this frame - since we don't need any SGL - we use SGL's space as
5495 * queue info structure
5497 * We will not get a NULL command below. We just created the pool.
5499 cmd = megasas_get_cmd(instance);
5501 init_frame = (struct megasas_init_frame *)cmd->frame;
5502 initq_info = (struct megasas_init_queue_info *)
5503 ((unsigned long)init_frame + 64);
5505 init_frame_h = cmd->frame_phys_addr;
5506 initq_info_h = init_frame_h + 64;
5508 context = init_frame->context;
5509 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5510 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5511 init_frame->context = context;
5513 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5514 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5516 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5517 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5519 init_frame->cmd = MFI_CMD_INIT;
5520 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5521 init_frame->queue_info_new_phys_addr_lo =
5522 cpu_to_le32(lower_32_bits(initq_info_h));
5523 init_frame->queue_info_new_phys_addr_hi =
5524 cpu_to_le32(upper_32_bits(initq_info_h));
5526 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5529 * disable the intr before firing the init frame to FW
5531 instance->instancet->disable_intr(instance);
5534 * Issue the init frame in polled mode
5537 if (megasas_issue_polled(instance, cmd)) {
5538 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5539 megasas_return_cmd(instance, cmd);
5543 megasas_return_cmd(instance, cmd);
5552 megasas_init_adapter_mfi(struct megasas_instance *instance)
5558 * Get various operational parameters from status register
5560 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5562 * Reduce the max supported cmds by 1. This is to ensure that the
5563 * reply_q_sz (1 more than the max cmd that driver may send)
5564 * does not exceed max cmds that the FW can support
5566 instance->max_fw_cmds = instance->max_fw_cmds-1;
5567 instance->max_mfi_cmds = instance->max_fw_cmds;
5568 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5571 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5572 * are reserved for IOCTL + driver's internal DCMDs.
5574 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5575 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5576 instance->max_scsi_cmds = (instance->max_fw_cmds -
5577 MEGASAS_SKINNY_INT_CMDS);
5578 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5580 instance->max_scsi_cmds = (instance->max_fw_cmds -
5582 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5585 instance->cur_can_queue = instance->max_scsi_cmds;
5587 * Create a pool of commands
5589 if (megasas_alloc_cmds(instance))
5590 goto fail_alloc_cmds;
5593 * Allocate memory for reply queue. Length of reply queue should
5594 * be _one_ more than the maximum commands handled by the firmware.
5596 * Note: When FW completes commands, it places corresponding contex
5597 * values in this circular reply queue. This circular queue is a fairly
5598 * typical producer-consumer queue. FW is the producer (of completed
5599 * commands) and the driver is the consumer.
5601 context_sz = sizeof(u32);
5602 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5604 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5605 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5607 if (!instance->reply_queue) {
5608 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5609 goto fail_reply_queue;
5612 if (megasas_issue_init_mfi(instance))
5615 if (megasas_get_ctrl_info(instance)) {
5616 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5617 "Fail from %s %d\n", instance->unique_id,
5618 __func__, __LINE__);
5622 instance->fw_support_ieee = 0;
5623 instance->fw_support_ieee =
5624 (instance->instancet->read_fw_status_reg(instance) &
5627 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5628 instance->fw_support_ieee);
5630 if (instance->fw_support_ieee)
5631 instance->flag_ieee = 1;
5637 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5638 instance->reply_queue, instance->reply_queue_h);
5640 megasas_free_cmds(instance);
5647 void megasas_setup_irq_poll(struct megasas_instance *instance)
5649 struct megasas_irq_context *irq_ctx;
5652 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5654 /* Initialize IRQ poll */
5655 for (i = 0; i < count; i++) {
5656 irq_ctx = &instance->irq_context[i];
5657 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5658 irq_ctx->irq_poll_scheduled = false;
5659 irq_poll_init(&irq_ctx->irqpoll,
5660 instance->threshold_reply_count,
5666 * megasas_setup_irqs_ioapic - register legacy interrupts.
5667 * @instance: Adapter soft state
5669 * Do not enable interrupt, only setup ISRs.
5671 * Return 0 on success.
5674 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5676 struct pci_dev *pdev;
5678 pdev = instance->pdev;
5679 instance->irq_context[0].instance = instance;
5680 instance->irq_context[0].MSIxIndex = 0;
5681 snprintf(instance->irq_context->name, MEGASAS_MSIX_NAME_LEN, "%s%u",
5682 "megasas", instance->host->host_no);
5683 if (request_irq(pci_irq_vector(pdev, 0),
5684 instance->instancet->service_isr, IRQF_SHARED,
5685 instance->irq_context->name, &instance->irq_context[0])) {
5686 dev_err(&instance->pdev->dev,
5687 "Failed to register IRQ from %s %d\n",
5688 __func__, __LINE__);
5691 instance->perf_mode = MR_LATENCY_PERF_MODE;
5692 instance->low_latency_index_start = 0;
5697 * megasas_setup_irqs_msix - register MSI-x interrupts.
5698 * @instance: Adapter soft state
5699 * @is_probe: Driver probe check
5701 * Do not enable interrupt, only setup ISRs.
5703 * Return 0 on success.
5706 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5709 struct pci_dev *pdev;
5711 pdev = instance->pdev;
5714 for (i = 0; i < instance->msix_vectors; i++) {
5715 instance->irq_context[i].instance = instance;
5716 instance->irq_context[i].MSIxIndex = i;
5717 snprintf(instance->irq_context[i].name, MEGASAS_MSIX_NAME_LEN, "%s%u-msix%u",
5718 "megasas", instance->host->host_no, i);
5719 if (request_irq(pci_irq_vector(pdev, i),
5720 instance->instancet->service_isr, 0, instance->irq_context[i].name,
5721 &instance->irq_context[i])) {
5722 dev_err(&instance->pdev->dev,
5723 "Failed to register IRQ for vector %d.\n", i);
5724 for (j = 0; j < i; j++) {
5725 if (j < instance->low_latency_index_start)
5726 irq_update_affinity_hint(
5727 pci_irq_vector(pdev, j), NULL);
5728 free_irq(pci_irq_vector(pdev, j),
5729 &instance->irq_context[j]);
5731 /* Retry irq register for IO_APIC*/
5732 instance->msix_vectors = 0;
5733 instance->msix_load_balance = false;
5735 pci_free_irq_vectors(instance->pdev);
5736 return megasas_setup_irqs_ioapic(instance);
5747 * megasas_destroy_irqs- unregister interrupts.
5748 * @instance: Adapter soft state
5752 megasas_destroy_irqs(struct megasas_instance *instance) {
5756 struct megasas_irq_context *irq_ctx;
5758 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5759 if (instance->adapter_type != MFI_SERIES) {
5760 for (i = 0; i < count; i++) {
5761 irq_ctx = &instance->irq_context[i];
5762 irq_poll_disable(&irq_ctx->irqpoll);
5766 if (instance->msix_vectors)
5767 for (i = 0; i < instance->msix_vectors; i++) {
5768 if (i < instance->low_latency_index_start)
5769 irq_update_affinity_hint(
5770 pci_irq_vector(instance->pdev, i), NULL);
5771 free_irq(pci_irq_vector(instance->pdev, i),
5772 &instance->irq_context[i]);
5775 free_irq(pci_irq_vector(instance->pdev, 0),
5776 &instance->irq_context[0]);
5780 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5781 * @instance: Adapter soft state
5783 * Return 0 on success.
5786 megasas_setup_jbod_map(struct megasas_instance *instance)
5789 struct fusion_context *fusion = instance->ctrl_context;
5790 size_t pd_seq_map_sz;
5792 pd_seq_map_sz = struct_size((struct MR_PD_CFG_SEQ_NUM_SYNC *)0, seq,
5793 MAX_PHYSICAL_DEVICES);
5795 instance->use_seqnum_jbod_fp =
5796 instance->support_seqnum_jbod_fp;
5797 if (reset_devices || !fusion ||
5798 !instance->support_seqnum_jbod_fp) {
5799 dev_info(&instance->pdev->dev,
5800 "JBOD sequence map is disabled %s %d\n",
5801 __func__, __LINE__);
5802 instance->use_seqnum_jbod_fp = false;
5806 if (fusion->pd_seq_sync[0])
5809 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5810 fusion->pd_seq_sync[i] = dma_alloc_coherent
5811 (&instance->pdev->dev, pd_seq_map_sz,
5812 &fusion->pd_seq_phys[i], GFP_KERNEL);
5813 if (!fusion->pd_seq_sync[i]) {
5814 dev_err(&instance->pdev->dev,
5815 "Failed to allocate memory from %s %d\n",
5816 __func__, __LINE__);
5818 dma_free_coherent(&instance->pdev->dev,
5819 pd_seq_map_sz, fusion->pd_seq_sync[0],
5820 fusion->pd_seq_phys[0]);
5821 fusion->pd_seq_sync[0] = NULL;
5823 instance->use_seqnum_jbod_fp = false;
5829 if (!megasas_sync_pd_seq_num(instance, false) &&
5830 !megasas_sync_pd_seq_num(instance, true))
5831 instance->use_seqnum_jbod_fp = true;
5833 instance->use_seqnum_jbod_fp = false;
5836 static void megasas_setup_reply_map(struct megasas_instance *instance)
5838 const struct cpumask *mask;
5839 unsigned int queue, cpu, low_latency_index_start;
5841 low_latency_index_start = instance->low_latency_index_start;
5843 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
5844 mask = pci_irq_get_affinity(instance->pdev, queue);
5848 for_each_cpu(cpu, mask)
5849 instance->reply_map[cpu] = queue;
5854 queue = low_latency_index_start;
5855 for_each_possible_cpu(cpu) {
5856 instance->reply_map[cpu] = queue;
5857 if (queue == (instance->msix_vectors - 1))
5858 queue = low_latency_index_start;
5865 * megasas_get_device_list - Get the PD and LD device list from FW.
5866 * @instance: Adapter soft state
5867 * @return: Success or failure
5869 * Issue DCMDs to Firmware to get the PD and LD list.
5870 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5871 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5874 int megasas_get_device_list(struct megasas_instance *instance)
5876 if (instance->enable_fw_dev_list) {
5877 if (megasas_host_device_list_query(instance, true))
5880 if (megasas_get_pd_list(instance) < 0) {
5881 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5885 if (megasas_ld_list_query(instance,
5886 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5887 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5896 * megasas_set_high_iops_queue_affinity_and_hint - Set affinity and hint
5897 * for high IOPS queues
5898 * @instance: Adapter soft state
5902 megasas_set_high_iops_queue_affinity_and_hint(struct megasas_instance *instance)
5906 const struct cpumask *mask;
5908 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
5909 mask = cpumask_of_node(dev_to_node(&instance->pdev->dev));
5911 for (i = 0; i < instance->low_latency_index_start; i++) {
5912 irq = pci_irq_vector(instance->pdev, i);
5913 irq_set_affinity_and_hint(irq, mask);
5919 __megasas_alloc_irq_vectors(struct megasas_instance *instance)
5922 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5923 struct irq_affinity *descp = &desc;
5925 irq_flags = PCI_IRQ_MSIX;
5927 if (instance->smp_affinity_enable)
5928 irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
5932 /* Do not allocate msix vectors for poll_queues.
5933 * msix_vectors is always within a range of FW supported reply queue.
5935 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5936 instance->low_latency_index_start,
5937 instance->msix_vectors - instance->iopoll_q_count, irq_flags, descp);
5943 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5944 * @instance: Adapter soft state
5948 megasas_alloc_irq_vectors(struct megasas_instance *instance)
5951 unsigned int num_msix_req;
5953 instance->iopoll_q_count = 0;
5954 if ((instance->adapter_type != MFI_SERIES) &&
5957 instance->perf_mode = MR_LATENCY_PERF_MODE;
5958 instance->low_latency_index_start = 1;
5960 /* reserve for default and non-mananged pre-vector. */
5961 if (instance->msix_vectors > (poll_queues + 2))
5962 instance->iopoll_q_count = poll_queues;
5964 instance->iopoll_q_count = 0;
5966 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5967 instance->msix_vectors = min(num_msix_req,
5968 instance->msix_vectors);
5972 i = __megasas_alloc_irq_vectors(instance);
5974 if (((instance->perf_mode == MR_BALANCED_PERF_MODE)
5975 || instance->iopoll_q_count) &&
5976 (i != (instance->msix_vectors - instance->iopoll_q_count))) {
5977 if (instance->msix_vectors)
5978 pci_free_irq_vectors(instance->pdev);
5979 /* Disable Balanced IOPS mode and try realloc vectors */
5980 instance->perf_mode = MR_LATENCY_PERF_MODE;
5981 instance->low_latency_index_start = 1;
5982 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5984 instance->msix_vectors = min(num_msix_req,
5985 instance->msix_vectors);
5987 instance->iopoll_q_count = 0;
5988 i = __megasas_alloc_irq_vectors(instance);
5992 dev_info(&instance->pdev->dev,
5993 "requested/available msix %d/%d poll_queue %d\n",
5994 instance->msix_vectors - instance->iopoll_q_count,
5995 i, instance->iopoll_q_count);
5998 instance->msix_vectors = i;
6000 instance->msix_vectors = 0;
6002 if (instance->smp_affinity_enable)
6003 megasas_set_high_iops_queue_affinity_and_hint(instance);
6007 * megasas_init_fw - Initializes the FW
6008 * @instance: Adapter soft state
6010 * This is the main function for initializing firmware
6013 static int megasas_init_fw(struct megasas_instance *instance)
6016 u32 max_sectors_2, tmp_sectors, msix_enable;
6017 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
6018 resource_size_t base_addr;
6019 void *base_addr_phys;
6020 struct megasas_ctrl_info *ctrl_info = NULL;
6021 unsigned long bar_list;
6023 struct IOV_111 *iovPtr;
6024 struct fusion_context *fusion;
6025 bool intr_coalescing;
6026 unsigned int num_msix_req;
6029 fusion = instance->ctrl_context;
6031 /* Find first memory bar */
6032 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
6033 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
6034 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
6036 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
6040 base_addr = pci_resource_start(instance->pdev, instance->bar);
6041 instance->reg_set = ioremap(base_addr, 8192);
6043 if (!instance->reg_set) {
6044 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
6048 base_addr_phys = &base_addr;
6049 dev_printk(KERN_DEBUG, &instance->pdev->dev,
6050 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
6051 instance->bar, base_addr_phys, instance->reg_set);
6053 if (instance->adapter_type != MFI_SERIES)
6054 instance->instancet = &megasas_instance_template_fusion;
6056 switch (instance->pdev->device) {
6057 case PCI_DEVICE_ID_LSI_SAS1078R:
6058 case PCI_DEVICE_ID_LSI_SAS1078DE:
6059 instance->instancet = &megasas_instance_template_ppc;
6061 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
6062 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
6063 instance->instancet = &megasas_instance_template_gen2;
6065 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
6066 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
6067 instance->instancet = &megasas_instance_template_skinny;
6069 case PCI_DEVICE_ID_LSI_SAS1064R:
6070 case PCI_DEVICE_ID_DELL_PERC5:
6072 instance->instancet = &megasas_instance_template_xscale;
6073 instance->pd_list_not_supported = 1;
6078 if (megasas_transition_to_ready(instance, 0)) {
6079 dev_info(&instance->pdev->dev,
6080 "Failed to transition controller to ready from %s!\n",
6082 if (instance->adapter_type != MFI_SERIES) {
6083 status_reg = instance->instancet->read_fw_status_reg(
6085 if (status_reg & MFI_RESET_ADAPTER) {
6086 if (megasas_adp_reset_wait_for_ready
6087 (instance, true, 0) == FAILED)
6088 goto fail_ready_state;
6090 goto fail_ready_state;
6093 atomic_set(&instance->fw_reset_no_pci_access, 1);
6094 instance->instancet->adp_reset
6095 (instance, instance->reg_set);
6096 atomic_set(&instance->fw_reset_no_pci_access, 0);
6098 /*waiting for about 30 second before retry*/
6101 if (megasas_transition_to_ready(instance, 0))
6102 goto fail_ready_state;
6105 dev_info(&instance->pdev->dev,
6106 "FW restarted successfully from %s!\n",
6110 megasas_init_ctrl_params(instance);
6112 if (megasas_set_dma_mask(instance))
6113 goto fail_ready_state;
6115 if (megasas_alloc_ctrl_mem(instance))
6116 goto fail_alloc_dma_buf;
6118 if (megasas_alloc_ctrl_dma_buffers(instance))
6119 goto fail_alloc_dma_buf;
6121 fusion = instance->ctrl_context;
6123 if (instance->adapter_type >= VENTURA_SERIES) {
6125 megasas_readl(instance,
6126 &instance->reg_set->outbound_scratch_pad_2);
6127 instance->max_raid_mapsize = ((scratch_pad_2 >>
6128 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
6129 MR_MAX_RAID_MAP_SIZE_MASK);
6132 instance->enable_sdev_max_qd = enable_sdev_max_qd;
6134 switch (instance->adapter_type) {
6135 case VENTURA_SERIES:
6136 fusion->pcie_bw_limitation = true;
6139 fusion->r56_div_offload = true;
6145 /* Check if MSI-X is supported while in ready state */
6146 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
6148 if (msix_enable && !msix_disable) {
6150 scratch_pad_1 = megasas_readl
6151 (instance, &instance->reg_set->outbound_scratch_pad_1);
6152 /* Check max MSI-X vectors */
6154 if (instance->adapter_type == THUNDERBOLT_SERIES) {
6155 /* Thunderbolt Series*/
6156 instance->msix_vectors = (scratch_pad_1
6157 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
6159 instance->msix_vectors = ((scratch_pad_1
6160 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
6161 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
6164 * For Invader series, > 8 MSI-x vectors
6165 * supported by FW/HW implies combined
6166 * reply queue mode is enabled.
6167 * For Ventura series, > 16 MSI-x vectors
6168 * supported by FW/HW implies combined
6169 * reply queue mode is enabled.
6171 switch (instance->adapter_type) {
6172 case INVADER_SERIES:
6173 if (instance->msix_vectors > 8)
6174 instance->msix_combined = true;
6177 case VENTURA_SERIES:
6178 if (instance->msix_vectors > 16)
6179 instance->msix_combined = true;
6184 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
6187 if (instance->adapter_type >= INVADER_SERIES &&
6188 !instance->msix_combined) {
6189 instance->msix_load_balance = true;
6190 instance->smp_affinity_enable = false;
6193 /* Save 1-15 reply post index address to local memory
6194 * Index 0 is already saved from reg offset
6195 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
6197 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
6198 instance->reply_post_host_index_addr[loop] =
6200 ((u8 __iomem *)instance->reg_set +
6201 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
6206 dev_info(&instance->pdev->dev,
6207 "firmware supports msix\t: (%d)",
6208 instance->msix_vectors);
6210 instance->msix_vectors = min(msix_vectors,
6211 instance->msix_vectors);
6212 } else /* MFI adapters */
6213 instance->msix_vectors = 1;
6217 * For Aero (if some conditions are met), driver will configure a
6218 * few additional reply queues with interrupt coalescing enabled.
6219 * These queues with interrupt coalescing enabled are called
6220 * High IOPS queues and rest of reply queues (based on number of
6221 * logical CPUs) are termed as Low latency queues.
6223 * Total Number of reply queues = High IOPS queues + low latency queues
6225 * For rest of fusion adapters, 1 additional reply queue will be
6226 * reserved for management commands, rest of reply queues
6227 * (based on number of logical CPUs) will be used for IOs and
6228 * referenced as IO queues.
6229 * Total Number of reply queues = 1 + IO queues
6231 * MFI adapters supports single MSI-x so single reply queue
6232 * will be used for IO and management commands.
6235 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
6237 if (intr_coalescing &&
6238 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6239 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
6240 instance->perf_mode = MR_BALANCED_PERF_MODE;
6242 instance->perf_mode = MR_LATENCY_PERF_MODE;
6245 if (instance->adapter_type == AERO_SERIES) {
6246 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6247 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6250 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6251 * in latency perf mode and enable R1 PCI bandwidth algorithm
6254 instance->perf_mode = MR_LATENCY_PERF_MODE;
6255 fusion->pcie_bw_limitation = true;
6259 * Performance mode settings provided through module parameter-perf_mode will
6260 * take affect only for:
6261 * 1. Aero family of adapters.
6262 * 2. When user sets module parameter- perf_mode in range of 0-2.
6264 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6265 (perf_mode <= MR_LATENCY_PERF_MODE))
6266 instance->perf_mode = perf_mode;
6268 * If intr coalescing is not supported by controller FW, then IOPS
6269 * and Balanced modes are not feasible.
6271 if (!intr_coalescing)
6272 instance->perf_mode = MR_LATENCY_PERF_MODE;
6276 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
6277 instance->low_latency_index_start =
6278 MR_HIGH_IOPS_QUEUE_COUNT;
6280 instance->low_latency_index_start = 1;
6282 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6284 instance->msix_vectors = min(num_msix_req,
6285 instance->msix_vectors);
6287 megasas_alloc_irq_vectors(instance);
6288 if (!instance->msix_vectors)
6289 instance->msix_load_balance = false;
6292 * MSI-X host index 0 is common for all adapter.
6293 * It is used for all MPT based Adapters.
6295 if (instance->msix_combined) {
6296 instance->reply_post_host_index_addr[0] =
6297 (u32 *)((u8 *)instance->reg_set +
6298 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6300 instance->reply_post_host_index_addr[0] =
6301 (u32 *)((u8 *)instance->reg_set +
6302 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6305 if (!instance->msix_vectors) {
6306 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6308 goto fail_init_adapter;
6311 megasas_setup_reply_map(instance);
6313 dev_info(&instance->pdev->dev,
6314 "current msix/online cpus\t: (%d/%d)\n",
6315 instance->msix_vectors, (unsigned int)num_online_cpus());
6316 dev_info(&instance->pdev->dev,
6317 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
6319 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6320 (unsigned long)instance);
6323 * Below are default value for legacy Firmware.
6324 * non-fusion based controllers
6326 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6327 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
6328 /* Get operational params, sge flags, send init cmd to controller */
6329 if (instance->instancet->init_adapter(instance))
6330 goto fail_init_adapter;
6332 if (instance->adapter_type >= VENTURA_SERIES) {
6334 megasas_readl(instance,
6335 &instance->reg_set->outbound_scratch_pad_3);
6336 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
6337 MR_DEFAULT_NVME_PAGE_SHIFT)
6338 instance->nvme_page_size =
6339 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
6341 dev_info(&instance->pdev->dev,
6342 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6345 if (instance->msix_vectors ?
6346 megasas_setup_irqs_msix(instance, 1) :
6347 megasas_setup_irqs_ioapic(instance))
6348 goto fail_init_adapter;
6350 if (instance->adapter_type != MFI_SERIES)
6351 megasas_setup_irq_poll(instance);
6353 instance->instancet->enable_intr(instance);
6355 dev_info(&instance->pdev->dev, "INIT adapter done\n");
6357 megasas_setup_jbod_map(instance);
6359 if (megasas_get_device_list(instance) != SUCCESS) {
6360 dev_err(&instance->pdev->dev,
6361 "%s: megasas_get_device_list failed\n",
6363 goto fail_get_ld_pd_list;
6366 /* stream detection initialization */
6367 if (instance->adapter_type >= VENTURA_SERIES) {
6368 fusion->stream_detect_by_ld =
6369 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6370 sizeof(struct LD_STREAM_DETECT *),
6372 if (!fusion->stream_detect_by_ld) {
6373 dev_err(&instance->pdev->dev,
6374 "unable to allocate stream detection for pool of LDs\n");
6375 goto fail_get_ld_pd_list;
6377 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6378 fusion->stream_detect_by_ld[i] =
6379 kzalloc(sizeof(struct LD_STREAM_DETECT),
6381 if (!fusion->stream_detect_by_ld[i]) {
6382 dev_err(&instance->pdev->dev,
6383 "unable to allocate stream detect by LD\n ");
6384 for (j = 0; j < i; ++j)
6385 kfree(fusion->stream_detect_by_ld[j]);
6386 kfree(fusion->stream_detect_by_ld);
6387 fusion->stream_detect_by_ld = NULL;
6388 goto fail_get_ld_pd_list;
6390 fusion->stream_detect_by_ld[i]->mru_bit_map
6396 * Compute the max allowed sectors per IO: The controller info has two
6397 * limits on max sectors. Driver should use the minimum of these two.
6399 * 1 << stripe_sz_ops.min = max sectors per strip
6401 * Note that older firmwares ( < FW ver 30) didn't report information
6402 * to calculate max_sectors_1. So the number ended up as zero always.
6405 ctrl_info = instance->ctrl_info_buf;
6407 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6408 le16_to_cpu(ctrl_info->max_strips_per_io);
6409 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
6411 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
6413 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6414 instance->passive = ctrl_info->cluster.passive;
6415 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
6416 instance->UnevenSpanSupport =
6417 ctrl_info->adapterOperations2.supportUnevenSpans;
6418 if (instance->UnevenSpanSupport) {
6419 struct fusion_context *fusion = instance->ctrl_context;
6420 if (MR_ValidateMapInfo(instance, instance->map_id))
6421 fusion->fast_path_io = 1;
6423 fusion->fast_path_io = 0;
6426 if (ctrl_info->host_interface.SRIOV) {
6427 instance->requestorId = ctrl_info->iov.requestorId;
6428 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6429 if (!ctrl_info->adapterOperations2.activePassive)
6430 instance->PlasmaFW111 = 1;
6432 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6433 instance->PlasmaFW111 ? "1.11" : "new");
6435 if (instance->PlasmaFW111) {
6436 iovPtr = (struct IOV_111 *)
6437 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6438 instance->requestorId = iovPtr->requestorId;
6441 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6442 instance->requestorId);
6445 instance->crash_dump_fw_support =
6446 ctrl_info->adapterOperations3.supportCrashDump;
6447 instance->crash_dump_drv_support =
6448 (instance->crash_dump_fw_support &&
6449 instance->crash_dump_buf);
6450 if (instance->crash_dump_drv_support)
6451 megasas_set_crash_dump_params(instance,
6452 MR_CRASH_BUF_TURN_OFF);
6455 if (instance->crash_dump_buf)
6456 dma_free_coherent(&instance->pdev->dev,
6458 instance->crash_dump_buf,
6459 instance->crash_dump_h);
6460 instance->crash_dump_buf = NULL;
6463 if (instance->snapdump_wait_time) {
6464 megasas_get_snapdump_properties(instance);
6465 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6466 instance->snapdump_wait_time);
6469 dev_info(&instance->pdev->dev,
6470 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6471 le16_to_cpu(ctrl_info->pci.vendor_id),
6472 le16_to_cpu(ctrl_info->pci.device_id),
6473 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6474 le16_to_cpu(ctrl_info->pci.sub_device_id));
6475 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6476 instance->UnevenSpanSupport ? "yes" : "no");
6477 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6478 instance->crash_dump_drv_support ? "yes" : "no");
6479 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6480 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
6482 instance->max_sectors_per_req = instance->max_num_sge *
6483 SGE_BUFFER_SIZE / 512;
6484 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6485 instance->max_sectors_per_req = tmp_sectors;
6487 /* Check for valid throttlequeuedepth module parameter */
6488 if (throttlequeuedepth &&
6489 throttlequeuedepth <= instance->max_scsi_cmds)
6490 instance->throttlequeuedepth = throttlequeuedepth;
6492 instance->throttlequeuedepth =
6493 MEGASAS_THROTTLE_QUEUE_DEPTH;
6495 if ((resetwaittime < 1) ||
6496 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6497 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6499 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6500 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6502 /* Launch SR-IOV heartbeat timer */
6503 if (instance->requestorId) {
6504 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6505 megasas_start_timer(instance);
6507 instance->skip_heartbeat_timer_del = 1;
6508 goto fail_get_ld_pd_list;
6513 * Create and start watchdog thread which will monitor
6514 * controller state every 1 sec and trigger OCR when
6515 * it enters fault state
6517 if (instance->adapter_type != MFI_SERIES)
6518 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6519 goto fail_start_watchdog;
6523 fail_start_watchdog:
6524 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6525 del_timer_sync(&instance->sriov_heartbeat_timer);
6526 fail_get_ld_pd_list:
6527 instance->instancet->disable_intr(instance);
6528 megasas_destroy_irqs(instance);
6530 if (instance->msix_vectors)
6531 pci_free_irq_vectors(instance->pdev);
6532 instance->msix_vectors = 0;
6534 megasas_free_ctrl_dma_buffers(instance);
6535 megasas_free_ctrl_mem(instance);
6537 iounmap(instance->reg_set);
6540 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6542 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6543 __func__, __LINE__);
6548 * megasas_release_mfi - Reverses the FW initialization
6549 * @instance: Adapter soft state
6551 static void megasas_release_mfi(struct megasas_instance *instance)
6553 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6555 if (instance->reply_queue)
6556 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6557 instance->reply_queue, instance->reply_queue_h);
6559 megasas_free_cmds(instance);
6561 iounmap(instance->reg_set);
6563 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6567 * megasas_get_seq_num - Gets latest event sequence numbers
6568 * @instance: Adapter soft state
6569 * @eli: FW event log sequence numbers information
6571 * FW maintains a log of all events in a non-volatile area. Upper layers would
6572 * usually find out the latest sequence number of the events, the seq number at
6573 * the boot etc. They would "read" all the events below the latest seq number
6574 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6575 * number), they would subsribe to AEN (asynchronous event notification) and
6576 * wait for the events to happen.
6579 megasas_get_seq_num(struct megasas_instance *instance,
6580 struct megasas_evt_log_info *eli)
6582 struct megasas_cmd *cmd;
6583 struct megasas_dcmd_frame *dcmd;
6584 struct megasas_evt_log_info *el_info;
6585 dma_addr_t el_info_h = 0;
6588 cmd = megasas_get_cmd(instance);
6594 dcmd = &cmd->frame->dcmd;
6595 el_info = dma_alloc_coherent(&instance->pdev->dev,
6596 sizeof(struct megasas_evt_log_info),
6597 &el_info_h, GFP_KERNEL);
6599 megasas_return_cmd(instance, cmd);
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_log_info));
6612 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6614 megasas_set_dma_settings(instance, dcmd, el_info_h,
6615 sizeof(struct megasas_evt_log_info));
6617 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6618 if (ret != DCMD_SUCCESS) {
6619 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6620 __func__, __LINE__);
6625 * Copy the data back into callers buffer
6627 eli->newest_seq_num = el_info->newest_seq_num;
6628 eli->oldest_seq_num = el_info->oldest_seq_num;
6629 eli->clear_seq_num = el_info->clear_seq_num;
6630 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6631 eli->boot_seq_num = el_info->boot_seq_num;
6634 dma_free_coherent(&instance->pdev->dev,
6635 sizeof(struct megasas_evt_log_info),
6636 el_info, el_info_h);
6638 megasas_return_cmd(instance, cmd);
6644 * megasas_register_aen - Registers for asynchronous event notification
6645 * @instance: Adapter soft state
6646 * @seq_num: The starting sequence number
6647 * @class_locale_word: Class of the event
6649 * This function subscribes for AEN for events beyond the @seq_num. It requests
6650 * to be notified if and only if the event is of type @class_locale
6653 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6654 u32 class_locale_word)
6657 struct megasas_cmd *cmd;
6658 struct megasas_dcmd_frame *dcmd;
6659 union megasas_evt_class_locale curr_aen;
6660 union megasas_evt_class_locale prev_aen;
6663 * If there an AEN pending already (aen_cmd), check if the
6664 * class_locale of that pending AEN is inclusive of the new
6665 * AEN request we currently have. If it is, then we don't have
6666 * to do anything. In other words, whichever events the current
6667 * AEN request is subscribing to, have already been subscribed
6670 * If the old_cmd is _not_ inclusive, then we have to abort
6671 * that command, form a class_locale that is superset of both
6672 * old and current and re-issue to the FW
6675 curr_aen.word = class_locale_word;
6677 if (instance->aen_cmd) {
6680 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6682 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6683 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6684 dev_info(&instance->pdev->dev,
6685 "%s %d out of range class %d send by application\n",
6686 __func__, __LINE__, curr_aen.members.class);
6691 * A class whose enum value is smaller is inclusive of all
6692 * higher values. If a PROGRESS (= -1) was previously
6693 * registered, then a new registration requests for higher
6694 * classes need not be sent to FW. They are automatically
6697 * Locale numbers don't have such hierarchy. They are bitmap
6700 if ((prev_aen.members.class <= curr_aen.members.class) &&
6701 !((prev_aen.members.locale & curr_aen.members.locale) ^
6702 curr_aen.members.locale)) {
6704 * Previously issued event registration includes
6705 * current request. Nothing to do.
6709 curr_aen.members.locale |= prev_aen.members.locale;
6711 if (prev_aen.members.class < curr_aen.members.class)
6712 curr_aen.members.class = prev_aen.members.class;
6714 instance->aen_cmd->abort_aen = 1;
6715 ret_val = megasas_issue_blocked_abort_cmd(instance,
6720 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6721 "previous AEN command\n");
6727 cmd = megasas_get_cmd(instance);
6732 dcmd = &cmd->frame->dcmd;
6734 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6737 * Prepare DCMD for aen registration
6739 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6741 dcmd->cmd = MFI_CMD_DCMD;
6742 dcmd->cmd_status = 0x0;
6743 dcmd->sge_count = 1;
6744 dcmd->flags = MFI_FRAME_DIR_READ;
6747 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6748 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6749 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6750 instance->last_seq_num = seq_num;
6751 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6753 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6754 sizeof(struct megasas_evt_detail));
6756 if (instance->aen_cmd != NULL) {
6757 megasas_return_cmd(instance, cmd);
6762 * Store reference to the cmd used to register for AEN. When an
6763 * application wants us to register for AEN, we have to abort this
6764 * cmd and re-register with a new EVENT LOCALE supplied by that app
6766 instance->aen_cmd = cmd;
6769 * Issue the aen registration frame
6771 instance->instancet->issue_dcmd(instance, cmd);
6776 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6778 * This DCMD will fetch few properties of LD/system PD defined
6779 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6781 * DCMD send by drivers whenever new target is added to the OS.
6783 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6784 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6785 * 0 = system PD, 1 = LD.
6786 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6787 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6789 * @instance: Adapter soft state
6790 * @sdev: OS provided scsi device
6792 * Returns 0 on success non-zero on failure.
6795 megasas_get_target_prop(struct megasas_instance *instance,
6796 struct scsi_device *sdev)
6799 struct megasas_cmd *cmd;
6800 struct megasas_dcmd_frame *dcmd;
6801 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6804 cmd = megasas_get_cmd(instance);
6807 dev_err(&instance->pdev->dev,
6808 "Failed to get cmd %s\n", __func__);
6812 dcmd = &cmd->frame->dcmd;
6814 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6815 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6816 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6818 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6819 dcmd->cmd = MFI_CMD_DCMD;
6820 dcmd->cmd_status = 0xFF;
6821 dcmd->sge_count = 1;
6822 dcmd->flags = MFI_FRAME_DIR_READ;
6825 dcmd->data_xfer_len =
6826 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6827 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6829 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6830 sizeof(struct MR_TARGET_PROPERTIES));
6832 if ((instance->adapter_type != MFI_SERIES) &&
6833 !instance->mask_interrupts)
6834 ret = megasas_issue_blocked_cmd(instance,
6835 cmd, MFI_IO_TIMEOUT_SECS);
6837 ret = megasas_issue_polled(instance, cmd);
6841 switch (dcmd_timeout_ocr_possible(instance)) {
6843 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6844 mutex_unlock(&instance->reset_mutex);
6845 megasas_reset_fusion(instance->host,
6846 MFI_IO_TIMEOUT_OCR);
6847 mutex_lock(&instance->reset_mutex);
6850 megaraid_sas_kill_hba(instance);
6852 case IGNORE_TIMEOUT:
6853 dev_info(&instance->pdev->dev,
6854 "Ignore DCMD timeout: %s %d\n",
6855 __func__, __LINE__);
6861 megasas_return_cmd(instance, cmd);
6863 if (ret != DCMD_SUCCESS)
6864 dev_err(&instance->pdev->dev,
6865 "return from %s %d return value %d\n",
6866 __func__, __LINE__, ret);
6872 * megasas_start_aen - Subscribes to AEN during driver load time
6873 * @instance: Adapter soft state
6875 static int megasas_start_aen(struct megasas_instance *instance)
6877 struct megasas_evt_log_info eli;
6878 union megasas_evt_class_locale class_locale;
6881 * Get the latest sequence number from FW
6883 memset(&eli, 0, sizeof(eli));
6885 if (megasas_get_seq_num(instance, &eli))
6889 * Register AEN with FW for latest sequence number plus 1
6891 class_locale.members.reserved = 0;
6892 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6893 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6895 return megasas_register_aen(instance,
6896 le32_to_cpu(eli.newest_seq_num) + 1,
6901 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6902 * @instance: Adapter soft state
6904 static int megasas_io_attach(struct megasas_instance *instance)
6906 struct Scsi_Host *host = instance->host;
6909 * Export parameters required by SCSI mid-layer
6911 host->unique_id = instance->unique_id;
6912 host->can_queue = instance->max_scsi_cmds;
6913 host->this_id = instance->init_id;
6914 host->sg_tablesize = instance->max_num_sge;
6916 if (instance->fw_support_ieee)
6917 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6920 * Check if the module parameter value for max_sectors can be used
6922 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6923 instance->max_sectors_per_req = max_sectors;
6926 if (((instance->pdev->device ==
6927 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6928 (instance->pdev->device ==
6929 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6930 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6931 instance->max_sectors_per_req = max_sectors;
6933 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6934 "and <= %d (or < 1MB for GEN2 controller)\n",
6935 instance->max_sectors_per_req);
6940 host->max_sectors = instance->max_sectors_per_req;
6941 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6942 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6943 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6944 host->max_lun = MEGASAS_MAX_LUN;
6945 host->max_cmd_len = 16;
6947 /* Use shared host tagset only for fusion adaptors
6948 * if there are managed interrupts (smp affinity enabled case).
6949 * Single msix_vectors in kdump, so shared host tag is also disabled.
6952 host->host_tagset = 0;
6953 host->nr_hw_queues = 1;
6955 if ((instance->adapter_type != MFI_SERIES) &&
6956 (instance->msix_vectors > instance->low_latency_index_start) &&
6957 host_tagset_enable &&
6958 instance->smp_affinity_enable) {
6959 host->host_tagset = 1;
6960 host->nr_hw_queues = instance->msix_vectors -
6961 instance->low_latency_index_start + instance->iopoll_q_count;
6962 if (instance->iopoll_q_count)
6965 instance->iopoll_q_count = 0;
6968 dev_info(&instance->pdev->dev,
6969 "Max firmware commands: %d shared with default "
6970 "hw_queues = %d poll_queues %d\n", instance->max_fw_cmds,
6971 host->nr_hw_queues - instance->iopoll_q_count,
6972 instance->iopoll_q_count);
6974 * Notify the mid-layer about the new controller
6976 if (scsi_add_host(host, &instance->pdev->dev)) {
6977 dev_err(&instance->pdev->dev,
6978 "Failed to add host from %s %d\n",
6979 __func__, __LINE__);
6987 * megasas_set_dma_mask - Set DMA mask for supported controllers
6989 * @instance: Adapter soft state
6992 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6995 * By default, driver/FW will operate in 32bit DMA addresses
6996 * for consistent DMA mapping but if 32 bit consistent
6997 * DMA mask fails, driver will try with 63 bit consistent
6998 * mask provided FW is true 63bit DMA capable
7000 * For older controllers(Thunderbolt and MFI based adapters)-
7001 * driver/FW will operate in 32 bit consistent DMA addresses.
7004 megasas_set_dma_mask(struct megasas_instance *instance)
7006 u64 consistent_mask;
7007 struct pci_dev *pdev;
7010 pdev = instance->pdev;
7011 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
7012 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
7015 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
7016 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
7017 goto fail_set_dma_mask;
7019 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
7020 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
7021 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
7023 * If 32 bit DMA mask fails, then try for 64 bit mask
7024 * for FW capable of handling 64 bit DMA.
7026 scratch_pad_1 = megasas_readl
7027 (instance, &instance->reg_set->outbound_scratch_pad_1);
7029 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
7030 goto fail_set_dma_mask;
7031 else if (dma_set_mask_and_coherent(&pdev->dev,
7033 goto fail_set_dma_mask;
7035 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
7036 goto fail_set_dma_mask;
7038 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
7039 instance->consistent_mask_64bit = false;
7041 instance->consistent_mask_64bit = true;
7043 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
7044 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
7045 (instance->consistent_mask_64bit ? "63" : "32"));
7050 dev_err(&pdev->dev, "Failed to set DMA mask\n");
7056 * megasas_set_adapter_type - Set adapter type.
7057 * Supported controllers can be divided in
7058 * different categories-
7059 * enum MR_ADAPTER_TYPE {
7061 * THUNDERBOLT_SERIES = 2,
7062 * INVADER_SERIES = 3,
7063 * VENTURA_SERIES = 4,
7066 * @instance: Adapter soft state
7069 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
7071 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
7072 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
7073 instance->adapter_type = MFI_SERIES;
7075 switch (instance->pdev->device) {
7076 case PCI_DEVICE_ID_LSI_AERO_10E1:
7077 case PCI_DEVICE_ID_LSI_AERO_10E2:
7078 case PCI_DEVICE_ID_LSI_AERO_10E5:
7079 case PCI_DEVICE_ID_LSI_AERO_10E6:
7080 instance->adapter_type = AERO_SERIES;
7082 case PCI_DEVICE_ID_LSI_VENTURA:
7083 case PCI_DEVICE_ID_LSI_CRUSADER:
7084 case PCI_DEVICE_ID_LSI_HARPOON:
7085 case PCI_DEVICE_ID_LSI_TOMCAT:
7086 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
7087 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
7088 instance->adapter_type = VENTURA_SERIES;
7090 case PCI_DEVICE_ID_LSI_FUSION:
7091 case PCI_DEVICE_ID_LSI_PLASMA:
7092 instance->adapter_type = THUNDERBOLT_SERIES;
7094 case PCI_DEVICE_ID_LSI_INVADER:
7095 case PCI_DEVICE_ID_LSI_INTRUDER:
7096 case PCI_DEVICE_ID_LSI_INTRUDER_24:
7097 case PCI_DEVICE_ID_LSI_CUTLASS_52:
7098 case PCI_DEVICE_ID_LSI_CUTLASS_53:
7099 case PCI_DEVICE_ID_LSI_FURY:
7100 instance->adapter_type = INVADER_SERIES;
7102 default: /* For all other supported controllers */
7103 instance->adapter_type = MFI_SERIES;
7109 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
7111 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
7112 sizeof(u32), &instance->producer_h, GFP_KERNEL);
7113 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
7114 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
7116 if (!instance->producer || !instance->consumer) {
7117 dev_err(&instance->pdev->dev,
7118 "Failed to allocate memory for producer, consumer\n");
7122 *instance->producer = 0;
7123 *instance->consumer = 0;
7128 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
7129 * structures which are not common across MFI
7130 * adapters and fusion adapters.
7131 * For MFI based adapters, allocate producer and
7132 * consumer buffers. For fusion adapters, allocate
7133 * memory for fusion context.
7134 * @instance: Adapter soft state
7135 * return: 0 for SUCCESS
7137 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
7139 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
7141 if (!instance->reply_map)
7144 switch (instance->adapter_type) {
7146 if (megasas_alloc_mfi_ctrl_mem(instance))
7150 case VENTURA_SERIES:
7151 case THUNDERBOLT_SERIES:
7152 case INVADER_SERIES:
7153 if (megasas_alloc_fusion_context(instance))
7162 * megasas_free_ctrl_mem - Free fusion context for fusion adapters and
7163 * producer, consumer buffers for MFI adapters
7165 * @instance - Adapter soft instance
7168 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
7170 kfree(instance->reply_map);
7171 if (instance->adapter_type == MFI_SERIES) {
7172 if (instance->producer)
7173 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7175 instance->producer_h);
7176 if (instance->consumer)
7177 dma_free_coherent(&instance->pdev->dev, sizeof(u32),
7179 instance->consumer_h);
7181 megasas_free_fusion_context(instance);
7186 * megasas_alloc_ctrl_dma_buffers - Allocate consistent DMA buffers during
7189 * @instance: Adapter soft instance
7191 * @return: O for SUCCESS
7194 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
7196 struct pci_dev *pdev = instance->pdev;
7197 struct fusion_context *fusion = instance->ctrl_context;
7199 instance->evt_detail = dma_alloc_coherent(&pdev->dev,
7200 sizeof(struct megasas_evt_detail),
7201 &instance->evt_detail_h, GFP_KERNEL);
7203 if (!instance->evt_detail) {
7204 dev_err(&instance->pdev->dev,
7205 "Failed to allocate event detail buffer\n");
7210 fusion->ioc_init_request =
7211 dma_alloc_coherent(&pdev->dev,
7212 sizeof(struct MPI2_IOC_INIT_REQUEST),
7213 &fusion->ioc_init_request_phys,
7216 if (!fusion->ioc_init_request) {
7218 "Failed to allocate ioc init request\n");
7222 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
7223 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7224 &instance->snapdump_prop_h, GFP_KERNEL);
7226 if (!instance->snapdump_prop)
7228 "Failed to allocate snapdump properties buffer\n");
7230 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
7231 HOST_DEVICE_LIST_SZ,
7232 &instance->host_device_list_buf_h,
7235 if (!instance->host_device_list_buf) {
7237 "Failed to allocate targetid list buffer\n");
7243 instance->pd_list_buf =
7244 dma_alloc_coherent(&pdev->dev,
7245 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7246 &instance->pd_list_buf_h, GFP_KERNEL);
7248 if (!instance->pd_list_buf) {
7249 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
7253 instance->ctrl_info_buf =
7254 dma_alloc_coherent(&pdev->dev,
7255 sizeof(struct megasas_ctrl_info),
7256 &instance->ctrl_info_buf_h, GFP_KERNEL);
7258 if (!instance->ctrl_info_buf) {
7260 "Failed to allocate controller info buffer\n");
7264 instance->ld_list_buf =
7265 dma_alloc_coherent(&pdev->dev,
7266 sizeof(struct MR_LD_LIST),
7267 &instance->ld_list_buf_h, GFP_KERNEL);
7269 if (!instance->ld_list_buf) {
7270 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7274 instance->ld_targetid_list_buf =
7275 dma_alloc_coherent(&pdev->dev,
7276 sizeof(struct MR_LD_TARGETID_LIST),
7277 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
7279 if (!instance->ld_targetid_list_buf) {
7281 "Failed to allocate LD targetid list buffer\n");
7285 if (!reset_devices) {
7286 instance->system_info_buf =
7287 dma_alloc_coherent(&pdev->dev,
7288 sizeof(struct MR_DRV_SYSTEM_INFO),
7289 &instance->system_info_h, GFP_KERNEL);
7291 dma_alloc_coherent(&pdev->dev,
7292 sizeof(struct MR_PD_INFO),
7293 &instance->pd_info_h, GFP_KERNEL);
7294 instance->tgt_prop =
7295 dma_alloc_coherent(&pdev->dev,
7296 sizeof(struct MR_TARGET_PROPERTIES),
7297 &instance->tgt_prop_h, GFP_KERNEL);
7298 instance->crash_dump_buf =
7299 dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7300 &instance->crash_dump_h, GFP_KERNEL);
7302 if (!instance->system_info_buf)
7303 dev_err(&instance->pdev->dev,
7304 "Failed to allocate system info buffer\n");
7306 if (!instance->pd_info)
7307 dev_err(&instance->pdev->dev,
7308 "Failed to allocate pd_info buffer\n");
7310 if (!instance->tgt_prop)
7311 dev_err(&instance->pdev->dev,
7312 "Failed to allocate tgt_prop buffer\n");
7314 if (!instance->crash_dump_buf)
7315 dev_err(&instance->pdev->dev,
7316 "Failed to allocate crash dump buffer\n");
7323 * megasas_free_ctrl_dma_buffers - Free consistent DMA buffers allocated
7324 * during driver load time
7326 * @instance- Adapter soft instance
7330 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7332 struct pci_dev *pdev = instance->pdev;
7333 struct fusion_context *fusion = instance->ctrl_context;
7335 if (instance->evt_detail)
7336 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
7337 instance->evt_detail,
7338 instance->evt_detail_h);
7340 if (fusion && fusion->ioc_init_request)
7341 dma_free_coherent(&pdev->dev,
7342 sizeof(struct MPI2_IOC_INIT_REQUEST),
7343 fusion->ioc_init_request,
7344 fusion->ioc_init_request_phys);
7346 if (instance->pd_list_buf)
7347 dma_free_coherent(&pdev->dev,
7348 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7349 instance->pd_list_buf,
7350 instance->pd_list_buf_h);
7352 if (instance->ld_list_buf)
7353 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
7354 instance->ld_list_buf,
7355 instance->ld_list_buf_h);
7357 if (instance->ld_targetid_list_buf)
7358 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
7359 instance->ld_targetid_list_buf,
7360 instance->ld_targetid_list_buf_h);
7362 if (instance->ctrl_info_buf)
7363 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
7364 instance->ctrl_info_buf,
7365 instance->ctrl_info_buf_h);
7367 if (instance->system_info_buf)
7368 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
7369 instance->system_info_buf,
7370 instance->system_info_h);
7372 if (instance->pd_info)
7373 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
7374 instance->pd_info, instance->pd_info_h);
7376 if (instance->tgt_prop)
7377 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
7378 instance->tgt_prop, instance->tgt_prop_h);
7380 if (instance->crash_dump_buf)
7381 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7382 instance->crash_dump_buf,
7383 instance->crash_dump_h);
7385 if (instance->snapdump_prop)
7386 dma_free_coherent(&pdev->dev,
7387 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7388 instance->snapdump_prop,
7389 instance->snapdump_prop_h);
7391 if (instance->host_device_list_buf)
7392 dma_free_coherent(&pdev->dev,
7393 HOST_DEVICE_LIST_SZ,
7394 instance->host_device_list_buf,
7395 instance->host_device_list_buf_h);
7400 * megasas_init_ctrl_params - Initialize controller's instance
7401 * parameters before FW init
7402 * @instance - Adapter soft instance
7405 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7407 instance->fw_crash_state = UNAVAILABLE;
7409 megasas_poll_wait_aen = 0;
7410 instance->issuepend_done = 1;
7411 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7414 * Initialize locks and queues
7416 INIT_LIST_HEAD(&instance->cmd_pool);
7417 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7419 atomic_set(&instance->fw_outstanding, 0);
7420 atomic64_set(&instance->total_io_count, 0);
7422 init_waitqueue_head(&instance->int_cmd_wait_q);
7423 init_waitqueue_head(&instance->abort_cmd_wait_q);
7425 spin_lock_init(&instance->crashdump_lock);
7426 spin_lock_init(&instance->mfi_pool_lock);
7427 spin_lock_init(&instance->hba_lock);
7428 spin_lock_init(&instance->stream_lock);
7429 spin_lock_init(&instance->completion_lock);
7431 mutex_init(&instance->reset_mutex);
7433 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7434 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7435 instance->flag_ieee = 1;
7438 instance->unload = 1;
7439 instance->last_time = 0;
7440 instance->disableOnlineCtrlReset = 1;
7441 instance->UnevenSpanSupport = 0;
7442 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7443 instance->msix_load_balance = false;
7445 if (instance->adapter_type != MFI_SERIES)
7446 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7448 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7452 * megasas_probe_one - PCI hotplug entry point
7453 * @pdev: PCI device structure
7454 * @id: PCI ids of supported hotplugged adapter
7456 static int megasas_probe_one(struct pci_dev *pdev,
7457 const struct pci_device_id *id)
7460 struct Scsi_Host *host;
7461 struct megasas_instance *instance;
7464 switch (pdev->device) {
7465 case PCI_DEVICE_ID_LSI_AERO_10E0:
7466 case PCI_DEVICE_ID_LSI_AERO_10E3:
7467 case PCI_DEVICE_ID_LSI_AERO_10E4:
7468 case PCI_DEVICE_ID_LSI_AERO_10E7:
7469 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7471 case PCI_DEVICE_ID_LSI_AERO_10E1:
7472 case PCI_DEVICE_ID_LSI_AERO_10E5:
7473 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7477 /* Reset MSI-X in the kdump kernel */
7478 if (reset_devices) {
7479 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7481 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7483 if (control & PCI_MSIX_FLAGS_ENABLE) {
7484 dev_info(&pdev->dev, "resetting MSI-X\n");
7485 pci_write_config_word(pdev,
7486 pos + PCI_MSIX_FLAGS,
7488 ~PCI_MSIX_FLAGS_ENABLE);
7494 * PCI prepping: enable device set bus mastering and dma mask
7496 rval = pci_enable_device_mem(pdev);
7502 pci_set_master(pdev);
7504 host = scsi_host_alloc(&megasas_template,
7505 sizeof(struct megasas_instance));
7508 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7509 goto fail_alloc_instance;
7512 instance = (struct megasas_instance *)host->hostdata;
7513 memset(instance, 0, sizeof(*instance));
7514 atomic_set(&instance->fw_reset_no_pci_access, 0);
7517 * Initialize PCI related and misc parameters
7519 instance->pdev = pdev;
7520 instance->host = host;
7521 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7522 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7524 megasas_set_adapter_type(instance);
7527 * Initialize MFI Firmware
7529 if (megasas_init_fw(instance))
7532 if (instance->requestorId) {
7533 if (instance->PlasmaFW111) {
7534 instance->vf_affiliation_111 =
7535 dma_alloc_coherent(&pdev->dev,
7536 sizeof(struct MR_LD_VF_AFFILIATION_111),
7537 &instance->vf_affiliation_111_h,
7539 if (!instance->vf_affiliation_111)
7540 dev_warn(&pdev->dev, "Can't allocate "
7541 "memory for VF affiliation buffer\n");
7543 instance->vf_affiliation =
7544 dma_alloc_coherent(&pdev->dev,
7545 (MAX_LOGICAL_DRIVES + 1) *
7546 sizeof(struct MR_LD_VF_AFFILIATION),
7547 &instance->vf_affiliation_h,
7549 if (!instance->vf_affiliation)
7550 dev_warn(&pdev->dev, "Can't allocate "
7551 "memory for VF affiliation buffer\n");
7556 * Store instance in PCI softstate
7558 pci_set_drvdata(pdev, instance);
7561 * Add this controller to megasas_mgmt_info structure so that it
7562 * can be exported to management applications
7564 megasas_mgmt_info.count++;
7565 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7566 megasas_mgmt_info.max_index++;
7569 * Register with SCSI mid-layer
7571 if (megasas_io_attach(instance))
7572 goto fail_io_attach;
7574 instance->unload = 0;
7576 * Trigger SCSI to scan our drives
7578 if (!instance->enable_fw_dev_list ||
7579 (instance->host_device_list_buf->count > 0))
7580 scsi_scan_host(host);
7583 * Initiate AEN (Asynchronous Event Notification)
7585 if (megasas_start_aen(instance)) {
7586 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7587 goto fail_start_aen;
7590 megasas_setup_debugfs(instance);
7592 /* Get current SR-IOV LD/VF affiliation */
7593 if (instance->requestorId)
7594 megasas_get_ld_vf_affiliation(instance, 1);
7599 instance->unload = 1;
7600 scsi_remove_host(instance->host);
7602 megasas_mgmt_info.count--;
7603 megasas_mgmt_info.max_index--;
7604 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7606 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7607 del_timer_sync(&instance->sriov_heartbeat_timer);
7609 instance->instancet->disable_intr(instance);
7610 megasas_destroy_irqs(instance);
7612 if (instance->adapter_type != MFI_SERIES)
7613 megasas_release_fusion(instance);
7615 megasas_release_mfi(instance);
7617 if (instance->msix_vectors)
7618 pci_free_irq_vectors(instance->pdev);
7619 instance->msix_vectors = 0;
7621 if (instance->fw_crash_state != UNAVAILABLE)
7622 megasas_free_host_crash_buffer(instance);
7624 if (instance->adapter_type != MFI_SERIES)
7625 megasas_fusion_stop_watchdog(instance);
7627 scsi_host_put(host);
7628 fail_alloc_instance:
7629 pci_disable_device(pdev);
7635 * megasas_flush_cache - Requests FW to flush all its caches
7636 * @instance: Adapter soft state
7638 static void megasas_flush_cache(struct megasas_instance *instance)
7640 struct megasas_cmd *cmd;
7641 struct megasas_dcmd_frame *dcmd;
7643 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7646 cmd = megasas_get_cmd(instance);
7651 dcmd = &cmd->frame->dcmd;
7653 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7655 dcmd->cmd = MFI_CMD_DCMD;
7656 dcmd->cmd_status = 0x0;
7657 dcmd->sge_count = 0;
7658 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7661 dcmd->data_xfer_len = 0;
7662 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7663 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7665 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7667 dev_err(&instance->pdev->dev,
7668 "return from %s %d\n", __func__, __LINE__);
7672 megasas_return_cmd(instance, cmd);
7676 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7677 * @instance: Adapter soft state
7678 * @opcode: Shutdown/Hibernate
7680 static void megasas_shutdown_controller(struct megasas_instance *instance,
7683 struct megasas_cmd *cmd;
7684 struct megasas_dcmd_frame *dcmd;
7686 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7689 cmd = megasas_get_cmd(instance);
7694 if (instance->aen_cmd)
7695 megasas_issue_blocked_abort_cmd(instance,
7696 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7697 if (instance->map_update_cmd)
7698 megasas_issue_blocked_abort_cmd(instance,
7699 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7700 if (instance->jbod_seq_cmd)
7701 megasas_issue_blocked_abort_cmd(instance,
7702 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7704 dcmd = &cmd->frame->dcmd;
7706 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7708 dcmd->cmd = MFI_CMD_DCMD;
7709 dcmd->cmd_status = 0x0;
7710 dcmd->sge_count = 0;
7711 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7714 dcmd->data_xfer_len = 0;
7715 dcmd->opcode = cpu_to_le32(opcode);
7717 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7719 dev_err(&instance->pdev->dev,
7720 "return from %s %d\n", __func__, __LINE__);
7724 megasas_return_cmd(instance, cmd);
7728 * megasas_suspend - driver suspend entry point
7729 * @dev: Device structure
7731 static int __maybe_unused
7732 megasas_suspend(struct device *dev)
7734 struct megasas_instance *instance;
7736 instance = dev_get_drvdata(dev);
7741 instance->unload = 1;
7743 dev_info(dev, "%s is called\n", __func__);
7745 /* Shutdown SR-IOV heartbeat timer */
7746 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7747 del_timer_sync(&instance->sriov_heartbeat_timer);
7749 /* Stop the FW fault detection watchdog */
7750 if (instance->adapter_type != MFI_SERIES)
7751 megasas_fusion_stop_watchdog(instance);
7753 megasas_flush_cache(instance);
7754 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7756 /* cancel the delayed work if this work still in queue */
7757 if (instance->ev != NULL) {
7758 struct megasas_aen_event *ev = instance->ev;
7759 cancel_delayed_work_sync(&ev->hotplug_work);
7760 instance->ev = NULL;
7763 tasklet_kill(&instance->isr_tasklet);
7765 pci_set_drvdata(instance->pdev, instance);
7766 instance->instancet->disable_intr(instance);
7768 megasas_destroy_irqs(instance);
7770 if (instance->msix_vectors)
7771 pci_free_irq_vectors(instance->pdev);
7777 * megasas_resume- driver resume entry point
7778 * @dev: Device structure
7780 static int __maybe_unused
7781 megasas_resume(struct device *dev)
7784 struct Scsi_Host *host;
7785 struct megasas_instance *instance;
7788 instance = dev_get_drvdata(dev);
7793 host = instance->host;
7795 dev_info(dev, "%s is called\n", __func__);
7798 * We expect the FW state to be READY
7801 if (megasas_transition_to_ready(instance, 0)) {
7802 dev_info(&instance->pdev->dev,
7803 "Failed to transition controller to ready from %s!\n",
7805 if (instance->adapter_type != MFI_SERIES) {
7807 instance->instancet->read_fw_status_reg(instance);
7808 if (!(status_reg & MFI_RESET_ADAPTER) ||
7809 ((megasas_adp_reset_wait_for_ready
7810 (instance, true, 0)) == FAILED))
7811 goto fail_ready_state;
7813 atomic_set(&instance->fw_reset_no_pci_access, 1);
7814 instance->instancet->adp_reset
7815 (instance, instance->reg_set);
7816 atomic_set(&instance->fw_reset_no_pci_access, 0);
7818 /* waiting for about 30 seconds before retry */
7821 if (megasas_transition_to_ready(instance, 0))
7822 goto fail_ready_state;
7825 dev_info(&instance->pdev->dev,
7826 "FW restarted successfully from %s!\n",
7829 if (megasas_set_dma_mask(instance))
7830 goto fail_set_dma_mask;
7833 * Initialize MFI Firmware
7836 atomic_set(&instance->fw_outstanding, 0);
7837 atomic_set(&instance->ldio_outstanding, 0);
7839 /* Now re-enable MSI-X */
7840 if (instance->msix_vectors)
7841 megasas_alloc_irq_vectors(instance);
7843 if (!instance->msix_vectors) {
7844 rval = pci_alloc_irq_vectors(instance->pdev, 1, 1,
7847 goto fail_reenable_msix;
7850 megasas_setup_reply_map(instance);
7852 if (instance->adapter_type != MFI_SERIES) {
7853 megasas_reset_reply_desc(instance);
7854 if (megasas_ioc_init_fusion(instance)) {
7855 megasas_free_cmds(instance);
7856 megasas_free_cmds_fusion(instance);
7859 if (!megasas_get_map_info(instance))
7860 megasas_sync_map_info(instance);
7862 *instance->producer = 0;
7863 *instance->consumer = 0;
7864 if (megasas_issue_init_mfi(instance))
7868 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7871 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7872 (unsigned long)instance);
7874 if (instance->msix_vectors ?
7875 megasas_setup_irqs_msix(instance, 0) :
7876 megasas_setup_irqs_ioapic(instance))
7879 if (instance->adapter_type != MFI_SERIES)
7880 megasas_setup_irq_poll(instance);
7882 /* Re-launch SR-IOV heartbeat timer */
7883 if (instance->requestorId) {
7884 if (!megasas_sriov_start_heartbeat(instance, 0))
7885 megasas_start_timer(instance);
7887 instance->skip_heartbeat_timer_del = 1;
7892 instance->instancet->enable_intr(instance);
7893 megasas_setup_jbod_map(instance);
7894 instance->unload = 0;
7897 * Initiate AEN (Asynchronous Event Notification)
7899 if (megasas_start_aen(instance))
7900 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7902 /* Re-launch FW fault watchdog */
7903 if (instance->adapter_type != MFI_SERIES)
7904 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7905 goto fail_start_watchdog;
7909 fail_start_watchdog:
7910 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7911 del_timer_sync(&instance->sriov_heartbeat_timer);
7913 megasas_free_ctrl_dma_buffers(instance);
7914 megasas_free_ctrl_mem(instance);
7915 scsi_host_put(host);
7925 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7927 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7931 for (i = 0; i < wait_time; i++) {
7932 adp_state = atomic_read(&instance->adprecovery);
7933 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7934 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7937 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7938 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7943 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7944 dev_info(&instance->pdev->dev,
7945 "%s HBA failed to become operational, adp_state %d\n",
7946 __func__, adp_state);
7954 * megasas_detach_one - PCI hot"un"plug entry point
7955 * @pdev: PCI device structure
7957 static void megasas_detach_one(struct pci_dev *pdev)
7960 struct Scsi_Host *host;
7961 struct megasas_instance *instance;
7962 struct fusion_context *fusion;
7963 size_t pd_seq_map_sz;
7965 instance = pci_get_drvdata(pdev);
7970 host = instance->host;
7971 fusion = instance->ctrl_context;
7973 /* Shutdown SR-IOV heartbeat timer */
7974 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7975 del_timer_sync(&instance->sriov_heartbeat_timer);
7977 /* Stop the FW fault detection watchdog */
7978 if (instance->adapter_type != MFI_SERIES)
7979 megasas_fusion_stop_watchdog(instance);
7981 if (instance->fw_crash_state != UNAVAILABLE)
7982 megasas_free_host_crash_buffer(instance);
7983 scsi_remove_host(instance->host);
7984 instance->unload = 1;
7986 if (megasas_wait_for_adapter_operational(instance))
7987 goto skip_firing_dcmds;
7989 megasas_flush_cache(instance);
7990 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7993 /* cancel the delayed work if this work still in queue*/
7994 if (instance->ev != NULL) {
7995 struct megasas_aen_event *ev = instance->ev;
7996 cancel_delayed_work_sync(&ev->hotplug_work);
7997 instance->ev = NULL;
8000 /* cancel all wait events */
8001 wake_up_all(&instance->int_cmd_wait_q);
8003 tasklet_kill(&instance->isr_tasklet);
8006 * Take the instance off the instance array. Note that we will not
8007 * decrement the max_index. We let this array be sparse array
8009 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8010 if (megasas_mgmt_info.instance[i] == instance) {
8011 megasas_mgmt_info.count--;
8012 megasas_mgmt_info.instance[i] = NULL;
8018 instance->instancet->disable_intr(instance);
8020 megasas_destroy_irqs(instance);
8022 if (instance->msix_vectors)
8023 pci_free_irq_vectors(instance->pdev);
8025 if (instance->adapter_type >= VENTURA_SERIES) {
8026 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
8027 kfree(fusion->stream_detect_by_ld[i]);
8028 kfree(fusion->stream_detect_by_ld);
8029 fusion->stream_detect_by_ld = NULL;
8033 if (instance->adapter_type != MFI_SERIES) {
8034 megasas_release_fusion(instance);
8036 struct_size((struct MR_PD_CFG_SEQ_NUM_SYNC *)0,
8037 seq, MAX_PHYSICAL_DEVICES);
8038 for (i = 0; i < 2 ; i++) {
8039 if (fusion->ld_map[i])
8040 dma_free_coherent(&instance->pdev->dev,
8043 fusion->ld_map_phys[i]);
8044 if (fusion->ld_drv_map[i]) {
8045 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
8046 vfree(fusion->ld_drv_map[i]);
8048 free_pages((ulong)fusion->ld_drv_map[i],
8049 fusion->drv_map_pages);
8052 if (fusion->pd_seq_sync[i])
8053 dma_free_coherent(&instance->pdev->dev,
8055 fusion->pd_seq_sync[i],
8056 fusion->pd_seq_phys[i]);
8059 megasas_release_mfi(instance);
8062 if (instance->vf_affiliation)
8063 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
8064 sizeof(struct MR_LD_VF_AFFILIATION),
8065 instance->vf_affiliation,
8066 instance->vf_affiliation_h);
8068 if (instance->vf_affiliation_111)
8069 dma_free_coherent(&pdev->dev,
8070 sizeof(struct MR_LD_VF_AFFILIATION_111),
8071 instance->vf_affiliation_111,
8072 instance->vf_affiliation_111_h);
8074 if (instance->hb_host_mem)
8075 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
8076 instance->hb_host_mem,
8077 instance->hb_host_mem_h);
8079 megasas_free_ctrl_dma_buffers(instance);
8081 megasas_free_ctrl_mem(instance);
8083 megasas_destroy_debugfs(instance);
8085 scsi_host_put(host);
8087 pci_disable_device(pdev);
8091 * megasas_shutdown - Shutdown entry point
8092 * @pdev: PCI device structure
8094 static void megasas_shutdown(struct pci_dev *pdev)
8096 struct megasas_instance *instance = pci_get_drvdata(pdev);
8101 instance->unload = 1;
8103 if (megasas_wait_for_adapter_operational(instance))
8104 goto skip_firing_dcmds;
8106 megasas_flush_cache(instance);
8107 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
8110 instance->instancet->disable_intr(instance);
8111 megasas_destroy_irqs(instance);
8113 if (instance->msix_vectors)
8114 pci_free_irq_vectors(instance->pdev);
8118 * megasas_mgmt_open - char node "open" entry point
8119 * @inode: char node inode
8120 * @filep: char node file
8122 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
8125 * Allow only those users with admin rights
8127 if (!capable(CAP_SYS_ADMIN))
8134 * megasas_mgmt_fasync - Async notifier registration from applications
8135 * @fd: char node file descriptor number
8136 * @filep: char node file
8137 * @mode: notifier on/off
8139 * This function adds the calling process to a driver global queue. When an
8140 * event occurs, SIGIO will be sent to all processes in this queue.
8142 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
8146 mutex_lock(&megasas_async_queue_mutex);
8148 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
8150 mutex_unlock(&megasas_async_queue_mutex);
8153 /* For sanity check when we get ioctl */
8154 filep->private_data = filep;
8158 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
8164 * megasas_mgmt_poll - char node "poll" entry point
8165 * @filep: char node file
8166 * @wait: Events to poll for
8168 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
8171 unsigned long flags;
8173 poll_wait(file, &megasas_poll_wait, wait);
8174 spin_lock_irqsave(&poll_aen_lock, flags);
8175 if (megasas_poll_wait_aen)
8176 mask = (EPOLLIN | EPOLLRDNORM);
8179 megasas_poll_wait_aen = 0;
8180 spin_unlock_irqrestore(&poll_aen_lock, flags);
8185 * megasas_set_crash_dump_params_ioctl:
8186 * Send CRASH_DUMP_MODE DCMD to all controllers
8187 * @cmd: MFI command frame
8190 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
8192 struct megasas_instance *local_instance;
8196 crash_support = cmd->frame->dcmd.mbox.w[0];
8198 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8199 local_instance = megasas_mgmt_info.instance[i];
8200 if (local_instance && local_instance->crash_dump_drv_support) {
8201 if ((atomic_read(&local_instance->adprecovery) ==
8202 MEGASAS_HBA_OPERATIONAL) &&
8203 !megasas_set_crash_dump_params(local_instance,
8205 local_instance->crash_dump_app_support =
8207 dev_info(&local_instance->pdev->dev,
8208 "Application firmware crash "
8209 "dump mode set success\n");
8212 dev_info(&local_instance->pdev->dev,
8213 "Application firmware crash "
8214 "dump mode set failed\n");
8223 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
8224 * @instance: Adapter soft state
8225 * @user_ioc: User's ioctl packet
8226 * @ioc: ioctl packet
8229 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
8230 struct megasas_iocpacket __user * user_ioc,
8231 struct megasas_iocpacket *ioc)
8233 struct megasas_sge64 *kern_sge64 = NULL;
8234 struct megasas_sge32 *kern_sge32 = NULL;
8235 struct megasas_cmd *cmd;
8236 void *kbuff_arr[MAX_IOCTL_SGE];
8237 dma_addr_t buf_handle = 0;
8240 dma_addr_t sense_handle;
8243 int ret = DCMD_SUCCESS;
8245 memset(kbuff_arr, 0, sizeof(kbuff_arr));
8247 if (ioc->sge_count > MAX_IOCTL_SGE) {
8248 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
8249 ioc->sge_count, MAX_IOCTL_SGE);
8253 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
8254 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
8255 !instance->support_nvme_passthru) ||
8256 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
8257 !instance->support_pci_lane_margining)) {
8258 dev_err(&instance->pdev->dev,
8259 "Received invalid ioctl command 0x%x\n",
8260 ioc->frame.hdr.cmd);
8264 cmd = megasas_get_cmd(instance);
8266 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
8271 * User's IOCTL packet has 2 frames (maximum). Copy those two
8272 * frames into our cmd's frames. cmd->frame's context will get
8273 * overwritten when we copy from user's frames. So set that value
8276 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
8277 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
8278 cmd->frame->hdr.pad_0 = 0;
8280 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8282 if (instance->consistent_mask_64bit)
8283 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8284 MFI_FRAME_SENSE64));
8286 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
8287 MFI_FRAME_SENSE64));
8289 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8290 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
8292 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
8293 mutex_lock(&instance->reset_mutex);
8294 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8295 megasas_return_cmd(instance, cmd);
8296 mutex_unlock(&instance->reset_mutex);
8299 mutex_unlock(&instance->reset_mutex);
8302 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
8303 error = megasas_set_crash_dump_params_ioctl(cmd);
8304 megasas_return_cmd(instance, cmd);
8309 * The management interface between applications and the fw uses
8310 * MFI frames. E.g, RAID configuration changes, LD property changes
8311 * etc are accomplishes through different kinds of MFI frames. The
8312 * driver needs to care only about substituting user buffers with
8313 * kernel buffers in SGLs. The location of SGL is embedded in the
8314 * struct iocpacket itself.
8316 if (instance->consistent_mask_64bit)
8317 kern_sge64 = (struct megasas_sge64 *)
8318 ((unsigned long)cmd->frame + ioc->sgl_off);
8320 kern_sge32 = (struct megasas_sge32 *)
8321 ((unsigned long)cmd->frame + ioc->sgl_off);
8324 * For each user buffer, create a mirror buffer and copy in
8326 for (i = 0; i < ioc->sge_count; i++) {
8327 if (!ioc->sgl[i].iov_len)
8330 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
8331 ioc->sgl[i].iov_len,
8332 &buf_handle, GFP_KERNEL);
8333 if (!kbuff_arr[i]) {
8334 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8335 "kernel SGL buffer for IOCTL\n");
8341 * We don't change the dma_coherent_mask, so
8342 * dma_alloc_coherent only returns 32bit addresses
8344 if (instance->consistent_mask_64bit) {
8345 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8346 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8348 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8349 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8353 * We created a kernel buffer corresponding to the
8354 * user buffer. Now copy in from the user buffer
8356 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8357 (u32) (ioc->sgl[i].iov_len))) {
8363 if (ioc->sense_len) {
8364 /* make sure the pointer is part of the frame */
8365 if (ioc->sense_off >
8366 (sizeof(union megasas_frame) - sizeof(__le64))) {
8371 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8372 &sense_handle, GFP_KERNEL);
8378 /* always store 64 bits regardless of addressing */
8379 sense_ptr = (void *)cmd->frame + ioc->sense_off;
8380 put_unaligned_le64(sense_handle, sense_ptr);
8384 * Set the sync_cmd flag so that the ISR knows not to complete this
8385 * cmd to the SCSI mid-layer
8389 ret = megasas_issue_blocked_cmd(instance, cmd, 0);
8394 dev_err(&instance->pdev->dev,
8395 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8396 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8397 cmd->cmd_status_drv);
8404 if (instance->unload == 1) {
8405 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8406 "don't submit data to application\n");
8410 * copy out the kernel buffers to user buffers
8412 for (i = 0; i < ioc->sge_count; i++) {
8413 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8414 ioc->sgl[i].iov_len)) {
8421 * copy out the sense
8423 if (ioc->sense_len) {
8426 * sense_ptr points to the location that has the user
8427 * sense buffer address
8429 sense_ptr = (void *)ioc->frame.raw + ioc->sense_off;
8430 if (in_compat_syscall())
8431 uptr = compat_ptr(get_unaligned((compat_uptr_t *)
8434 uptr = get_unaligned((void __user **)sense_ptr);
8436 if (copy_to_user(uptr, sense, ioc->sense_len)) {
8437 dev_err(&instance->pdev->dev, "Failed to copy out to user "
8445 * copy the status codes returned by the fw
8447 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8448 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
8449 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
8455 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
8456 sense, sense_handle);
8459 for (i = 0; i < ioc->sge_count; i++) {
8461 if (instance->consistent_mask_64bit)
8462 dma_free_coherent(&instance->pdev->dev,
8463 le32_to_cpu(kern_sge64[i].length),
8465 le64_to_cpu(kern_sge64[i].phys_addr));
8467 dma_free_coherent(&instance->pdev->dev,
8468 le32_to_cpu(kern_sge32[i].length),
8470 le32_to_cpu(kern_sge32[i].phys_addr));
8471 kbuff_arr[i] = NULL;
8475 megasas_return_cmd(instance, cmd);
8479 static struct megasas_iocpacket *
8480 megasas_compat_iocpacket_get_user(void __user *arg)
8482 struct megasas_iocpacket *ioc;
8483 struct compat_megasas_iocpacket __user *cioc = arg;
8488 ioc = kzalloc(sizeof(*ioc), GFP_KERNEL);
8490 return ERR_PTR(-ENOMEM);
8491 size = offsetof(struct megasas_iocpacket, frame) + sizeof(ioc->frame);
8492 if (copy_from_user(ioc, arg, size))
8495 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8496 compat_uptr_t iov_base;
8498 if (get_user(iov_base, &cioc->sgl[i].iov_base) ||
8499 get_user(ioc->sgl[i].iov_len, &cioc->sgl[i].iov_len))
8502 ioc->sgl[i].iov_base = compat_ptr(iov_base);
8508 return ERR_PTR(err);
8511 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8513 struct megasas_iocpacket __user *user_ioc =
8514 (struct megasas_iocpacket __user *)arg;
8515 struct megasas_iocpacket *ioc;
8516 struct megasas_instance *instance;
8519 if (in_compat_syscall())
8520 ioc = megasas_compat_iocpacket_get_user(user_ioc);
8522 ioc = memdup_user(user_ioc, sizeof(struct megasas_iocpacket));
8525 return PTR_ERR(ioc);
8527 instance = megasas_lookup_instance(ioc->host_no);
8533 /* Block ioctls in VF mode */
8534 if (instance->requestorId && !allow_vf_ioctls) {
8539 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8540 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8545 if (instance->unload == 1) {
8550 if (down_interruptible(&instance->ioctl_sem)) {
8551 error = -ERESTARTSYS;
8555 if (megasas_wait_for_adapter_operational(instance)) {
8560 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8562 up(&instance->ioctl_sem);
8569 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8571 struct megasas_instance *instance;
8572 struct megasas_aen aen;
8575 if (file->private_data != file) {
8576 printk(KERN_DEBUG "megasas: fasync_helper was not "
8581 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8584 instance = megasas_lookup_instance(aen.host_no);
8589 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8593 if (instance->unload == 1) {
8597 if (megasas_wait_for_adapter_operational(instance))
8600 mutex_lock(&instance->reset_mutex);
8601 error = megasas_register_aen(instance, aen.seq_num,
8602 aen.class_locale_word);
8603 mutex_unlock(&instance->reset_mutex);
8608 * megasas_mgmt_ioctl - char node ioctl entry point
8609 * @file: char device file pointer
8610 * @cmd: ioctl command
8611 * @arg: ioctl command arguments address
8614 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8617 case MEGASAS_IOC_FIRMWARE:
8618 return megasas_mgmt_ioctl_fw(file, arg);
8620 case MEGASAS_IOC_GET_AEN:
8621 return megasas_mgmt_ioctl_aen(file, arg);
8627 #ifdef CONFIG_COMPAT
8629 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8633 case MEGASAS_IOC_FIRMWARE32:
8634 return megasas_mgmt_ioctl_fw(file, arg);
8635 case MEGASAS_IOC_GET_AEN:
8636 return megasas_mgmt_ioctl_aen(file, arg);
8644 * File operations structure for management interface
8646 static const struct file_operations megasas_mgmt_fops = {
8647 .owner = THIS_MODULE,
8648 .open = megasas_mgmt_open,
8649 .fasync = megasas_mgmt_fasync,
8650 .unlocked_ioctl = megasas_mgmt_ioctl,
8651 .poll = megasas_mgmt_poll,
8652 #ifdef CONFIG_COMPAT
8653 .compat_ioctl = megasas_mgmt_compat_ioctl,
8655 .llseek = noop_llseek,
8658 static SIMPLE_DEV_PM_OPS(megasas_pm_ops, megasas_suspend, megasas_resume);
8661 * PCI hotplug support registration structure
8663 static struct pci_driver megasas_pci_driver = {
8665 .name = "megaraid_sas",
8666 .id_table = megasas_pci_table,
8667 .probe = megasas_probe_one,
8668 .remove = megasas_detach_one,
8669 .driver.pm = &megasas_pm_ops,
8670 .shutdown = megasas_shutdown,
8674 * Sysfs driver attributes
8676 static ssize_t version_show(struct device_driver *dd, char *buf)
8678 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8681 static DRIVER_ATTR_RO(version);
8683 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8685 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8688 static DRIVER_ATTR_RO(release_date);
8690 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8692 return sprintf(buf, "%u\n", support_poll_for_event);
8694 static DRIVER_ATTR_RO(support_poll_for_event);
8696 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8698 return sprintf(buf, "%u\n", support_device_change);
8700 static DRIVER_ATTR_RO(support_device_change);
8702 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8704 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8707 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8712 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8713 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8718 static DRIVER_ATTR_RW(dbg_lvl);
8721 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8723 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8726 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8729 support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8731 return sprintf(buf, "%u\n", support_pci_lane_margining);
8734 static DRIVER_ATTR_RO(support_pci_lane_margining);
8736 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8738 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8739 scsi_remove_device(sdev);
8740 scsi_device_put(sdev);
8744 * megasas_update_device_list - Update the PD and LD device list from FW
8745 * after an AEN event notification
8746 * @instance: Adapter soft state
8747 * @event_type: Indicates type of event (PD or LD event)
8749 * @return: Success or failure
8751 * Issue DCMDs to Firmware to update the internal device list in driver.
8752 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8753 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8756 int megasas_update_device_list(struct megasas_instance *instance,
8761 if (instance->enable_fw_dev_list) {
8762 return megasas_host_device_list_query(instance, false);
8764 if (event_type & SCAN_PD_CHANNEL) {
8765 dcmd_ret = megasas_get_pd_list(instance);
8766 if (dcmd_ret != DCMD_SUCCESS)
8770 if (event_type & SCAN_VD_CHANNEL) {
8771 if (!instance->requestorId ||
8772 megasas_get_ld_vf_affiliation(instance, 0)) {
8773 return megasas_ld_list_query(instance,
8774 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8778 return DCMD_SUCCESS;
8782 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8783 * after an AEN event notification
8784 * @instance: Adapter soft state
8785 * @scan_type: Indicates type of devices (PD/LD) to add
8789 void megasas_add_remove_devices(struct megasas_instance *instance,
8795 u16 channel = 0, id = 0;
8796 struct Scsi_Host *host;
8797 struct scsi_device *sdev1;
8798 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8799 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8801 host = instance->host;
8803 if (instance->enable_fw_dev_list) {
8804 targetid_list = instance->host_device_list_buf;
8805 for (i = 0; i < targetid_list->count; i++) {
8806 targetid_entry = &targetid_list->host_device_list[i];
8807 if (targetid_entry->flags.u.bits.is_sys_pd) {
8808 channel = le16_to_cpu(targetid_entry->target_id) /
8809 MEGASAS_MAX_DEV_PER_CHANNEL;
8810 id = le16_to_cpu(targetid_entry->target_id) %
8811 MEGASAS_MAX_DEV_PER_CHANNEL;
8813 channel = MEGASAS_MAX_PD_CHANNELS +
8814 (le16_to_cpu(targetid_entry->target_id) /
8815 MEGASAS_MAX_DEV_PER_CHANNEL);
8816 id = le16_to_cpu(targetid_entry->target_id) %
8817 MEGASAS_MAX_DEV_PER_CHANNEL;
8819 sdev1 = scsi_device_lookup(host, channel, id, 0);
8821 scsi_add_device(host, channel, id, 0);
8823 scsi_device_put(sdev1);
8828 if (scan_type & SCAN_PD_CHANNEL) {
8829 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8830 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8831 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8832 sdev1 = scsi_device_lookup(host, i, j, 0);
8833 if (instance->pd_list[pd_index].driveState ==
8834 MR_PD_STATE_SYSTEM) {
8836 scsi_add_device(host, i, j, 0);
8838 scsi_device_put(sdev1);
8841 megasas_remove_scsi_device(sdev1);
8847 if (scan_type & SCAN_VD_CHANNEL) {
8848 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8849 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8850 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8851 sdev1 = scsi_device_lookup(host,
8852 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8853 if (instance->ld_ids[ld_index] != 0xff) {
8855 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8857 scsi_device_put(sdev1);
8860 megasas_remove_scsi_device(sdev1);
8869 megasas_aen_polling(struct work_struct *work)
8871 struct megasas_aen_event *ev =
8872 container_of(work, struct megasas_aen_event, hotplug_work.work);
8873 struct megasas_instance *instance = ev->instance;
8874 union megasas_evt_class_locale class_locale;
8879 u8 dcmd_ret = DCMD_SUCCESS;
8880 struct scsi_device *sdev1;
8883 printk(KERN_ERR "invalid instance!\n");
8888 /* Don't run the event workqueue thread if OCR is running */
8889 mutex_lock(&instance->reset_mutex);
8891 instance->ev = NULL;
8892 if (instance->evt_detail) {
8893 megasas_decode_evt(instance);
8895 switch (le32_to_cpu(instance->evt_detail->code)) {
8897 case MR_EVT_PD_INSERTED:
8898 case MR_EVT_PD_REMOVED:
8899 event_type = SCAN_PD_CHANNEL;
8902 case MR_EVT_LD_OFFLINE:
8903 case MR_EVT_LD_DELETED:
8904 ld_target_id = instance->evt_detail->args.ld.target_id;
8905 sdev1 = scsi_device_lookup(instance->host,
8906 MEGASAS_MAX_PD_CHANNELS +
8907 (ld_target_id / MEGASAS_MAX_DEV_PER_CHANNEL),
8908 (ld_target_id % MEGASAS_MAX_DEV_PER_CHANNEL),
8911 megasas_remove_scsi_device(sdev1);
8913 event_type = SCAN_VD_CHANNEL;
8915 case MR_EVT_LD_CREATED:
8916 event_type = SCAN_VD_CHANNEL;
8919 case MR_EVT_CFG_CLEARED:
8920 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8921 case MR_EVT_FOREIGN_CFG_IMPORTED:
8922 case MR_EVT_LD_STATE_CHANGE:
8923 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8924 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8925 instance->host->host_no);
8928 case MR_EVT_CTRL_PROP_CHANGED:
8929 dcmd_ret = megasas_get_ctrl_info(instance);
8930 if (dcmd_ret == DCMD_SUCCESS &&
8931 instance->snapdump_wait_time) {
8932 megasas_get_snapdump_properties(instance);
8933 dev_info(&instance->pdev->dev,
8934 "Snap dump wait time\t: %d\n",
8935 instance->snapdump_wait_time);
8943 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8944 mutex_unlock(&instance->reset_mutex);
8950 dcmd_ret = megasas_update_device_list(instance, event_type);
8952 mutex_unlock(&instance->reset_mutex);
8954 if (event_type && dcmd_ret == DCMD_SUCCESS)
8955 megasas_add_remove_devices(instance, event_type);
8957 if (dcmd_ret == DCMD_SUCCESS)
8958 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8960 seq_num = instance->last_seq_num;
8962 /* Register AEN with FW for latest sequence number plus 1 */
8963 class_locale.members.reserved = 0;
8964 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8965 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8967 if (instance->aen_cmd != NULL) {
8972 mutex_lock(&instance->reset_mutex);
8973 error = megasas_register_aen(instance, seq_num,
8976 dev_err(&instance->pdev->dev,
8977 "register aen failed error %x\n", error);
8979 mutex_unlock(&instance->reset_mutex);
8984 * megasas_init - Driver load entry point
8986 static int __init megasas_init(void)
8991 * Booted in kdump kernel, minimize memory footprints by
8992 * disabling few features
8994 if (reset_devices) {
8997 dual_qdepth_disable = 1;
9002 * Announce driver version and other information
9004 pr_info("megasas: %s\n", MEGASAS_VERSION);
9006 megasas_dbg_lvl = 0;
9007 support_poll_for_event = 2;
9008 support_device_change = 1;
9009 support_nvme_encapsulation = true;
9010 support_pci_lane_margining = true;
9012 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
9015 * Register character device node
9017 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
9020 printk(KERN_DEBUG "megasas: failed to open device node\n");
9024 megasas_mgmt_majorno = rval;
9026 megasas_init_debugfs();
9029 * Register ourselves as PCI hotplug module
9031 rval = pci_register_driver(&megasas_pci_driver);
9034 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
9038 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
9039 (event_log_level > MFI_EVT_CLASS_DEAD)) {
9040 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");
9041 event_log_level = MFI_EVT_CLASS_CRITICAL;
9044 rval = driver_create_file(&megasas_pci_driver.driver,
9045 &driver_attr_version);
9047 goto err_dcf_attr_ver;
9049 rval = driver_create_file(&megasas_pci_driver.driver,
9050 &driver_attr_release_date);
9052 goto err_dcf_rel_date;
9054 rval = driver_create_file(&megasas_pci_driver.driver,
9055 &driver_attr_support_poll_for_event);
9057 goto err_dcf_support_poll_for_event;
9059 rval = driver_create_file(&megasas_pci_driver.driver,
9060 &driver_attr_dbg_lvl);
9062 goto err_dcf_dbg_lvl;
9063 rval = driver_create_file(&megasas_pci_driver.driver,
9064 &driver_attr_support_device_change);
9066 goto err_dcf_support_device_change;
9068 rval = driver_create_file(&megasas_pci_driver.driver,
9069 &driver_attr_support_nvme_encapsulation);
9071 goto err_dcf_support_nvme_encapsulation;
9073 rval = driver_create_file(&megasas_pci_driver.driver,
9074 &driver_attr_support_pci_lane_margining);
9076 goto err_dcf_support_pci_lane_margining;
9080 err_dcf_support_pci_lane_margining:
9081 driver_remove_file(&megasas_pci_driver.driver,
9082 &driver_attr_support_nvme_encapsulation);
9084 err_dcf_support_nvme_encapsulation:
9085 driver_remove_file(&megasas_pci_driver.driver,
9086 &driver_attr_support_device_change);
9088 err_dcf_support_device_change:
9089 driver_remove_file(&megasas_pci_driver.driver,
9090 &driver_attr_dbg_lvl);
9092 driver_remove_file(&megasas_pci_driver.driver,
9093 &driver_attr_support_poll_for_event);
9094 err_dcf_support_poll_for_event:
9095 driver_remove_file(&megasas_pci_driver.driver,
9096 &driver_attr_release_date);
9098 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9100 pci_unregister_driver(&megasas_pci_driver);
9102 megasas_exit_debugfs();
9103 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9108 * megasas_exit - Driver unload entry point
9110 static void __exit megasas_exit(void)
9112 driver_remove_file(&megasas_pci_driver.driver,
9113 &driver_attr_dbg_lvl);
9114 driver_remove_file(&megasas_pci_driver.driver,
9115 &driver_attr_support_poll_for_event);
9116 driver_remove_file(&megasas_pci_driver.driver,
9117 &driver_attr_support_device_change);
9118 driver_remove_file(&megasas_pci_driver.driver,
9119 &driver_attr_release_date);
9120 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9121 driver_remove_file(&megasas_pci_driver.driver,
9122 &driver_attr_support_nvme_encapsulation);
9123 driver_remove_file(&megasas_pci_driver.driver,
9124 &driver_attr_support_pci_lane_margining);
9126 pci_unregister_driver(&megasas_pci_driver);
9127 megasas_exit_debugfs();
9128 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9131 module_init(megasas_init);
9132 module_exit(megasas_exit);