1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux MegaRAID driver for SAS based RAID controllers
5 * Copyright (c) 2003-2013 LSI Corporation
6 * Copyright (c) 2013-2016 Avago Technologies
7 * Copyright (c) 2016-2018 Broadcom Inc.
9 * Authors: Broadcom Inc.
14 * Kashyap Desai <kashyap.desai@broadcom.com>
15 * Sumit Saxena <sumit.saxena@broadcom.com>
17 * Send feedback to: megaraidlinux.pdl@broadcom.com
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <asm/unaligned.h>
34 #include <linux/compat.h>
35 #include <linux/blkdev.h>
36 #include <linux/mutex.h>
37 #include <linux/poll.h>
38 #include <linux/vmalloc.h>
39 #include <linux/irq_poll.h>
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);
132 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
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 scmd->SCp.ptr = (char *)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;
1797 scmd->scsi_done(scmd);
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;
1812 scmd->scsi_done(scmd);
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;
1821 scmd->scsi_done(scmd);
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;
1829 scmd->scsi_done(scmd);
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);
1860 scmd->scsi_done(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)) {
2132 mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2134 if (!mr_device_priv_data)
2137 if (MEGASAS_IS_LOGICAL(sdev)) {
2138 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2139 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_ACTIVE;
2140 if (megasas_dbg_lvl & LD_PD_DEBUG)
2141 sdev_printk(KERN_INFO, sdev, "LD target ID %d created.\n", ld_tgt_id);
2144 sdev->hostdata = mr_device_priv_data;
2146 atomic_set(&mr_device_priv_data->r1_ldio_hint,
2147 instance->r1_ldio_hint_default);
2151 static void megasas_slave_destroy(struct scsi_device *sdev)
2154 struct megasas_instance *instance;
2156 instance = megasas_lookup_instance(sdev->host->host_no);
2158 if (MEGASAS_IS_LOGICAL(sdev)) {
2159 ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2160 instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_DELETED;
2161 if (megasas_dbg_lvl & LD_PD_DEBUG)
2162 sdev_printk(KERN_INFO, sdev,
2163 "LD target ID %d removed from OS stack\n", ld_tgt_id);
2166 kfree(sdev->hostdata);
2167 sdev->hostdata = NULL;
2171 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2173 * @instance: Adapter soft state
2176 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2179 struct megasas_cmd *cmd_mfi;
2180 struct megasas_cmd_fusion *cmd_fusion;
2181 struct fusion_context *fusion = instance->ctrl_context;
2183 /* Find all outstanding ioctls */
2185 for (i = 0; i < instance->max_fw_cmds; i++) {
2186 cmd_fusion = fusion->cmd_list[i];
2187 if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2188 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2189 if (cmd_mfi->sync_cmd &&
2190 (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2191 cmd_mfi->frame->hdr.cmd_status =
2192 MFI_STAT_WRONG_STATE;
2193 megasas_complete_cmd(instance,
2199 for (i = 0; i < instance->max_fw_cmds; i++) {
2200 cmd_mfi = instance->cmd_list[i];
2201 if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2203 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2209 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2211 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2212 dev_warn(&instance->pdev->dev,
2213 "Adapter already dead, skipping kill HBA\n");
2217 /* Set critical error to block I/O & ioctls in case caller didn't */
2218 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2219 /* Wait 1 second to ensure IO or ioctls in build have posted */
2221 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2222 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2223 (instance->adapter_type != MFI_SERIES)) {
2224 if (!instance->requestorId) {
2225 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2227 readl(&instance->reg_set->doorbell);
2229 if (instance->requestorId && instance->peerIsPresent)
2230 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2232 writel(MFI_STOP_ADP,
2233 &instance->reg_set->inbound_doorbell);
2235 /* Complete outstanding ioctls when adapter is killed */
2236 megasas_complete_outstanding_ioctls(instance);
2240 * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2241 * restored to max value
2242 * @instance: Adapter soft state
2246 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2248 unsigned long flags;
2250 if (instance->flag & MEGASAS_FW_BUSY
2251 && time_after(jiffies, instance->last_time + 5 * HZ)
2252 && atomic_read(&instance->fw_outstanding) <
2253 instance->throttlequeuedepth + 1) {
2255 spin_lock_irqsave(instance->host->host_lock, flags);
2256 instance->flag &= ~MEGASAS_FW_BUSY;
2258 instance->host->can_queue = instance->cur_can_queue;
2259 spin_unlock_irqrestore(instance->host->host_lock, flags);
2264 * megasas_complete_cmd_dpc - Returns FW's controller structure
2265 * @instance_addr: Address of adapter soft state
2267 * Tasklet to complete cmds
2269 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2274 struct megasas_cmd *cmd;
2275 struct megasas_instance *instance =
2276 (struct megasas_instance *)instance_addr;
2277 unsigned long flags;
2279 /* If we have already declared adapter dead, donot complete cmds */
2280 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2283 spin_lock_irqsave(&instance->completion_lock, flags);
2285 producer = le32_to_cpu(*instance->producer);
2286 consumer = le32_to_cpu(*instance->consumer);
2288 while (consumer != producer) {
2289 context = le32_to_cpu(instance->reply_queue[consumer]);
2290 if (context >= instance->max_fw_cmds) {
2291 dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2296 cmd = instance->cmd_list[context];
2298 megasas_complete_cmd(instance, cmd, DID_OK);
2301 if (consumer == (instance->max_fw_cmds + 1)) {
2306 *instance->consumer = cpu_to_le32(producer);
2308 spin_unlock_irqrestore(&instance->completion_lock, flags);
2311 * Check if we can restore can_queue
2313 megasas_check_and_restore_queue_depth(instance);
2316 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2319 * megasas_start_timer - Initializes sriov heartbeat timer object
2320 * @instance: Adapter soft state
2323 void megasas_start_timer(struct megasas_instance *instance)
2325 struct timer_list *timer = &instance->sriov_heartbeat_timer;
2327 timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2328 timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2333 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2336 process_fw_state_change_wq(struct work_struct *work);
2338 static void megasas_do_ocr(struct megasas_instance *instance)
2340 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2341 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2342 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2343 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2345 instance->instancet->disable_intr(instance);
2346 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2347 instance->issuepend_done = 0;
2349 atomic_set(&instance->fw_outstanding, 0);
2350 megasas_internal_reset_defer_cmds(instance);
2351 process_fw_state_change_wq(&instance->work_init);
2354 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2357 struct megasas_cmd *cmd;
2358 struct megasas_dcmd_frame *dcmd;
2359 struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2360 dma_addr_t new_affiliation_111_h;
2364 cmd = megasas_get_cmd(instance);
2367 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2368 "Failed to get cmd for scsi%d\n",
2369 instance->host->host_no);
2373 dcmd = &cmd->frame->dcmd;
2375 if (!instance->vf_affiliation_111) {
2376 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2377 "affiliation for scsi%d\n", instance->host->host_no);
2378 megasas_return_cmd(instance, cmd);
2383 memset(instance->vf_affiliation_111, 0,
2384 sizeof(struct MR_LD_VF_AFFILIATION_111));
2386 new_affiliation_111 =
2387 dma_alloc_coherent(&instance->pdev->dev,
2388 sizeof(struct MR_LD_VF_AFFILIATION_111),
2389 &new_affiliation_111_h, GFP_KERNEL);
2390 if (!new_affiliation_111) {
2391 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2392 "memory for new affiliation for scsi%d\n",
2393 instance->host->host_no);
2394 megasas_return_cmd(instance, cmd);
2399 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2401 dcmd->cmd = MFI_CMD_DCMD;
2402 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2403 dcmd->sge_count = 1;
2404 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2407 dcmd->data_xfer_len =
2408 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2409 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2412 dcmd->sgl.sge32[0].phys_addr =
2413 cpu_to_le32(instance->vf_affiliation_111_h);
2415 dcmd->sgl.sge32[0].phys_addr =
2416 cpu_to_le32(new_affiliation_111_h);
2418 dcmd->sgl.sge32[0].length = cpu_to_le32(
2419 sizeof(struct MR_LD_VF_AFFILIATION_111));
2421 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2422 "scsi%d\n", instance->host->host_no);
2424 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2425 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2426 " failed with status 0x%x for scsi%d\n",
2427 dcmd->cmd_status, instance->host->host_no);
2428 retval = 1; /* Do a scan if we couldn't get affiliation */
2433 thisVf = new_affiliation_111->thisVf;
2434 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2435 if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2436 new_affiliation_111->map[ld].policy[thisVf]) {
2437 dev_warn(&instance->pdev->dev, "SR-IOV: "
2438 "Got new LD/VF affiliation for scsi%d\n",
2439 instance->host->host_no);
2440 memcpy(instance->vf_affiliation_111,
2441 new_affiliation_111,
2442 sizeof(struct MR_LD_VF_AFFILIATION_111));
2448 if (new_affiliation_111) {
2449 dma_free_coherent(&instance->pdev->dev,
2450 sizeof(struct MR_LD_VF_AFFILIATION_111),
2451 new_affiliation_111,
2452 new_affiliation_111_h);
2455 megasas_return_cmd(instance, cmd);
2460 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2463 struct megasas_cmd *cmd;
2464 struct megasas_dcmd_frame *dcmd;
2465 struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2466 struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2467 dma_addr_t new_affiliation_h;
2468 int i, j, retval = 0, found = 0, doscan = 0;
2471 cmd = megasas_get_cmd(instance);
2474 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2475 "Failed to get cmd for scsi%d\n",
2476 instance->host->host_no);
2480 dcmd = &cmd->frame->dcmd;
2482 if (!instance->vf_affiliation) {
2483 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2484 "affiliation for scsi%d\n", instance->host->host_no);
2485 megasas_return_cmd(instance, cmd);
2490 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2491 sizeof(struct MR_LD_VF_AFFILIATION));
2494 dma_alloc_coherent(&instance->pdev->dev,
2495 (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2496 &new_affiliation_h, GFP_KERNEL);
2497 if (!new_affiliation) {
2498 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2499 "memory for new affiliation for scsi%d\n",
2500 instance->host->host_no);
2501 megasas_return_cmd(instance, cmd);
2506 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2508 dcmd->cmd = MFI_CMD_DCMD;
2509 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2510 dcmd->sge_count = 1;
2511 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2514 dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2515 sizeof(struct MR_LD_VF_AFFILIATION));
2516 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2519 dcmd->sgl.sge32[0].phys_addr =
2520 cpu_to_le32(instance->vf_affiliation_h);
2522 dcmd->sgl.sge32[0].phys_addr =
2523 cpu_to_le32(new_affiliation_h);
2525 dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2526 sizeof(struct MR_LD_VF_AFFILIATION));
2528 dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2529 "scsi%d\n", instance->host->host_no);
2532 if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2533 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2534 " failed with status 0x%x for scsi%d\n",
2535 dcmd->cmd_status, instance->host->host_no);
2536 retval = 1; /* Do a scan if we couldn't get affiliation */
2541 if (!new_affiliation->ldCount) {
2542 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2543 "affiliation for passive path for scsi%d\n",
2544 instance->host->host_no);
2548 newmap = new_affiliation->map;
2549 savedmap = instance->vf_affiliation->map;
2550 thisVf = new_affiliation->thisVf;
2551 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2553 for (j = 0; j < instance->vf_affiliation->ldCount;
2555 if (newmap->ref.targetId ==
2556 savedmap->ref.targetId) {
2558 if (newmap->policy[thisVf] !=
2559 savedmap->policy[thisVf]) {
2564 savedmap = (struct MR_LD_VF_MAP *)
2565 ((unsigned char *)savedmap +
2568 if (!found && newmap->policy[thisVf] !=
2569 MR_LD_ACCESS_HIDDEN) {
2573 newmap = (struct MR_LD_VF_MAP *)
2574 ((unsigned char *)newmap + newmap->size);
2577 newmap = new_affiliation->map;
2578 savedmap = instance->vf_affiliation->map;
2580 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2582 for (j = 0 ; j < new_affiliation->ldCount; j++) {
2583 if (savedmap->ref.targetId ==
2584 newmap->ref.targetId) {
2586 if (savedmap->policy[thisVf] !=
2587 newmap->policy[thisVf]) {
2592 newmap = (struct MR_LD_VF_MAP *)
2593 ((unsigned char *)newmap +
2596 if (!found && savedmap->policy[thisVf] !=
2597 MR_LD_ACCESS_HIDDEN) {
2601 savedmap = (struct MR_LD_VF_MAP *)
2602 ((unsigned char *)savedmap +
2608 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2609 "affiliation for scsi%d\n", instance->host->host_no);
2610 memcpy(instance->vf_affiliation, new_affiliation,
2611 new_affiliation->size);
2615 if (new_affiliation)
2616 dma_free_coherent(&instance->pdev->dev,
2617 (MAX_LOGICAL_DRIVES + 1) *
2618 sizeof(struct MR_LD_VF_AFFILIATION),
2619 new_affiliation, new_affiliation_h);
2620 megasas_return_cmd(instance, cmd);
2625 /* This function will get the current SR-IOV LD/VF affiliation */
2626 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2631 if (instance->PlasmaFW111)
2632 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2634 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2638 /* This function will tell FW to start the SR-IOV heartbeat */
2639 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2642 struct megasas_cmd *cmd;
2643 struct megasas_dcmd_frame *dcmd;
2646 cmd = megasas_get_cmd(instance);
2649 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2650 "Failed to get cmd for scsi%d\n",
2651 instance->host->host_no);
2655 dcmd = &cmd->frame->dcmd;
2658 instance->hb_host_mem =
2659 dma_alloc_coherent(&instance->pdev->dev,
2660 sizeof(struct MR_CTRL_HB_HOST_MEM),
2661 &instance->hb_host_mem_h,
2663 if (!instance->hb_host_mem) {
2664 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2665 " memory for heartbeat host memory for scsi%d\n",
2666 instance->host->host_no);
2672 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2674 dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2675 dcmd->cmd = MFI_CMD_DCMD;
2676 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2677 dcmd->sge_count = 1;
2678 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2681 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2682 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2684 megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2685 sizeof(struct MR_CTRL_HB_HOST_MEM));
2687 dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2688 instance->host->host_no);
2690 if ((instance->adapter_type != MFI_SERIES) &&
2691 !instance->mask_interrupts)
2692 retval = megasas_issue_blocked_cmd(instance, cmd,
2693 MEGASAS_ROUTINE_WAIT_TIME_VF);
2695 retval = megasas_issue_polled(instance, cmd);
2698 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2699 "_MEM_ALLOC DCMD %s for scsi%d\n",
2700 (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2701 "timed out" : "failed", instance->host->host_no);
2706 megasas_return_cmd(instance, cmd);
2711 /* Handler for SR-IOV heartbeat */
2712 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2714 struct megasas_instance *instance =
2715 from_timer(instance, t, sriov_heartbeat_timer);
2717 if (instance->hb_host_mem->HB.fwCounter !=
2718 instance->hb_host_mem->HB.driverCounter) {
2719 instance->hb_host_mem->HB.driverCounter =
2720 instance->hb_host_mem->HB.fwCounter;
2721 mod_timer(&instance->sriov_heartbeat_timer,
2722 jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2724 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2725 "completed for scsi%d\n", instance->host->host_no);
2726 schedule_work(&instance->work_init);
2731 * megasas_wait_for_outstanding - Wait for all outstanding cmds
2732 * @instance: Adapter soft state
2734 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2735 * complete all its outstanding commands. Returns error if one or more IOs
2736 * are pending after this time period. It also marks the controller dead.
2738 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2740 int i, sl, outstanding;
2742 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2743 unsigned long flags;
2744 struct list_head clist_local;
2745 struct megasas_cmd *reset_cmd;
2748 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2749 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2750 __func__, __LINE__);
2754 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2756 INIT_LIST_HEAD(&clist_local);
2757 spin_lock_irqsave(&instance->hba_lock, flags);
2758 list_splice_init(&instance->internal_reset_pending_q,
2760 spin_unlock_irqrestore(&instance->hba_lock, flags);
2762 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2763 for (i = 0; i < wait_time; i++) {
2765 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2769 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2770 dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2771 atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2776 while (!list_empty(&clist_local)) {
2777 reset_cmd = list_entry((&clist_local)->next,
2778 struct megasas_cmd, list);
2779 list_del_init(&reset_cmd->list);
2780 if (reset_cmd->scmd) {
2781 reset_cmd->scmd->result = DID_REQUEUE << 16;
2782 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2783 reset_index, reset_cmd,
2784 reset_cmd->scmd->cmnd[0]);
2786 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2787 megasas_return_cmd(instance, reset_cmd);
2788 } else if (reset_cmd->sync_cmd) {
2789 dev_notice(&instance->pdev->dev, "%p synch cmds"
2793 reset_cmd->cmd_status_drv = DCMD_INIT;
2794 instance->instancet->fire_cmd(instance,
2795 reset_cmd->frame_phys_addr,
2796 0, instance->reg_set);
2798 dev_notice(&instance->pdev->dev, "%p unexpected"
2808 for (i = 0; i < resetwaittime; i++) {
2809 outstanding = atomic_read(&instance->fw_outstanding);
2814 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2815 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2816 "commands to complete\n",i,outstanding);
2818 * Call cmd completion routine. Cmd to be
2819 * be completed directly without depending on isr.
2821 megasas_complete_cmd_dpc((unsigned long)instance);
2828 outstanding = atomic_read(&instance->fw_outstanding);
2829 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2831 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2832 goto no_outstanding;
2834 if (instance->disableOnlineCtrlReset)
2835 goto kill_hba_and_failed;
2837 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2838 dev_info(&instance->pdev->dev,
2839 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2840 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2842 goto kill_hba_and_failed;
2843 megasas_do_ocr(instance);
2845 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2846 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2847 __func__, __LINE__);
2850 dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2851 __func__, __LINE__);
2853 for (sl = 0; sl < 10; sl++)
2856 outstanding = atomic_read(&instance->fw_outstanding);
2858 fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2859 if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2860 goto no_outstanding;
2867 dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2868 __func__, __LINE__);
2871 kill_hba_and_failed:
2873 /* Reset not supported, kill adapter */
2874 dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2875 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2876 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2877 atomic_read(&instance->fw_outstanding));
2878 megasas_dump_pending_frames(instance);
2879 megaraid_sas_kill_hba(instance);
2885 * megasas_generic_reset - Generic reset routine
2886 * @scmd: Mid-layer SCSI command
2888 * This routine implements a generic reset handler for device, bus and host
2889 * reset requests. Device, bus and host specific reset handlers can use this
2890 * function after they do their specific tasks.
2892 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2895 struct megasas_instance *instance;
2897 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2899 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2900 scmd->cmnd[0], scmd->retries);
2902 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2903 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2907 ret_val = megasas_wait_for_outstanding(instance);
2908 if (ret_val == SUCCESS)
2909 dev_notice(&instance->pdev->dev, "reset successful\n");
2911 dev_err(&instance->pdev->dev, "failed to do reset\n");
2917 * megasas_reset_timer - quiesce the adapter if required
2920 * Sets the FW busy flag and reduces the host->can_queue if the
2921 * cmd has not been completed within the timeout period.
2924 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2926 struct megasas_instance *instance;
2927 unsigned long flags;
2929 if (time_after(jiffies, scmd->jiffies_at_alloc +
2930 (scmd_timeout * 2) * HZ)) {
2934 instance = (struct megasas_instance *)scmd->device->host->hostdata;
2935 if (!(instance->flag & MEGASAS_FW_BUSY)) {
2936 /* FW is busy, throttle IO */
2937 spin_lock_irqsave(instance->host->host_lock, flags);
2939 instance->host->can_queue = instance->throttlequeuedepth;
2940 instance->last_time = jiffies;
2941 instance->flag |= MEGASAS_FW_BUSY;
2943 spin_unlock_irqrestore(instance->host->host_lock, flags);
2945 return BLK_EH_RESET_TIMER;
2949 * megasas_dump - This function will print hexdump of provided buffer.
2950 * @buf: Buffer to be dumped
2951 * @sz: Size in bytes
2952 * @format: Different formats of dumping e.g. format=n will
2953 * cause only 'n' 32 bit words to be dumped in a single
2957 megasas_dump(void *buf, int sz, int format)
2960 __le32 *buf_loc = (__le32 *)buf;
2962 for (i = 0; i < (sz / sizeof(__le32)); i++) {
2963 if ((i % format) == 0) {
2965 printk(KERN_CONT "\n");
2966 printk(KERN_CONT "%08x: ", (i * 4));
2968 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2970 printk(KERN_CONT "\n");
2974 * megasas_dump_reg_set - This function will print hexdump of register set
2975 * @reg_set: Register set to be dumped
2978 megasas_dump_reg_set(void __iomem *reg_set)
2980 unsigned int i, sz = 256;
2981 u32 __iomem *reg = (u32 __iomem *)reg_set;
2983 for (i = 0; i < (sz / sizeof(u32)); i++)
2984 printk("%08x: %08x\n", (i * 4), readl(®[i]));
2988 * megasas_dump_fusion_io - This function will print key details
2990 * @scmd: SCSI command pointer of SCSI IO
2993 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2995 struct megasas_cmd_fusion *cmd;
2996 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2997 struct megasas_instance *instance;
2999 cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
3000 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3002 scmd_printk(KERN_INFO, scmd,
3003 "scmd: (0x%p) retries: 0x%x allowed: 0x%x\n",
3004 scmd, scmd->retries, scmd->allowed);
3005 scsi_print_command(scmd);
3008 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
3009 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
3010 scmd_printk(KERN_INFO, scmd,
3011 "RequestFlags:0x%x MSIxIndex:0x%x SMID:0x%x LMID:0x%x DevHandle:0x%x\n",
3012 req_desc->SCSIIO.RequestFlags,
3013 req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
3014 req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
3016 printk(KERN_INFO "IO request frame:\n");
3017 megasas_dump(cmd->io_request,
3018 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
3019 printk(KERN_INFO "Chain frame:\n");
3020 megasas_dump(cmd->sg_frame,
3021 instance->max_chain_frame_sz, 8);
3027 * megasas_dump_sys_regs - This function will dump system registers through
3029 * @reg_set: Pointer to System register set.
3030 * @buf: Buffer to which output is to be written.
3031 * @return: Number of bytes written to buffer.
3033 static inline ssize_t
3034 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
3036 unsigned int i, sz = 256;
3037 int bytes_wrote = 0;
3038 char *loc = (char *)buf;
3039 u32 __iomem *reg = (u32 __iomem *)reg_set;
3041 for (i = 0; i < sz / sizeof(u32); i++) {
3042 bytes_wrote += scnprintf(loc + bytes_wrote,
3043 PAGE_SIZE - bytes_wrote,
3044 "%08x: %08x\n", (i * 4),
3051 * megasas_reset_bus_host - Bus & host reset handler entry point
3052 * @scmd: Mid-layer SCSI command
3054 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
3057 struct megasas_instance *instance;
3059 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3061 scmd_printk(KERN_INFO, scmd,
3062 "OCR is requested due to IO timeout!!\n");
3064 scmd_printk(KERN_INFO, scmd,
3065 "SCSI host state: %d SCSI host busy: %d FW outstanding: %d\n",
3066 scmd->device->host->shost_state,
3067 scsi_host_busy(scmd->device->host),
3068 atomic_read(&instance->fw_outstanding));
3070 * First wait for all commands to complete
3072 if (instance->adapter_type == MFI_SERIES) {
3073 ret = megasas_generic_reset(scmd);
3075 megasas_dump_fusion_io(scmd);
3076 ret = megasas_reset_fusion(scmd->device->host,
3077 SCSIIO_TIMEOUT_OCR);
3084 * megasas_task_abort - Issues task abort request to firmware
3085 * (supported only for fusion adapters)
3086 * @scmd: SCSI command pointer
3088 static int megasas_task_abort(struct scsi_cmnd *scmd)
3091 struct megasas_instance *instance;
3093 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3095 if (instance->adapter_type != MFI_SERIES)
3096 ret = megasas_task_abort_fusion(scmd);
3098 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3106 * megasas_reset_target: Issues target reset request to firmware
3107 * (supported only for fusion adapters)
3108 * @scmd: SCSI command pointer
3110 static int megasas_reset_target(struct scsi_cmnd *scmd)
3113 struct megasas_instance *instance;
3115 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3117 if (instance->adapter_type != MFI_SERIES)
3118 ret = megasas_reset_target_fusion(scmd);
3120 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3128 * megasas_bios_param - Returns disk geometry for a disk
3129 * @sdev: device handle
3130 * @bdev: block device
3131 * @capacity: drive capacity
3132 * @geom: geometry parameters
3135 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3136 sector_t capacity, int geom[])
3143 /* Default heads (64) & sectors (32) */
3147 tmp = heads * sectors;
3148 cylinders = capacity;
3150 sector_div(cylinders, tmp);
3153 * Handle extended translation size for logical drives > 1Gb
3156 if (capacity >= 0x200000) {
3159 tmp = heads*sectors;
3160 cylinders = capacity;
3161 sector_div(cylinders, tmp);
3166 geom[2] = cylinders;
3171 static int megasas_map_queues(struct Scsi_Host *shost)
3173 struct megasas_instance *instance;
3174 int qoff = 0, offset;
3175 struct blk_mq_queue_map *map;
3177 instance = (struct megasas_instance *)shost->hostdata;
3179 if (shost->nr_hw_queues == 1)
3182 offset = instance->low_latency_index_start;
3184 /* Setup Default hctx */
3185 map = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
3186 map->nr_queues = instance->msix_vectors - offset;
3187 map->queue_offset = 0;
3188 blk_mq_pci_map_queues(map, instance->pdev, offset);
3189 qoff += map->nr_queues;
3190 offset += map->nr_queues;
3192 /* Setup Poll hctx */
3193 map = &shost->tag_set.map[HCTX_TYPE_POLL];
3194 map->nr_queues = instance->iopoll_q_count;
3195 if (map->nr_queues) {
3197 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3198 * affinity), so use the regular blk-mq cpu mapping
3200 map->queue_offset = qoff;
3201 blk_mq_map_queues(map);
3207 static void megasas_aen_polling(struct work_struct *work);
3210 * megasas_service_aen - Processes an event notification
3211 * @instance: Adapter soft state
3212 * @cmd: AEN command completed by the ISR
3214 * For AEN, driver sends a command down to FW that is held by the FW till an
3215 * event occurs. When an event of interest occurs, FW completes the command
3216 * that it was previously holding.
3218 * This routines sends SIGIO signal to processes that have registered with the
3222 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3224 unsigned long flags;
3227 * Don't signal app if it is just an aborted previously registered aen
3229 if ((!cmd->abort_aen) && (instance->unload == 0)) {
3230 spin_lock_irqsave(&poll_aen_lock, flags);
3231 megasas_poll_wait_aen = 1;
3232 spin_unlock_irqrestore(&poll_aen_lock, flags);
3233 wake_up(&megasas_poll_wait);
3234 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3239 instance->aen_cmd = NULL;
3241 megasas_return_cmd(instance, cmd);
3243 if ((instance->unload == 0) &&
3244 ((instance->issuepend_done == 1))) {
3245 struct megasas_aen_event *ev;
3247 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3249 dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3251 ev->instance = instance;
3253 INIT_DELAYED_WORK(&ev->hotplug_work,
3254 megasas_aen_polling);
3255 schedule_delayed_work(&ev->hotplug_work, 0);
3261 fw_crash_buffer_store(struct device *cdev,
3262 struct device_attribute *attr, const char *buf, size_t count)
3264 struct Scsi_Host *shost = class_to_shost(cdev);
3265 struct megasas_instance *instance =
3266 (struct megasas_instance *) shost->hostdata;
3268 unsigned long flags;
3270 if (kstrtoint(buf, 0, &val) != 0)
3273 spin_lock_irqsave(&instance->crashdump_lock, flags);
3274 instance->fw_crash_buffer_offset = val;
3275 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3280 fw_crash_buffer_show(struct device *cdev,
3281 struct device_attribute *attr, char *buf)
3283 struct Scsi_Host *shost = class_to_shost(cdev);
3284 struct megasas_instance *instance =
3285 (struct megasas_instance *) shost->hostdata;
3287 unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3288 unsigned long chunk_left_bytes;
3289 unsigned long src_addr;
3290 unsigned long flags;
3293 spin_lock_irqsave(&instance->crashdump_lock, flags);
3294 buff_offset = instance->fw_crash_buffer_offset;
3295 if (!instance->crash_dump_buf &&
3296 !((instance->fw_crash_state == AVAILABLE) ||
3297 (instance->fw_crash_state == COPYING))) {
3298 dev_err(&instance->pdev->dev,
3299 "Firmware crash dump is not available\n");
3300 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3304 if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3305 dev_err(&instance->pdev->dev,
3306 "Firmware crash dump offset is out of range\n");
3307 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3311 size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3312 chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3313 size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3314 size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3316 src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3317 (buff_offset % dmachunk);
3318 memcpy(buf, (void *)src_addr, size);
3319 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3325 fw_crash_buffer_size_show(struct device *cdev,
3326 struct device_attribute *attr, char *buf)
3328 struct Scsi_Host *shost = class_to_shost(cdev);
3329 struct megasas_instance *instance =
3330 (struct megasas_instance *) shost->hostdata;
3332 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3333 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3337 fw_crash_state_store(struct device *cdev,
3338 struct device_attribute *attr, const char *buf, size_t count)
3340 struct Scsi_Host *shost = class_to_shost(cdev);
3341 struct megasas_instance *instance =
3342 (struct megasas_instance *) shost->hostdata;
3344 unsigned long flags;
3346 if (kstrtoint(buf, 0, &val) != 0)
3349 if ((val <= AVAILABLE || val > COPY_ERROR)) {
3350 dev_err(&instance->pdev->dev, "application updates invalid "
3351 "firmware crash state\n");
3355 instance->fw_crash_state = val;
3357 if ((val == COPIED) || (val == COPY_ERROR)) {
3358 spin_lock_irqsave(&instance->crashdump_lock, flags);
3359 megasas_free_host_crash_buffer(instance);
3360 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3361 if (val == COPY_ERROR)
3362 dev_info(&instance->pdev->dev, "application failed to "
3363 "copy Firmware crash dump\n");
3365 dev_info(&instance->pdev->dev, "Firmware crash dump "
3366 "copied successfully\n");
3372 fw_crash_state_show(struct device *cdev,
3373 struct device_attribute *attr, char *buf)
3375 struct Scsi_Host *shost = class_to_shost(cdev);
3376 struct megasas_instance *instance =
3377 (struct megasas_instance *) shost->hostdata;
3379 return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3383 page_size_show(struct device *cdev,
3384 struct device_attribute *attr, char *buf)
3386 return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3390 ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3393 struct Scsi_Host *shost = class_to_shost(cdev);
3394 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3396 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3400 fw_cmds_outstanding_show(struct device *cdev,
3401 struct device_attribute *attr, char *buf)
3403 struct Scsi_Host *shost = class_to_shost(cdev);
3404 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3406 return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3410 enable_sdev_max_qd_show(struct device *cdev,
3411 struct device_attribute *attr, char *buf)
3413 struct Scsi_Host *shost = class_to_shost(cdev);
3414 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3416 return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3420 enable_sdev_max_qd_store(struct device *cdev,
3421 struct device_attribute *attr, const char *buf, size_t count)
3423 struct Scsi_Host *shost = class_to_shost(cdev);
3424 struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3426 bool is_target_prop;
3427 int ret_target_prop = DCMD_FAILED;
3428 struct scsi_device *sdev;
3430 if (kstrtou32(buf, 0, &val) != 0) {
3431 pr_err("megasas: could not set enable_sdev_max_qd\n");
3435 mutex_lock(&instance->reset_mutex);
3437 instance->enable_sdev_max_qd = true;
3439 instance->enable_sdev_max_qd = false;
3441 shost_for_each_device(sdev, shost) {
3442 ret_target_prop = megasas_get_target_prop(instance, sdev);
3443 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3444 megasas_set_fw_assisted_qd(sdev, is_target_prop);
3446 mutex_unlock(&instance->reset_mutex);
3452 dump_system_regs_show(struct device *cdev,
3453 struct device_attribute *attr, char *buf)
3455 struct Scsi_Host *shost = class_to_shost(cdev);
3456 struct megasas_instance *instance =
3457 (struct megasas_instance *)shost->hostdata;
3459 return megasas_dump_sys_regs(instance->reg_set, buf);
3463 raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3466 struct Scsi_Host *shost = class_to_shost(cdev);
3467 struct megasas_instance *instance =
3468 (struct megasas_instance *)shost->hostdata;
3470 return snprintf(buf, PAGE_SIZE, "%ld\n",
3471 (unsigned long)instance->map_id);
3474 static DEVICE_ATTR_RW(fw_crash_buffer);
3475 static DEVICE_ATTR_RO(fw_crash_buffer_size);
3476 static DEVICE_ATTR_RW(fw_crash_state);
3477 static DEVICE_ATTR_RO(page_size);
3478 static DEVICE_ATTR_RO(ldio_outstanding);
3479 static DEVICE_ATTR_RO(fw_cmds_outstanding);
3480 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3481 static DEVICE_ATTR_RO(dump_system_regs);
3482 static DEVICE_ATTR_RO(raid_map_id);
3484 static struct device_attribute *megaraid_host_attrs[] = {
3485 &dev_attr_fw_crash_buffer_size,
3486 &dev_attr_fw_crash_buffer,
3487 &dev_attr_fw_crash_state,
3488 &dev_attr_page_size,
3489 &dev_attr_ldio_outstanding,
3490 &dev_attr_fw_cmds_outstanding,
3491 &dev_attr_enable_sdev_max_qd,
3492 &dev_attr_dump_system_regs,
3493 &dev_attr_raid_map_id,
3498 * Scsi host template for megaraid_sas driver
3500 static struct scsi_host_template megasas_template = {
3502 .module = THIS_MODULE,
3503 .name = "Avago SAS based MegaRAID driver",
3504 .proc_name = "megaraid_sas",
3505 .slave_configure = megasas_slave_configure,
3506 .slave_alloc = megasas_slave_alloc,
3507 .slave_destroy = megasas_slave_destroy,
3508 .queuecommand = megasas_queue_command,
3509 .eh_target_reset_handler = megasas_reset_target,
3510 .eh_abort_handler = megasas_task_abort,
3511 .eh_host_reset_handler = megasas_reset_bus_host,
3512 .eh_timed_out = megasas_reset_timer,
3513 .shost_attrs = megaraid_host_attrs,
3514 .bios_param = megasas_bios_param,
3515 .map_queues = megasas_map_queues,
3516 .mq_poll = megasas_blk_mq_poll,
3517 .change_queue_depth = scsi_change_queue_depth,
3518 .max_segment_size = 0xffffffff,
3522 * megasas_complete_int_cmd - Completes an internal command
3523 * @instance: Adapter soft state
3524 * @cmd: Command to be completed
3526 * The megasas_issue_blocked_cmd() function waits for a command to complete
3527 * after it issues a command. This function wakes up that waiting routine by
3528 * calling wake_up() on the wait queue.
3531 megasas_complete_int_cmd(struct megasas_instance *instance,
3532 struct megasas_cmd *cmd)
3534 if (cmd->cmd_status_drv == DCMD_INIT)
3535 cmd->cmd_status_drv =
3536 (cmd->frame->io.cmd_status == MFI_STAT_OK) ?
3537 DCMD_SUCCESS : DCMD_FAILED;
3539 wake_up(&instance->int_cmd_wait_q);
3543 * megasas_complete_abort - Completes aborting a command
3544 * @instance: Adapter soft state
3545 * @cmd: Cmd that was issued to abort another cmd
3547 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3548 * after it issues an abort on a previously issued command. This function
3549 * wakes up all functions waiting on the same wait queue.
3552 megasas_complete_abort(struct megasas_instance *instance,
3553 struct megasas_cmd *cmd)
3555 if (cmd->sync_cmd) {
3557 cmd->cmd_status_drv = DCMD_SUCCESS;
3558 wake_up(&instance->abort_cmd_wait_q);
3563 megasas_set_ld_removed_by_fw(struct megasas_instance *instance)
3567 for (i = 0; (i < MEGASAS_MAX_LD_IDS); i++) {
3568 if (instance->ld_ids_prev[i] != 0xff &&
3569 instance->ld_ids_from_raidmap[i] == 0xff) {
3570 if (megasas_dbg_lvl & LD_PD_DEBUG)
3571 dev_info(&instance->pdev->dev,
3572 "LD target ID %d removed from RAID map\n", i);
3573 instance->ld_tgtid_status[i] = LD_TARGET_ID_DELETED;
3579 * megasas_complete_cmd - Completes a command
3580 * @instance: Adapter soft state
3581 * @cmd: Command to be completed
3582 * @alt_status: If non-zero, use this value as status to
3583 * SCSI mid-layer instead of the value returned
3584 * by the FW. This should be used if caller wants
3585 * an alternate status (as in the case of aborted
3589 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3593 struct megasas_header *hdr = &cmd->frame->hdr;
3594 unsigned long flags;
3595 struct fusion_context *fusion = instance->ctrl_context;
3598 /* flag for the retry reset */
3599 cmd->retry_for_fw_reset = 0;
3602 cmd->scmd->SCp.ptr = NULL;
3605 case MFI_CMD_INVALID:
3606 /* Some older 1068 controller FW may keep a pended
3607 MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3608 when booting the kdump kernel. Ignore this command to
3609 prevent a kernel panic on shutdown of the kdump kernel. */
3610 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3612 dev_warn(&instance->pdev->dev, "If you have a controller "
3613 "other than PERC5, please upgrade your firmware\n");
3615 case MFI_CMD_PD_SCSI_IO:
3616 case MFI_CMD_LD_SCSI_IO:
3619 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3620 * issued either through an IO path or an IOCTL path. If it
3621 * was via IOCTL, we will send it to internal completion.
3623 if (cmd->sync_cmd) {
3625 megasas_complete_int_cmd(instance, cmd);
3630 case MFI_CMD_LD_READ:
3631 case MFI_CMD_LD_WRITE:
3634 cmd->scmd->result = alt_status << 16;
3640 atomic_dec(&instance->fw_outstanding);
3642 scsi_dma_unmap(cmd->scmd);
3643 cmd->scmd->scsi_done(cmd->scmd);
3644 megasas_return_cmd(instance, cmd);
3649 switch (hdr->cmd_status) {
3652 cmd->scmd->result = DID_OK << 16;
3655 case MFI_STAT_SCSI_IO_FAILED:
3656 case MFI_STAT_LD_INIT_IN_PROGRESS:
3658 (DID_ERROR << 16) | hdr->scsi_status;
3661 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3663 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3665 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3666 memset(cmd->scmd->sense_buffer, 0,
3667 SCSI_SENSE_BUFFERSIZE);
3668 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3674 case MFI_STAT_LD_OFFLINE:
3675 case MFI_STAT_DEVICE_NOT_FOUND:
3676 cmd->scmd->result = DID_BAD_TARGET << 16;
3680 dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3682 cmd->scmd->result = DID_ERROR << 16;
3686 atomic_dec(&instance->fw_outstanding);
3688 scsi_dma_unmap(cmd->scmd);
3689 cmd->scmd->scsi_done(cmd->scmd);
3690 megasas_return_cmd(instance, cmd);
3697 case MFI_CMD_TOOLBOX:
3698 megasas_complete_int_cmd(instance, cmd);
3702 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3703 /* Check for LD map update */
3704 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3705 && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3706 fusion->fast_path_io = 0;
3707 spin_lock_irqsave(instance->host->host_lock, flags);
3708 status = cmd->frame->hdr.cmd_status;
3709 instance->map_update_cmd = NULL;
3710 if (status != MFI_STAT_OK) {
3711 if (status != MFI_STAT_NOT_FOUND)
3712 dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3713 cmd->frame->hdr.cmd_status);
3715 megasas_return_cmd(instance, cmd);
3716 spin_unlock_irqrestore(
3717 instance->host->host_lock,
3723 megasas_return_cmd(instance, cmd);
3726 * Set fast path IO to ZERO.
3727 * Validate Map will set proper value.
3728 * Meanwhile all IOs will go as LD IO.
3730 if (status == MFI_STAT_OK &&
3731 (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3733 fusion->fast_path_io = 1;
3735 fusion->fast_path_io = 0;
3738 if (instance->adapter_type >= INVADER_SERIES)
3739 megasas_set_ld_removed_by_fw(instance);
3741 megasas_sync_map_info(instance);
3742 spin_unlock_irqrestore(instance->host->host_lock,
3747 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3748 opcode == MR_DCMD_CTRL_EVENT_GET) {
3749 spin_lock_irqsave(&poll_aen_lock, flags);
3750 megasas_poll_wait_aen = 0;
3751 spin_unlock_irqrestore(&poll_aen_lock, flags);
3754 /* FW has an updated PD sequence */
3755 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3756 (cmd->frame->dcmd.mbox.b[0] == 1)) {
3758 spin_lock_irqsave(instance->host->host_lock, flags);
3759 status = cmd->frame->hdr.cmd_status;
3760 instance->jbod_seq_cmd = NULL;
3761 megasas_return_cmd(instance, cmd);
3763 if (status == MFI_STAT_OK) {
3764 instance->pd_seq_map_id++;
3765 /* Re-register a pd sync seq num cmd */
3766 if (megasas_sync_pd_seq_num(instance, true))
3767 instance->use_seqnum_jbod_fp = false;
3769 instance->use_seqnum_jbod_fp = false;
3771 spin_unlock_irqrestore(instance->host->host_lock, flags);
3776 * See if got an event notification
3778 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3779 megasas_service_aen(instance, cmd);
3781 megasas_complete_int_cmd(instance, cmd);
3787 * Cmd issued to abort another cmd returned
3789 megasas_complete_abort(instance, cmd);
3793 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3795 megasas_complete_int_cmd(instance, cmd);
3801 * megasas_issue_pending_cmds_again - issue all pending cmds
3802 * in FW again because of the fw reset
3803 * @instance: Adapter soft state
3806 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3808 struct megasas_cmd *cmd;
3809 struct list_head clist_local;
3810 union megasas_evt_class_locale class_locale;
3811 unsigned long flags;
3814 INIT_LIST_HEAD(&clist_local);
3815 spin_lock_irqsave(&instance->hba_lock, flags);
3816 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3817 spin_unlock_irqrestore(&instance->hba_lock, flags);
3819 while (!list_empty(&clist_local)) {
3820 cmd = list_entry((&clist_local)->next,
3821 struct megasas_cmd, list);
3822 list_del_init(&cmd->list);
3824 if (cmd->sync_cmd || cmd->scmd) {
3825 dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3826 "detected to be pending while HBA reset\n",
3827 cmd, cmd->scmd, cmd->sync_cmd);
3829 cmd->retry_for_fw_reset++;
3831 if (cmd->retry_for_fw_reset == 3) {
3832 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3833 "was tried multiple times during reset."
3834 "Shutting down the HBA\n",
3835 cmd, cmd->scmd, cmd->sync_cmd);
3836 instance->instancet->disable_intr(instance);
3837 atomic_set(&instance->fw_reset_no_pci_access, 1);
3838 megaraid_sas_kill_hba(instance);
3843 if (cmd->sync_cmd == 1) {
3845 dev_notice(&instance->pdev->dev, "unexpected"
3846 "cmd attached to internal command!\n");
3848 dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3849 "on the internal reset queue,"
3850 "issue it again.\n", cmd);
3851 cmd->cmd_status_drv = DCMD_INIT;
3852 instance->instancet->fire_cmd(instance,
3853 cmd->frame_phys_addr,
3854 0, instance->reg_set);
3855 } else if (cmd->scmd) {
3856 dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3857 "detected on the internal queue, issue again.\n",
3858 cmd, cmd->scmd->cmnd[0]);
3860 atomic_inc(&instance->fw_outstanding);
3861 instance->instancet->fire_cmd(instance,
3862 cmd->frame_phys_addr,
3863 cmd->frame_count-1, instance->reg_set);
3865 dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3866 "internal reset defer list while re-issue!!\n",
3871 if (instance->aen_cmd) {
3872 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3873 megasas_return_cmd(instance, instance->aen_cmd);
3875 instance->aen_cmd = NULL;
3879 * Initiate AEN (Asynchronous Event Notification)
3881 seq_num = instance->last_seq_num;
3882 class_locale.members.reserved = 0;
3883 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3884 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3886 megasas_register_aen(instance, seq_num, class_locale.word);
3890 * Move the internal reset pending commands to a deferred queue.
3892 * We move the commands pending at internal reset time to a
3893 * pending queue. This queue would be flushed after successful
3894 * completion of the internal reset sequence. if the internal reset
3895 * did not complete in time, the kernel reset handler would flush
3899 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3901 struct megasas_cmd *cmd;
3903 u16 max_cmd = instance->max_fw_cmds;
3905 unsigned long flags;
3908 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3909 for (i = 0; i < max_cmd; i++) {
3910 cmd = instance->cmd_list[i];
3911 if (cmd->sync_cmd == 1 || cmd->scmd) {
3912 dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3913 "on the defer queue as internal\n",
3914 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3916 if (!list_empty(&cmd->list)) {
3917 dev_notice(&instance->pdev->dev, "ERROR while"
3918 " moving this cmd:%p, %d %p, it was"
3919 "discovered on some list?\n",
3920 cmd, cmd->sync_cmd, cmd->scmd);
3922 list_del_init(&cmd->list);
3925 list_add_tail(&cmd->list,
3926 &instance->internal_reset_pending_q);
3929 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3934 process_fw_state_change_wq(struct work_struct *work)
3936 struct megasas_instance *instance =
3937 container_of(work, struct megasas_instance, work_init);
3939 unsigned long flags;
3941 if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3942 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3943 atomic_read(&instance->adprecovery));
3947 if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3948 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3949 "state, restarting it...\n");
3951 instance->instancet->disable_intr(instance);
3952 atomic_set(&instance->fw_outstanding, 0);
3954 atomic_set(&instance->fw_reset_no_pci_access, 1);
3955 instance->instancet->adp_reset(instance, instance->reg_set);
3956 atomic_set(&instance->fw_reset_no_pci_access, 0);
3958 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3959 "initiating next stage...\n");
3961 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3962 "state 2 starting...\n");
3964 /* waiting for about 20 second before start the second init */
3965 for (wait = 0; wait < 30; wait++) {
3969 if (megasas_transition_to_ready(instance, 1)) {
3970 dev_notice(&instance->pdev->dev, "adapter not ready\n");
3972 atomic_set(&instance->fw_reset_no_pci_access, 1);
3973 megaraid_sas_kill_hba(instance);
3977 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3978 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3979 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3981 *instance->consumer = *instance->producer;
3983 *instance->consumer = 0;
3984 *instance->producer = 0;
3987 megasas_issue_init_mfi(instance);
3989 spin_lock_irqsave(&instance->hba_lock, flags);
3990 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3991 spin_unlock_irqrestore(&instance->hba_lock, flags);
3992 instance->instancet->enable_intr(instance);
3994 megasas_issue_pending_cmds_again(instance);
3995 instance->issuepend_done = 1;
4000 * megasas_deplete_reply_queue - Processes all completed commands
4001 * @instance: Adapter soft state
4002 * @alt_status: Alternate status to be returned to
4003 * SCSI mid-layer instead of the status
4004 * returned by the FW
4005 * Note: this must be called with hba lock held
4008 megasas_deplete_reply_queue(struct megasas_instance *instance,
4014 if ((mfiStatus = instance->instancet->check_reset(instance,
4015 instance->reg_set)) == 1) {
4019 mfiStatus = instance->instancet->clear_intr(instance);
4020 if (mfiStatus == 0) {
4021 /* Hardware may not set outbound_intr_status in MSI-X mode */
4022 if (!instance->msix_vectors)
4026 instance->mfiStatus = mfiStatus;
4028 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
4029 fw_state = instance->instancet->read_fw_status_reg(
4030 instance) & MFI_STATE_MASK;
4032 if (fw_state != MFI_STATE_FAULT) {
4033 dev_notice(&instance->pdev->dev, "fw state:%x\n",
4037 if ((fw_state == MFI_STATE_FAULT) &&
4038 (instance->disableOnlineCtrlReset == 0)) {
4039 dev_notice(&instance->pdev->dev, "wait adp restart\n");
4041 if ((instance->pdev->device ==
4042 PCI_DEVICE_ID_LSI_SAS1064R) ||
4043 (instance->pdev->device ==
4044 PCI_DEVICE_ID_DELL_PERC5) ||
4045 (instance->pdev->device ==
4046 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
4048 *instance->consumer =
4049 cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
4053 instance->instancet->disable_intr(instance);
4054 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
4055 instance->issuepend_done = 0;
4057 atomic_set(&instance->fw_outstanding, 0);
4058 megasas_internal_reset_defer_cmds(instance);
4060 dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
4061 fw_state, atomic_read(&instance->adprecovery));
4063 schedule_work(&instance->work_init);
4067 dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
4068 fw_state, instance->disableOnlineCtrlReset);
4072 tasklet_schedule(&instance->isr_tasklet);
4077 * megasas_isr - isr entry point
4079 * @devp: IRQ context address
4081 static irqreturn_t megasas_isr(int irq, void *devp)
4083 struct megasas_irq_context *irq_context = devp;
4084 struct megasas_instance *instance = irq_context->instance;
4085 unsigned long flags;
4088 if (atomic_read(&instance->fw_reset_no_pci_access))
4091 spin_lock_irqsave(&instance->hba_lock, flags);
4092 rc = megasas_deplete_reply_queue(instance, DID_OK);
4093 spin_unlock_irqrestore(&instance->hba_lock, flags);
4099 * megasas_transition_to_ready - Move the FW to READY state
4100 * @instance: Adapter soft state
4101 * @ocr: Adapter reset state
4103 * During the initialization, FW passes can potentially be in any one of
4104 * several possible states. If the FW in operational, waiting-for-handshake
4105 * states, driver must take steps to bring it to ready state. Otherwise, it
4106 * has to wait for the ready state.
4109 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
4114 u32 abs_state, curr_abs_state;
4116 abs_state = instance->instancet->read_fw_status_reg(instance);
4117 fw_state = abs_state & MFI_STATE_MASK;
4119 if (fw_state != MFI_STATE_READY)
4120 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
4123 while (fw_state != MFI_STATE_READY) {
4127 case MFI_STATE_FAULT:
4128 dev_printk(KERN_ERR, &instance->pdev->dev,
4129 "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
4130 abs_state & MFI_STATE_FAULT_CODE,
4131 abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4133 max_wait = MEGASAS_RESET_WAIT_TIME;
4136 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4137 megasas_dump_reg_set(instance->reg_set);
4141 case MFI_STATE_WAIT_HANDSHAKE:
4143 * Set the CLR bit in inbound doorbell
4145 if ((instance->pdev->device ==
4146 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4147 (instance->pdev->device ==
4148 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4149 (instance->adapter_type != MFI_SERIES))
4151 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4152 &instance->reg_set->doorbell);
4155 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4156 &instance->reg_set->inbound_doorbell);
4158 max_wait = MEGASAS_RESET_WAIT_TIME;
4161 case MFI_STATE_BOOT_MESSAGE_PENDING:
4162 if ((instance->pdev->device ==
4163 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4164 (instance->pdev->device ==
4165 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4166 (instance->adapter_type != MFI_SERIES))
4167 writel(MFI_INIT_HOTPLUG,
4168 &instance->reg_set->doorbell);
4170 writel(MFI_INIT_HOTPLUG,
4171 &instance->reg_set->inbound_doorbell);
4173 max_wait = MEGASAS_RESET_WAIT_TIME;
4176 case MFI_STATE_OPERATIONAL:
4178 * Bring it to READY state; assuming max wait 10 secs
4180 instance->instancet->disable_intr(instance);
4181 if ((instance->pdev->device ==
4182 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4183 (instance->pdev->device ==
4184 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4185 (instance->adapter_type != MFI_SERIES)) {
4186 writel(MFI_RESET_FLAGS,
4187 &instance->reg_set->doorbell);
4189 if (instance->adapter_type != MFI_SERIES) {
4190 for (i = 0; i < (10 * 1000); i += 20) {
4202 writel(MFI_RESET_FLAGS,
4203 &instance->reg_set->inbound_doorbell);
4205 max_wait = MEGASAS_RESET_WAIT_TIME;
4208 case MFI_STATE_UNDEFINED:
4210 * This state should not last for more than 2 seconds
4212 max_wait = MEGASAS_RESET_WAIT_TIME;
4215 case MFI_STATE_BB_INIT:
4216 max_wait = MEGASAS_RESET_WAIT_TIME;
4219 case MFI_STATE_FW_INIT:
4220 max_wait = MEGASAS_RESET_WAIT_TIME;
4223 case MFI_STATE_FW_INIT_2:
4224 max_wait = MEGASAS_RESET_WAIT_TIME;
4227 case MFI_STATE_DEVICE_SCAN:
4228 max_wait = MEGASAS_RESET_WAIT_TIME;
4231 case MFI_STATE_FLUSH_CACHE:
4232 max_wait = MEGASAS_RESET_WAIT_TIME;
4236 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4238 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4239 megasas_dump_reg_set(instance->reg_set);
4244 * The cur_state should not last for more than max_wait secs
4246 for (i = 0; i < max_wait * 50; i++) {
4247 curr_abs_state = instance->instancet->
4248 read_fw_status_reg(instance);
4250 if (abs_state == curr_abs_state) {
4257 * Return error if fw_state hasn't changed after max_wait
4259 if (curr_abs_state == abs_state) {
4260 dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4261 "in %d secs\n", fw_state, max_wait);
4262 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4263 megasas_dump_reg_set(instance->reg_set);
4267 abs_state = curr_abs_state;
4268 fw_state = curr_abs_state & MFI_STATE_MASK;
4270 dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4276 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
4277 * @instance: Adapter soft state
4279 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4282 u16 max_cmd = instance->max_mfi_cmds;
4283 struct megasas_cmd *cmd;
4285 if (!instance->frame_dma_pool)
4289 * Return all frames to pool
4291 for (i = 0; i < max_cmd; i++) {
4293 cmd = instance->cmd_list[i];
4296 dma_pool_free(instance->frame_dma_pool, cmd->frame,
4297 cmd->frame_phys_addr);
4300 dma_pool_free(instance->sense_dma_pool, cmd->sense,
4301 cmd->sense_phys_addr);
4305 * Now destroy the pool itself
4307 dma_pool_destroy(instance->frame_dma_pool);
4308 dma_pool_destroy(instance->sense_dma_pool);
4310 instance->frame_dma_pool = NULL;
4311 instance->sense_dma_pool = NULL;
4315 * megasas_create_frame_pool - Creates DMA pool for cmd frames
4316 * @instance: Adapter soft state
4318 * Each command packet has an embedded DMA memory buffer that is used for
4319 * filling MFI frame and the SG list that immediately follows the frame. This
4320 * function creates those DMA memory buffers for each command packet by using
4321 * PCI pool facility.
4323 static int megasas_create_frame_pool(struct megasas_instance *instance)
4328 struct megasas_cmd *cmd;
4330 max_cmd = instance->max_mfi_cmds;
4333 * For MFI controllers.
4335 * max_sge_sz = 16 byte (sizeof megasas_sge_skinny)
4336 * Total 960 byte (15 MFI frame of 64 byte)
4338 * Fusion adapter require only 3 extra frame.
4339 * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4340 * max_sge_sz = 12 byte (sizeof megasas_sge64)
4341 * Total 192 byte (3 MFI frame of 64 byte)
4343 frame_count = (instance->adapter_type == MFI_SERIES) ?
4345 instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4347 * Use DMA pool facility provided by PCI layer
4349 instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4350 &instance->pdev->dev,
4351 instance->mfi_frame_size, 256, 0);
4353 if (!instance->frame_dma_pool) {
4354 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4358 instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4359 &instance->pdev->dev, 128,
4362 if (!instance->sense_dma_pool) {
4363 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4365 dma_pool_destroy(instance->frame_dma_pool);
4366 instance->frame_dma_pool = NULL;
4372 * Allocate and attach a frame to each of the commands in cmd_list.
4373 * By making cmd->index as the context instead of the &cmd, we can
4374 * always use 32bit context regardless of the architecture
4376 for (i = 0; i < max_cmd; i++) {
4378 cmd = instance->cmd_list[i];
4380 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4381 GFP_KERNEL, &cmd->frame_phys_addr);
4383 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4384 GFP_KERNEL, &cmd->sense_phys_addr);
4387 * megasas_teardown_frame_pool() takes care of freeing
4388 * whatever has been allocated
4390 if (!cmd->frame || !cmd->sense) {
4391 dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4392 megasas_teardown_frame_pool(instance);
4396 cmd->frame->io.context = cpu_to_le32(cmd->index);
4397 cmd->frame->io.pad_0 = 0;
4398 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4399 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4406 * megasas_free_cmds - Free all the cmds in the free cmd pool
4407 * @instance: Adapter soft state
4409 void megasas_free_cmds(struct megasas_instance *instance)
4413 /* First free the MFI frame pool */
4414 megasas_teardown_frame_pool(instance);
4416 /* Free all the commands in the cmd_list */
4417 for (i = 0; i < instance->max_mfi_cmds; i++)
4419 kfree(instance->cmd_list[i]);
4421 /* Free the cmd_list buffer itself */
4422 kfree(instance->cmd_list);
4423 instance->cmd_list = NULL;
4425 INIT_LIST_HEAD(&instance->cmd_pool);
4429 * megasas_alloc_cmds - Allocates the command packets
4430 * @instance: Adapter soft state
4432 * Each command that is issued to the FW, whether IO commands from the OS or
4433 * internal commands like IOCTLs, are wrapped in local data structure called
4434 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4437 * Each frame has a 32-bit field called context (tag). This context is used
4438 * to get back the megasas_cmd from the frame when a frame gets completed in
4439 * the ISR. Typically the address of the megasas_cmd itself would be used as
4440 * the context. But we wanted to keep the differences between 32 and 64 bit
4441 * systems to the mininum. We always use 32 bit integers for the context. In
4442 * this driver, the 32 bit values are the indices into an array cmd_list.
4443 * This array is used only to look up the megasas_cmd given the context. The
4444 * free commands themselves are maintained in a linked list called cmd_pool.
4446 int megasas_alloc_cmds(struct megasas_instance *instance)
4451 struct megasas_cmd *cmd;
4453 max_cmd = instance->max_mfi_cmds;
4456 * instance->cmd_list is an array of struct megasas_cmd pointers.
4457 * Allocate the dynamic array first and then allocate individual
4460 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4462 if (!instance->cmd_list) {
4463 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4467 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4469 for (i = 0; i < max_cmd; i++) {
4470 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4473 if (!instance->cmd_list[i]) {
4475 for (j = 0; j < i; j++)
4476 kfree(instance->cmd_list[j]);
4478 kfree(instance->cmd_list);
4479 instance->cmd_list = NULL;
4485 for (i = 0; i < max_cmd; i++) {
4486 cmd = instance->cmd_list[i];
4487 memset(cmd, 0, sizeof(struct megasas_cmd));
4490 cmd->instance = instance;
4492 list_add_tail(&cmd->list, &instance->cmd_pool);
4496 * Create a frame pool and assign one frame to each cmd
4498 if (megasas_create_frame_pool(instance)) {
4499 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4500 megasas_free_cmds(instance);
4508 * dcmd_timeout_ocr_possible - Check if OCR is possible based on Driver/FW state.
4509 * @instance: Adapter soft state
4511 * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4512 * or FW is not under OCR.
4515 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4517 if (instance->adapter_type == MFI_SERIES)
4518 return KILL_ADAPTER;
4519 else if (instance->unload ||
4520 test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4521 &instance->reset_flags))
4522 return IGNORE_TIMEOUT;
4524 return INITIATE_OCR;
4528 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4531 struct megasas_cmd *cmd;
4532 struct megasas_dcmd_frame *dcmd;
4534 struct MR_PRIV_DEVICE *mr_device_priv_data;
4537 device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4538 cmd = megasas_get_cmd(instance);
4541 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4545 dcmd = &cmd->frame->dcmd;
4547 memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4548 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4550 dcmd->mbox.s[0] = cpu_to_le16(device_id);
4551 dcmd->cmd = MFI_CMD_DCMD;
4552 dcmd->cmd_status = 0xFF;
4553 dcmd->sge_count = 1;
4554 dcmd->flags = MFI_FRAME_DIR_READ;
4557 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4558 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4560 megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4561 sizeof(struct MR_PD_INFO));
4563 if ((instance->adapter_type != MFI_SERIES) &&
4564 !instance->mask_interrupts)
4565 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4567 ret = megasas_issue_polled(instance, cmd);
4571 mr_device_priv_data = sdev->hostdata;
4572 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4573 mr_device_priv_data->interface_type =
4574 instance->pd_info->state.ddf.pdType.intf;
4579 switch (dcmd_timeout_ocr_possible(instance)) {
4581 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4582 mutex_unlock(&instance->reset_mutex);
4583 megasas_reset_fusion(instance->host,
4584 MFI_IO_TIMEOUT_OCR);
4585 mutex_lock(&instance->reset_mutex);
4588 megaraid_sas_kill_hba(instance);
4590 case IGNORE_TIMEOUT:
4591 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4592 __func__, __LINE__);
4599 if (ret != DCMD_TIMEOUT)
4600 megasas_return_cmd(instance, cmd);
4605 * megasas_get_pd_list_info - Returns FW's pd_list structure
4606 * @instance: Adapter soft state
4607 * @pd_list: pd_list structure
4609 * Issues an internal command (DCMD) to get the FW's controller PD
4610 * list structure. This information is mainly used to find out SYSTEM
4611 * supported by the FW.
4614 megasas_get_pd_list(struct megasas_instance *instance)
4616 int ret = 0, pd_index = 0;
4617 struct megasas_cmd *cmd;
4618 struct megasas_dcmd_frame *dcmd;
4619 struct MR_PD_LIST *ci;
4620 struct MR_PD_ADDRESS *pd_addr;
4622 if (instance->pd_list_not_supported) {
4623 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4624 "not supported by firmware\n");
4628 ci = instance->pd_list_buf;
4630 cmd = megasas_get_cmd(instance);
4633 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4637 dcmd = &cmd->frame->dcmd;
4639 memset(ci, 0, sizeof(*ci));
4640 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4642 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4643 dcmd->mbox.b[1] = 0;
4644 dcmd->cmd = MFI_CMD_DCMD;
4645 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4646 dcmd->sge_count = 1;
4647 dcmd->flags = MFI_FRAME_DIR_READ;
4650 dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4651 dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4653 megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4654 (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4656 if ((instance->adapter_type != MFI_SERIES) &&
4657 !instance->mask_interrupts)
4658 ret = megasas_issue_blocked_cmd(instance, cmd,
4659 MFI_IO_TIMEOUT_SECS);
4661 ret = megasas_issue_polled(instance, cmd);
4665 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4666 "failed/not supported by firmware\n");
4668 if (instance->adapter_type != MFI_SERIES)
4669 megaraid_sas_kill_hba(instance);
4671 instance->pd_list_not_supported = 1;
4675 switch (dcmd_timeout_ocr_possible(instance)) {
4677 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4679 * DCMD failed from AEN path.
4680 * AEN path already hold reset_mutex to avoid PCI access
4681 * while OCR is in progress.
4683 mutex_unlock(&instance->reset_mutex);
4684 megasas_reset_fusion(instance->host,
4685 MFI_IO_TIMEOUT_OCR);
4686 mutex_lock(&instance->reset_mutex);
4689 megaraid_sas_kill_hba(instance);
4691 case IGNORE_TIMEOUT:
4692 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4693 __func__, __LINE__);
4701 if (megasas_dbg_lvl & LD_PD_DEBUG)
4702 dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4703 __func__, le32_to_cpu(ci->count));
4705 if ((le32_to_cpu(ci->count) >
4706 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4709 memset(instance->local_pd_list, 0,
4710 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4712 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4713 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid =
4714 le16_to_cpu(pd_addr->deviceId);
4715 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType =
4716 pd_addr->scsiDevType;
4717 instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState =
4719 if (megasas_dbg_lvl & LD_PD_DEBUG)
4720 dev_info(&instance->pdev->dev,
4721 "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4722 pd_index, le16_to_cpu(pd_addr->deviceId),
4723 pd_addr->scsiDevType);
4727 memcpy(instance->pd_list, instance->local_pd_list,
4728 sizeof(instance->pd_list));
4733 if (ret != DCMD_TIMEOUT)
4734 megasas_return_cmd(instance, cmd);
4740 * megasas_get_ld_list_info - Returns FW's ld_list structure
4741 * @instance: Adapter soft state
4742 * @ld_list: ld_list structure
4744 * Issues an internal command (DCMD) to get the FW's controller PD
4745 * list structure. This information is mainly used to find out SYSTEM
4746 * supported by the FW.
4749 megasas_get_ld_list(struct megasas_instance *instance)
4751 int ret = 0, ld_index = 0, ids = 0;
4752 struct megasas_cmd *cmd;
4753 struct megasas_dcmd_frame *dcmd;
4754 struct MR_LD_LIST *ci;
4755 dma_addr_t ci_h = 0;
4758 ci = instance->ld_list_buf;
4759 ci_h = instance->ld_list_buf_h;
4761 cmd = megasas_get_cmd(instance);
4764 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4768 dcmd = &cmd->frame->dcmd;
4770 memset(ci, 0, sizeof(*ci));
4771 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4773 if (instance->supportmax256vd)
4774 dcmd->mbox.b[0] = 1;
4775 dcmd->cmd = MFI_CMD_DCMD;
4776 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4777 dcmd->sge_count = 1;
4778 dcmd->flags = MFI_FRAME_DIR_READ;
4780 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4781 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4784 megasas_set_dma_settings(instance, dcmd, ci_h,
4785 sizeof(struct MR_LD_LIST));
4787 if ((instance->adapter_type != MFI_SERIES) &&
4788 !instance->mask_interrupts)
4789 ret = megasas_issue_blocked_cmd(instance, cmd,
4790 MFI_IO_TIMEOUT_SECS);
4792 ret = megasas_issue_polled(instance, cmd);
4794 ld_count = le32_to_cpu(ci->ldCount);
4798 megaraid_sas_kill_hba(instance);
4802 switch (dcmd_timeout_ocr_possible(instance)) {
4804 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4806 * DCMD failed from AEN path.
4807 * AEN path already hold reset_mutex to avoid PCI access
4808 * while OCR is in progress.
4810 mutex_unlock(&instance->reset_mutex);
4811 megasas_reset_fusion(instance->host,
4812 MFI_IO_TIMEOUT_OCR);
4813 mutex_lock(&instance->reset_mutex);
4816 megaraid_sas_kill_hba(instance);
4818 case IGNORE_TIMEOUT:
4819 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4820 __func__, __LINE__);
4827 if (megasas_dbg_lvl & LD_PD_DEBUG)
4828 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4829 __func__, ld_count);
4831 if (ld_count > instance->fw_supported_vd_count)
4834 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4836 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4837 if (ci->ldList[ld_index].state != 0) {
4838 ids = ci->ldList[ld_index].ref.targetId;
4839 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4840 if (megasas_dbg_lvl & LD_PD_DEBUG)
4841 dev_info(&instance->pdev->dev,
4842 "LD%d: targetID: 0x%03x\n",
4850 if (ret != DCMD_TIMEOUT)
4851 megasas_return_cmd(instance, cmd);
4857 * megasas_ld_list_query - Returns FW's ld_list structure
4858 * @instance: Adapter soft state
4859 * @query_type: ld_list structure type
4861 * Issues an internal command (DCMD) to get the FW's controller PD
4862 * list structure. This information is mainly used to find out SYSTEM
4863 * supported by the FW.
4866 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4868 int ret = 0, ld_index = 0, ids = 0;
4869 struct megasas_cmd *cmd;
4870 struct megasas_dcmd_frame *dcmd;
4871 struct MR_LD_TARGETID_LIST *ci;
4872 dma_addr_t ci_h = 0;
4875 ci = instance->ld_targetid_list_buf;
4876 ci_h = instance->ld_targetid_list_buf_h;
4878 cmd = megasas_get_cmd(instance);
4881 dev_warn(&instance->pdev->dev,
4882 "megasas_ld_list_query: Failed to get cmd\n");
4886 dcmd = &cmd->frame->dcmd;
4888 memset(ci, 0, sizeof(*ci));
4889 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4891 dcmd->mbox.b[0] = query_type;
4892 if (instance->supportmax256vd)
4893 dcmd->mbox.b[2] = 1;
4895 dcmd->cmd = MFI_CMD_DCMD;
4896 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4897 dcmd->sge_count = 1;
4898 dcmd->flags = MFI_FRAME_DIR_READ;
4900 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4901 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4904 megasas_set_dma_settings(instance, dcmd, ci_h,
4905 sizeof(struct MR_LD_TARGETID_LIST));
4907 if ((instance->adapter_type != MFI_SERIES) &&
4908 !instance->mask_interrupts)
4909 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4911 ret = megasas_issue_polled(instance, cmd);
4915 dev_info(&instance->pdev->dev,
4916 "DCMD not supported by firmware - %s %d\n",
4917 __func__, __LINE__);
4918 ret = megasas_get_ld_list(instance);
4921 switch (dcmd_timeout_ocr_possible(instance)) {
4923 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4925 * DCMD failed from AEN path.
4926 * AEN path already hold reset_mutex to avoid PCI access
4927 * while OCR is in progress.
4929 mutex_unlock(&instance->reset_mutex);
4930 megasas_reset_fusion(instance->host,
4931 MFI_IO_TIMEOUT_OCR);
4932 mutex_lock(&instance->reset_mutex);
4935 megaraid_sas_kill_hba(instance);
4937 case IGNORE_TIMEOUT:
4938 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4939 __func__, __LINE__);
4945 tgtid_count = le32_to_cpu(ci->count);
4947 if (megasas_dbg_lvl & LD_PD_DEBUG)
4948 dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4949 __func__, tgtid_count);
4951 if ((tgtid_count > (instance->fw_supported_vd_count)))
4954 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4955 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4956 ids = ci->targetId[ld_index];
4957 instance->ld_ids[ids] = ci->targetId[ld_index];
4958 if (megasas_dbg_lvl & LD_PD_DEBUG)
4959 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4960 ld_index, ci->targetId[ld_index]);
4966 if (ret != DCMD_TIMEOUT)
4967 megasas_return_cmd(instance, cmd);
4973 * megasas_host_device_list_query
4974 * dcmd.opcode - MR_DCMD_CTRL_DEVICE_LIST_GET
4975 * dcmd.mbox - reserved
4976 * dcmd.sge IN - ptr to return MR_HOST_DEVICE_LIST structure
4977 * Desc: This DCMD will return the combined device list
4978 * Status: MFI_STAT_OK - List returned successfully
4979 * MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4981 * @instance: Adapter soft state
4982 * @is_probe: Driver probe check
4983 * Return: 0 if DCMD succeeded
4984 * non-zero if failed
4987 megasas_host_device_list_query(struct megasas_instance *instance,
4990 int ret, i, target_id;
4991 struct megasas_cmd *cmd;
4992 struct megasas_dcmd_frame *dcmd;
4993 struct MR_HOST_DEVICE_LIST *ci;
4997 ci = instance->host_device_list_buf;
4998 ci_h = instance->host_device_list_buf_h;
5000 cmd = megasas_get_cmd(instance);
5003 dev_warn(&instance->pdev->dev,
5004 "%s: failed to get cmd\n",
5009 dcmd = &cmd->frame->dcmd;
5011 memset(ci, 0, sizeof(*ci));
5012 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5014 dcmd->mbox.b[0] = is_probe ? 0 : 1;
5015 dcmd->cmd = MFI_CMD_DCMD;
5016 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5017 dcmd->sge_count = 1;
5018 dcmd->flags = MFI_FRAME_DIR_READ;
5021 dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
5022 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
5024 megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
5026 if (!instance->mask_interrupts) {
5027 ret = megasas_issue_blocked_cmd(instance, cmd,
5028 MFI_IO_TIMEOUT_SECS);
5030 ret = megasas_issue_polled(instance, cmd);
5031 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5036 /* Fill the internal pd_list and ld_ids array based on
5037 * targetIds returned by FW
5039 count = le32_to_cpu(ci->count);
5041 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
5044 if (megasas_dbg_lvl & LD_PD_DEBUG)
5045 dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
5048 memset(instance->local_pd_list, 0,
5049 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
5050 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
5051 for (i = 0; i < count; i++) {
5052 target_id = le16_to_cpu(ci->host_device_list[i].target_id);
5053 if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
5054 instance->local_pd_list[target_id].tid = target_id;
5055 instance->local_pd_list[target_id].driveType =
5056 ci->host_device_list[i].scsi_type;
5057 instance->local_pd_list[target_id].driveState =
5059 if (megasas_dbg_lvl & LD_PD_DEBUG)
5060 dev_info(&instance->pdev->dev,
5061 "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
5062 i, target_id, ci->host_device_list[i].scsi_type);
5064 instance->ld_ids[target_id] = target_id;
5065 if (megasas_dbg_lvl & LD_PD_DEBUG)
5066 dev_info(&instance->pdev->dev,
5067 "Device %d: LD targetID: 0x%03x\n",
5072 memcpy(instance->pd_list, instance->local_pd_list,
5073 sizeof(instance->pd_list));
5077 switch (dcmd_timeout_ocr_possible(instance)) {
5079 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5080 mutex_unlock(&instance->reset_mutex);
5081 megasas_reset_fusion(instance->host,
5082 MFI_IO_TIMEOUT_OCR);
5083 mutex_lock(&instance->reset_mutex);
5086 megaraid_sas_kill_hba(instance);
5088 case IGNORE_TIMEOUT:
5089 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5090 __func__, __LINE__);
5095 dev_err(&instance->pdev->dev,
5096 "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
5101 if (ret != DCMD_TIMEOUT)
5102 megasas_return_cmd(instance, cmd);
5108 * megasas_update_ext_vd_details : Update details w.r.t Extended VD
5109 * instance : Controller's instance
5111 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
5113 struct fusion_context *fusion;
5114 u32 ventura_map_sz = 0;
5116 fusion = instance->ctrl_context;
5117 /* For MFI based controllers return dummy success */
5121 instance->supportmax256vd =
5122 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
5123 /* Below is additional check to address future FW enhancement */
5124 if (instance->ctrl_info_buf->max_lds > 64)
5125 instance->supportmax256vd = 1;
5127 instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
5128 * MEGASAS_MAX_DEV_PER_CHANNEL;
5129 instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
5130 * MEGASAS_MAX_DEV_PER_CHANNEL;
5131 if (instance->supportmax256vd) {
5132 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
5133 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5135 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5136 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5139 dev_info(&instance->pdev->dev,
5140 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
5141 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
5142 instance->ctrl_info_buf->max_lds);
5144 if (instance->max_raid_mapsize) {
5145 ventura_map_sz = instance->max_raid_mapsize *
5146 MR_MIN_MAP_SIZE; /* 64k */
5147 fusion->current_map_sz = ventura_map_sz;
5148 fusion->max_map_sz = ventura_map_sz;
5150 fusion->old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
5151 (sizeof(struct MR_LD_SPAN_MAP) *
5152 (instance->fw_supported_vd_count - 1));
5153 fusion->new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
5155 fusion->max_map_sz =
5156 max(fusion->old_map_sz, fusion->new_map_sz);
5158 if (instance->supportmax256vd)
5159 fusion->current_map_sz = fusion->new_map_sz;
5161 fusion->current_map_sz = fusion->old_map_sz;
5163 /* irrespective of FW raid maps, driver raid map is constant */
5164 fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
5168 * dcmd.opcode - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
5169 * dcmd.hdr.length - number of bytes to read
5170 * dcmd.sge - Ptr to MR_SNAPDUMP_PROPERTIES
5171 * Desc: Fill in snapdump properties
5172 * Status: MFI_STAT_OK- Command successful
5174 void megasas_get_snapdump_properties(struct megasas_instance *instance)
5177 struct megasas_cmd *cmd;
5178 struct megasas_dcmd_frame *dcmd;
5179 struct MR_SNAPDUMP_PROPERTIES *ci;
5180 dma_addr_t ci_h = 0;
5182 ci = instance->snapdump_prop;
5183 ci_h = instance->snapdump_prop_h;
5188 cmd = megasas_get_cmd(instance);
5191 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
5195 dcmd = &cmd->frame->dcmd;
5197 memset(ci, 0, sizeof(*ci));
5198 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5200 dcmd->cmd = MFI_CMD_DCMD;
5201 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5202 dcmd->sge_count = 1;
5203 dcmd->flags = MFI_FRAME_DIR_READ;
5206 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5207 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5209 megasas_set_dma_settings(instance, dcmd, ci_h,
5210 sizeof(struct MR_SNAPDUMP_PROPERTIES));
5212 if (!instance->mask_interrupts) {
5213 ret = megasas_issue_blocked_cmd(instance, cmd,
5214 MFI_IO_TIMEOUT_SECS);
5216 ret = megasas_issue_polled(instance, cmd);
5217 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5222 instance->snapdump_wait_time =
5223 min_t(u8, ci->trigger_min_num_sec_before_ocr,
5224 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5228 switch (dcmd_timeout_ocr_possible(instance)) {
5230 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5231 mutex_unlock(&instance->reset_mutex);
5232 megasas_reset_fusion(instance->host,
5233 MFI_IO_TIMEOUT_OCR);
5234 mutex_lock(&instance->reset_mutex);
5237 megaraid_sas_kill_hba(instance);
5239 case IGNORE_TIMEOUT:
5240 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5241 __func__, __LINE__);
5246 if (ret != DCMD_TIMEOUT)
5247 megasas_return_cmd(instance, cmd);
5251 * megasas_get_ctrl_info - Returns FW's controller structure
5252 * @instance: Adapter soft state
5254 * Issues an internal command (DCMD) to get the FW's controller structure.
5255 * This information is mainly used to find out the maximum IO transfer per
5256 * command supported by the FW.
5259 megasas_get_ctrl_info(struct megasas_instance *instance)
5262 struct megasas_cmd *cmd;
5263 struct megasas_dcmd_frame *dcmd;
5264 struct megasas_ctrl_info *ci;
5265 dma_addr_t ci_h = 0;
5267 ci = instance->ctrl_info_buf;
5268 ci_h = instance->ctrl_info_buf_h;
5270 cmd = megasas_get_cmd(instance);
5273 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5277 dcmd = &cmd->frame->dcmd;
5279 memset(ci, 0, sizeof(*ci));
5280 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5282 dcmd->cmd = MFI_CMD_DCMD;
5283 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5284 dcmd->sge_count = 1;
5285 dcmd->flags = MFI_FRAME_DIR_READ;
5288 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5289 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5290 dcmd->mbox.b[0] = 1;
5292 megasas_set_dma_settings(instance, dcmd, ci_h,
5293 sizeof(struct megasas_ctrl_info));
5295 if ((instance->adapter_type != MFI_SERIES) &&
5296 !instance->mask_interrupts) {
5297 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5299 ret = megasas_issue_polled(instance, cmd);
5300 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5305 /* Save required controller information in
5306 * CPU endianness format.
5308 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5309 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5310 le32_to_cpus((u32 *)&ci->adapterOperations2);
5311 le32_to_cpus((u32 *)&ci->adapterOperations3);
5312 le16_to_cpus((u16 *)&ci->adapter_operations4);
5313 le32_to_cpus((u32 *)&ci->adapter_operations5);
5315 /* Update the latest Ext VD info.
5316 * From Init path, store current firmware details.
5317 * From OCR path, detect any firmware properties changes.
5318 * in case of Firmware upgrade without system reboot.
5320 megasas_update_ext_vd_details(instance);
5321 instance->support_seqnum_jbod_fp =
5322 ci->adapterOperations3.useSeqNumJbodFP;
5323 instance->support_morethan256jbod =
5324 ci->adapter_operations4.support_pd_map_target_id;
5325 instance->support_nvme_passthru =
5326 ci->adapter_operations4.support_nvme_passthru;
5327 instance->support_pci_lane_margining =
5328 ci->adapter_operations5.support_pci_lane_margining;
5329 instance->task_abort_tmo = ci->TaskAbortTO;
5330 instance->max_reset_tmo = ci->MaxResetTO;
5332 /*Check whether controller is iMR or MR */
5333 instance->is_imr = (ci->memory_size ? 0 : 1);
5335 instance->snapdump_wait_time =
5336 (ci->properties.on_off_properties2.enable_snap_dump ?
5337 MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5339 instance->enable_fw_dev_list =
5340 ci->properties.on_off_properties2.enable_fw_dev_list;
5342 dev_info(&instance->pdev->dev,
5343 "controller type\t: %s(%dMB)\n",
5344 instance->is_imr ? "iMR" : "MR",
5345 le16_to_cpu(ci->memory_size));
5347 instance->disableOnlineCtrlReset =
5348 ci->properties.OnOffProperties.disableOnlineCtrlReset;
5349 instance->secure_jbod_support =
5350 ci->adapterOperations3.supportSecurityonJBOD;
5351 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5352 instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5353 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5354 instance->secure_jbod_support ? "Yes" : "No");
5355 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5356 instance->support_nvme_passthru ? "Yes" : "No");
5357 dev_info(&instance->pdev->dev,
5358 "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5359 instance->task_abort_tmo, instance->max_reset_tmo);
5360 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5361 instance->support_seqnum_jbod_fp ? "Yes" : "No");
5362 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5363 instance->support_pci_lane_margining ? "Yes" : "No");
5368 switch (dcmd_timeout_ocr_possible(instance)) {
5370 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5371 mutex_unlock(&instance->reset_mutex);
5372 megasas_reset_fusion(instance->host,
5373 MFI_IO_TIMEOUT_OCR);
5374 mutex_lock(&instance->reset_mutex);
5377 megaraid_sas_kill_hba(instance);
5379 case IGNORE_TIMEOUT:
5380 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5381 __func__, __LINE__);
5386 megaraid_sas_kill_hba(instance);
5391 if (ret != DCMD_TIMEOUT)
5392 megasas_return_cmd(instance, cmd);
5398 * megasas_set_crash_dump_params - Sends address of crash dump DMA buffer
5401 * @instance: Adapter soft state
5402 * @crash_buf_state - tell FW to turn ON/OFF crash dump feature
5403 MR_CRASH_BUF_TURN_OFF = 0
5404 MR_CRASH_BUF_TURN_ON = 1
5405 * @return 0 on success non-zero on failure.
5406 * Issues an internal command (DCMD) to set parameters for crash dump feature.
5407 * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5408 * that driver supports crash dump feature. This DCMD will be sent only if
5409 * crash dump feature is supported by the FW.
5412 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5416 struct megasas_cmd *cmd;
5417 struct megasas_dcmd_frame *dcmd;
5419 cmd = megasas_get_cmd(instance);
5422 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5427 dcmd = &cmd->frame->dcmd;
5429 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5430 dcmd->mbox.b[0] = crash_buf_state;
5431 dcmd->cmd = MFI_CMD_DCMD;
5432 dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5433 dcmd->sge_count = 1;
5434 dcmd->flags = MFI_FRAME_DIR_NONE;
5437 dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5438 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5440 megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5441 CRASH_DMA_BUF_SIZE);
5443 if ((instance->adapter_type != MFI_SERIES) &&
5444 !instance->mask_interrupts)
5445 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5447 ret = megasas_issue_polled(instance, cmd);
5449 if (ret == DCMD_TIMEOUT) {
5450 switch (dcmd_timeout_ocr_possible(instance)) {
5452 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5453 megasas_reset_fusion(instance->host,
5454 MFI_IO_TIMEOUT_OCR);
5457 megaraid_sas_kill_hba(instance);
5459 case IGNORE_TIMEOUT:
5460 dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5461 __func__, __LINE__);
5465 megasas_return_cmd(instance, cmd);
5471 * megasas_issue_init_mfi - Initializes the FW
5472 * @instance: Adapter soft state
5474 * Issues the INIT MFI cmd
5477 megasas_issue_init_mfi(struct megasas_instance *instance)
5480 struct megasas_cmd *cmd;
5481 struct megasas_init_frame *init_frame;
5482 struct megasas_init_queue_info *initq_info;
5483 dma_addr_t init_frame_h;
5484 dma_addr_t initq_info_h;
5487 * Prepare a init frame. Note the init frame points to queue info
5488 * structure. Each frame has SGL allocated after first 64 bytes. For
5489 * this frame - since we don't need any SGL - we use SGL's space as
5490 * queue info structure
5492 * We will not get a NULL command below. We just created the pool.
5494 cmd = megasas_get_cmd(instance);
5496 init_frame = (struct megasas_init_frame *)cmd->frame;
5497 initq_info = (struct megasas_init_queue_info *)
5498 ((unsigned long)init_frame + 64);
5500 init_frame_h = cmd->frame_phys_addr;
5501 initq_info_h = init_frame_h + 64;
5503 context = init_frame->context;
5504 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5505 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5506 init_frame->context = context;
5508 initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5509 initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5511 initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5512 initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5514 init_frame->cmd = MFI_CMD_INIT;
5515 init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5516 init_frame->queue_info_new_phys_addr_lo =
5517 cpu_to_le32(lower_32_bits(initq_info_h));
5518 init_frame->queue_info_new_phys_addr_hi =
5519 cpu_to_le32(upper_32_bits(initq_info_h));
5521 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5524 * disable the intr before firing the init frame to FW
5526 instance->instancet->disable_intr(instance);
5529 * Issue the init frame in polled mode
5532 if (megasas_issue_polled(instance, cmd)) {
5533 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5534 megasas_return_cmd(instance, cmd);
5538 megasas_return_cmd(instance, cmd);
5547 megasas_init_adapter_mfi(struct megasas_instance *instance)
5553 * Get various operational parameters from status register
5555 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5557 * Reduce the max supported cmds by 1. This is to ensure that the
5558 * reply_q_sz (1 more than the max cmd that driver may send)
5559 * does not exceed max cmds that the FW can support
5561 instance->max_fw_cmds = instance->max_fw_cmds-1;
5562 instance->max_mfi_cmds = instance->max_fw_cmds;
5563 instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5566 * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5567 * are reserved for IOCTL + driver's internal DCMDs.
5569 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5570 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5571 instance->max_scsi_cmds = (instance->max_fw_cmds -
5572 MEGASAS_SKINNY_INT_CMDS);
5573 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5575 instance->max_scsi_cmds = (instance->max_fw_cmds -
5577 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5580 instance->cur_can_queue = instance->max_scsi_cmds;
5582 * Create a pool of commands
5584 if (megasas_alloc_cmds(instance))
5585 goto fail_alloc_cmds;
5588 * Allocate memory for reply queue. Length of reply queue should
5589 * be _one_ more than the maximum commands handled by the firmware.
5591 * Note: When FW completes commands, it places corresponding contex
5592 * values in this circular reply queue. This circular queue is a fairly
5593 * typical producer-consumer queue. FW is the producer (of completed
5594 * commands) and the driver is the consumer.
5596 context_sz = sizeof(u32);
5597 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5599 instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5600 reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5602 if (!instance->reply_queue) {
5603 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5604 goto fail_reply_queue;
5607 if (megasas_issue_init_mfi(instance))
5610 if (megasas_get_ctrl_info(instance)) {
5611 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5612 "Fail from %s %d\n", instance->unique_id,
5613 __func__, __LINE__);
5617 instance->fw_support_ieee = 0;
5618 instance->fw_support_ieee =
5619 (instance->instancet->read_fw_status_reg(instance) &
5622 dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5623 instance->fw_support_ieee);
5625 if (instance->fw_support_ieee)
5626 instance->flag_ieee = 1;
5632 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5633 instance->reply_queue, instance->reply_queue_h);
5635 megasas_free_cmds(instance);
5642 void megasas_setup_irq_poll(struct megasas_instance *instance)
5644 struct megasas_irq_context *irq_ctx;
5647 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5649 /* Initialize IRQ poll */
5650 for (i = 0; i < count; i++) {
5651 irq_ctx = &instance->irq_context[i];
5652 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5653 irq_ctx->irq_poll_scheduled = false;
5654 irq_poll_init(&irq_ctx->irqpoll,
5655 instance->threshold_reply_count,
5661 * megasas_setup_irqs_ioapic - register legacy interrupts.
5662 * @instance: Adapter soft state
5664 * Do not enable interrupt, only setup ISRs.
5666 * Return 0 on success.
5669 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5671 struct pci_dev *pdev;
5673 pdev = instance->pdev;
5674 instance->irq_context[0].instance = instance;
5675 instance->irq_context[0].MSIxIndex = 0;
5676 snprintf(instance->irq_context->name, MEGASAS_MSIX_NAME_LEN, "%s%u",
5677 "megasas", instance->host->host_no);
5678 if (request_irq(pci_irq_vector(pdev, 0),
5679 instance->instancet->service_isr, IRQF_SHARED,
5680 instance->irq_context->name, &instance->irq_context[0])) {
5681 dev_err(&instance->pdev->dev,
5682 "Failed to register IRQ from %s %d\n",
5683 __func__, __LINE__);
5686 instance->perf_mode = MR_LATENCY_PERF_MODE;
5687 instance->low_latency_index_start = 0;
5692 * megasas_setup_irqs_msix - register MSI-x interrupts.
5693 * @instance: Adapter soft state
5694 * @is_probe: Driver probe check
5696 * Do not enable interrupt, only setup ISRs.
5698 * Return 0 on success.
5701 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5704 struct pci_dev *pdev;
5706 pdev = instance->pdev;
5709 for (i = 0; i < instance->msix_vectors; i++) {
5710 instance->irq_context[i].instance = instance;
5711 instance->irq_context[i].MSIxIndex = i;
5712 snprintf(instance->irq_context[i].name, MEGASAS_MSIX_NAME_LEN, "%s%u-msix%u",
5713 "megasas", instance->host->host_no, i);
5714 if (request_irq(pci_irq_vector(pdev, i),
5715 instance->instancet->service_isr, 0, instance->irq_context[i].name,
5716 &instance->irq_context[i])) {
5717 dev_err(&instance->pdev->dev,
5718 "Failed to register IRQ for vector %d.\n", i);
5719 for (j = 0; j < i; j++) {
5720 if (j < instance->low_latency_index_start)
5721 irq_set_affinity_hint(
5722 pci_irq_vector(pdev, j), NULL);
5723 free_irq(pci_irq_vector(pdev, j),
5724 &instance->irq_context[j]);
5726 /* Retry irq register for IO_APIC*/
5727 instance->msix_vectors = 0;
5728 instance->msix_load_balance = false;
5730 pci_free_irq_vectors(instance->pdev);
5731 return megasas_setup_irqs_ioapic(instance);
5742 * megasas_destroy_irqs- unregister interrupts.
5743 * @instance: Adapter soft state
5747 megasas_destroy_irqs(struct megasas_instance *instance) {
5751 struct megasas_irq_context *irq_ctx;
5753 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5754 if (instance->adapter_type != MFI_SERIES) {
5755 for (i = 0; i < count; i++) {
5756 irq_ctx = &instance->irq_context[i];
5757 irq_poll_disable(&irq_ctx->irqpoll);
5761 if (instance->msix_vectors)
5762 for (i = 0; i < instance->msix_vectors; i++) {
5763 if (i < instance->low_latency_index_start)
5764 irq_set_affinity_hint(
5765 pci_irq_vector(instance->pdev, i), NULL);
5766 free_irq(pci_irq_vector(instance->pdev, i),
5767 &instance->irq_context[i]);
5770 free_irq(pci_irq_vector(instance->pdev, 0),
5771 &instance->irq_context[0]);
5775 * megasas_setup_jbod_map - setup jbod map for FP seq_number.
5776 * @instance: Adapter soft state
5778 * Return 0 on success.
5781 megasas_setup_jbod_map(struct megasas_instance *instance)
5784 struct fusion_context *fusion = instance->ctrl_context;
5787 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5788 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5790 instance->use_seqnum_jbod_fp =
5791 instance->support_seqnum_jbod_fp;
5792 if (reset_devices || !fusion ||
5793 !instance->support_seqnum_jbod_fp) {
5794 dev_info(&instance->pdev->dev,
5795 "JBOD sequence map is disabled %s %d\n",
5796 __func__, __LINE__);
5797 instance->use_seqnum_jbod_fp = false;
5801 if (fusion->pd_seq_sync[0])
5804 for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5805 fusion->pd_seq_sync[i] = dma_alloc_coherent
5806 (&instance->pdev->dev, pd_seq_map_sz,
5807 &fusion->pd_seq_phys[i], GFP_KERNEL);
5808 if (!fusion->pd_seq_sync[i]) {
5809 dev_err(&instance->pdev->dev,
5810 "Failed to allocate memory from %s %d\n",
5811 __func__, __LINE__);
5813 dma_free_coherent(&instance->pdev->dev,
5814 pd_seq_map_sz, fusion->pd_seq_sync[0],
5815 fusion->pd_seq_phys[0]);
5816 fusion->pd_seq_sync[0] = NULL;
5818 instance->use_seqnum_jbod_fp = false;
5824 if (!megasas_sync_pd_seq_num(instance, false) &&
5825 !megasas_sync_pd_seq_num(instance, true))
5826 instance->use_seqnum_jbod_fp = true;
5828 instance->use_seqnum_jbod_fp = false;
5831 static void megasas_setup_reply_map(struct megasas_instance *instance)
5833 const struct cpumask *mask;
5834 unsigned int queue, cpu, low_latency_index_start;
5836 low_latency_index_start = instance->low_latency_index_start;
5838 for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
5839 mask = pci_irq_get_affinity(instance->pdev, queue);
5843 for_each_cpu(cpu, mask)
5844 instance->reply_map[cpu] = queue;
5849 queue = low_latency_index_start;
5850 for_each_possible_cpu(cpu) {
5851 instance->reply_map[cpu] = queue;
5852 if (queue == (instance->msix_vectors - 1))
5853 queue = low_latency_index_start;
5860 * megasas_get_device_list - Get the PD and LD device list from FW.
5861 * @instance: Adapter soft state
5862 * @return: Success or failure
5864 * Issue DCMDs to Firmware to get the PD and LD list.
5865 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5866 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5869 int megasas_get_device_list(struct megasas_instance *instance)
5871 memset(instance->pd_list, 0,
5872 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5873 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5875 if (instance->enable_fw_dev_list) {
5876 if (megasas_host_device_list_query(instance, true))
5879 if (megasas_get_pd_list(instance) < 0) {
5880 dev_err(&instance->pdev->dev, "failed to get PD list\n");
5884 if (megasas_ld_list_query(instance,
5885 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5886 dev_err(&instance->pdev->dev, "failed to get LD list\n");
5895 * megasas_set_high_iops_queue_affinity_hint - Set affinity hint for high IOPS queues
5896 * @instance: Adapter soft state
5900 megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5903 int local_numa_node;
5905 if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
5906 local_numa_node = dev_to_node(&instance->pdev->dev);
5908 for (i = 0; i < instance->low_latency_index_start; i++)
5909 irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5910 cpumask_of_node(local_numa_node));
5915 __megasas_alloc_irq_vectors(struct megasas_instance *instance)
5918 struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5919 struct irq_affinity *descp = &desc;
5921 irq_flags = PCI_IRQ_MSIX;
5923 if (instance->smp_affinity_enable)
5924 irq_flags |= PCI_IRQ_AFFINITY | PCI_IRQ_ALL_TYPES;
5928 /* Do not allocate msix vectors for poll_queues.
5929 * msix_vectors is always within a range of FW supported reply queue.
5931 i = pci_alloc_irq_vectors_affinity(instance->pdev,
5932 instance->low_latency_index_start,
5933 instance->msix_vectors - instance->iopoll_q_count, irq_flags, descp);
5939 * megasas_alloc_irq_vectors - Allocate IRQ vectors/enable MSI-x vectors
5940 * @instance: Adapter soft state
5944 megasas_alloc_irq_vectors(struct megasas_instance *instance)
5947 unsigned int num_msix_req;
5949 instance->iopoll_q_count = 0;
5950 if ((instance->adapter_type != MFI_SERIES) &&
5953 instance->perf_mode = MR_LATENCY_PERF_MODE;
5954 instance->low_latency_index_start = 1;
5956 /* reserve for default and non-mananged pre-vector. */
5957 if (instance->msix_vectors > (poll_queues + 2))
5958 instance->iopoll_q_count = poll_queues;
5960 instance->iopoll_q_count = 0;
5962 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5963 instance->msix_vectors = min(num_msix_req,
5964 instance->msix_vectors);
5968 i = __megasas_alloc_irq_vectors(instance);
5970 if (((instance->perf_mode == MR_BALANCED_PERF_MODE)
5971 || instance->iopoll_q_count) &&
5972 (i != (instance->msix_vectors - instance->iopoll_q_count))) {
5973 if (instance->msix_vectors)
5974 pci_free_irq_vectors(instance->pdev);
5975 /* Disable Balanced IOPS mode and try realloc vectors */
5976 instance->perf_mode = MR_LATENCY_PERF_MODE;
5977 instance->low_latency_index_start = 1;
5978 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5980 instance->msix_vectors = min(num_msix_req,
5981 instance->msix_vectors);
5983 instance->iopoll_q_count = 0;
5984 i = __megasas_alloc_irq_vectors(instance);
5988 dev_info(&instance->pdev->dev,
5989 "requested/available msix %d/%d poll_queue %d\n",
5990 instance->msix_vectors - instance->iopoll_q_count,
5991 i, instance->iopoll_q_count);
5994 instance->msix_vectors = i;
5996 instance->msix_vectors = 0;
5998 if (instance->smp_affinity_enable)
5999 megasas_set_high_iops_queue_affinity_hint(instance);
6003 * megasas_init_fw - Initializes the FW
6004 * @instance: Adapter soft state
6006 * This is the main function for initializing firmware
6009 static int megasas_init_fw(struct megasas_instance *instance)
6012 u32 max_sectors_2, tmp_sectors, msix_enable;
6013 u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
6014 resource_size_t base_addr;
6015 void *base_addr_phys;
6016 struct megasas_ctrl_info *ctrl_info = NULL;
6017 unsigned long bar_list;
6019 struct IOV_111 *iovPtr;
6020 struct fusion_context *fusion;
6021 bool intr_coalescing;
6022 unsigned int num_msix_req;
6025 fusion = instance->ctrl_context;
6027 /* Find first memory bar */
6028 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
6029 instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
6030 if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
6032 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
6036 base_addr = pci_resource_start(instance->pdev, instance->bar);
6037 instance->reg_set = ioremap(base_addr, 8192);
6039 if (!instance->reg_set) {
6040 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
6044 base_addr_phys = &base_addr;
6045 dev_printk(KERN_DEBUG, &instance->pdev->dev,
6046 "BAR:0x%lx BAR's base_addr(phys):%pa mapped virt_addr:0x%p\n",
6047 instance->bar, base_addr_phys, instance->reg_set);
6049 if (instance->adapter_type != MFI_SERIES)
6050 instance->instancet = &megasas_instance_template_fusion;
6052 switch (instance->pdev->device) {
6053 case PCI_DEVICE_ID_LSI_SAS1078R:
6054 case PCI_DEVICE_ID_LSI_SAS1078DE:
6055 instance->instancet = &megasas_instance_template_ppc;
6057 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
6058 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
6059 instance->instancet = &megasas_instance_template_gen2;
6061 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
6062 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
6063 instance->instancet = &megasas_instance_template_skinny;
6065 case PCI_DEVICE_ID_LSI_SAS1064R:
6066 case PCI_DEVICE_ID_DELL_PERC5:
6068 instance->instancet = &megasas_instance_template_xscale;
6069 instance->pd_list_not_supported = 1;
6074 if (megasas_transition_to_ready(instance, 0)) {
6075 dev_info(&instance->pdev->dev,
6076 "Failed to transition controller to ready from %s!\n",
6078 if (instance->adapter_type != MFI_SERIES) {
6079 status_reg = instance->instancet->read_fw_status_reg(
6081 if (status_reg & MFI_RESET_ADAPTER) {
6082 if (megasas_adp_reset_wait_for_ready
6083 (instance, true, 0) == FAILED)
6084 goto fail_ready_state;
6086 goto fail_ready_state;
6089 atomic_set(&instance->fw_reset_no_pci_access, 1);
6090 instance->instancet->adp_reset
6091 (instance, instance->reg_set);
6092 atomic_set(&instance->fw_reset_no_pci_access, 0);
6094 /*waiting for about 30 second before retry*/
6097 if (megasas_transition_to_ready(instance, 0))
6098 goto fail_ready_state;
6101 dev_info(&instance->pdev->dev,
6102 "FW restarted successfully from %s!\n",
6106 megasas_init_ctrl_params(instance);
6108 if (megasas_set_dma_mask(instance))
6109 goto fail_ready_state;
6111 if (megasas_alloc_ctrl_mem(instance))
6112 goto fail_alloc_dma_buf;
6114 if (megasas_alloc_ctrl_dma_buffers(instance))
6115 goto fail_alloc_dma_buf;
6117 fusion = instance->ctrl_context;
6119 if (instance->adapter_type >= VENTURA_SERIES) {
6121 megasas_readl(instance,
6122 &instance->reg_set->outbound_scratch_pad_2);
6123 instance->max_raid_mapsize = ((scratch_pad_2 >>
6124 MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
6125 MR_MAX_RAID_MAP_SIZE_MASK);
6128 instance->enable_sdev_max_qd = enable_sdev_max_qd;
6130 switch (instance->adapter_type) {
6131 case VENTURA_SERIES:
6132 fusion->pcie_bw_limitation = true;
6135 fusion->r56_div_offload = true;
6141 /* Check if MSI-X is supported while in ready state */
6142 msix_enable = (instance->instancet->read_fw_status_reg(instance) &
6144 if (msix_enable && !msix_disable) {
6146 scratch_pad_1 = megasas_readl
6147 (instance, &instance->reg_set->outbound_scratch_pad_1);
6148 /* Check max MSI-X vectors */
6150 if (instance->adapter_type == THUNDERBOLT_SERIES) {
6151 /* Thunderbolt Series*/
6152 instance->msix_vectors = (scratch_pad_1
6153 & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
6155 instance->msix_vectors = ((scratch_pad_1
6156 & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
6157 >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
6160 * For Invader series, > 8 MSI-x vectors
6161 * supported by FW/HW implies combined
6162 * reply queue mode is enabled.
6163 * For Ventura series, > 16 MSI-x vectors
6164 * supported by FW/HW implies combined
6165 * reply queue mode is enabled.
6167 switch (instance->adapter_type) {
6168 case INVADER_SERIES:
6169 if (instance->msix_vectors > 8)
6170 instance->msix_combined = true;
6173 case VENTURA_SERIES:
6174 if (instance->msix_vectors > 16)
6175 instance->msix_combined = true;
6180 instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
6183 if (instance->adapter_type >= INVADER_SERIES &&
6184 !instance->msix_combined) {
6185 instance->msix_load_balance = true;
6186 instance->smp_affinity_enable = false;
6189 /* Save 1-15 reply post index address to local memory
6190 * Index 0 is already saved from reg offset
6191 * MPI2_REPLY_POST_HOST_INDEX_OFFSET
6193 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
6194 instance->reply_post_host_index_addr[loop] =
6196 ((u8 __iomem *)instance->reg_set +
6197 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
6202 dev_info(&instance->pdev->dev,
6203 "firmware supports msix\t: (%d)",
6204 instance->msix_vectors);
6206 instance->msix_vectors = min(msix_vectors,
6207 instance->msix_vectors);
6208 } else /* MFI adapters */
6209 instance->msix_vectors = 1;
6213 * For Aero (if some conditions are met), driver will configure a
6214 * few additional reply queues with interrupt coalescing enabled.
6215 * These queues with interrupt coalescing enabled are called
6216 * High IOPS queues and rest of reply queues (based on number of
6217 * logical CPUs) are termed as Low latency queues.
6219 * Total Number of reply queues = High IOPS queues + low latency queues
6221 * For rest of fusion adapters, 1 additional reply queue will be
6222 * reserved for management commands, rest of reply queues
6223 * (based on number of logical CPUs) will be used for IOs and
6224 * referenced as IO queues.
6225 * Total Number of reply queues = 1 + IO queues
6227 * MFI adapters supports single MSI-x so single reply queue
6228 * will be used for IO and management commands.
6231 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
6233 if (intr_coalescing &&
6234 (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6235 (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
6236 instance->perf_mode = MR_BALANCED_PERF_MODE;
6238 instance->perf_mode = MR_LATENCY_PERF_MODE;
6241 if (instance->adapter_type == AERO_SERIES) {
6242 pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6243 speed = lnksta & PCI_EXP_LNKSTA_CLS;
6246 * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6247 * in latency perf mode and enable R1 PCI bandwidth algorithm
6250 instance->perf_mode = MR_LATENCY_PERF_MODE;
6251 fusion->pcie_bw_limitation = true;
6255 * Performance mode settings provided through module parameter-perf_mode will
6256 * take affect only for:
6257 * 1. Aero family of adapters.
6258 * 2. When user sets module parameter- perf_mode in range of 0-2.
6260 if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6261 (perf_mode <= MR_LATENCY_PERF_MODE))
6262 instance->perf_mode = perf_mode;
6264 * If intr coalescing is not supported by controller FW, then IOPS
6265 * and Balanced modes are not feasible.
6267 if (!intr_coalescing)
6268 instance->perf_mode = MR_LATENCY_PERF_MODE;
6272 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
6273 instance->low_latency_index_start =
6274 MR_HIGH_IOPS_QUEUE_COUNT;
6276 instance->low_latency_index_start = 1;
6278 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6280 instance->msix_vectors = min(num_msix_req,
6281 instance->msix_vectors);
6283 megasas_alloc_irq_vectors(instance);
6284 if (!instance->msix_vectors)
6285 instance->msix_load_balance = false;
6288 * MSI-X host index 0 is common for all adapter.
6289 * It is used for all MPT based Adapters.
6291 if (instance->msix_combined) {
6292 instance->reply_post_host_index_addr[0] =
6293 (u32 *)((u8 *)instance->reg_set +
6294 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6296 instance->reply_post_host_index_addr[0] =
6297 (u32 *)((u8 *)instance->reg_set +
6298 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6301 if (!instance->msix_vectors) {
6302 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6304 goto fail_init_adapter;
6307 megasas_setup_reply_map(instance);
6309 dev_info(&instance->pdev->dev,
6310 "current msix/online cpus\t: (%d/%d)\n",
6311 instance->msix_vectors, (unsigned int)num_online_cpus());
6312 dev_info(&instance->pdev->dev,
6313 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
6315 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6316 (unsigned long)instance);
6319 * Below are default value for legacy Firmware.
6320 * non-fusion based controllers
6322 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6323 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
6324 /* Get operational params, sge flags, send init cmd to controller */
6325 if (instance->instancet->init_adapter(instance))
6326 goto fail_init_adapter;
6328 if (instance->adapter_type >= VENTURA_SERIES) {
6330 megasas_readl(instance,
6331 &instance->reg_set->outbound_scratch_pad_3);
6332 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
6333 MR_DEFAULT_NVME_PAGE_SHIFT)
6334 instance->nvme_page_size =
6335 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
6337 dev_info(&instance->pdev->dev,
6338 "NVME page size\t: (%d)\n", instance->nvme_page_size);
6341 if (instance->msix_vectors ?
6342 megasas_setup_irqs_msix(instance, 1) :
6343 megasas_setup_irqs_ioapic(instance))
6344 goto fail_init_adapter;
6346 if (instance->adapter_type != MFI_SERIES)
6347 megasas_setup_irq_poll(instance);
6349 instance->instancet->enable_intr(instance);
6351 dev_info(&instance->pdev->dev, "INIT adapter done\n");
6353 megasas_setup_jbod_map(instance);
6355 if (megasas_get_device_list(instance) != SUCCESS) {
6356 dev_err(&instance->pdev->dev,
6357 "%s: megasas_get_device_list failed\n",
6359 goto fail_get_ld_pd_list;
6362 /* stream detection initialization */
6363 if (instance->adapter_type >= VENTURA_SERIES) {
6364 fusion->stream_detect_by_ld =
6365 kcalloc(MAX_LOGICAL_DRIVES_EXT,
6366 sizeof(struct LD_STREAM_DETECT *),
6368 if (!fusion->stream_detect_by_ld) {
6369 dev_err(&instance->pdev->dev,
6370 "unable to allocate stream detection for pool of LDs\n");
6371 goto fail_get_ld_pd_list;
6373 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6374 fusion->stream_detect_by_ld[i] =
6375 kzalloc(sizeof(struct LD_STREAM_DETECT),
6377 if (!fusion->stream_detect_by_ld[i]) {
6378 dev_err(&instance->pdev->dev,
6379 "unable to allocate stream detect by LD\n ");
6380 for (j = 0; j < i; ++j)
6381 kfree(fusion->stream_detect_by_ld[j]);
6382 kfree(fusion->stream_detect_by_ld);
6383 fusion->stream_detect_by_ld = NULL;
6384 goto fail_get_ld_pd_list;
6386 fusion->stream_detect_by_ld[i]->mru_bit_map
6392 * Compute the max allowed sectors per IO: The controller info has two
6393 * limits on max sectors. Driver should use the minimum of these two.
6395 * 1 << stripe_sz_ops.min = max sectors per strip
6397 * Note that older firmwares ( < FW ver 30) didn't report information
6398 * to calculate max_sectors_1. So the number ended up as zero always.
6401 ctrl_info = instance->ctrl_info_buf;
6403 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6404 le16_to_cpu(ctrl_info->max_strips_per_io);
6405 max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
6407 tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
6409 instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6410 instance->passive = ctrl_info->cluster.passive;
6411 memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
6412 instance->UnevenSpanSupport =
6413 ctrl_info->adapterOperations2.supportUnevenSpans;
6414 if (instance->UnevenSpanSupport) {
6415 struct fusion_context *fusion = instance->ctrl_context;
6416 if (MR_ValidateMapInfo(instance, instance->map_id))
6417 fusion->fast_path_io = 1;
6419 fusion->fast_path_io = 0;
6422 if (ctrl_info->host_interface.SRIOV) {
6423 instance->requestorId = ctrl_info->iov.requestorId;
6424 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6425 if (!ctrl_info->adapterOperations2.activePassive)
6426 instance->PlasmaFW111 = 1;
6428 dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6429 instance->PlasmaFW111 ? "1.11" : "new");
6431 if (instance->PlasmaFW111) {
6432 iovPtr = (struct IOV_111 *)
6433 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6434 instance->requestorId = iovPtr->requestorId;
6437 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6438 instance->requestorId);
6441 instance->crash_dump_fw_support =
6442 ctrl_info->adapterOperations3.supportCrashDump;
6443 instance->crash_dump_drv_support =
6444 (instance->crash_dump_fw_support &&
6445 instance->crash_dump_buf);
6446 if (instance->crash_dump_drv_support)
6447 megasas_set_crash_dump_params(instance,
6448 MR_CRASH_BUF_TURN_OFF);
6451 if (instance->crash_dump_buf)
6452 dma_free_coherent(&instance->pdev->dev,
6454 instance->crash_dump_buf,
6455 instance->crash_dump_h);
6456 instance->crash_dump_buf = NULL;
6459 if (instance->snapdump_wait_time) {
6460 megasas_get_snapdump_properties(instance);
6461 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6462 instance->snapdump_wait_time);
6465 dev_info(&instance->pdev->dev,
6466 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6467 le16_to_cpu(ctrl_info->pci.vendor_id),
6468 le16_to_cpu(ctrl_info->pci.device_id),
6469 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6470 le16_to_cpu(ctrl_info->pci.sub_device_id));
6471 dev_info(&instance->pdev->dev, "unevenspan support : %s\n",
6472 instance->UnevenSpanSupport ? "yes" : "no");
6473 dev_info(&instance->pdev->dev, "firmware crash dump : %s\n",
6474 instance->crash_dump_drv_support ? "yes" : "no");
6475 dev_info(&instance->pdev->dev, "JBOD sequence map : %s\n",
6476 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
6478 instance->max_sectors_per_req = instance->max_num_sge *
6479 SGE_BUFFER_SIZE / 512;
6480 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6481 instance->max_sectors_per_req = tmp_sectors;
6483 /* Check for valid throttlequeuedepth module parameter */
6484 if (throttlequeuedepth &&
6485 throttlequeuedepth <= instance->max_scsi_cmds)
6486 instance->throttlequeuedepth = throttlequeuedepth;
6488 instance->throttlequeuedepth =
6489 MEGASAS_THROTTLE_QUEUE_DEPTH;
6491 if ((resetwaittime < 1) ||
6492 (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6493 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6495 if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6496 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6498 /* Launch SR-IOV heartbeat timer */
6499 if (instance->requestorId) {
6500 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6501 megasas_start_timer(instance);
6503 instance->skip_heartbeat_timer_del = 1;
6504 goto fail_get_ld_pd_list;
6509 * Create and start watchdog thread which will monitor
6510 * controller state every 1 sec and trigger OCR when
6511 * it enters fault state
6513 if (instance->adapter_type != MFI_SERIES)
6514 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6515 goto fail_start_watchdog;
6519 fail_start_watchdog:
6520 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6521 del_timer_sync(&instance->sriov_heartbeat_timer);
6522 fail_get_ld_pd_list:
6523 instance->instancet->disable_intr(instance);
6524 megasas_destroy_irqs(instance);
6526 if (instance->msix_vectors)
6527 pci_free_irq_vectors(instance->pdev);
6528 instance->msix_vectors = 0;
6530 megasas_free_ctrl_dma_buffers(instance);
6531 megasas_free_ctrl_mem(instance);
6533 iounmap(instance->reg_set);
6536 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6538 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6539 __func__, __LINE__);
6544 * megasas_release_mfi - Reverses the FW initialization
6545 * @instance: Adapter soft state
6547 static void megasas_release_mfi(struct megasas_instance *instance)
6549 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6551 if (instance->reply_queue)
6552 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6553 instance->reply_queue, instance->reply_queue_h);
6555 megasas_free_cmds(instance);
6557 iounmap(instance->reg_set);
6559 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6563 * megasas_get_seq_num - Gets latest event sequence numbers
6564 * @instance: Adapter soft state
6565 * @eli: FW event log sequence numbers information
6567 * FW maintains a log of all events in a non-volatile area. Upper layers would
6568 * usually find out the latest sequence number of the events, the seq number at
6569 * the boot etc. They would "read" all the events below the latest seq number
6570 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6571 * number), they would subsribe to AEN (asynchronous event notification) and
6572 * wait for the events to happen.
6575 megasas_get_seq_num(struct megasas_instance *instance,
6576 struct megasas_evt_log_info *eli)
6578 struct megasas_cmd *cmd;
6579 struct megasas_dcmd_frame *dcmd;
6580 struct megasas_evt_log_info *el_info;
6581 dma_addr_t el_info_h = 0;
6584 cmd = megasas_get_cmd(instance);
6590 dcmd = &cmd->frame->dcmd;
6591 el_info = dma_alloc_coherent(&instance->pdev->dev,
6592 sizeof(struct megasas_evt_log_info),
6593 &el_info_h, GFP_KERNEL);
6595 megasas_return_cmd(instance, cmd);
6599 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6601 dcmd->cmd = MFI_CMD_DCMD;
6602 dcmd->cmd_status = 0x0;
6603 dcmd->sge_count = 1;
6604 dcmd->flags = MFI_FRAME_DIR_READ;
6607 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6608 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6610 megasas_set_dma_settings(instance, dcmd, el_info_h,
6611 sizeof(struct megasas_evt_log_info));
6613 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6614 if (ret != DCMD_SUCCESS) {
6615 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6616 __func__, __LINE__);
6621 * Copy the data back into callers buffer
6623 eli->newest_seq_num = el_info->newest_seq_num;
6624 eli->oldest_seq_num = el_info->oldest_seq_num;
6625 eli->clear_seq_num = el_info->clear_seq_num;
6626 eli->shutdown_seq_num = el_info->shutdown_seq_num;
6627 eli->boot_seq_num = el_info->boot_seq_num;
6630 dma_free_coherent(&instance->pdev->dev,
6631 sizeof(struct megasas_evt_log_info),
6632 el_info, el_info_h);
6634 megasas_return_cmd(instance, cmd);
6640 * megasas_register_aen - Registers for asynchronous event notification
6641 * @instance: Adapter soft state
6642 * @seq_num: The starting sequence number
6643 * @class_locale_word: Class of the event
6645 * This function subscribes for AEN for events beyond the @seq_num. It requests
6646 * to be notified if and only if the event is of type @class_locale
6649 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6650 u32 class_locale_word)
6653 struct megasas_cmd *cmd;
6654 struct megasas_dcmd_frame *dcmd;
6655 union megasas_evt_class_locale curr_aen;
6656 union megasas_evt_class_locale prev_aen;
6659 * If there an AEN pending already (aen_cmd), check if the
6660 * class_locale of that pending AEN is inclusive of the new
6661 * AEN request we currently have. If it is, then we don't have
6662 * to do anything. In other words, whichever events the current
6663 * AEN request is subscribing to, have already been subscribed
6666 * If the old_cmd is _not_ inclusive, then we have to abort
6667 * that command, form a class_locale that is superset of both
6668 * old and current and re-issue to the FW
6671 curr_aen.word = class_locale_word;
6673 if (instance->aen_cmd) {
6676 le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6678 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6679 (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6680 dev_info(&instance->pdev->dev,
6681 "%s %d out of range class %d send by application\n",
6682 __func__, __LINE__, curr_aen.members.class);
6687 * A class whose enum value is smaller is inclusive of all
6688 * higher values. If a PROGRESS (= -1) was previously
6689 * registered, then a new registration requests for higher
6690 * classes need not be sent to FW. They are automatically
6693 * Locale numbers don't have such hierarchy. They are bitmap
6696 if ((prev_aen.members.class <= curr_aen.members.class) &&
6697 !((prev_aen.members.locale & curr_aen.members.locale) ^
6698 curr_aen.members.locale)) {
6700 * Previously issued event registration includes
6701 * current request. Nothing to do.
6705 curr_aen.members.locale |= prev_aen.members.locale;
6707 if (prev_aen.members.class < curr_aen.members.class)
6708 curr_aen.members.class = prev_aen.members.class;
6710 instance->aen_cmd->abort_aen = 1;
6711 ret_val = megasas_issue_blocked_abort_cmd(instance,
6716 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6717 "previous AEN command\n");
6723 cmd = megasas_get_cmd(instance);
6728 dcmd = &cmd->frame->dcmd;
6730 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6733 * Prepare DCMD for aen registration
6735 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6737 dcmd->cmd = MFI_CMD_DCMD;
6738 dcmd->cmd_status = 0x0;
6739 dcmd->sge_count = 1;
6740 dcmd->flags = MFI_FRAME_DIR_READ;
6743 dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6744 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6745 dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6746 instance->last_seq_num = seq_num;
6747 dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6749 megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6750 sizeof(struct megasas_evt_detail));
6752 if (instance->aen_cmd != NULL) {
6753 megasas_return_cmd(instance, cmd);
6758 * Store reference to the cmd used to register for AEN. When an
6759 * application wants us to register for AEN, we have to abort this
6760 * cmd and re-register with a new EVENT LOCALE supplied by that app
6762 instance->aen_cmd = cmd;
6765 * Issue the aen registration frame
6767 instance->instancet->issue_dcmd(instance, cmd);
6772 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6774 * This DCMD will fetch few properties of LD/system PD defined
6775 * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6777 * DCMD send by drivers whenever new target is added to the OS.
6779 * dcmd.opcode - MR_DCMD_DEV_GET_TARGET_PROP
6780 * dcmd.mbox.b[0] - DCMD is to be fired for LD or system PD.
6781 * 0 = system PD, 1 = LD.
6782 * dcmd.mbox.s[1] - TargetID for LD/system PD.
6783 * dcmd.sge IN - Pointer to return MR_TARGET_DEV_PROPERTIES.
6785 * @instance: Adapter soft state
6786 * @sdev: OS provided scsi device
6788 * Returns 0 on success non-zero on failure.
6791 megasas_get_target_prop(struct megasas_instance *instance,
6792 struct scsi_device *sdev)
6795 struct megasas_cmd *cmd;
6796 struct megasas_dcmd_frame *dcmd;
6797 u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6800 cmd = megasas_get_cmd(instance);
6803 dev_err(&instance->pdev->dev,
6804 "Failed to get cmd %s\n", __func__);
6808 dcmd = &cmd->frame->dcmd;
6810 memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6811 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6812 dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6814 dcmd->mbox.s[1] = cpu_to_le16(targetId);
6815 dcmd->cmd = MFI_CMD_DCMD;
6816 dcmd->cmd_status = 0xFF;
6817 dcmd->sge_count = 1;
6818 dcmd->flags = MFI_FRAME_DIR_READ;
6821 dcmd->data_xfer_len =
6822 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6823 dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6825 megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6826 sizeof(struct MR_TARGET_PROPERTIES));
6828 if ((instance->adapter_type != MFI_SERIES) &&
6829 !instance->mask_interrupts)
6830 ret = megasas_issue_blocked_cmd(instance,
6831 cmd, MFI_IO_TIMEOUT_SECS);
6833 ret = megasas_issue_polled(instance, cmd);
6837 switch (dcmd_timeout_ocr_possible(instance)) {
6839 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6840 mutex_unlock(&instance->reset_mutex);
6841 megasas_reset_fusion(instance->host,
6842 MFI_IO_TIMEOUT_OCR);
6843 mutex_lock(&instance->reset_mutex);
6846 megaraid_sas_kill_hba(instance);
6848 case IGNORE_TIMEOUT:
6849 dev_info(&instance->pdev->dev,
6850 "Ignore DCMD timeout: %s %d\n",
6851 __func__, __LINE__);
6857 megasas_return_cmd(instance, cmd);
6859 if (ret != DCMD_SUCCESS)
6860 dev_err(&instance->pdev->dev,
6861 "return from %s %d return value %d\n",
6862 __func__, __LINE__, ret);
6868 * megasas_start_aen - Subscribes to AEN during driver load time
6869 * @instance: Adapter soft state
6871 static int megasas_start_aen(struct megasas_instance *instance)
6873 struct megasas_evt_log_info eli;
6874 union megasas_evt_class_locale class_locale;
6877 * Get the latest sequence number from FW
6879 memset(&eli, 0, sizeof(eli));
6881 if (megasas_get_seq_num(instance, &eli))
6885 * Register AEN with FW for latest sequence number plus 1
6887 class_locale.members.reserved = 0;
6888 class_locale.members.locale = MR_EVT_LOCALE_ALL;
6889 class_locale.members.class = MR_EVT_CLASS_DEBUG;
6891 return megasas_register_aen(instance,
6892 le32_to_cpu(eli.newest_seq_num) + 1,
6897 * megasas_io_attach - Attaches this driver to SCSI mid-layer
6898 * @instance: Adapter soft state
6900 static int megasas_io_attach(struct megasas_instance *instance)
6902 struct Scsi_Host *host = instance->host;
6905 * Export parameters required by SCSI mid-layer
6907 host->unique_id = instance->unique_id;
6908 host->can_queue = instance->max_scsi_cmds;
6909 host->this_id = instance->init_id;
6910 host->sg_tablesize = instance->max_num_sge;
6912 if (instance->fw_support_ieee)
6913 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6916 * Check if the module parameter value for max_sectors can be used
6918 if (max_sectors && max_sectors < instance->max_sectors_per_req)
6919 instance->max_sectors_per_req = max_sectors;
6922 if (((instance->pdev->device ==
6923 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6924 (instance->pdev->device ==
6925 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6926 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6927 instance->max_sectors_per_req = max_sectors;
6929 dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6930 "and <= %d (or < 1MB for GEN2 controller)\n",
6931 instance->max_sectors_per_req);
6936 host->max_sectors = instance->max_sectors_per_req;
6937 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6938 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6939 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6940 host->max_lun = MEGASAS_MAX_LUN;
6941 host->max_cmd_len = 16;
6943 /* Use shared host tagset only for fusion adaptors
6944 * if there are managed interrupts (smp affinity enabled case).
6945 * Single msix_vectors in kdump, so shared host tag is also disabled.
6948 host->host_tagset = 0;
6949 host->nr_hw_queues = 1;
6951 if ((instance->adapter_type != MFI_SERIES) &&
6952 (instance->msix_vectors > instance->low_latency_index_start) &&
6953 host_tagset_enable &&
6954 instance->smp_affinity_enable) {
6955 host->host_tagset = 1;
6956 host->nr_hw_queues = instance->msix_vectors -
6957 instance->low_latency_index_start + instance->iopoll_q_count;
6958 if (instance->iopoll_q_count)
6961 instance->iopoll_q_count = 0;
6964 dev_info(&instance->pdev->dev,
6965 "Max firmware commands: %d shared with default "
6966 "hw_queues = %d poll_queues %d\n", instance->max_fw_cmds,
6967 host->nr_hw_queues - instance->iopoll_q_count,
6968 instance->iopoll_q_count);
6970 * Notify the mid-layer about the new controller
6972 if (scsi_add_host(host, &instance->pdev->dev)) {
6973 dev_err(&instance->pdev->dev,
6974 "Failed to add host from %s %d\n",
6975 __func__, __LINE__);
6983 * megasas_set_dma_mask - Set DMA mask for supported controllers
6985 * @instance: Adapter soft state
6988 * For Ventura, driver/FW will operate in 63bit DMA addresses.
6991 * By default, driver/FW will operate in 32bit DMA addresses
6992 * for consistent DMA mapping but if 32 bit consistent
6993 * DMA mask fails, driver will try with 63 bit consistent
6994 * mask provided FW is true 63bit DMA capable
6996 * For older controllers(Thunderbolt and MFI based adapters)-
6997 * driver/FW will operate in 32 bit consistent DMA addresses.
7000 megasas_set_dma_mask(struct megasas_instance *instance)
7002 u64 consistent_mask;
7003 struct pci_dev *pdev;
7006 pdev = instance->pdev;
7007 consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
7008 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
7011 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
7012 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
7013 goto fail_set_dma_mask;
7015 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
7016 (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
7017 dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
7019 * If 32 bit DMA mask fails, then try for 64 bit mask
7020 * for FW capable of handling 64 bit DMA.
7022 scratch_pad_1 = megasas_readl
7023 (instance, &instance->reg_set->outbound_scratch_pad_1);
7025 if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
7026 goto fail_set_dma_mask;
7027 else if (dma_set_mask_and_coherent(&pdev->dev,
7029 goto fail_set_dma_mask;
7031 } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
7032 goto fail_set_dma_mask;
7034 if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
7035 instance->consistent_mask_64bit = false;
7037 instance->consistent_mask_64bit = true;
7039 dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
7040 ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
7041 (instance->consistent_mask_64bit ? "63" : "32"));
7046 dev_err(&pdev->dev, "Failed to set DMA mask\n");
7052 * megasas_set_adapter_type - Set adapter type.
7053 * Supported controllers can be divided in
7054 * different categories-
7055 * enum MR_ADAPTER_TYPE {
7057 * THUNDERBOLT_SERIES = 2,
7058 * INVADER_SERIES = 3,
7059 * VENTURA_SERIES = 4,
7062 * @instance: Adapter soft state
7065 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
7067 if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
7068 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
7069 instance->adapter_type = MFI_SERIES;
7071 switch (instance->pdev->device) {
7072 case PCI_DEVICE_ID_LSI_AERO_10E1:
7073 case PCI_DEVICE_ID_LSI_AERO_10E2:
7074 case PCI_DEVICE_ID_LSI_AERO_10E5:
7075 case PCI_DEVICE_ID_LSI_AERO_10E6:
7076 instance->adapter_type = AERO_SERIES;
7078 case PCI_DEVICE_ID_LSI_VENTURA:
7079 case PCI_DEVICE_ID_LSI_CRUSADER:
7080 case PCI_DEVICE_ID_LSI_HARPOON:
7081 case PCI_DEVICE_ID_LSI_TOMCAT:
7082 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
7083 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
7084 instance->adapter_type = VENTURA_SERIES;
7086 case PCI_DEVICE_ID_LSI_FUSION:
7087 case PCI_DEVICE_ID_LSI_PLASMA:
7088 instance->adapter_type = THUNDERBOLT_SERIES;
7090 case PCI_DEVICE_ID_LSI_INVADER:
7091 case PCI_DEVICE_ID_LSI_INTRUDER:
7092 case PCI_DEVICE_ID_LSI_INTRUDER_24:
7093 case PCI_DEVICE_ID_LSI_CUTLASS_52:
7094 case PCI_DEVICE_ID_LSI_CUTLASS_53:
7095 case PCI_DEVICE_ID_LSI_FURY:
7096 instance->adapter_type = INVADER_SERIES;
7098 default: /* For all other supported controllers */
7099 instance->adapter_type = MFI_SERIES;
7105 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
7107 instance->producer = dma_alloc_coherent(&instance->pdev->dev,
7108 sizeof(u32), &instance->producer_h, GFP_KERNEL);
7109 instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
7110 sizeof(u32), &instance->consumer_h, GFP_KERNEL);
7112 if (!instance->producer || !instance->consumer) {
7113 dev_err(&instance->pdev->dev,
7114 "Failed to allocate memory for producer, consumer\n");
7118 *instance->producer = 0;
7119 *instance->consumer = 0;
7124 * megasas_alloc_ctrl_mem - Allocate per controller memory for core data
7125 * structures which are not common across MFI
7126 * adapters and fusion adapters.
7127 * For MFI based adapters, allocate producer and
7128 * consumer buffers. For fusion adapters, allocate
7129 * memory for fusion context.
7130 * @instance: Adapter soft state
7131 * return: 0 for SUCCESS
7133 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
7135 instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
7137 if (!instance->reply_map)
7140 switch (instance->adapter_type) {
7142 if (megasas_alloc_mfi_ctrl_mem(instance))
7146 case VENTURA_SERIES:
7147 case THUNDERBOLT_SERIES:
7148 case INVADER_SERIES:
7149 if (megasas_alloc_fusion_context(instance))
7156 kfree(instance->reply_map);
7157 instance->reply_map = NULL;
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 PD list buffer\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;
7437 megasas_dbg_lvl = 0;
7439 instance->unload = 1;
7440 instance->last_time = 0;
7441 instance->disableOnlineCtrlReset = 1;
7442 instance->UnevenSpanSupport = 0;
7443 instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7444 instance->msix_load_balance = false;
7446 if (instance->adapter_type != MFI_SERIES)
7447 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7449 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7453 * megasas_probe_one - PCI hotplug entry point
7454 * @pdev: PCI device structure
7455 * @id: PCI ids of supported hotplugged adapter
7457 static int megasas_probe_one(struct pci_dev *pdev,
7458 const struct pci_device_id *id)
7461 struct Scsi_Host *host;
7462 struct megasas_instance *instance;
7465 switch (pdev->device) {
7466 case PCI_DEVICE_ID_LSI_AERO_10E0:
7467 case PCI_DEVICE_ID_LSI_AERO_10E3:
7468 case PCI_DEVICE_ID_LSI_AERO_10E4:
7469 case PCI_DEVICE_ID_LSI_AERO_10E7:
7470 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7472 case PCI_DEVICE_ID_LSI_AERO_10E1:
7473 case PCI_DEVICE_ID_LSI_AERO_10E5:
7474 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7478 /* Reset MSI-X in the kdump kernel */
7479 if (reset_devices) {
7480 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7482 pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7484 if (control & PCI_MSIX_FLAGS_ENABLE) {
7485 dev_info(&pdev->dev, "resetting MSI-X\n");
7486 pci_write_config_word(pdev,
7487 pos + PCI_MSIX_FLAGS,
7489 ~PCI_MSIX_FLAGS_ENABLE);
7495 * PCI prepping: enable device set bus mastering and dma mask
7497 rval = pci_enable_device_mem(pdev);
7503 pci_set_master(pdev);
7505 host = scsi_host_alloc(&megasas_template,
7506 sizeof(struct megasas_instance));
7509 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7510 goto fail_alloc_instance;
7513 instance = (struct megasas_instance *)host->hostdata;
7514 memset(instance, 0, sizeof(*instance));
7515 atomic_set(&instance->fw_reset_no_pci_access, 0);
7518 * Initialize PCI related and misc parameters
7520 instance->pdev = pdev;
7521 instance->host = host;
7522 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7523 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7525 megasas_set_adapter_type(instance);
7528 * Initialize MFI Firmware
7530 if (megasas_init_fw(instance))
7533 if (instance->requestorId) {
7534 if (instance->PlasmaFW111) {
7535 instance->vf_affiliation_111 =
7536 dma_alloc_coherent(&pdev->dev,
7537 sizeof(struct MR_LD_VF_AFFILIATION_111),
7538 &instance->vf_affiliation_111_h,
7540 if (!instance->vf_affiliation_111)
7541 dev_warn(&pdev->dev, "Can't allocate "
7542 "memory for VF affiliation buffer\n");
7544 instance->vf_affiliation =
7545 dma_alloc_coherent(&pdev->dev,
7546 (MAX_LOGICAL_DRIVES + 1) *
7547 sizeof(struct MR_LD_VF_AFFILIATION),
7548 &instance->vf_affiliation_h,
7550 if (!instance->vf_affiliation)
7551 dev_warn(&pdev->dev, "Can't allocate "
7552 "memory for VF affiliation buffer\n");
7557 * Store instance in PCI softstate
7559 pci_set_drvdata(pdev, instance);
7562 * Add this controller to megasas_mgmt_info structure so that it
7563 * can be exported to management applications
7565 megasas_mgmt_info.count++;
7566 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7567 megasas_mgmt_info.max_index++;
7570 * Register with SCSI mid-layer
7572 if (megasas_io_attach(instance))
7573 goto fail_io_attach;
7575 instance->unload = 0;
7577 * Trigger SCSI to scan our drives
7579 if (!instance->enable_fw_dev_list ||
7580 (instance->host_device_list_buf->count > 0))
7581 scsi_scan_host(host);
7584 * Initiate AEN (Asynchronous Event Notification)
7586 if (megasas_start_aen(instance)) {
7587 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7588 goto fail_start_aen;
7591 megasas_setup_debugfs(instance);
7593 /* Get current SR-IOV LD/VF affiliation */
7594 if (instance->requestorId)
7595 megasas_get_ld_vf_affiliation(instance, 1);
7600 instance->unload = 1;
7601 scsi_remove_host(instance->host);
7603 megasas_mgmt_info.count--;
7604 megasas_mgmt_info.max_index--;
7605 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7607 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7608 del_timer_sync(&instance->sriov_heartbeat_timer);
7610 instance->instancet->disable_intr(instance);
7611 megasas_destroy_irqs(instance);
7613 if (instance->adapter_type != MFI_SERIES)
7614 megasas_release_fusion(instance);
7616 megasas_release_mfi(instance);
7618 if (instance->msix_vectors)
7619 pci_free_irq_vectors(instance->pdev);
7620 instance->msix_vectors = 0;
7622 if (instance->fw_crash_state != UNAVAILABLE)
7623 megasas_free_host_crash_buffer(instance);
7625 if (instance->adapter_type != MFI_SERIES)
7626 megasas_fusion_stop_watchdog(instance);
7628 scsi_host_put(host);
7629 fail_alloc_instance:
7630 pci_disable_device(pdev);
7636 * megasas_flush_cache - Requests FW to flush all its caches
7637 * @instance: Adapter soft state
7639 static void megasas_flush_cache(struct megasas_instance *instance)
7641 struct megasas_cmd *cmd;
7642 struct megasas_dcmd_frame *dcmd;
7644 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7647 cmd = megasas_get_cmd(instance);
7652 dcmd = &cmd->frame->dcmd;
7654 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7656 dcmd->cmd = MFI_CMD_DCMD;
7657 dcmd->cmd_status = 0x0;
7658 dcmd->sge_count = 0;
7659 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7662 dcmd->data_xfer_len = 0;
7663 dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7664 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7666 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7668 dev_err(&instance->pdev->dev,
7669 "return from %s %d\n", __func__, __LINE__);
7673 megasas_return_cmd(instance, cmd);
7677 * megasas_shutdown_controller - Instructs FW to shutdown the controller
7678 * @instance: Adapter soft state
7679 * @opcode: Shutdown/Hibernate
7681 static void megasas_shutdown_controller(struct megasas_instance *instance,
7684 struct megasas_cmd *cmd;
7685 struct megasas_dcmd_frame *dcmd;
7687 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7690 cmd = megasas_get_cmd(instance);
7695 if (instance->aen_cmd)
7696 megasas_issue_blocked_abort_cmd(instance,
7697 instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7698 if (instance->map_update_cmd)
7699 megasas_issue_blocked_abort_cmd(instance,
7700 instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7701 if (instance->jbod_seq_cmd)
7702 megasas_issue_blocked_abort_cmd(instance,
7703 instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7705 dcmd = &cmd->frame->dcmd;
7707 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7709 dcmd->cmd = MFI_CMD_DCMD;
7710 dcmd->cmd_status = 0x0;
7711 dcmd->sge_count = 0;
7712 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7715 dcmd->data_xfer_len = 0;
7716 dcmd->opcode = cpu_to_le32(opcode);
7718 if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7720 dev_err(&instance->pdev->dev,
7721 "return from %s %d\n", __func__, __LINE__);
7725 megasas_return_cmd(instance, cmd);
7729 * megasas_suspend - driver suspend entry point
7730 * @dev: Device structure
7732 static int __maybe_unused
7733 megasas_suspend(struct device *dev)
7735 struct megasas_instance *instance;
7737 instance = dev_get_drvdata(dev);
7742 instance->unload = 1;
7744 dev_info(dev, "%s is called\n", __func__);
7746 /* Shutdown SR-IOV heartbeat timer */
7747 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7748 del_timer_sync(&instance->sriov_heartbeat_timer);
7750 /* Stop the FW fault detection watchdog */
7751 if (instance->adapter_type != MFI_SERIES)
7752 megasas_fusion_stop_watchdog(instance);
7754 megasas_flush_cache(instance);
7755 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7757 /* cancel the delayed work if this work still in queue */
7758 if (instance->ev != NULL) {
7759 struct megasas_aen_event *ev = instance->ev;
7760 cancel_delayed_work_sync(&ev->hotplug_work);
7761 instance->ev = NULL;
7764 tasklet_kill(&instance->isr_tasklet);
7766 pci_set_drvdata(instance->pdev, instance);
7767 instance->instancet->disable_intr(instance);
7769 megasas_destroy_irqs(instance);
7771 if (instance->msix_vectors)
7772 pci_free_irq_vectors(instance->pdev);
7778 * megasas_resume- driver resume entry point
7779 * @dev: Device structure
7781 static int __maybe_unused
7782 megasas_resume(struct device *dev)
7785 struct Scsi_Host *host;
7786 struct megasas_instance *instance;
7789 instance = dev_get_drvdata(dev);
7794 host = instance->host;
7796 dev_info(dev, "%s is called\n", __func__);
7799 * We expect the FW state to be READY
7802 if (megasas_transition_to_ready(instance, 0)) {
7803 dev_info(&instance->pdev->dev,
7804 "Failed to transition controller to ready from %s!\n",
7806 if (instance->adapter_type != MFI_SERIES) {
7808 instance->instancet->read_fw_status_reg(instance);
7809 if (!(status_reg & MFI_RESET_ADAPTER) ||
7810 ((megasas_adp_reset_wait_for_ready
7811 (instance, true, 0)) == FAILED))
7812 goto fail_ready_state;
7814 atomic_set(&instance->fw_reset_no_pci_access, 1);
7815 instance->instancet->adp_reset
7816 (instance, instance->reg_set);
7817 atomic_set(&instance->fw_reset_no_pci_access, 0);
7819 /* waiting for about 30 seconds before retry */
7822 if (megasas_transition_to_ready(instance, 0))
7823 goto fail_ready_state;
7826 dev_info(&instance->pdev->dev,
7827 "FW restarted successfully from %s!\n",
7830 if (megasas_set_dma_mask(instance))
7831 goto fail_set_dma_mask;
7834 * Initialize MFI Firmware
7837 atomic_set(&instance->fw_outstanding, 0);
7838 atomic_set(&instance->ldio_outstanding, 0);
7840 /* Now re-enable MSI-X */
7841 if (instance->msix_vectors)
7842 megasas_alloc_irq_vectors(instance);
7844 if (!instance->msix_vectors) {
7845 rval = pci_alloc_irq_vectors(instance->pdev, 1, 1,
7848 goto fail_reenable_msix;
7851 megasas_setup_reply_map(instance);
7853 if (instance->adapter_type != MFI_SERIES) {
7854 megasas_reset_reply_desc(instance);
7855 if (megasas_ioc_init_fusion(instance)) {
7856 megasas_free_cmds(instance);
7857 megasas_free_cmds_fusion(instance);
7860 if (!megasas_get_map_info(instance))
7861 megasas_sync_map_info(instance);
7863 *instance->producer = 0;
7864 *instance->consumer = 0;
7865 if (megasas_issue_init_mfi(instance))
7869 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7872 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7873 (unsigned long)instance);
7875 if (instance->msix_vectors ?
7876 megasas_setup_irqs_msix(instance, 0) :
7877 megasas_setup_irqs_ioapic(instance))
7880 if (instance->adapter_type != MFI_SERIES)
7881 megasas_setup_irq_poll(instance);
7883 /* Re-launch SR-IOV heartbeat timer */
7884 if (instance->requestorId) {
7885 if (!megasas_sriov_start_heartbeat(instance, 0))
7886 megasas_start_timer(instance);
7888 instance->skip_heartbeat_timer_del = 1;
7893 instance->instancet->enable_intr(instance);
7894 megasas_setup_jbod_map(instance);
7895 instance->unload = 0;
7898 * Initiate AEN (Asynchronous Event Notification)
7900 if (megasas_start_aen(instance))
7901 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7903 /* Re-launch FW fault watchdog */
7904 if (instance->adapter_type != MFI_SERIES)
7905 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7906 goto fail_start_watchdog;
7910 fail_start_watchdog:
7911 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7912 del_timer_sync(&instance->sriov_heartbeat_timer);
7914 megasas_free_ctrl_dma_buffers(instance);
7915 megasas_free_ctrl_mem(instance);
7916 scsi_host_put(host);
7926 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7928 int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7932 for (i = 0; i < wait_time; i++) {
7933 adp_state = atomic_read(&instance->adprecovery);
7934 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7935 (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7938 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7939 dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7944 if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7945 dev_info(&instance->pdev->dev,
7946 "%s HBA failed to become operational, adp_state %d\n",
7947 __func__, adp_state);
7955 * megasas_detach_one - PCI hot"un"plug entry point
7956 * @pdev: PCI device structure
7958 static void megasas_detach_one(struct pci_dev *pdev)
7961 struct Scsi_Host *host;
7962 struct megasas_instance *instance;
7963 struct fusion_context *fusion;
7966 instance = pci_get_drvdata(pdev);
7971 host = instance->host;
7972 fusion = instance->ctrl_context;
7974 /* Shutdown SR-IOV heartbeat timer */
7975 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7976 del_timer_sync(&instance->sriov_heartbeat_timer);
7978 /* Stop the FW fault detection watchdog */
7979 if (instance->adapter_type != MFI_SERIES)
7980 megasas_fusion_stop_watchdog(instance);
7982 if (instance->fw_crash_state != UNAVAILABLE)
7983 megasas_free_host_crash_buffer(instance);
7984 scsi_remove_host(instance->host);
7985 instance->unload = 1;
7987 if (megasas_wait_for_adapter_operational(instance))
7988 goto skip_firing_dcmds;
7990 megasas_flush_cache(instance);
7991 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7994 /* cancel the delayed work if this work still in queue*/
7995 if (instance->ev != NULL) {
7996 struct megasas_aen_event *ev = instance->ev;
7997 cancel_delayed_work_sync(&ev->hotplug_work);
7998 instance->ev = NULL;
8001 /* cancel all wait events */
8002 wake_up_all(&instance->int_cmd_wait_q);
8004 tasklet_kill(&instance->isr_tasklet);
8007 * Take the instance off the instance array. Note that we will not
8008 * decrement the max_index. We let this array be sparse array
8010 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8011 if (megasas_mgmt_info.instance[i] == instance) {
8012 megasas_mgmt_info.count--;
8013 megasas_mgmt_info.instance[i] = NULL;
8019 instance->instancet->disable_intr(instance);
8021 megasas_destroy_irqs(instance);
8023 if (instance->msix_vectors)
8024 pci_free_irq_vectors(instance->pdev);
8026 if (instance->adapter_type >= VENTURA_SERIES) {
8027 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
8028 kfree(fusion->stream_detect_by_ld[i]);
8029 kfree(fusion->stream_detect_by_ld);
8030 fusion->stream_detect_by_ld = NULL;
8034 if (instance->adapter_type != MFI_SERIES) {
8035 megasas_release_fusion(instance);
8036 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
8037 (sizeof(struct MR_PD_CFG_SEQ) *
8038 (MAX_PHYSICAL_DEVICES - 1));
8039 for (i = 0; i < 2 ; i++) {
8040 if (fusion->ld_map[i])
8041 dma_free_coherent(&instance->pdev->dev,
8044 fusion->ld_map_phys[i]);
8045 if (fusion->ld_drv_map[i]) {
8046 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
8047 vfree(fusion->ld_drv_map[i]);
8049 free_pages((ulong)fusion->ld_drv_map[i],
8050 fusion->drv_map_pages);
8053 if (fusion->pd_seq_sync[i])
8054 dma_free_coherent(&instance->pdev->dev,
8056 fusion->pd_seq_sync[i],
8057 fusion->pd_seq_phys[i]);
8060 megasas_release_mfi(instance);
8063 if (instance->vf_affiliation)
8064 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
8065 sizeof(struct MR_LD_VF_AFFILIATION),
8066 instance->vf_affiliation,
8067 instance->vf_affiliation_h);
8069 if (instance->vf_affiliation_111)
8070 dma_free_coherent(&pdev->dev,
8071 sizeof(struct MR_LD_VF_AFFILIATION_111),
8072 instance->vf_affiliation_111,
8073 instance->vf_affiliation_111_h);
8075 if (instance->hb_host_mem)
8076 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
8077 instance->hb_host_mem,
8078 instance->hb_host_mem_h);
8080 megasas_free_ctrl_dma_buffers(instance);
8082 megasas_free_ctrl_mem(instance);
8084 megasas_destroy_debugfs(instance);
8086 scsi_host_put(host);
8088 pci_disable_device(pdev);
8092 * megasas_shutdown - Shutdown entry point
8093 * @pdev: PCI device structure
8095 static void megasas_shutdown(struct pci_dev *pdev)
8097 struct megasas_instance *instance = pci_get_drvdata(pdev);
8102 instance->unload = 1;
8104 if (megasas_wait_for_adapter_operational(instance))
8105 goto skip_firing_dcmds;
8107 megasas_flush_cache(instance);
8108 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
8111 instance->instancet->disable_intr(instance);
8112 megasas_destroy_irqs(instance);
8114 if (instance->msix_vectors)
8115 pci_free_irq_vectors(instance->pdev);
8119 * megasas_mgmt_open - char node "open" entry point
8120 * @inode: char node inode
8121 * @filep: char node file
8123 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
8126 * Allow only those users with admin rights
8128 if (!capable(CAP_SYS_ADMIN))
8135 * megasas_mgmt_fasync - Async notifier registration from applications
8136 * @fd: char node file descriptor number
8137 * @filep: char node file
8138 * @mode: notifier on/off
8140 * This function adds the calling process to a driver global queue. When an
8141 * event occurs, SIGIO will be sent to all processes in this queue.
8143 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
8147 mutex_lock(&megasas_async_queue_mutex);
8149 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
8151 mutex_unlock(&megasas_async_queue_mutex);
8154 /* For sanity check when we get ioctl */
8155 filep->private_data = filep;
8159 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
8165 * megasas_mgmt_poll - char node "poll" entry point
8166 * @filep: char node file
8167 * @wait: Events to poll for
8169 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
8172 unsigned long flags;
8174 poll_wait(file, &megasas_poll_wait, wait);
8175 spin_lock_irqsave(&poll_aen_lock, flags);
8176 if (megasas_poll_wait_aen)
8177 mask = (EPOLLIN | EPOLLRDNORM);
8180 megasas_poll_wait_aen = 0;
8181 spin_unlock_irqrestore(&poll_aen_lock, flags);
8186 * megasas_set_crash_dump_params_ioctl:
8187 * Send CRASH_DUMP_MODE DCMD to all controllers
8188 * @cmd: MFI command frame
8191 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
8193 struct megasas_instance *local_instance;
8197 crash_support = cmd->frame->dcmd.mbox.w[0];
8199 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
8200 local_instance = megasas_mgmt_info.instance[i];
8201 if (local_instance && local_instance->crash_dump_drv_support) {
8202 if ((atomic_read(&local_instance->adprecovery) ==
8203 MEGASAS_HBA_OPERATIONAL) &&
8204 !megasas_set_crash_dump_params(local_instance,
8206 local_instance->crash_dump_app_support =
8208 dev_info(&local_instance->pdev->dev,
8209 "Application firmware crash "
8210 "dump mode set success\n");
8213 dev_info(&local_instance->pdev->dev,
8214 "Application firmware crash "
8215 "dump mode set failed\n");
8224 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
8225 * @instance: Adapter soft state
8226 * @user_ioc: User's ioctl packet
8227 * @ioc: ioctl packet
8230 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
8231 struct megasas_iocpacket __user * user_ioc,
8232 struct megasas_iocpacket *ioc)
8234 struct megasas_sge64 *kern_sge64 = NULL;
8235 struct megasas_sge32 *kern_sge32 = NULL;
8236 struct megasas_cmd *cmd;
8237 void *kbuff_arr[MAX_IOCTL_SGE];
8238 dma_addr_t buf_handle = 0;
8241 dma_addr_t sense_handle;
8244 int ret = DCMD_SUCCESS;
8246 memset(kbuff_arr, 0, sizeof(kbuff_arr));
8248 if (ioc->sge_count > MAX_IOCTL_SGE) {
8249 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] > max limit [%d]\n",
8250 ioc->sge_count, MAX_IOCTL_SGE);
8254 if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
8255 ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
8256 !instance->support_nvme_passthru) ||
8257 ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
8258 !instance->support_pci_lane_margining)) {
8259 dev_err(&instance->pdev->dev,
8260 "Received invalid ioctl command 0x%x\n",
8261 ioc->frame.hdr.cmd);
8265 cmd = megasas_get_cmd(instance);
8267 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
8272 * User's IOCTL packet has 2 frames (maximum). Copy those two
8273 * frames into our cmd's frames. cmd->frame's context will get
8274 * overwritten when we copy from user's frames. So set that value
8277 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
8278 cmd->frame->hdr.context = cpu_to_le32(cmd->index);
8279 cmd->frame->hdr.pad_0 = 0;
8281 cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8283 if (instance->consistent_mask_64bit)
8284 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8285 MFI_FRAME_SENSE64));
8287 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
8288 MFI_FRAME_SENSE64));
8290 if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8291 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
8293 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
8294 mutex_lock(&instance->reset_mutex);
8295 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8296 megasas_return_cmd(instance, cmd);
8297 mutex_unlock(&instance->reset_mutex);
8300 mutex_unlock(&instance->reset_mutex);
8303 if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
8304 error = megasas_set_crash_dump_params_ioctl(cmd);
8305 megasas_return_cmd(instance, cmd);
8310 * The management interface between applications and the fw uses
8311 * MFI frames. E.g, RAID configuration changes, LD property changes
8312 * etc are accomplishes through different kinds of MFI frames. The
8313 * driver needs to care only about substituting user buffers with
8314 * kernel buffers in SGLs. The location of SGL is embedded in the
8315 * struct iocpacket itself.
8317 if (instance->consistent_mask_64bit)
8318 kern_sge64 = (struct megasas_sge64 *)
8319 ((unsigned long)cmd->frame + ioc->sgl_off);
8321 kern_sge32 = (struct megasas_sge32 *)
8322 ((unsigned long)cmd->frame + ioc->sgl_off);
8325 * For each user buffer, create a mirror buffer and copy in
8327 for (i = 0; i < ioc->sge_count; i++) {
8328 if (!ioc->sgl[i].iov_len)
8331 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
8332 ioc->sgl[i].iov_len,
8333 &buf_handle, GFP_KERNEL);
8334 if (!kbuff_arr[i]) {
8335 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8336 "kernel SGL buffer for IOCTL\n");
8342 * We don't change the dma_coherent_mask, so
8343 * dma_alloc_coherent only returns 32bit addresses
8345 if (instance->consistent_mask_64bit) {
8346 kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8347 kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8349 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8350 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8354 * We created a kernel buffer corresponding to the
8355 * user buffer. Now copy in from the user buffer
8357 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8358 (u32) (ioc->sgl[i].iov_len))) {
8364 if (ioc->sense_len) {
8365 /* make sure the pointer is part of the frame */
8366 if (ioc->sense_off >
8367 (sizeof(union megasas_frame) - sizeof(__le64))) {
8372 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8373 &sense_handle, GFP_KERNEL);
8379 /* always store 64 bits regardless of addressing */
8380 sense_ptr = (void *)cmd->frame + ioc->sense_off;
8381 put_unaligned_le64(sense_handle, sense_ptr);
8385 * Set the sync_cmd flag so that the ISR knows not to complete this
8386 * cmd to the SCSI mid-layer
8390 ret = megasas_issue_blocked_cmd(instance, cmd, 0);
8395 dev_err(&instance->pdev->dev,
8396 "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8397 __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8398 cmd->cmd_status_drv);
8405 if (instance->unload == 1) {
8406 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8407 "don't submit data to application\n");
8411 * copy out the kernel buffers to user buffers
8413 for (i = 0; i < ioc->sge_count; i++) {
8414 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8415 ioc->sgl[i].iov_len)) {
8422 * copy out the sense
8424 if (ioc->sense_len) {
8427 * sense_ptr points to the location that has the user
8428 * sense buffer address
8430 sense_ptr = (void *)ioc->frame.raw + ioc->sense_off;
8431 if (in_compat_syscall())
8432 uptr = compat_ptr(get_unaligned((compat_uptr_t *)
8435 uptr = get_unaligned((void __user **)sense_ptr);
8437 if (copy_to_user(uptr, sense, ioc->sense_len)) {
8438 dev_err(&instance->pdev->dev, "Failed to copy out to user "
8446 * copy the status codes returned by the fw
8448 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8449 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
8450 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
8456 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
8457 sense, sense_handle);
8460 for (i = 0; i < ioc->sge_count; i++) {
8462 if (instance->consistent_mask_64bit)
8463 dma_free_coherent(&instance->pdev->dev,
8464 le32_to_cpu(kern_sge64[i].length),
8466 le64_to_cpu(kern_sge64[i].phys_addr));
8468 dma_free_coherent(&instance->pdev->dev,
8469 le32_to_cpu(kern_sge32[i].length),
8471 le32_to_cpu(kern_sge32[i].phys_addr));
8472 kbuff_arr[i] = NULL;
8476 megasas_return_cmd(instance, cmd);
8480 static struct megasas_iocpacket *
8481 megasas_compat_iocpacket_get_user(void __user *arg)
8483 struct megasas_iocpacket *ioc;
8484 struct compat_megasas_iocpacket __user *cioc = arg;
8489 ioc = kzalloc(sizeof(*ioc), GFP_KERNEL);
8491 return ERR_PTR(-ENOMEM);
8492 size = offsetof(struct megasas_iocpacket, frame) + sizeof(ioc->frame);
8493 if (copy_from_user(ioc, arg, size))
8496 for (i = 0; i < MAX_IOCTL_SGE; i++) {
8497 compat_uptr_t iov_base;
8499 if (get_user(iov_base, &cioc->sgl[i].iov_base) ||
8500 get_user(ioc->sgl[i].iov_len, &cioc->sgl[i].iov_len))
8503 ioc->sgl[i].iov_base = compat_ptr(iov_base);
8509 return ERR_PTR(err);
8512 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8514 struct megasas_iocpacket __user *user_ioc =
8515 (struct megasas_iocpacket __user *)arg;
8516 struct megasas_iocpacket *ioc;
8517 struct megasas_instance *instance;
8520 if (in_compat_syscall())
8521 ioc = megasas_compat_iocpacket_get_user(user_ioc);
8523 ioc = memdup_user(user_ioc, sizeof(struct megasas_iocpacket));
8526 return PTR_ERR(ioc);
8528 instance = megasas_lookup_instance(ioc->host_no);
8534 /* Block ioctls in VF mode */
8535 if (instance->requestorId && !allow_vf_ioctls) {
8540 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8541 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8546 if (instance->unload == 1) {
8551 if (down_interruptible(&instance->ioctl_sem)) {
8552 error = -ERESTARTSYS;
8556 if (megasas_wait_for_adapter_operational(instance)) {
8561 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8563 up(&instance->ioctl_sem);
8570 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8572 struct megasas_instance *instance;
8573 struct megasas_aen aen;
8576 if (file->private_data != file) {
8577 printk(KERN_DEBUG "megasas: fasync_helper was not "
8582 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8585 instance = megasas_lookup_instance(aen.host_no);
8590 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8594 if (instance->unload == 1) {
8598 if (megasas_wait_for_adapter_operational(instance))
8601 mutex_lock(&instance->reset_mutex);
8602 error = megasas_register_aen(instance, aen.seq_num,
8603 aen.class_locale_word);
8604 mutex_unlock(&instance->reset_mutex);
8609 * megasas_mgmt_ioctl - char node ioctl entry point
8610 * @file: char device file pointer
8611 * @cmd: ioctl command
8612 * @arg: ioctl command arguments address
8615 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8618 case MEGASAS_IOC_FIRMWARE:
8619 return megasas_mgmt_ioctl_fw(file, arg);
8621 case MEGASAS_IOC_GET_AEN:
8622 return megasas_mgmt_ioctl_aen(file, arg);
8628 #ifdef CONFIG_COMPAT
8630 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8634 case MEGASAS_IOC_FIRMWARE32:
8635 return megasas_mgmt_ioctl_fw(file, arg);
8636 case MEGASAS_IOC_GET_AEN:
8637 return megasas_mgmt_ioctl_aen(file, arg);
8645 * File operations structure for management interface
8647 static const struct file_operations megasas_mgmt_fops = {
8648 .owner = THIS_MODULE,
8649 .open = megasas_mgmt_open,
8650 .fasync = megasas_mgmt_fasync,
8651 .unlocked_ioctl = megasas_mgmt_ioctl,
8652 .poll = megasas_mgmt_poll,
8653 #ifdef CONFIG_COMPAT
8654 .compat_ioctl = megasas_mgmt_compat_ioctl,
8656 .llseek = noop_llseek,
8659 static SIMPLE_DEV_PM_OPS(megasas_pm_ops, megasas_suspend, megasas_resume);
8662 * PCI hotplug support registration structure
8664 static struct pci_driver megasas_pci_driver = {
8666 .name = "megaraid_sas",
8667 .id_table = megasas_pci_table,
8668 .probe = megasas_probe_one,
8669 .remove = megasas_detach_one,
8670 .driver.pm = &megasas_pm_ops,
8671 .shutdown = megasas_shutdown,
8675 * Sysfs driver attributes
8677 static ssize_t version_show(struct device_driver *dd, char *buf)
8679 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8682 static DRIVER_ATTR_RO(version);
8684 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8686 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8689 static DRIVER_ATTR_RO(release_date);
8691 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8693 return sprintf(buf, "%u\n", support_poll_for_event);
8695 static DRIVER_ATTR_RO(support_poll_for_event);
8697 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8699 return sprintf(buf, "%u\n", support_device_change);
8701 static DRIVER_ATTR_RO(support_device_change);
8703 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8705 return sprintf(buf, "%u\n", megasas_dbg_lvl);
8708 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8713 if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8714 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8719 static DRIVER_ATTR_RW(dbg_lvl);
8722 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8724 return sprintf(buf, "%u\n", support_nvme_encapsulation);
8727 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8730 support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8732 return sprintf(buf, "%u\n", support_pci_lane_margining);
8735 static DRIVER_ATTR_RO(support_pci_lane_margining);
8737 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8739 sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8740 scsi_remove_device(sdev);
8741 scsi_device_put(sdev);
8745 * megasas_update_device_list - Update the PD and LD device list from FW
8746 * after an AEN event notification
8747 * @instance: Adapter soft state
8748 * @event_type: Indicates type of event (PD or LD event)
8750 * @return: Success or failure
8752 * Issue DCMDs to Firmware to update the internal device list in driver.
8753 * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8754 * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8757 int megasas_update_device_list(struct megasas_instance *instance,
8760 int dcmd_ret = DCMD_SUCCESS;
8762 if (instance->enable_fw_dev_list) {
8763 dcmd_ret = megasas_host_device_list_query(instance, false);
8764 if (dcmd_ret != DCMD_SUCCESS)
8767 if (event_type & SCAN_PD_CHANNEL) {
8768 dcmd_ret = megasas_get_pd_list(instance);
8770 if (dcmd_ret != DCMD_SUCCESS)
8774 if (event_type & SCAN_VD_CHANNEL) {
8775 if (!instance->requestorId ||
8776 megasas_get_ld_vf_affiliation(instance, 0)) {
8777 dcmd_ret = megasas_ld_list_query(instance,
8778 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8779 if (dcmd_ret != DCMD_SUCCESS)
8790 * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8791 * after an AEN event notification
8792 * @instance: Adapter soft state
8793 * @scan_type: Indicates type of devices (PD/LD) to add
8797 void megasas_add_remove_devices(struct megasas_instance *instance,
8803 u16 channel = 0, id = 0;
8804 struct Scsi_Host *host;
8805 struct scsi_device *sdev1;
8806 struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8807 struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8809 host = instance->host;
8811 if (instance->enable_fw_dev_list) {
8812 targetid_list = instance->host_device_list_buf;
8813 for (i = 0; i < targetid_list->count; i++) {
8814 targetid_entry = &targetid_list->host_device_list[i];
8815 if (targetid_entry->flags.u.bits.is_sys_pd) {
8816 channel = le16_to_cpu(targetid_entry->target_id) /
8817 MEGASAS_MAX_DEV_PER_CHANNEL;
8818 id = le16_to_cpu(targetid_entry->target_id) %
8819 MEGASAS_MAX_DEV_PER_CHANNEL;
8821 channel = MEGASAS_MAX_PD_CHANNELS +
8822 (le16_to_cpu(targetid_entry->target_id) /
8823 MEGASAS_MAX_DEV_PER_CHANNEL);
8824 id = le16_to_cpu(targetid_entry->target_id) %
8825 MEGASAS_MAX_DEV_PER_CHANNEL;
8827 sdev1 = scsi_device_lookup(host, channel, id, 0);
8829 scsi_add_device(host, channel, id, 0);
8831 scsi_device_put(sdev1);
8836 if (scan_type & SCAN_PD_CHANNEL) {
8837 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8838 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8839 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8840 sdev1 = scsi_device_lookup(host, i, j, 0);
8841 if (instance->pd_list[pd_index].driveState ==
8842 MR_PD_STATE_SYSTEM) {
8844 scsi_add_device(host, i, j, 0);
8846 scsi_device_put(sdev1);
8849 megasas_remove_scsi_device(sdev1);
8855 if (scan_type & SCAN_VD_CHANNEL) {
8856 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8857 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8858 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8859 sdev1 = scsi_device_lookup(host,
8860 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8861 if (instance->ld_ids[ld_index] != 0xff) {
8863 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8865 scsi_device_put(sdev1);
8868 megasas_remove_scsi_device(sdev1);
8877 megasas_aen_polling(struct work_struct *work)
8879 struct megasas_aen_event *ev =
8880 container_of(work, struct megasas_aen_event, hotplug_work.work);
8881 struct megasas_instance *instance = ev->instance;
8882 union megasas_evt_class_locale class_locale;
8887 u8 dcmd_ret = DCMD_SUCCESS;
8888 struct scsi_device *sdev1;
8891 printk(KERN_ERR "invalid instance!\n");
8896 /* Don't run the event workqueue thread if OCR is running */
8897 mutex_lock(&instance->reset_mutex);
8899 instance->ev = NULL;
8900 if (instance->evt_detail) {
8901 megasas_decode_evt(instance);
8903 switch (le32_to_cpu(instance->evt_detail->code)) {
8905 case MR_EVT_PD_INSERTED:
8906 case MR_EVT_PD_REMOVED:
8907 event_type = SCAN_PD_CHANNEL;
8910 case MR_EVT_LD_OFFLINE:
8911 case MR_EVT_LD_DELETED:
8912 ld_target_id = instance->evt_detail->args.ld.target_id;
8913 sdev1 = scsi_device_lookup(instance->host,
8914 MEGASAS_MAX_PD_CHANNELS +
8915 (ld_target_id / MEGASAS_MAX_DEV_PER_CHANNEL),
8916 (ld_target_id - MEGASAS_MAX_DEV_PER_CHANNEL),
8919 megasas_remove_scsi_device(sdev1);
8921 event_type = SCAN_VD_CHANNEL;
8923 case MR_EVT_LD_CREATED:
8924 event_type = SCAN_VD_CHANNEL;
8927 case MR_EVT_CFG_CLEARED:
8928 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8929 case MR_EVT_FOREIGN_CFG_IMPORTED:
8930 case MR_EVT_LD_STATE_CHANGE:
8931 event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8932 dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8933 instance->host->host_no);
8936 case MR_EVT_CTRL_PROP_CHANGED:
8937 dcmd_ret = megasas_get_ctrl_info(instance);
8938 if (dcmd_ret == DCMD_SUCCESS &&
8939 instance->snapdump_wait_time) {
8940 megasas_get_snapdump_properties(instance);
8941 dev_info(&instance->pdev->dev,
8942 "Snap dump wait time\t: %d\n",
8943 instance->snapdump_wait_time);
8951 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8952 mutex_unlock(&instance->reset_mutex);
8958 dcmd_ret = megasas_update_device_list(instance, event_type);
8960 mutex_unlock(&instance->reset_mutex);
8962 if (event_type && dcmd_ret == DCMD_SUCCESS)
8963 megasas_add_remove_devices(instance, event_type);
8965 if (dcmd_ret == DCMD_SUCCESS)
8966 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8968 seq_num = instance->last_seq_num;
8970 /* Register AEN with FW for latest sequence number plus 1 */
8971 class_locale.members.reserved = 0;
8972 class_locale.members.locale = MR_EVT_LOCALE_ALL;
8973 class_locale.members.class = MR_EVT_CLASS_DEBUG;
8975 if (instance->aen_cmd != NULL) {
8980 mutex_lock(&instance->reset_mutex);
8981 error = megasas_register_aen(instance, seq_num,
8984 dev_err(&instance->pdev->dev,
8985 "register aen failed error %x\n", error);
8987 mutex_unlock(&instance->reset_mutex);
8992 * megasas_init - Driver load entry point
8994 static int __init megasas_init(void)
8999 * Booted in kdump kernel, minimize memory footprints by
9000 * disabling few features
9002 if (reset_devices) {
9005 dual_qdepth_disable = 1;
9010 * Announce driver version and other information
9012 pr_info("megasas: %s\n", MEGASAS_VERSION);
9014 support_poll_for_event = 2;
9015 support_device_change = 1;
9016 support_nvme_encapsulation = true;
9017 support_pci_lane_margining = true;
9019 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
9022 * Register character device node
9024 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
9027 printk(KERN_DEBUG "megasas: failed to open device node\n");
9031 megasas_mgmt_majorno = rval;
9033 megasas_init_debugfs();
9036 * Register ourselves as PCI hotplug module
9038 rval = pci_register_driver(&megasas_pci_driver);
9041 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
9045 if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
9046 (event_log_level > MFI_EVT_CLASS_DEAD)) {
9047 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");
9048 event_log_level = MFI_EVT_CLASS_CRITICAL;
9051 rval = driver_create_file(&megasas_pci_driver.driver,
9052 &driver_attr_version);
9054 goto err_dcf_attr_ver;
9056 rval = driver_create_file(&megasas_pci_driver.driver,
9057 &driver_attr_release_date);
9059 goto err_dcf_rel_date;
9061 rval = driver_create_file(&megasas_pci_driver.driver,
9062 &driver_attr_support_poll_for_event);
9064 goto err_dcf_support_poll_for_event;
9066 rval = driver_create_file(&megasas_pci_driver.driver,
9067 &driver_attr_dbg_lvl);
9069 goto err_dcf_dbg_lvl;
9070 rval = driver_create_file(&megasas_pci_driver.driver,
9071 &driver_attr_support_device_change);
9073 goto err_dcf_support_device_change;
9075 rval = driver_create_file(&megasas_pci_driver.driver,
9076 &driver_attr_support_nvme_encapsulation);
9078 goto err_dcf_support_nvme_encapsulation;
9080 rval = driver_create_file(&megasas_pci_driver.driver,
9081 &driver_attr_support_pci_lane_margining);
9083 goto err_dcf_support_pci_lane_margining;
9087 err_dcf_support_pci_lane_margining:
9088 driver_remove_file(&megasas_pci_driver.driver,
9089 &driver_attr_support_nvme_encapsulation);
9091 err_dcf_support_nvme_encapsulation:
9092 driver_remove_file(&megasas_pci_driver.driver,
9093 &driver_attr_support_device_change);
9095 err_dcf_support_device_change:
9096 driver_remove_file(&megasas_pci_driver.driver,
9097 &driver_attr_dbg_lvl);
9099 driver_remove_file(&megasas_pci_driver.driver,
9100 &driver_attr_support_poll_for_event);
9101 err_dcf_support_poll_for_event:
9102 driver_remove_file(&megasas_pci_driver.driver,
9103 &driver_attr_release_date);
9105 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9107 pci_unregister_driver(&megasas_pci_driver);
9109 megasas_exit_debugfs();
9110 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9115 * megasas_exit - Driver unload entry point
9117 static void __exit megasas_exit(void)
9119 driver_remove_file(&megasas_pci_driver.driver,
9120 &driver_attr_dbg_lvl);
9121 driver_remove_file(&megasas_pci_driver.driver,
9122 &driver_attr_support_poll_for_event);
9123 driver_remove_file(&megasas_pci_driver.driver,
9124 &driver_attr_support_device_change);
9125 driver_remove_file(&megasas_pci_driver.driver,
9126 &driver_attr_release_date);
9127 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
9128 driver_remove_file(&megasas_pci_driver.driver,
9129 &driver_attr_support_nvme_encapsulation);
9130 driver_remove_file(&megasas_pci_driver.driver,
9131 &driver_attr_support_pci_lane_margining);
9133 pci_unregister_driver(&megasas_pci_driver);
9134 megasas_exit_debugfs();
9135 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
9138 module_init(megasas_init);
9139 module_exit(megasas_exit);