root/drivers/scsi/megaraid/megaraid_sas_base.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. megasas_readl
  2. megasas_set_dma_settings
  3. megasas_issue_dcmd
  4. megasas_get_cmd
  5. megasas_return_cmd
  6. format_timestamp
  7. format_class
  8. megasas_decode_evt
  9. megasas_enable_intr_xscale
  10. megasas_disable_intr_xscale
  11. megasas_read_fw_status_reg_xscale
  12. megasas_clear_intr_xscale
  13. megasas_fire_cmd_xscale
  14. megasas_adp_reset_xscale
  15. megasas_check_reset_xscale
  16. megasas_enable_intr_ppc
  17. megasas_disable_intr_ppc
  18. megasas_read_fw_status_reg_ppc
  19. megasas_clear_intr_ppc
  20. megasas_fire_cmd_ppc
  21. megasas_check_reset_ppc
  22. megasas_enable_intr_skinny
  23. megasas_disable_intr_skinny
  24. megasas_read_fw_status_reg_skinny
  25. megasas_clear_intr_skinny
  26. megasas_fire_cmd_skinny
  27. megasas_check_reset_skinny
  28. megasas_enable_intr_gen2
  29. megasas_disable_intr_gen2
  30. megasas_read_fw_status_reg_gen2
  31. megasas_clear_intr_gen2
  32. megasas_fire_cmd_gen2
  33. megasas_adp_reset_gen2
  34. megasas_check_reset_gen2
  35. megasas_issue_polled
  36. megasas_issue_blocked_cmd
  37. megasas_issue_blocked_abort_cmd
  38. megasas_make_sgl32
  39. megasas_make_sgl64
  40. megasas_make_sgl_skinny
  41. megasas_get_frame_count
  42. megasas_build_dcdb
  43. megasas_build_ldio
  44. megasas_cmd_type
  45. megasas_dump_pending_frames
  46. megasas_build_and_issue_cmd
  47. megasas_queue_command
  48. megasas_lookup_instance
  49. megasas_set_dynamic_target_properties
  50. megasas_set_nvme_device_properties
  51. megasas_set_fw_assisted_qd
  52. megasas_set_static_target_properties
  53. megasas_slave_configure
  54. megasas_slave_alloc
  55. megasas_slave_destroy
  56. megasas_complete_outstanding_ioctls
  57. megaraid_sas_kill_hba
  58. megasas_check_and_restore_queue_depth
  59. megasas_complete_cmd_dpc
  60. megasas_start_timer
  61. megasas_do_ocr
  62. megasas_get_ld_vf_affiliation_111
  63. megasas_get_ld_vf_affiliation_12
  64. megasas_get_ld_vf_affiliation
  65. megasas_sriov_start_heartbeat
  66. megasas_sriov_heartbeat_handler
  67. megasas_wait_for_outstanding
  68. megasas_generic_reset
  69. megasas_reset_timer
  70. megasas_dump
  71. megasas_dump_reg_set
  72. megasas_dump_fusion_io
  73. megasas_dump_sys_regs
  74. megasas_reset_bus_host
  75. megasas_task_abort
  76. megasas_reset_target
  77. megasas_bios_param
  78. megasas_service_aen
  79. fw_crash_buffer_store
  80. fw_crash_buffer_show
  81. fw_crash_buffer_size_show
  82. fw_crash_state_store
  83. fw_crash_state_show
  84. page_size_show
  85. ldio_outstanding_show
  86. fw_cmds_outstanding_show
  87. enable_sdev_max_qd_show
  88. enable_sdev_max_qd_store
  89. dump_system_regs_show
  90. raid_map_id_show
  91. megasas_complete_int_cmd
  92. megasas_complete_abort
  93. megasas_complete_cmd
  94. megasas_issue_pending_cmds_again
  95. megasas_internal_reset_defer_cmds
  96. process_fw_state_change_wq
  97. megasas_deplete_reply_queue
  98. megasas_isr
  99. megasas_transition_to_ready
  100. megasas_teardown_frame_pool
  101. megasas_create_frame_pool
  102. megasas_free_cmds
  103. megasas_alloc_cmds
  104. dcmd_timeout_ocr_possible
  105. megasas_get_pd_info
  106. megasas_get_pd_list
  107. megasas_get_ld_list
  108. megasas_ld_list_query
  109. megasas_host_device_list_query
  110. megasas_update_ext_vd_details
  111. megasas_get_snapdump_properties
  112. megasas_get_ctrl_info
  113. megasas_set_crash_dump_params
  114. megasas_issue_init_mfi
  115. megasas_init_adapter_mfi
  116. megasas_setup_irq_poll
  117. megasas_setup_irqs_ioapic
  118. megasas_setup_irqs_msix
  119. megasas_destroy_irqs
  120. megasas_setup_jbod_map
  121. megasas_setup_reply_map
  122. megasas_get_device_list
  123. megasas_set_high_iops_queue_affinity_hint
  124. __megasas_alloc_irq_vectors
  125. megasas_alloc_irq_vectors
  126. megasas_init_fw
  127. megasas_release_mfi
  128. megasas_get_seq_num
  129. megasas_register_aen
  130. megasas_get_target_prop
  131. megasas_start_aen
  132. megasas_io_attach
  133. megasas_set_dma_mask
  134. megasas_set_adapter_type
  135. megasas_alloc_mfi_ctrl_mem
  136. megasas_alloc_ctrl_mem
  137. megasas_free_ctrl_mem
  138. megasas_alloc_ctrl_dma_buffers
  139. megasas_free_ctrl_dma_buffers
  140. megasas_init_ctrl_params
  141. megasas_probe_one
  142. megasas_flush_cache
  143. megasas_shutdown_controller
  144. megasas_suspend
  145. megasas_resume
  146. megasas_wait_for_adapter_operational
  147. megasas_detach_one
  148. megasas_shutdown
  149. megasas_mgmt_open
  150. megasas_mgmt_fasync
  151. megasas_mgmt_poll
  152. megasas_set_crash_dump_params_ioctl
  153. megasas_mgmt_fw_ioctl
  154. megasas_mgmt_ioctl_fw
  155. megasas_mgmt_ioctl_aen
  156. megasas_mgmt_ioctl
  157. megasas_mgmt_compat_ioctl_fw
  158. megasas_mgmt_compat_ioctl
  159. version_show
  160. release_date_show
  161. support_poll_for_event_show
  162. support_device_change_show
  163. dbg_lvl_show
  164. dbg_lvl_store
  165. support_nvme_encapsulation_show
  166. support_pci_lane_margining_show
  167. megasas_remove_scsi_device
  168. megasas_update_device_list
  169. megasas_add_remove_devices
  170. megasas_aen_polling
  171. megasas_init
  172. megasas_exit

   1 // SPDX-License-Identifier: GPL-2.0-or-later
   2 /*
   3  *  Linux MegaRAID driver for SAS based RAID controllers
   4  *
   5  *  Copyright (c) 2003-2013  LSI Corporation
   6  *  Copyright (c) 2013-2016  Avago Technologies
   7  *  Copyright (c) 2016-2018  Broadcom Inc.
   8  *
   9  *  Authors: Broadcom Inc.
  10  *           Sreenivas Bagalkote
  11  *           Sumant Patro
  12  *           Bo Yang
  13  *           Adam Radford
  14  *           Kashyap Desai <kashyap.desai@broadcom.com>
  15  *           Sumit Saxena <sumit.saxena@broadcom.com>
  16  *
  17  *  Send feedback to: megaraidlinux.pdl@broadcom.com
  18  */
  19 
  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>
  33 #include <linux/fs.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 
  41 #include <scsi/scsi.h>
  42 #include <scsi/scsi_cmnd.h>
  43 #include <scsi/scsi_device.h>
  44 #include <scsi/scsi_host.h>
  45 #include <scsi/scsi_tcq.h>
  46 #include <scsi/scsi_dbg.h>
  47 #include "megaraid_sas_fusion.h"
  48 #include "megaraid_sas.h"
  49 
  50 /*
  51  * Number of sectors per IO command
  52  * Will be set in megasas_init_mfi if user does not provide
  53  */
  54 static unsigned int max_sectors;
  55 module_param_named(max_sectors, max_sectors, int, 0444);
  56 MODULE_PARM_DESC(max_sectors,
  57         "Maximum number of sectors per IO command");
  58 
  59 static int msix_disable;
  60 module_param(msix_disable, int, 0444);
  61 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  62 
  63 static unsigned int msix_vectors;
  64 module_param(msix_vectors, int, 0444);
  65 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  66 
  67 static int allow_vf_ioctls;
  68 module_param(allow_vf_ioctls, int, 0444);
  69 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
  70 
  71 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  72 module_param(throttlequeuedepth, int, 0444);
  73 MODULE_PARM_DESC(throttlequeuedepth,
  74         "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  75 
  76 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  77 module_param(resetwaittime, int, 0444);
  78 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
  79 
  80 int smp_affinity_enable = 1;
  81 module_param(smp_affinity_enable, int, 0444);
  82 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
  83 
  84 int rdpq_enable = 1;
  85 module_param(rdpq_enable, int, 0444);
  86 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
  87 
  88 unsigned int dual_qdepth_disable;
  89 module_param(dual_qdepth_disable, int, 0444);
  90 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
  91 
  92 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  93 module_param(scmd_timeout, int, 0444);
  94 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
  95 
  96 int perf_mode = -1;
  97 module_param(perf_mode, int, 0444);
  98 MODULE_PARM_DESC(perf_mode, "Performance mode (only for Aero adapters), options:\n\t\t"
  99                 "0 - balanced: High iops and low latency queues are allocated &\n\t\t"
 100                 "interrupt coalescing is enabled only on high iops queues\n\t\t"
 101                 "1 - iops: High iops queues are not allocated &\n\t\t"
 102                 "interrupt coalescing is enabled on all queues\n\t\t"
 103                 "2 - latency: High iops queues are not allocated &\n\t\t"
 104                 "interrupt coalescing is disabled on all queues\n\t\t"
 105                 "default mode is 'balanced'"
 106                 );
 107 
 108 int event_log_level = MFI_EVT_CLASS_CRITICAL;
 109 module_param(event_log_level, int, 0644);
 110 MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
 111 
 112 unsigned int enable_sdev_max_qd;
 113 module_param(enable_sdev_max_qd, int, 0444);
 114 MODULE_PARM_DESC(enable_sdev_max_qd, "Enable sdev max qd as can_queue. Default: 0");
 115 
 116 MODULE_LICENSE("GPL");
 117 MODULE_VERSION(MEGASAS_VERSION);
 118 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
 119 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
 120 
 121 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
 122 static int megasas_get_pd_list(struct megasas_instance *instance);
 123 static int megasas_ld_list_query(struct megasas_instance *instance,
 124                                  u8 query_type);
 125 static int megasas_issue_init_mfi(struct megasas_instance *instance);
 126 static int megasas_register_aen(struct megasas_instance *instance,
 127                                 u32 seq_num, u32 class_locale_word);
 128 static void megasas_get_pd_info(struct megasas_instance *instance,
 129                                 struct scsi_device *sdev);
 130 
 131 /*
 132  * PCI ID table for all supported controllers
 133  */
 134 static struct pci_device_id megasas_pci_table[] = {
 135 
 136         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
 137         /* xscale IOP */
 138         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
 139         /* ppc IOP */
 140         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
 141         /* ppc IOP */
 142         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
 143         /* gen2*/
 144         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
 145         /* gen2*/
 146         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
 147         /* skinny*/
 148         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
 149         /* skinny*/
 150         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
 151         /* xscale IOP, vega */
 152         {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
 153         /* xscale IOP */
 154         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
 155         /* Fusion */
 156         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
 157         /* Plasma */
 158         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
 159         /* Invader */
 160         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
 161         /* Fury */
 162         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
 163         /* Intruder */
 164         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
 165         /* Intruder 24 port*/
 166         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
 167         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
 168         /* VENTURA */
 169         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
 170         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
 171         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
 172         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
 173         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
 174         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
 175         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
 176         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
 177         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
 178         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
 179         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
 180         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
 181         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
 182         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
 183         {}
 184 };
 185 
 186 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
 187 
 188 static int megasas_mgmt_majorno;
 189 struct megasas_mgmt_info megasas_mgmt_info;
 190 static struct fasync_struct *megasas_async_queue;
 191 static DEFINE_MUTEX(megasas_async_queue_mutex);
 192 
 193 static int megasas_poll_wait_aen;
 194 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
 195 static u32 support_poll_for_event;
 196 u32 megasas_dbg_lvl;
 197 static u32 support_device_change;
 198 static bool support_nvme_encapsulation;
 199 static bool support_pci_lane_margining;
 200 
 201 /* define lock for aen poll */
 202 spinlock_t poll_aen_lock;
 203 
 204 extern struct dentry *megasas_debugfs_root;
 205 extern void megasas_init_debugfs(void);
 206 extern void megasas_exit_debugfs(void);
 207 extern void megasas_setup_debugfs(struct megasas_instance *instance);
 208 extern void megasas_destroy_debugfs(struct megasas_instance *instance);
 209 
 210 void
 211 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
 212                      u8 alt_status);
 213 static u32
 214 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
 215 static int
 216 megasas_adp_reset_gen2(struct megasas_instance *instance,
 217                        struct megasas_register_set __iomem *reg_set);
 218 static irqreturn_t megasas_isr(int irq, void *devp);
 219 static u32
 220 megasas_init_adapter_mfi(struct megasas_instance *instance);
 221 u32
 222 megasas_build_and_issue_cmd(struct megasas_instance *instance,
 223                             struct scsi_cmnd *scmd);
 224 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
 225 int
 226 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
 227         int seconds);
 228 void megasas_fusion_ocr_wq(struct work_struct *work);
 229 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
 230                                          int initial);
 231 static int
 232 megasas_set_dma_mask(struct megasas_instance *instance);
 233 static int
 234 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
 235 static inline void
 236 megasas_free_ctrl_mem(struct megasas_instance *instance);
 237 static inline int
 238 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
 239 static inline void
 240 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
 241 static inline void
 242 megasas_init_ctrl_params(struct megasas_instance *instance);
 243 
 244 u32 megasas_readl(struct megasas_instance *instance,
 245                   const volatile void __iomem *addr)
 246 {
 247         u32 i = 0, ret_val;
 248         /*
 249          * Due to a HW errata in Aero controllers, reads to certain
 250          * Fusion registers could intermittently return all zeroes.
 251          * This behavior is transient in nature and subsequent reads will
 252          * return valid value. As a workaround in driver, retry readl for
 253          * upto three times until a non-zero value is read.
 254          */
 255         if (instance->adapter_type == AERO_SERIES) {
 256                 do {
 257                         ret_val = readl(addr);
 258                         i++;
 259                 } while (ret_val == 0 && i < 3);
 260                 return ret_val;
 261         } else {
 262                 return readl(addr);
 263         }
 264 }
 265 
 266 /**
 267  * megasas_set_dma_settings -   Populate DMA address, length and flags for DCMDs
 268  * @instance:                   Adapter soft state
 269  * @dcmd:                       DCMD frame inside MFI command
 270  * @dma_addr:                   DMA address of buffer to be passed to FW
 271  * @dma_len:                    Length of DMA buffer to be passed to FW
 272  * @return:                     void
 273  */
 274 void megasas_set_dma_settings(struct megasas_instance *instance,
 275                               struct megasas_dcmd_frame *dcmd,
 276                               dma_addr_t dma_addr, u32 dma_len)
 277 {
 278         if (instance->consistent_mask_64bit) {
 279                 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
 280                 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
 281                 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
 282 
 283         } else {
 284                 dcmd->sgl.sge32[0].phys_addr =
 285                                 cpu_to_le32(lower_32_bits(dma_addr));
 286                 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
 287                 dcmd->flags = cpu_to_le16(dcmd->flags);
 288         }
 289 }
 290 
 291 static void
 292 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 293 {
 294         instance->instancet->fire_cmd(instance,
 295                 cmd->frame_phys_addr, 0, instance->reg_set);
 296         return;
 297 }
 298 
 299 /**
 300  * megasas_get_cmd -    Get a command from the free pool
 301  * @instance:           Adapter soft state
 302  *
 303  * Returns a free command from the pool
 304  */
 305 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
 306                                                   *instance)
 307 {
 308         unsigned long flags;
 309         struct megasas_cmd *cmd = NULL;
 310 
 311         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
 312 
 313         if (!list_empty(&instance->cmd_pool)) {
 314                 cmd = list_entry((&instance->cmd_pool)->next,
 315                                  struct megasas_cmd, list);
 316                 list_del_init(&cmd->list);
 317         } else {
 318                 dev_err(&instance->pdev->dev, "Command pool empty!\n");
 319         }
 320 
 321         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
 322         return cmd;
 323 }
 324 
 325 /**
 326  * megasas_return_cmd - Return a cmd to free command pool
 327  * @instance:           Adapter soft state
 328  * @cmd:                Command packet to be returned to free command pool
 329  */
 330 void
 331 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 332 {
 333         unsigned long flags;
 334         u32 blk_tags;
 335         struct megasas_cmd_fusion *cmd_fusion;
 336         struct fusion_context *fusion = instance->ctrl_context;
 337 
 338         /* This flag is used only for fusion adapter.
 339          * Wait for Interrupt for Polled mode DCMD
 340          */
 341         if (cmd->flags & DRV_DCMD_POLLED_MODE)
 342                 return;
 343 
 344         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
 345 
 346         if (fusion) {
 347                 blk_tags = instance->max_scsi_cmds + cmd->index;
 348                 cmd_fusion = fusion->cmd_list[blk_tags];
 349                 megasas_return_cmd_fusion(instance, cmd_fusion);
 350         }
 351         cmd->scmd = NULL;
 352         cmd->frame_count = 0;
 353         cmd->flags = 0;
 354         memset(cmd->frame, 0, instance->mfi_frame_size);
 355         cmd->frame->io.context = cpu_to_le32(cmd->index);
 356         if (!fusion && reset_devices)
 357                 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
 358         list_add(&cmd->list, (&instance->cmd_pool)->next);
 359 
 360         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
 361 
 362 }
 363 
 364 static const char *
 365 format_timestamp(uint32_t timestamp)
 366 {
 367         static char buffer[32];
 368 
 369         if ((timestamp & 0xff000000) == 0xff000000)
 370                 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
 371                 0x00ffffff);
 372         else
 373                 snprintf(buffer, sizeof(buffer), "%us", timestamp);
 374         return buffer;
 375 }
 376 
 377 static const char *
 378 format_class(int8_t class)
 379 {
 380         static char buffer[6];
 381 
 382         switch (class) {
 383         case MFI_EVT_CLASS_DEBUG:
 384                 return "debug";
 385         case MFI_EVT_CLASS_PROGRESS:
 386                 return "progress";
 387         case MFI_EVT_CLASS_INFO:
 388                 return "info";
 389         case MFI_EVT_CLASS_WARNING:
 390                 return "WARN";
 391         case MFI_EVT_CLASS_CRITICAL:
 392                 return "CRIT";
 393         case MFI_EVT_CLASS_FATAL:
 394                 return "FATAL";
 395         case MFI_EVT_CLASS_DEAD:
 396                 return "DEAD";
 397         default:
 398                 snprintf(buffer, sizeof(buffer), "%d", class);
 399                 return buffer;
 400         }
 401 }
 402 
 403 /**
 404   * megasas_decode_evt: Decode FW AEN event and print critical event
 405   * for information.
 406   * @instance:                  Adapter soft state
 407   */
 408 static void
 409 megasas_decode_evt(struct megasas_instance *instance)
 410 {
 411         struct megasas_evt_detail *evt_detail = instance->evt_detail;
 412         union megasas_evt_class_locale class_locale;
 413         class_locale.word = le32_to_cpu(evt_detail->cl.word);
 414 
 415         if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
 416             (event_log_level > MFI_EVT_CLASS_DEAD)) {
 417                 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");
 418                 event_log_level = MFI_EVT_CLASS_CRITICAL;
 419         }
 420 
 421         if (class_locale.members.class >= event_log_level)
 422                 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
 423                         le32_to_cpu(evt_detail->seq_num),
 424                         format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
 425                         (class_locale.members.locale),
 426                         format_class(class_locale.members.class),
 427                         evt_detail->description);
 428 }
 429 
 430 /**
 431 *       The following functions are defined for xscale
 432 *       (deviceid : 1064R, PERC5) controllers
 433 */
 434 
 435 /**
 436  * megasas_enable_intr_xscale - Enables interrupts
 437  * @regs:                       MFI register set
 438  */
 439 static inline void
 440 megasas_enable_intr_xscale(struct megasas_instance *instance)
 441 {
 442         struct megasas_register_set __iomem *regs;
 443 
 444         regs = instance->reg_set;
 445         writel(0, &(regs)->outbound_intr_mask);
 446 
 447         /* Dummy readl to force pci flush */
 448         readl(&regs->outbound_intr_mask);
 449 }
 450 
 451 /**
 452  * megasas_disable_intr_xscale -Disables interrupt
 453  * @regs:                       MFI register set
 454  */
 455 static inline void
 456 megasas_disable_intr_xscale(struct megasas_instance *instance)
 457 {
 458         struct megasas_register_set __iomem *regs;
 459         u32 mask = 0x1f;
 460 
 461         regs = instance->reg_set;
 462         writel(mask, &regs->outbound_intr_mask);
 463         /* Dummy readl to force pci flush */
 464         readl(&regs->outbound_intr_mask);
 465 }
 466 
 467 /**
 468  * megasas_read_fw_status_reg_xscale - returns the current FW status value
 469  * @regs:                       MFI register set
 470  */
 471 static u32
 472 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
 473 {
 474         return readl(&instance->reg_set->outbound_msg_0);
 475 }
 476 /**
 477  * megasas_clear_interrupt_xscale -     Check & clear interrupt
 478  * @regs:                               MFI register set
 479  */
 480 static int
 481 megasas_clear_intr_xscale(struct megasas_instance *instance)
 482 {
 483         u32 status;
 484         u32 mfiStatus = 0;
 485         struct megasas_register_set __iomem *regs;
 486         regs = instance->reg_set;
 487 
 488         /*
 489          * Check if it is our interrupt
 490          */
 491         status = readl(&regs->outbound_intr_status);
 492 
 493         if (status & MFI_OB_INTR_STATUS_MASK)
 494                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 495         if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
 496                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 497 
 498         /*
 499          * Clear the interrupt by writing back the same value
 500          */
 501         if (mfiStatus)
 502                 writel(status, &regs->outbound_intr_status);
 503 
 504         /* Dummy readl to force pci flush */
 505         readl(&regs->outbound_intr_status);
 506 
 507         return mfiStatus;
 508 }
 509 
 510 /**
 511  * megasas_fire_cmd_xscale -    Sends command to the FW
 512  * @frame_phys_addr :           Physical address of cmd
 513  * @frame_count :               Number of frames for the command
 514  * @regs :                      MFI register set
 515  */
 516 static inline void
 517 megasas_fire_cmd_xscale(struct megasas_instance *instance,
 518                 dma_addr_t frame_phys_addr,
 519                 u32 frame_count,
 520                 struct megasas_register_set __iomem *regs)
 521 {
 522         unsigned long flags;
 523 
 524         spin_lock_irqsave(&instance->hba_lock, flags);
 525         writel((frame_phys_addr >> 3)|(frame_count),
 526                &(regs)->inbound_queue_port);
 527         spin_unlock_irqrestore(&instance->hba_lock, flags);
 528 }
 529 
 530 /**
 531  * megasas_adp_reset_xscale -  For controller reset
 532  * @regs:                              MFI register set
 533  */
 534 static int
 535 megasas_adp_reset_xscale(struct megasas_instance *instance,
 536         struct megasas_register_set __iomem *regs)
 537 {
 538         u32 i;
 539         u32 pcidata;
 540 
 541         writel(MFI_ADP_RESET, &regs->inbound_doorbell);
 542 
 543         for (i = 0; i < 3; i++)
 544                 msleep(1000); /* sleep for 3 secs */
 545         pcidata  = 0;
 546         pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
 547         dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
 548         if (pcidata & 0x2) {
 549                 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
 550                 pcidata &= ~0x2;
 551                 pci_write_config_dword(instance->pdev,
 552                                 MFI_1068_PCSR_OFFSET, pcidata);
 553 
 554                 for (i = 0; i < 2; i++)
 555                         msleep(1000); /* need to wait 2 secs again */
 556 
 557                 pcidata  = 0;
 558                 pci_read_config_dword(instance->pdev,
 559                                 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
 560                 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
 561                 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
 562                         dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
 563                         pcidata = 0;
 564                         pci_write_config_dword(instance->pdev,
 565                                 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
 566                 }
 567         }
 568         return 0;
 569 }
 570 
 571 /**
 572  * megasas_check_reset_xscale - For controller reset check
 573  * @regs:                               MFI register set
 574  */
 575 static int
 576 megasas_check_reset_xscale(struct megasas_instance *instance,
 577                 struct megasas_register_set __iomem *regs)
 578 {
 579         if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
 580             (le32_to_cpu(*instance->consumer) ==
 581                 MEGASAS_ADPRESET_INPROG_SIGN))
 582                 return 1;
 583         return 0;
 584 }
 585 
 586 static struct megasas_instance_template megasas_instance_template_xscale = {
 587 
 588         .fire_cmd = megasas_fire_cmd_xscale,
 589         .enable_intr = megasas_enable_intr_xscale,
 590         .disable_intr = megasas_disable_intr_xscale,
 591         .clear_intr = megasas_clear_intr_xscale,
 592         .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
 593         .adp_reset = megasas_adp_reset_xscale,
 594         .check_reset = megasas_check_reset_xscale,
 595         .service_isr = megasas_isr,
 596         .tasklet = megasas_complete_cmd_dpc,
 597         .init_adapter = megasas_init_adapter_mfi,
 598         .build_and_issue_cmd = megasas_build_and_issue_cmd,
 599         .issue_dcmd = megasas_issue_dcmd,
 600 };
 601 
 602 /**
 603 *       This is the end of set of functions & definitions specific
 604 *       to xscale (deviceid : 1064R, PERC5) controllers
 605 */
 606 
 607 /**
 608 *       The following functions are defined for ppc (deviceid : 0x60)
 609 *       controllers
 610 */
 611 
 612 /**
 613  * megasas_enable_intr_ppc -    Enables interrupts
 614  * @regs:                       MFI register set
 615  */
 616 static inline void
 617 megasas_enable_intr_ppc(struct megasas_instance *instance)
 618 {
 619         struct megasas_register_set __iomem *regs;
 620 
 621         regs = instance->reg_set;
 622         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 623 
 624         writel(~0x80000000, &(regs)->outbound_intr_mask);
 625 
 626         /* Dummy readl to force pci flush */
 627         readl(&regs->outbound_intr_mask);
 628 }
 629 
 630 /**
 631  * megasas_disable_intr_ppc -   Disable interrupt
 632  * @regs:                       MFI register set
 633  */
 634 static inline void
 635 megasas_disable_intr_ppc(struct megasas_instance *instance)
 636 {
 637         struct megasas_register_set __iomem *regs;
 638         u32 mask = 0xFFFFFFFF;
 639 
 640         regs = instance->reg_set;
 641         writel(mask, &regs->outbound_intr_mask);
 642         /* Dummy readl to force pci flush */
 643         readl(&regs->outbound_intr_mask);
 644 }
 645 
 646 /**
 647  * megasas_read_fw_status_reg_ppc - returns the current FW status value
 648  * @regs:                       MFI register set
 649  */
 650 static u32
 651 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
 652 {
 653         return readl(&instance->reg_set->outbound_scratch_pad_0);
 654 }
 655 
 656 /**
 657  * megasas_clear_interrupt_ppc -        Check & clear interrupt
 658  * @regs:                               MFI register set
 659  */
 660 static int
 661 megasas_clear_intr_ppc(struct megasas_instance *instance)
 662 {
 663         u32 status, mfiStatus = 0;
 664         struct megasas_register_set __iomem *regs;
 665         regs = instance->reg_set;
 666 
 667         /*
 668          * Check if it is our interrupt
 669          */
 670         status = readl(&regs->outbound_intr_status);
 671 
 672         if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
 673                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 674 
 675         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
 676                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 677 
 678         /*
 679          * Clear the interrupt by writing back the same value
 680          */
 681         writel(status, &regs->outbound_doorbell_clear);
 682 
 683         /* Dummy readl to force pci flush */
 684         readl(&regs->outbound_doorbell_clear);
 685 
 686         return mfiStatus;
 687 }
 688 
 689 /**
 690  * megasas_fire_cmd_ppc -       Sends command to the FW
 691  * @frame_phys_addr :           Physical address of cmd
 692  * @frame_count :               Number of frames for the command
 693  * @regs :                      MFI register set
 694  */
 695 static inline void
 696 megasas_fire_cmd_ppc(struct megasas_instance *instance,
 697                 dma_addr_t frame_phys_addr,
 698                 u32 frame_count,
 699                 struct megasas_register_set __iomem *regs)
 700 {
 701         unsigned long flags;
 702 
 703         spin_lock_irqsave(&instance->hba_lock, flags);
 704         writel((frame_phys_addr | (frame_count<<1))|1,
 705                         &(regs)->inbound_queue_port);
 706         spin_unlock_irqrestore(&instance->hba_lock, flags);
 707 }
 708 
 709 /**
 710  * megasas_check_reset_ppc -    For controller reset check
 711  * @regs:                               MFI register set
 712  */
 713 static int
 714 megasas_check_reset_ppc(struct megasas_instance *instance,
 715                         struct megasas_register_set __iomem *regs)
 716 {
 717         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
 718                 return 1;
 719 
 720         return 0;
 721 }
 722 
 723 static struct megasas_instance_template megasas_instance_template_ppc = {
 724 
 725         .fire_cmd = megasas_fire_cmd_ppc,
 726         .enable_intr = megasas_enable_intr_ppc,
 727         .disable_intr = megasas_disable_intr_ppc,
 728         .clear_intr = megasas_clear_intr_ppc,
 729         .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
 730         .adp_reset = megasas_adp_reset_xscale,
 731         .check_reset = megasas_check_reset_ppc,
 732         .service_isr = megasas_isr,
 733         .tasklet = megasas_complete_cmd_dpc,
 734         .init_adapter = megasas_init_adapter_mfi,
 735         .build_and_issue_cmd = megasas_build_and_issue_cmd,
 736         .issue_dcmd = megasas_issue_dcmd,
 737 };
 738 
 739 /**
 740  * megasas_enable_intr_skinny - Enables interrupts
 741  * @regs:                       MFI register set
 742  */
 743 static inline void
 744 megasas_enable_intr_skinny(struct megasas_instance *instance)
 745 {
 746         struct megasas_register_set __iomem *regs;
 747 
 748         regs = instance->reg_set;
 749         writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
 750 
 751         writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 752 
 753         /* Dummy readl to force pci flush */
 754         readl(&regs->outbound_intr_mask);
 755 }
 756 
 757 /**
 758  * megasas_disable_intr_skinny -        Disables interrupt
 759  * @regs:                       MFI register set
 760  */
 761 static inline void
 762 megasas_disable_intr_skinny(struct megasas_instance *instance)
 763 {
 764         struct megasas_register_set __iomem *regs;
 765         u32 mask = 0xFFFFFFFF;
 766 
 767         regs = instance->reg_set;
 768         writel(mask, &regs->outbound_intr_mask);
 769         /* Dummy readl to force pci flush */
 770         readl(&regs->outbound_intr_mask);
 771 }
 772 
 773 /**
 774  * megasas_read_fw_status_reg_skinny - returns the current FW status value
 775  * @regs:                       MFI register set
 776  */
 777 static u32
 778 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
 779 {
 780         return readl(&instance->reg_set->outbound_scratch_pad_0);
 781 }
 782 
 783 /**
 784  * megasas_clear_interrupt_skinny -     Check & clear interrupt
 785  * @regs:                               MFI register set
 786  */
 787 static int
 788 megasas_clear_intr_skinny(struct megasas_instance *instance)
 789 {
 790         u32 status;
 791         u32 mfiStatus = 0;
 792         struct megasas_register_set __iomem *regs;
 793         regs = instance->reg_set;
 794 
 795         /*
 796          * Check if it is our interrupt
 797          */
 798         status = readl(&regs->outbound_intr_status);
 799 
 800         if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
 801                 return 0;
 802         }
 803 
 804         /*
 805          * Check if it is our interrupt
 806          */
 807         if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
 808             MFI_STATE_FAULT) {
 809                 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 810         } else
 811                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 812 
 813         /*
 814          * Clear the interrupt by writing back the same value
 815          */
 816         writel(status, &regs->outbound_intr_status);
 817 
 818         /*
 819          * dummy read to flush PCI
 820          */
 821         readl(&regs->outbound_intr_status);
 822 
 823         return mfiStatus;
 824 }
 825 
 826 /**
 827  * megasas_fire_cmd_skinny -    Sends command to the FW
 828  * @frame_phys_addr :           Physical address of cmd
 829  * @frame_count :               Number of frames for the command
 830  * @regs :                      MFI register set
 831  */
 832 static inline void
 833 megasas_fire_cmd_skinny(struct megasas_instance *instance,
 834                         dma_addr_t frame_phys_addr,
 835                         u32 frame_count,
 836                         struct megasas_register_set __iomem *regs)
 837 {
 838         unsigned long flags;
 839 
 840         spin_lock_irqsave(&instance->hba_lock, flags);
 841         writel(upper_32_bits(frame_phys_addr),
 842                &(regs)->inbound_high_queue_port);
 843         writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
 844                &(regs)->inbound_low_queue_port);
 845         spin_unlock_irqrestore(&instance->hba_lock, flags);
 846 }
 847 
 848 /**
 849  * megasas_check_reset_skinny - For controller reset check
 850  * @regs:                               MFI register set
 851  */
 852 static int
 853 megasas_check_reset_skinny(struct megasas_instance *instance,
 854                                 struct megasas_register_set __iomem *regs)
 855 {
 856         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
 857                 return 1;
 858 
 859         return 0;
 860 }
 861 
 862 static struct megasas_instance_template megasas_instance_template_skinny = {
 863 
 864         .fire_cmd = megasas_fire_cmd_skinny,
 865         .enable_intr = megasas_enable_intr_skinny,
 866         .disable_intr = megasas_disable_intr_skinny,
 867         .clear_intr = megasas_clear_intr_skinny,
 868         .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
 869         .adp_reset = megasas_adp_reset_gen2,
 870         .check_reset = megasas_check_reset_skinny,
 871         .service_isr = megasas_isr,
 872         .tasklet = megasas_complete_cmd_dpc,
 873         .init_adapter = megasas_init_adapter_mfi,
 874         .build_and_issue_cmd = megasas_build_and_issue_cmd,
 875         .issue_dcmd = megasas_issue_dcmd,
 876 };
 877 
 878 
 879 /**
 880 *       The following functions are defined for gen2 (deviceid : 0x78 0x79)
 881 *       controllers
 882 */
 883 
 884 /**
 885  * megasas_enable_intr_gen2 -  Enables interrupts
 886  * @regs:                      MFI register set
 887  */
 888 static inline void
 889 megasas_enable_intr_gen2(struct megasas_instance *instance)
 890 {
 891         struct megasas_register_set __iomem *regs;
 892 
 893         regs = instance->reg_set;
 894         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 895 
 896         /* write ~0x00000005 (4 & 1) to the intr mask*/
 897         writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 898 
 899         /* Dummy readl to force pci flush */
 900         readl(&regs->outbound_intr_mask);
 901 }
 902 
 903 /**
 904  * megasas_disable_intr_gen2 - Disables interrupt
 905  * @regs:                      MFI register set
 906  */
 907 static inline void
 908 megasas_disable_intr_gen2(struct megasas_instance *instance)
 909 {
 910         struct megasas_register_set __iomem *regs;
 911         u32 mask = 0xFFFFFFFF;
 912 
 913         regs = instance->reg_set;
 914         writel(mask, &regs->outbound_intr_mask);
 915         /* Dummy readl to force pci flush */
 916         readl(&regs->outbound_intr_mask);
 917 }
 918 
 919 /**
 920  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
 921  * @regs:                      MFI register set
 922  */
 923 static u32
 924 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
 925 {
 926         return readl(&instance->reg_set->outbound_scratch_pad_0);
 927 }
 928 
 929 /**
 930  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
 931  * @regs:                              MFI register set
 932  */
 933 static int
 934 megasas_clear_intr_gen2(struct megasas_instance *instance)
 935 {
 936         u32 status;
 937         u32 mfiStatus = 0;
 938         struct megasas_register_set __iomem *regs;
 939         regs = instance->reg_set;
 940 
 941         /*
 942          * Check if it is our interrupt
 943          */
 944         status = readl(&regs->outbound_intr_status);
 945 
 946         if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
 947                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 948         }
 949         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
 950                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 951         }
 952 
 953         /*
 954          * Clear the interrupt by writing back the same value
 955          */
 956         if (mfiStatus)
 957                 writel(status, &regs->outbound_doorbell_clear);
 958 
 959         /* Dummy readl to force pci flush */
 960         readl(&regs->outbound_intr_status);
 961 
 962         return mfiStatus;
 963 }
 964 /**
 965  * megasas_fire_cmd_gen2 -     Sends command to the FW
 966  * @frame_phys_addr :          Physical address of cmd
 967  * @frame_count :              Number of frames for the command
 968  * @regs :                     MFI register set
 969  */
 970 static inline void
 971 megasas_fire_cmd_gen2(struct megasas_instance *instance,
 972                         dma_addr_t frame_phys_addr,
 973                         u32 frame_count,
 974                         struct megasas_register_set __iomem *regs)
 975 {
 976         unsigned long flags;
 977 
 978         spin_lock_irqsave(&instance->hba_lock, flags);
 979         writel((frame_phys_addr | (frame_count<<1))|1,
 980                         &(regs)->inbound_queue_port);
 981         spin_unlock_irqrestore(&instance->hba_lock, flags);
 982 }
 983 
 984 /**
 985  * megasas_adp_reset_gen2 -     For controller reset
 986  * @regs:                               MFI register set
 987  */
 988 static int
 989 megasas_adp_reset_gen2(struct megasas_instance *instance,
 990                         struct megasas_register_set __iomem *reg_set)
 991 {
 992         u32 retry = 0 ;
 993         u32 HostDiag;
 994         u32 __iomem *seq_offset = &reg_set->seq_offset;
 995         u32 __iomem *hostdiag_offset = &reg_set->host_diag;
 996 
 997         if (instance->instancet == &megasas_instance_template_skinny) {
 998                 seq_offset = &reg_set->fusion_seq_offset;
 999                 hostdiag_offset = &reg_set->fusion_host_diag;
1000         }
1001 
1002         writel(0, seq_offset);
1003         writel(4, seq_offset);
1004         writel(0xb, seq_offset);
1005         writel(2, seq_offset);
1006         writel(7, seq_offset);
1007         writel(0xd, seq_offset);
1008 
1009         msleep(1000);
1010 
1011         HostDiag = (u32)readl(hostdiag_offset);
1012 
1013         while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1014                 msleep(100);
1015                 HostDiag = (u32)readl(hostdiag_offset);
1016                 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1017                                         retry, HostDiag);
1018 
1019                 if (retry++ >= 100)
1020                         return 1;
1021 
1022         }
1023 
1024         dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1025 
1026         writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1027 
1028         ssleep(10);
1029 
1030         HostDiag = (u32)readl(hostdiag_offset);
1031         while (HostDiag & DIAG_RESET_ADAPTER) {
1032                 msleep(100);
1033                 HostDiag = (u32)readl(hostdiag_offset);
1034                 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1035                                 retry, HostDiag);
1036 
1037                 if (retry++ >= 1000)
1038                         return 1;
1039 
1040         }
1041         return 0;
1042 }
1043 
1044 /**
1045  * megasas_check_reset_gen2 -   For controller reset check
1046  * @regs:                               MFI register set
1047  */
1048 static int
1049 megasas_check_reset_gen2(struct megasas_instance *instance,
1050                 struct megasas_register_set __iomem *regs)
1051 {
1052         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1053                 return 1;
1054 
1055         return 0;
1056 }
1057 
1058 static struct megasas_instance_template megasas_instance_template_gen2 = {
1059 
1060         .fire_cmd = megasas_fire_cmd_gen2,
1061         .enable_intr = megasas_enable_intr_gen2,
1062         .disable_intr = megasas_disable_intr_gen2,
1063         .clear_intr = megasas_clear_intr_gen2,
1064         .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1065         .adp_reset = megasas_adp_reset_gen2,
1066         .check_reset = megasas_check_reset_gen2,
1067         .service_isr = megasas_isr,
1068         .tasklet = megasas_complete_cmd_dpc,
1069         .init_adapter = megasas_init_adapter_mfi,
1070         .build_and_issue_cmd = megasas_build_and_issue_cmd,
1071         .issue_dcmd = megasas_issue_dcmd,
1072 };
1073 
1074 /**
1075 *       This is the end of set of functions & definitions
1076 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
1077 */
1078 
1079 /*
1080  * Template added for TB (Fusion)
1081  */
1082 extern struct megasas_instance_template megasas_instance_template_fusion;
1083 
1084 /**
1085  * megasas_issue_polled -       Issues a polling command
1086  * @instance:                   Adapter soft state
1087  * @cmd:                        Command packet to be issued
1088  *
1089  * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1090  */
1091 int
1092 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1093 {
1094         struct megasas_header *frame_hdr = &cmd->frame->hdr;
1095 
1096         frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1097         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1098 
1099         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1100                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1101                         __func__, __LINE__);
1102                 return DCMD_NOT_FIRED;
1103         }
1104 
1105         instance->instancet->issue_dcmd(instance, cmd);
1106 
1107         return wait_and_poll(instance, cmd, instance->requestorId ?
1108                         MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1109 }
1110 
1111 /**
1112  * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
1113  * @instance:                   Adapter soft state
1114  * @cmd:                        Command to be issued
1115  * @timeout:                    Timeout in seconds
1116  *
1117  * This function waits on an event for the command to be returned from ISR.
1118  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1119  * Used to issue ioctl commands.
1120  */
1121 int
1122 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1123                           struct megasas_cmd *cmd, int timeout)
1124 {
1125         int ret = 0;
1126         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1127 
1128         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1129                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1130                         __func__, __LINE__);
1131                 return DCMD_NOT_FIRED;
1132         }
1133 
1134         instance->instancet->issue_dcmd(instance, cmd);
1135 
1136         if (timeout) {
1137                 ret = wait_event_timeout(instance->int_cmd_wait_q,
1138                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1139                 if (!ret) {
1140                         dev_err(&instance->pdev->dev,
1141                                 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1142                                 cmd->frame->dcmd.opcode, __func__);
1143                         return DCMD_TIMEOUT;
1144                 }
1145         } else
1146                 wait_event(instance->int_cmd_wait_q,
1147                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1148 
1149         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1150                 DCMD_SUCCESS : DCMD_FAILED;
1151 }
1152 
1153 /**
1154  * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
1155  * @instance:                           Adapter soft state
1156  * @cmd_to_abort:                       Previously issued cmd to be aborted
1157  * @timeout:                            Timeout in seconds
1158  *
1159  * MFI firmware can abort previously issued AEN comamnd (automatic event
1160  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1161  * cmd and waits for return status.
1162  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1163  */
1164 static int
1165 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1166                                 struct megasas_cmd *cmd_to_abort, int timeout)
1167 {
1168         struct megasas_cmd *cmd;
1169         struct megasas_abort_frame *abort_fr;
1170         int ret = 0;
1171         u32 opcode;
1172 
1173         cmd = megasas_get_cmd(instance);
1174 
1175         if (!cmd)
1176                 return -1;
1177 
1178         abort_fr = &cmd->frame->abort;
1179 
1180         /*
1181          * Prepare and issue the abort frame
1182          */
1183         abort_fr->cmd = MFI_CMD_ABORT;
1184         abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1185         abort_fr->flags = cpu_to_le16(0);
1186         abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1187         abort_fr->abort_mfi_phys_addr_lo =
1188                 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1189         abort_fr->abort_mfi_phys_addr_hi =
1190                 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1191 
1192         cmd->sync_cmd = 1;
1193         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1194 
1195         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1196                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1197                         __func__, __LINE__);
1198                 return DCMD_NOT_FIRED;
1199         }
1200 
1201         instance->instancet->issue_dcmd(instance, cmd);
1202 
1203         if (timeout) {
1204                 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1205                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1206                 if (!ret) {
1207                         opcode = cmd_to_abort->frame->dcmd.opcode;
1208                         dev_err(&instance->pdev->dev,
1209                                 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1210                                 opcode,  __func__);
1211                         return DCMD_TIMEOUT;
1212                 }
1213         } else
1214                 wait_event(instance->abort_cmd_wait_q,
1215                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1216 
1217         cmd->sync_cmd = 0;
1218 
1219         megasas_return_cmd(instance, cmd);
1220         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1221                 DCMD_SUCCESS : DCMD_FAILED;
1222 }
1223 
1224 /**
1225  * megasas_make_sgl32 - Prepares 32-bit SGL
1226  * @instance:           Adapter soft state
1227  * @scp:                SCSI command from the mid-layer
1228  * @mfi_sgl:            SGL to be filled in
1229  *
1230  * If successful, this function returns the number of SG elements. Otherwise,
1231  * it returnes -1.
1232  */
1233 static int
1234 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1235                    union megasas_sgl *mfi_sgl)
1236 {
1237         int i;
1238         int sge_count;
1239         struct scatterlist *os_sgl;
1240 
1241         sge_count = scsi_dma_map(scp);
1242         BUG_ON(sge_count < 0);
1243 
1244         if (sge_count) {
1245                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1246                         mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1247                         mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1248                 }
1249         }
1250         return sge_count;
1251 }
1252 
1253 /**
1254  * megasas_make_sgl64 - Prepares 64-bit SGL
1255  * @instance:           Adapter soft state
1256  * @scp:                SCSI command from the mid-layer
1257  * @mfi_sgl:            SGL to be filled in
1258  *
1259  * If successful, this function returns the number of SG elements. Otherwise,
1260  * it returnes -1.
1261  */
1262 static int
1263 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1264                    union megasas_sgl *mfi_sgl)
1265 {
1266         int i;
1267         int sge_count;
1268         struct scatterlist *os_sgl;
1269 
1270         sge_count = scsi_dma_map(scp);
1271         BUG_ON(sge_count < 0);
1272 
1273         if (sge_count) {
1274                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1275                         mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1276                         mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1277                 }
1278         }
1279         return sge_count;
1280 }
1281 
1282 /**
1283  * megasas_make_sgl_skinny - Prepares IEEE SGL
1284  * @instance:           Adapter soft state
1285  * @scp:                SCSI command from the mid-layer
1286  * @mfi_sgl:            SGL to be filled in
1287  *
1288  * If successful, this function returns the number of SG elements. Otherwise,
1289  * it returnes -1.
1290  */
1291 static int
1292 megasas_make_sgl_skinny(struct megasas_instance *instance,
1293                 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1294 {
1295         int i;
1296         int sge_count;
1297         struct scatterlist *os_sgl;
1298 
1299         sge_count = scsi_dma_map(scp);
1300 
1301         if (sge_count) {
1302                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1303                         mfi_sgl->sge_skinny[i].length =
1304                                 cpu_to_le32(sg_dma_len(os_sgl));
1305                         mfi_sgl->sge_skinny[i].phys_addr =
1306                                 cpu_to_le64(sg_dma_address(os_sgl));
1307                         mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1308                 }
1309         }
1310         return sge_count;
1311 }
1312 
1313  /**
1314  * megasas_get_frame_count - Computes the number of frames
1315  * @frame_type          : type of frame- io or pthru frame
1316  * @sge_count           : number of sg elements
1317  *
1318  * Returns the number of frames required for numnber of sge's (sge_count)
1319  */
1320 
1321 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1322                         u8 sge_count, u8 frame_type)
1323 {
1324         int num_cnt;
1325         int sge_bytes;
1326         u32 sge_sz;
1327         u32 frame_count = 0;
1328 
1329         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1330             sizeof(struct megasas_sge32);
1331 
1332         if (instance->flag_ieee) {
1333                 sge_sz = sizeof(struct megasas_sge_skinny);
1334         }
1335 
1336         /*
1337          * Main frame can contain 2 SGEs for 64-bit SGLs and
1338          * 3 SGEs for 32-bit SGLs for ldio &
1339          * 1 SGEs for 64-bit SGLs and
1340          * 2 SGEs for 32-bit SGLs for pthru frame
1341          */
1342         if (unlikely(frame_type == PTHRU_FRAME)) {
1343                 if (instance->flag_ieee == 1) {
1344                         num_cnt = sge_count - 1;
1345                 } else if (IS_DMA64)
1346                         num_cnt = sge_count - 1;
1347                 else
1348                         num_cnt = sge_count - 2;
1349         } else {
1350                 if (instance->flag_ieee == 1) {
1351                         num_cnt = sge_count - 1;
1352                 } else if (IS_DMA64)
1353                         num_cnt = sge_count - 2;
1354                 else
1355                         num_cnt = sge_count - 3;
1356         }
1357 
1358         if (num_cnt > 0) {
1359                 sge_bytes = sge_sz * num_cnt;
1360 
1361                 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1362                     ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1363         }
1364         /* Main frame */
1365         frame_count += 1;
1366 
1367         if (frame_count > 7)
1368                 frame_count = 8;
1369         return frame_count;
1370 }
1371 
1372 /**
1373  * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1374  * @instance:           Adapter soft state
1375  * @scp:                SCSI command
1376  * @cmd:                Command to be prepared in
1377  *
1378  * This function prepares CDB commands. These are typcially pass-through
1379  * commands to the devices.
1380  */
1381 static int
1382 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1383                    struct megasas_cmd *cmd)
1384 {
1385         u32 is_logical;
1386         u32 device_id;
1387         u16 flags = 0;
1388         struct megasas_pthru_frame *pthru;
1389 
1390         is_logical = MEGASAS_IS_LOGICAL(scp->device);
1391         device_id = MEGASAS_DEV_INDEX(scp);
1392         pthru = (struct megasas_pthru_frame *)cmd->frame;
1393 
1394         if (scp->sc_data_direction == DMA_TO_DEVICE)
1395                 flags = MFI_FRAME_DIR_WRITE;
1396         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1397                 flags = MFI_FRAME_DIR_READ;
1398         else if (scp->sc_data_direction == DMA_NONE)
1399                 flags = MFI_FRAME_DIR_NONE;
1400 
1401         if (instance->flag_ieee == 1) {
1402                 flags |= MFI_FRAME_IEEE;
1403         }
1404 
1405         /*
1406          * Prepare the DCDB frame
1407          */
1408         pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1409         pthru->cmd_status = 0x0;
1410         pthru->scsi_status = 0x0;
1411         pthru->target_id = device_id;
1412         pthru->lun = scp->device->lun;
1413         pthru->cdb_len = scp->cmd_len;
1414         pthru->timeout = 0;
1415         pthru->pad_0 = 0;
1416         pthru->flags = cpu_to_le16(flags);
1417         pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1418 
1419         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1420 
1421         /*
1422          * If the command is for the tape device, set the
1423          * pthru timeout to the os layer timeout value.
1424          */
1425         if (scp->device->type == TYPE_TAPE) {
1426                 if ((scp->request->timeout / HZ) > 0xFFFF)
1427                         pthru->timeout = cpu_to_le16(0xFFFF);
1428                 else
1429                         pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1430         }
1431 
1432         /*
1433          * Construct SGL
1434          */
1435         if (instance->flag_ieee == 1) {
1436                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1437                 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1438                                                       &pthru->sgl);
1439         } else if (IS_DMA64) {
1440                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1441                 pthru->sge_count = megasas_make_sgl64(instance, scp,
1442                                                       &pthru->sgl);
1443         } else
1444                 pthru->sge_count = megasas_make_sgl32(instance, scp,
1445                                                       &pthru->sgl);
1446 
1447         if (pthru->sge_count > instance->max_num_sge) {
1448                 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1449                         pthru->sge_count);
1450                 return 0;
1451         }
1452 
1453         /*
1454          * Sense info specific
1455          */
1456         pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1457         pthru->sense_buf_phys_addr_hi =
1458                 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1459         pthru->sense_buf_phys_addr_lo =
1460                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1461 
1462         /*
1463          * Compute the total number of frames this command consumes. FW uses
1464          * this number to pull sufficient number of frames from host memory.
1465          */
1466         cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1467                                                         PTHRU_FRAME);
1468 
1469         return cmd->frame_count;
1470 }
1471 
1472 /**
1473  * megasas_build_ldio - Prepares IOs to logical devices
1474  * @instance:           Adapter soft state
1475  * @scp:                SCSI command
1476  * @cmd:                Command to be prepared
1477  *
1478  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1479  */
1480 static int
1481 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1482                    struct megasas_cmd *cmd)
1483 {
1484         u32 device_id;
1485         u8 sc = scp->cmnd[0];
1486         u16 flags = 0;
1487         struct megasas_io_frame *ldio;
1488 
1489         device_id = MEGASAS_DEV_INDEX(scp);
1490         ldio = (struct megasas_io_frame *)cmd->frame;
1491 
1492         if (scp->sc_data_direction == DMA_TO_DEVICE)
1493                 flags = MFI_FRAME_DIR_WRITE;
1494         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1495                 flags = MFI_FRAME_DIR_READ;
1496 
1497         if (instance->flag_ieee == 1) {
1498                 flags |= MFI_FRAME_IEEE;
1499         }
1500 
1501         /*
1502          * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1503          */
1504         ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1505         ldio->cmd_status = 0x0;
1506         ldio->scsi_status = 0x0;
1507         ldio->target_id = device_id;
1508         ldio->timeout = 0;
1509         ldio->reserved_0 = 0;
1510         ldio->pad_0 = 0;
1511         ldio->flags = cpu_to_le16(flags);
1512         ldio->start_lba_hi = 0;
1513         ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1514 
1515         /*
1516          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1517          */
1518         if (scp->cmd_len == 6) {
1519                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1520                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1521                                                  ((u32) scp->cmnd[2] << 8) |
1522                                                  (u32) scp->cmnd[3]);
1523 
1524                 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1525         }
1526 
1527         /*
1528          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1529          */
1530         else if (scp->cmd_len == 10) {
1531                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1532                                               ((u32) scp->cmnd[7] << 8));
1533                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1534                                                  ((u32) scp->cmnd[3] << 16) |
1535                                                  ((u32) scp->cmnd[4] << 8) |
1536                                                  (u32) scp->cmnd[5]);
1537         }
1538 
1539         /*
1540          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1541          */
1542         else if (scp->cmd_len == 12) {
1543                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1544                                               ((u32) scp->cmnd[7] << 16) |
1545                                               ((u32) scp->cmnd[8] << 8) |
1546                                               (u32) scp->cmnd[9]);
1547 
1548                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1549                                                  ((u32) scp->cmnd[3] << 16) |
1550                                                  ((u32) scp->cmnd[4] << 8) |
1551                                                  (u32) scp->cmnd[5]);
1552         }
1553 
1554         /*
1555          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1556          */
1557         else if (scp->cmd_len == 16) {
1558                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1559                                               ((u32) scp->cmnd[11] << 16) |
1560                                               ((u32) scp->cmnd[12] << 8) |
1561                                               (u32) scp->cmnd[13]);
1562 
1563                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1564                                                  ((u32) scp->cmnd[7] << 16) |
1565                                                  ((u32) scp->cmnd[8] << 8) |
1566                                                  (u32) scp->cmnd[9]);
1567 
1568                 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1569                                                  ((u32) scp->cmnd[3] << 16) |
1570                                                  ((u32) scp->cmnd[4] << 8) |
1571                                                  (u32) scp->cmnd[5]);
1572 
1573         }
1574 
1575         /*
1576          * Construct SGL
1577          */
1578         if (instance->flag_ieee) {
1579                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1580                 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1581                                               &ldio->sgl);
1582         } else if (IS_DMA64) {
1583                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1584                 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1585         } else
1586                 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1587 
1588         if (ldio->sge_count > instance->max_num_sge) {
1589                 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1590                         ldio->sge_count);
1591                 return 0;
1592         }
1593 
1594         /*
1595          * Sense info specific
1596          */
1597         ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1598         ldio->sense_buf_phys_addr_hi = 0;
1599         ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1600 
1601         /*
1602          * Compute the total number of frames this command consumes. FW uses
1603          * this number to pull sufficient number of frames from host memory.
1604          */
1605         cmd->frame_count = megasas_get_frame_count(instance,
1606                         ldio->sge_count, IO_FRAME);
1607 
1608         return cmd->frame_count;
1609 }
1610 
1611 /**
1612  * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1613  *                              and whether it's RW or non RW
1614  * @scmd:                       SCSI command
1615  *
1616  */
1617 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1618 {
1619         int ret;
1620 
1621         switch (cmd->cmnd[0]) {
1622         case READ_10:
1623         case WRITE_10:
1624         case READ_12:
1625         case WRITE_12:
1626         case READ_6:
1627         case WRITE_6:
1628         case READ_16:
1629         case WRITE_16:
1630                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1631                         READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1632                 break;
1633         default:
1634                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1635                         NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1636         }
1637         return ret;
1638 }
1639 
1640  /**
1641  * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1642  *                                      in FW
1643  * @instance:                           Adapter soft state
1644  */
1645 static inline void
1646 megasas_dump_pending_frames(struct megasas_instance *instance)
1647 {
1648         struct megasas_cmd *cmd;
1649         int i,n;
1650         union megasas_sgl *mfi_sgl;
1651         struct megasas_io_frame *ldio;
1652         struct megasas_pthru_frame *pthru;
1653         u32 sgcount;
1654         u16 max_cmd = instance->max_fw_cmds;
1655 
1656         dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1657         dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1658         if (IS_DMA64)
1659                 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1660         else
1661                 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1662 
1663         dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1664         for (i = 0; i < max_cmd; i++) {
1665                 cmd = instance->cmd_list[i];
1666                 if (!cmd->scmd)
1667                         continue;
1668                 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1669                 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1670                         ldio = (struct megasas_io_frame *)cmd->frame;
1671                         mfi_sgl = &ldio->sgl;
1672                         sgcount = ldio->sge_count;
1673                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1674                         " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1675                         instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1676                         le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1677                         le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1678                 } else {
1679                         pthru = (struct megasas_pthru_frame *) cmd->frame;
1680                         mfi_sgl = &pthru->sgl;
1681                         sgcount = pthru->sge_count;
1682                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1683                         "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1684                         instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1685                         pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1686                         le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1687                 }
1688                 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1689                         for (n = 0; n < sgcount; n++) {
1690                                 if (IS_DMA64)
1691                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1692                                                 le32_to_cpu(mfi_sgl->sge64[n].length),
1693                                                 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1694                                 else
1695                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1696                                                 le32_to_cpu(mfi_sgl->sge32[n].length),
1697                                                 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1698                         }
1699                 }
1700         } /*for max_cmd*/
1701         dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1702         for (i = 0; i < max_cmd; i++) {
1703 
1704                 cmd = instance->cmd_list[i];
1705 
1706                 if (cmd->sync_cmd == 1)
1707                         dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1708         }
1709         dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1710 }
1711 
1712 u32
1713 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1714                             struct scsi_cmnd *scmd)
1715 {
1716         struct megasas_cmd *cmd;
1717         u32 frame_count;
1718 
1719         cmd = megasas_get_cmd(instance);
1720         if (!cmd)
1721                 return SCSI_MLQUEUE_HOST_BUSY;
1722 
1723         /*
1724          * Logical drive command
1725          */
1726         if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1727                 frame_count = megasas_build_ldio(instance, scmd, cmd);
1728         else
1729                 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1730 
1731         if (!frame_count)
1732                 goto out_return_cmd;
1733 
1734         cmd->scmd = scmd;
1735         scmd->SCp.ptr = (char *)cmd;
1736 
1737         /*
1738          * Issue the command to the FW
1739          */
1740         atomic_inc(&instance->fw_outstanding);
1741 
1742         instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1743                                 cmd->frame_count-1, instance->reg_set);
1744 
1745         return 0;
1746 out_return_cmd:
1747         megasas_return_cmd(instance, cmd);
1748         return SCSI_MLQUEUE_HOST_BUSY;
1749 }
1750 
1751 
1752 /**
1753  * megasas_queue_command -      Queue entry point
1754  * @scmd:                       SCSI command to be queued
1755  * @done:                       Callback entry point
1756  */
1757 static int
1758 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1759 {
1760         struct megasas_instance *instance;
1761         struct MR_PRIV_DEVICE *mr_device_priv_data;
1762 
1763         instance = (struct megasas_instance *)
1764             scmd->device->host->hostdata;
1765 
1766         if (instance->unload == 1) {
1767                 scmd->result = DID_NO_CONNECT << 16;
1768                 scmd->scsi_done(scmd);
1769                 return 0;
1770         }
1771 
1772         if (instance->issuepend_done == 0)
1773                 return SCSI_MLQUEUE_HOST_BUSY;
1774 
1775 
1776         /* Check for an mpio path and adjust behavior */
1777         if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1778                 if (megasas_check_mpio_paths(instance, scmd) ==
1779                     (DID_REQUEUE << 16)) {
1780                         return SCSI_MLQUEUE_HOST_BUSY;
1781                 } else {
1782                         scmd->result = DID_NO_CONNECT << 16;
1783                         scmd->scsi_done(scmd);
1784                         return 0;
1785                 }
1786         }
1787 
1788         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1789                 scmd->result = DID_NO_CONNECT << 16;
1790                 scmd->scsi_done(scmd);
1791                 return 0;
1792         }
1793 
1794         mr_device_priv_data = scmd->device->hostdata;
1795         if (!mr_device_priv_data) {
1796                 scmd->result = DID_NO_CONNECT << 16;
1797                 scmd->scsi_done(scmd);
1798                 return 0;
1799         }
1800 
1801         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1802                 return SCSI_MLQUEUE_HOST_BUSY;
1803 
1804         if (mr_device_priv_data->tm_busy)
1805                 return SCSI_MLQUEUE_DEVICE_BUSY;
1806 
1807 
1808         scmd->result = 0;
1809 
1810         if (MEGASAS_IS_LOGICAL(scmd->device) &&
1811             (scmd->device->id >= instance->fw_supported_vd_count ||
1812                 scmd->device->lun)) {
1813                 scmd->result = DID_BAD_TARGET << 16;
1814                 goto out_done;
1815         }
1816 
1817         if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1818             MEGASAS_IS_LOGICAL(scmd->device) &&
1819             (!instance->fw_sync_cache_support)) {
1820                 scmd->result = DID_OK << 16;
1821                 goto out_done;
1822         }
1823 
1824         return instance->instancet->build_and_issue_cmd(instance, scmd);
1825 
1826  out_done:
1827         scmd->scsi_done(scmd);
1828         return 0;
1829 }
1830 
1831 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1832 {
1833         int i;
1834 
1835         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1836 
1837                 if ((megasas_mgmt_info.instance[i]) &&
1838                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1839                         return megasas_mgmt_info.instance[i];
1840         }
1841 
1842         return NULL;
1843 }
1844 
1845 /*
1846 * megasas_set_dynamic_target_properties -
1847 * Device property set by driver may not be static and it is required to be
1848 * updated after OCR
1849 *
1850 * set tm_capable.
1851 * set dma alignment (only for eedp protection enable vd).
1852 *
1853 * @sdev: OS provided scsi device
1854 *
1855 * Returns void
1856 */
1857 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1858                                            bool is_target_prop)
1859 {
1860         u16 pd_index = 0, ld;
1861         u32 device_id;
1862         struct megasas_instance *instance;
1863         struct fusion_context *fusion;
1864         struct MR_PRIV_DEVICE *mr_device_priv_data;
1865         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1866         struct MR_LD_RAID *raid;
1867         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1868 
1869         instance = megasas_lookup_instance(sdev->host->host_no);
1870         fusion = instance->ctrl_context;
1871         mr_device_priv_data = sdev->hostdata;
1872 
1873         if (!fusion || !mr_device_priv_data)
1874                 return;
1875 
1876         if (MEGASAS_IS_LOGICAL(sdev)) {
1877                 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1878                                         + sdev->id;
1879                 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1880                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1881                 if (ld >= instance->fw_supported_vd_count)
1882                         return;
1883                 raid = MR_LdRaidGet(ld, local_map_ptr);
1884 
1885                 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1886                 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1887 
1888                 mr_device_priv_data->is_tm_capable =
1889                         raid->capability.tmCapable;
1890         } else if (instance->use_seqnum_jbod_fp) {
1891                 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1892                         sdev->id;
1893                 pd_sync = (void *)fusion->pd_seq_sync
1894                                 [(instance->pd_seq_map_id - 1) & 1];
1895                 mr_device_priv_data->is_tm_capable =
1896                         pd_sync->seq[pd_index].capability.tmCapable;
1897         }
1898 
1899         if (is_target_prop && instance->tgt_prop->reset_tmo) {
1900                 /*
1901                  * If FW provides a target reset timeout value, driver will use
1902                  * it. If not set, fallback to default values.
1903                  */
1904                 mr_device_priv_data->target_reset_tmo =
1905                         min_t(u8, instance->max_reset_tmo,
1906                               instance->tgt_prop->reset_tmo);
1907                 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1908         } else {
1909                 mr_device_priv_data->target_reset_tmo =
1910                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1911                 mr_device_priv_data->task_abort_tmo =
1912                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1913         }
1914 }
1915 
1916 /*
1917  * megasas_set_nvme_device_properties -
1918  * set nomerges=2
1919  * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1920  * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1921  *
1922  * MR firmware provides value in KB. Caller of this function converts
1923  * kb into bytes.
1924  *
1925  * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1926  * MR firmware provides value 128 as (32 * 4K) = 128K.
1927  *
1928  * @sdev:                               scsi device
1929  * @max_io_size:                                maximum io transfer size
1930  *
1931  */
1932 static inline void
1933 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1934 {
1935         struct megasas_instance *instance;
1936         u32 mr_nvme_pg_size;
1937 
1938         instance = (struct megasas_instance *)sdev->host->hostdata;
1939         mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1940                                 MR_DEFAULT_NVME_PAGE_SIZE);
1941 
1942         blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1943 
1944         blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1945         blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1946 }
1947 
1948 /*
1949  * megasas_set_fw_assisted_qd -
1950  * set device queue depth to can_queue
1951  * set device queue depth to fw assisted qd
1952  *
1953  * @sdev:                               scsi device
1954  * @is_target_prop                      true, if fw provided target properties.
1955  */
1956 static void megasas_set_fw_assisted_qd(struct scsi_device *sdev,
1957                                                  bool is_target_prop)
1958 {
1959         u8 interface_type;
1960         u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1961         u32 tgt_device_qd;
1962         struct megasas_instance *instance;
1963         struct MR_PRIV_DEVICE *mr_device_priv_data;
1964 
1965         instance = megasas_lookup_instance(sdev->host->host_no);
1966         mr_device_priv_data = sdev->hostdata;
1967         interface_type  = mr_device_priv_data->interface_type;
1968 
1969         switch (interface_type) {
1970         case SAS_PD:
1971                 device_qd = MEGASAS_SAS_QD;
1972                 break;
1973         case SATA_PD:
1974                 device_qd = MEGASAS_SATA_QD;
1975                 break;
1976         case NVME_PD:
1977                 device_qd = MEGASAS_NVME_QD;
1978                 break;
1979         }
1980 
1981         if (is_target_prop) {
1982                 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1983                 if (tgt_device_qd &&
1984                     (tgt_device_qd <= instance->host->can_queue))
1985                         device_qd = tgt_device_qd;
1986         }
1987 
1988         if (instance->enable_sdev_max_qd && interface_type != UNKNOWN_DRIVE)
1989                 device_qd = instance->host->can_queue;
1990 
1991         scsi_change_queue_depth(sdev, device_qd);
1992 }
1993 
1994 /*
1995  * megasas_set_static_target_properties -
1996  * Device property set by driver are static and it is not required to be
1997  * updated after OCR.
1998  *
1999  * set io timeout
2000  * set device queue depth
2001  * set nvme device properties. see - megasas_set_nvme_device_properties
2002  *
2003  * @sdev:                               scsi device
2004  * @is_target_prop                      true, if fw provided target properties.
2005  */
2006 static void megasas_set_static_target_properties(struct scsi_device *sdev,
2007                                                  bool is_target_prop)
2008 {
2009         u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
2010         struct megasas_instance *instance;
2011 
2012         instance = megasas_lookup_instance(sdev->host->host_no);
2013 
2014         /*
2015          * The RAID firmware may require extended timeouts.
2016          */
2017         blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
2018 
2019         /* max_io_size_kb will be set to non zero for
2020          * nvme based vd and syspd.
2021          */
2022         if (is_target_prop)
2023                 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
2024 
2025         if (instance->nvme_page_size && max_io_size_kb)
2026                 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2027 
2028         megasas_set_fw_assisted_qd(sdev, is_target_prop);
2029 }
2030 
2031 
2032 static int megasas_slave_configure(struct scsi_device *sdev)
2033 {
2034         u16 pd_index = 0;
2035         struct megasas_instance *instance;
2036         int ret_target_prop = DCMD_FAILED;
2037         bool is_target_prop = false;
2038 
2039         instance = megasas_lookup_instance(sdev->host->host_no);
2040         if (instance->pd_list_not_supported) {
2041                 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2042                         pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2043                                 sdev->id;
2044                         if (instance->pd_list[pd_index].driveState !=
2045                                 MR_PD_STATE_SYSTEM)
2046                                 return -ENXIO;
2047                 }
2048         }
2049 
2050         mutex_lock(&instance->reset_mutex);
2051         /* Send DCMD to Firmware and cache the information */
2052         if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2053                 megasas_get_pd_info(instance, sdev);
2054 
2055         /* Some ventura firmware may not have instance->nvme_page_size set.
2056          * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2057          */
2058         if ((instance->tgt_prop) && (instance->nvme_page_size))
2059                 ret_target_prop = megasas_get_target_prop(instance, sdev);
2060 
2061         is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2062         megasas_set_static_target_properties(sdev, is_target_prop);
2063 
2064         /* This sdev property may change post OCR */
2065         megasas_set_dynamic_target_properties(sdev, is_target_prop);
2066 
2067         mutex_unlock(&instance->reset_mutex);
2068 
2069         return 0;
2070 }
2071 
2072 static int megasas_slave_alloc(struct scsi_device *sdev)
2073 {
2074         u16 pd_index = 0;
2075         struct megasas_instance *instance ;
2076         struct MR_PRIV_DEVICE *mr_device_priv_data;
2077 
2078         instance = megasas_lookup_instance(sdev->host->host_no);
2079         if (!MEGASAS_IS_LOGICAL(sdev)) {
2080                 /*
2081                  * Open the OS scan to the SYSTEM PD
2082                  */
2083                 pd_index =
2084                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2085                         sdev->id;
2086                 if ((instance->pd_list_not_supported ||
2087                         instance->pd_list[pd_index].driveState ==
2088                         MR_PD_STATE_SYSTEM)) {
2089                         goto scan_target;
2090                 }
2091                 return -ENXIO;
2092         }
2093 
2094 scan_target:
2095         mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2096                                         GFP_KERNEL);
2097         if (!mr_device_priv_data)
2098                 return -ENOMEM;
2099         sdev->hostdata = mr_device_priv_data;
2100 
2101         atomic_set(&mr_device_priv_data->r1_ldio_hint,
2102                    instance->r1_ldio_hint_default);
2103         return 0;
2104 }
2105 
2106 static void megasas_slave_destroy(struct scsi_device *sdev)
2107 {
2108         kfree(sdev->hostdata);
2109         sdev->hostdata = NULL;
2110 }
2111 
2112 /*
2113 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2114 *                                       kill adapter
2115 * @instance:                            Adapter soft state
2116 *
2117 */
2118 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2119 {
2120         int i;
2121         struct megasas_cmd *cmd_mfi;
2122         struct megasas_cmd_fusion *cmd_fusion;
2123         struct fusion_context *fusion = instance->ctrl_context;
2124 
2125         /* Find all outstanding ioctls */
2126         if (fusion) {
2127                 for (i = 0; i < instance->max_fw_cmds; i++) {
2128                         cmd_fusion = fusion->cmd_list[i];
2129                         if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2130                                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2131                                 if (cmd_mfi->sync_cmd &&
2132                                     (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2133                                         cmd_mfi->frame->hdr.cmd_status =
2134                                                         MFI_STAT_WRONG_STATE;
2135                                         megasas_complete_cmd(instance,
2136                                                              cmd_mfi, DID_OK);
2137                                 }
2138                         }
2139                 }
2140         } else {
2141                 for (i = 0; i < instance->max_fw_cmds; i++) {
2142                         cmd_mfi = instance->cmd_list[i];
2143                         if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2144                                 MFI_CMD_ABORT)
2145                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2146                 }
2147         }
2148 }
2149 
2150 
2151 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2152 {
2153         /* Set critical error to block I/O & ioctls in case caller didn't */
2154         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2155         /* Wait 1 second to ensure IO or ioctls in build have posted */
2156         msleep(1000);
2157         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2158                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2159                 (instance->adapter_type != MFI_SERIES)) {
2160                 if (!instance->requestorId) {
2161                         writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2162                         /* Flush */
2163                         readl(&instance->reg_set->doorbell);
2164                 }
2165                 if (instance->requestorId && instance->peerIsPresent)
2166                         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2167         } else {
2168                 writel(MFI_STOP_ADP,
2169                         &instance->reg_set->inbound_doorbell);
2170         }
2171         /* Complete outstanding ioctls when adapter is killed */
2172         megasas_complete_outstanding_ioctls(instance);
2173 }
2174 
2175  /**
2176   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2177   *                                     restored to max value
2178   * @instance:                  Adapter soft state
2179   *
2180   */
2181 void
2182 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2183 {
2184         unsigned long flags;
2185 
2186         if (instance->flag & MEGASAS_FW_BUSY
2187             && time_after(jiffies, instance->last_time + 5 * HZ)
2188             && atomic_read(&instance->fw_outstanding) <
2189             instance->throttlequeuedepth + 1) {
2190 
2191                 spin_lock_irqsave(instance->host->host_lock, flags);
2192                 instance->flag &= ~MEGASAS_FW_BUSY;
2193 
2194                 instance->host->can_queue = instance->cur_can_queue;
2195                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2196         }
2197 }
2198 
2199 /**
2200  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
2201  * @instance_addr:                      Address of adapter soft state
2202  *
2203  * Tasklet to complete cmds
2204  */
2205 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2206 {
2207         u32 producer;
2208         u32 consumer;
2209         u32 context;
2210         struct megasas_cmd *cmd;
2211         struct megasas_instance *instance =
2212                                 (struct megasas_instance *)instance_addr;
2213         unsigned long flags;
2214 
2215         /* If we have already declared adapter dead, donot complete cmds */
2216         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2217                 return;
2218 
2219         spin_lock_irqsave(&instance->completion_lock, flags);
2220 
2221         producer = le32_to_cpu(*instance->producer);
2222         consumer = le32_to_cpu(*instance->consumer);
2223 
2224         while (consumer != producer) {
2225                 context = le32_to_cpu(instance->reply_queue[consumer]);
2226                 if (context >= instance->max_fw_cmds) {
2227                         dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2228                                 context);
2229                         BUG();
2230                 }
2231 
2232                 cmd = instance->cmd_list[context];
2233 
2234                 megasas_complete_cmd(instance, cmd, DID_OK);
2235 
2236                 consumer++;
2237                 if (consumer == (instance->max_fw_cmds + 1)) {
2238                         consumer = 0;
2239                 }
2240         }
2241 
2242         *instance->consumer = cpu_to_le32(producer);
2243 
2244         spin_unlock_irqrestore(&instance->completion_lock, flags);
2245 
2246         /*
2247          * Check if we can restore can_queue
2248          */
2249         megasas_check_and_restore_queue_depth(instance);
2250 }
2251 
2252 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2253 
2254 /**
2255  * megasas_start_timer - Initializes sriov heartbeat timer object
2256  * @instance:           Adapter soft state
2257  *
2258  */
2259 void megasas_start_timer(struct megasas_instance *instance)
2260 {
2261         struct timer_list *timer = &instance->sriov_heartbeat_timer;
2262 
2263         timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2264         timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2265         add_timer(timer);
2266 }
2267 
2268 static void
2269 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2270 
2271 static void
2272 process_fw_state_change_wq(struct work_struct *work);
2273 
2274 static void megasas_do_ocr(struct megasas_instance *instance)
2275 {
2276         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2277         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2278         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2279                 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2280         }
2281         instance->instancet->disable_intr(instance);
2282         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2283         instance->issuepend_done = 0;
2284 
2285         atomic_set(&instance->fw_outstanding, 0);
2286         megasas_internal_reset_defer_cmds(instance);
2287         process_fw_state_change_wq(&instance->work_init);
2288 }
2289 
2290 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2291                                             int initial)
2292 {
2293         struct megasas_cmd *cmd;
2294         struct megasas_dcmd_frame *dcmd;
2295         struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2296         dma_addr_t new_affiliation_111_h;
2297         int ld, retval = 0;
2298         u8 thisVf;
2299 
2300         cmd = megasas_get_cmd(instance);
2301 
2302         if (!cmd) {
2303                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2304                        "Failed to get cmd for scsi%d\n",
2305                         instance->host->host_no);
2306                 return -ENOMEM;
2307         }
2308 
2309         dcmd = &cmd->frame->dcmd;
2310 
2311         if (!instance->vf_affiliation_111) {
2312                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2313                        "affiliation for scsi%d\n", instance->host->host_no);
2314                 megasas_return_cmd(instance, cmd);
2315                 return -ENOMEM;
2316         }
2317 
2318         if (initial)
2319                         memset(instance->vf_affiliation_111, 0,
2320                                sizeof(struct MR_LD_VF_AFFILIATION_111));
2321         else {
2322                 new_affiliation_111 =
2323                         dma_alloc_coherent(&instance->pdev->dev,
2324                                            sizeof(struct MR_LD_VF_AFFILIATION_111),
2325                                            &new_affiliation_111_h, GFP_KERNEL);
2326                 if (!new_affiliation_111) {
2327                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2328                                "memory for new affiliation for scsi%d\n",
2329                                instance->host->host_no);
2330                         megasas_return_cmd(instance, cmd);
2331                         return -ENOMEM;
2332                 }
2333         }
2334 
2335         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2336 
2337         dcmd->cmd = MFI_CMD_DCMD;
2338         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2339         dcmd->sge_count = 1;
2340         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2341         dcmd->timeout = 0;
2342         dcmd->pad_0 = 0;
2343         dcmd->data_xfer_len =
2344                 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2345         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2346 
2347         if (initial)
2348                 dcmd->sgl.sge32[0].phys_addr =
2349                         cpu_to_le32(instance->vf_affiliation_111_h);
2350         else
2351                 dcmd->sgl.sge32[0].phys_addr =
2352                         cpu_to_le32(new_affiliation_111_h);
2353 
2354         dcmd->sgl.sge32[0].length = cpu_to_le32(
2355                 sizeof(struct MR_LD_VF_AFFILIATION_111));
2356 
2357         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2358                "scsi%d\n", instance->host->host_no);
2359 
2360         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2361                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2362                        " failed with status 0x%x for scsi%d\n",
2363                        dcmd->cmd_status, instance->host->host_no);
2364                 retval = 1; /* Do a scan if we couldn't get affiliation */
2365                 goto out;
2366         }
2367 
2368         if (!initial) {
2369                 thisVf = new_affiliation_111->thisVf;
2370                 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2371                         if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2372                             new_affiliation_111->map[ld].policy[thisVf]) {
2373                                 dev_warn(&instance->pdev->dev, "SR-IOV: "
2374                                        "Got new LD/VF affiliation for scsi%d\n",
2375                                        instance->host->host_no);
2376                                 memcpy(instance->vf_affiliation_111,
2377                                        new_affiliation_111,
2378                                        sizeof(struct MR_LD_VF_AFFILIATION_111));
2379                                 retval = 1;
2380                                 goto out;
2381                         }
2382         }
2383 out:
2384         if (new_affiliation_111) {
2385                 dma_free_coherent(&instance->pdev->dev,
2386                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
2387                                     new_affiliation_111,
2388                                     new_affiliation_111_h);
2389         }
2390 
2391         megasas_return_cmd(instance, cmd);
2392 
2393         return retval;
2394 }
2395 
2396 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2397                                             int initial)
2398 {
2399         struct megasas_cmd *cmd;
2400         struct megasas_dcmd_frame *dcmd;
2401         struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2402         struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2403         dma_addr_t new_affiliation_h;
2404         int i, j, retval = 0, found = 0, doscan = 0;
2405         u8 thisVf;
2406 
2407         cmd = megasas_get_cmd(instance);
2408 
2409         if (!cmd) {
2410                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2411                        "Failed to get cmd for scsi%d\n",
2412                        instance->host->host_no);
2413                 return -ENOMEM;
2414         }
2415 
2416         dcmd = &cmd->frame->dcmd;
2417 
2418         if (!instance->vf_affiliation) {
2419                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2420                        "affiliation for scsi%d\n", instance->host->host_no);
2421                 megasas_return_cmd(instance, cmd);
2422                 return -ENOMEM;
2423         }
2424 
2425         if (initial)
2426                 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2427                        sizeof(struct MR_LD_VF_AFFILIATION));
2428         else {
2429                 new_affiliation =
2430                         dma_alloc_coherent(&instance->pdev->dev,
2431                                            (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2432                                            &new_affiliation_h, GFP_KERNEL);
2433                 if (!new_affiliation) {
2434                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2435                                "memory for new affiliation for scsi%d\n",
2436                                instance->host->host_no);
2437                         megasas_return_cmd(instance, cmd);
2438                         return -ENOMEM;
2439                 }
2440         }
2441 
2442         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2443 
2444         dcmd->cmd = MFI_CMD_DCMD;
2445         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2446         dcmd->sge_count = 1;
2447         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2448         dcmd->timeout = 0;
2449         dcmd->pad_0 = 0;
2450         dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2451                 sizeof(struct MR_LD_VF_AFFILIATION));
2452         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2453 
2454         if (initial)
2455                 dcmd->sgl.sge32[0].phys_addr =
2456                         cpu_to_le32(instance->vf_affiliation_h);
2457         else
2458                 dcmd->sgl.sge32[0].phys_addr =
2459                         cpu_to_le32(new_affiliation_h);
2460 
2461         dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2462                 sizeof(struct MR_LD_VF_AFFILIATION));
2463 
2464         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2465                "scsi%d\n", instance->host->host_no);
2466 
2467 
2468         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2469                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2470                        " failed with status 0x%x for scsi%d\n",
2471                        dcmd->cmd_status, instance->host->host_no);
2472                 retval = 1; /* Do a scan if we couldn't get affiliation */
2473                 goto out;
2474         }
2475 
2476         if (!initial) {
2477                 if (!new_affiliation->ldCount) {
2478                         dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2479                                "affiliation for passive path for scsi%d\n",
2480                                instance->host->host_no);
2481                         retval = 1;
2482                         goto out;
2483                 }
2484                 newmap = new_affiliation->map;
2485                 savedmap = instance->vf_affiliation->map;
2486                 thisVf = new_affiliation->thisVf;
2487                 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2488                         found = 0;
2489                         for (j = 0; j < instance->vf_affiliation->ldCount;
2490                              j++) {
2491                                 if (newmap->ref.targetId ==
2492                                     savedmap->ref.targetId) {
2493                                         found = 1;
2494                                         if (newmap->policy[thisVf] !=
2495                                             savedmap->policy[thisVf]) {
2496                                                 doscan = 1;
2497                                                 goto out;
2498                                         }
2499                                 }
2500                                 savedmap = (struct MR_LD_VF_MAP *)
2501                                         ((unsigned char *)savedmap +
2502                                          savedmap->size);
2503                         }
2504                         if (!found && newmap->policy[thisVf] !=
2505                             MR_LD_ACCESS_HIDDEN) {
2506                                 doscan = 1;
2507                                 goto out;
2508                         }
2509                         newmap = (struct MR_LD_VF_MAP *)
2510                                 ((unsigned char *)newmap + newmap->size);
2511                 }
2512 
2513                 newmap = new_affiliation->map;
2514                 savedmap = instance->vf_affiliation->map;
2515 
2516                 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2517                         found = 0;
2518                         for (j = 0 ; j < new_affiliation->ldCount; j++) {
2519                                 if (savedmap->ref.targetId ==
2520                                     newmap->ref.targetId) {
2521                                         found = 1;
2522                                         if (savedmap->policy[thisVf] !=
2523                                             newmap->policy[thisVf]) {
2524                                                 doscan = 1;
2525                                                 goto out;
2526                                         }
2527                                 }
2528                                 newmap = (struct MR_LD_VF_MAP *)
2529                                         ((unsigned char *)newmap +
2530                                          newmap->size);
2531                         }
2532                         if (!found && savedmap->policy[thisVf] !=
2533                             MR_LD_ACCESS_HIDDEN) {
2534                                 doscan = 1;
2535                                 goto out;
2536                         }
2537                         savedmap = (struct MR_LD_VF_MAP *)
2538                                 ((unsigned char *)savedmap +
2539                                  savedmap->size);
2540                 }
2541         }
2542 out:
2543         if (doscan) {
2544                 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2545                        "affiliation for scsi%d\n", instance->host->host_no);
2546                 memcpy(instance->vf_affiliation, new_affiliation,
2547                        new_affiliation->size);
2548                 retval = 1;
2549         }
2550 
2551         if (new_affiliation)
2552                 dma_free_coherent(&instance->pdev->dev,
2553                                     (MAX_LOGICAL_DRIVES + 1) *
2554                                     sizeof(struct MR_LD_VF_AFFILIATION),
2555                                     new_affiliation, new_affiliation_h);
2556         megasas_return_cmd(instance, cmd);
2557 
2558         return retval;
2559 }
2560 
2561 /* This function will get the current SR-IOV LD/VF affiliation */
2562 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2563         int initial)
2564 {
2565         int retval;
2566 
2567         if (instance->PlasmaFW111)
2568                 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2569         else
2570                 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2571         return retval;
2572 }
2573 
2574 /* This function will tell FW to start the SR-IOV heartbeat */
2575 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2576                                          int initial)
2577 {
2578         struct megasas_cmd *cmd;
2579         struct megasas_dcmd_frame *dcmd;
2580         int retval = 0;
2581 
2582         cmd = megasas_get_cmd(instance);
2583 
2584         if (!cmd) {
2585                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2586                        "Failed to get cmd for scsi%d\n",
2587                        instance->host->host_no);
2588                 return -ENOMEM;
2589         }
2590 
2591         dcmd = &cmd->frame->dcmd;
2592 
2593         if (initial) {
2594                 instance->hb_host_mem =
2595                         dma_alloc_coherent(&instance->pdev->dev,
2596                                            sizeof(struct MR_CTRL_HB_HOST_MEM),
2597                                            &instance->hb_host_mem_h,
2598                                            GFP_KERNEL);
2599                 if (!instance->hb_host_mem) {
2600                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2601                                " memory for heartbeat host memory for scsi%d\n",
2602                                instance->host->host_no);
2603                         retval = -ENOMEM;
2604                         goto out;
2605                 }
2606         }
2607 
2608         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2609 
2610         dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2611         dcmd->cmd = MFI_CMD_DCMD;
2612         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2613         dcmd->sge_count = 1;
2614         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2615         dcmd->timeout = 0;
2616         dcmd->pad_0 = 0;
2617         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2618         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2619 
2620         megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2621                                  sizeof(struct MR_CTRL_HB_HOST_MEM));
2622 
2623         dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2624                instance->host->host_no);
2625 
2626         if ((instance->adapter_type != MFI_SERIES) &&
2627             !instance->mask_interrupts)
2628                 retval = megasas_issue_blocked_cmd(instance, cmd,
2629                         MEGASAS_ROUTINE_WAIT_TIME_VF);
2630         else
2631                 retval = megasas_issue_polled(instance, cmd);
2632 
2633         if (retval) {
2634                 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2635                         "_MEM_ALLOC DCMD %s for scsi%d\n",
2636                         (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2637                         "timed out" : "failed", instance->host->host_no);
2638                 retval = 1;
2639         }
2640 
2641 out:
2642         megasas_return_cmd(instance, cmd);
2643 
2644         return retval;
2645 }
2646 
2647 /* Handler for SR-IOV heartbeat */
2648 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2649 {
2650         struct megasas_instance *instance =
2651                 from_timer(instance, t, sriov_heartbeat_timer);
2652 
2653         if (instance->hb_host_mem->HB.fwCounter !=
2654             instance->hb_host_mem->HB.driverCounter) {
2655                 instance->hb_host_mem->HB.driverCounter =
2656                         instance->hb_host_mem->HB.fwCounter;
2657                 mod_timer(&instance->sriov_heartbeat_timer,
2658                           jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2659         } else {
2660                 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2661                        "completed for scsi%d\n", instance->host->host_no);
2662                 schedule_work(&instance->work_init);
2663         }
2664 }
2665 
2666 /**
2667  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2668  * @instance:                           Adapter soft state
2669  *
2670  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2671  * complete all its outstanding commands. Returns error if one or more IOs
2672  * are pending after this time period. It also marks the controller dead.
2673  */
2674 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2675 {
2676         int i, sl, outstanding;
2677         u32 reset_index;
2678         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2679         unsigned long flags;
2680         struct list_head clist_local;
2681         struct megasas_cmd *reset_cmd;
2682         u32 fw_state;
2683 
2684         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2685                 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2686                 __func__, __LINE__);
2687                 return FAILED;
2688         }
2689 
2690         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2691 
2692                 INIT_LIST_HEAD(&clist_local);
2693                 spin_lock_irqsave(&instance->hba_lock, flags);
2694                 list_splice_init(&instance->internal_reset_pending_q,
2695                                 &clist_local);
2696                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2697 
2698                 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2699                 for (i = 0; i < wait_time; i++) {
2700                         msleep(1000);
2701                         if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2702                                 break;
2703                 }
2704 
2705                 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2706                         dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2707                         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2708                         return FAILED;
2709                 }
2710 
2711                 reset_index = 0;
2712                 while (!list_empty(&clist_local)) {
2713                         reset_cmd = list_entry((&clist_local)->next,
2714                                                 struct megasas_cmd, list);
2715                         list_del_init(&reset_cmd->list);
2716                         if (reset_cmd->scmd) {
2717                                 reset_cmd->scmd->result = DID_REQUEUE << 16;
2718                                 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2719                                         reset_index, reset_cmd,
2720                                         reset_cmd->scmd->cmnd[0]);
2721 
2722                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2723                                 megasas_return_cmd(instance, reset_cmd);
2724                         } else if (reset_cmd->sync_cmd) {
2725                                 dev_notice(&instance->pdev->dev, "%p synch cmds"
2726                                                 "reset queue\n",
2727                                                 reset_cmd);
2728 
2729                                 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2730                                 instance->instancet->fire_cmd(instance,
2731                                                 reset_cmd->frame_phys_addr,
2732                                                 0, instance->reg_set);
2733                         } else {
2734                                 dev_notice(&instance->pdev->dev, "%p unexpected"
2735                                         "cmds lst\n",
2736                                         reset_cmd);
2737                         }
2738                         reset_index++;
2739                 }
2740 
2741                 return SUCCESS;
2742         }
2743 
2744         for (i = 0; i < resetwaittime; i++) {
2745                 outstanding = atomic_read(&instance->fw_outstanding);
2746 
2747                 if (!outstanding)
2748                         break;
2749 
2750                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2751                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2752                                "commands to complete\n",i,outstanding);
2753                         /*
2754                          * Call cmd completion routine. Cmd to be
2755                          * be completed directly without depending on isr.
2756                          */
2757                         megasas_complete_cmd_dpc((unsigned long)instance);
2758                 }
2759 
2760                 msleep(1000);
2761         }
2762 
2763         i = 0;
2764         outstanding = atomic_read(&instance->fw_outstanding);
2765         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2766 
2767         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2768                 goto no_outstanding;
2769 
2770         if (instance->disableOnlineCtrlReset)
2771                 goto kill_hba_and_failed;
2772         do {
2773                 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2774                         dev_info(&instance->pdev->dev,
2775                                 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2776                                 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2777                         if (i == 3)
2778                                 goto kill_hba_and_failed;
2779                         megasas_do_ocr(instance);
2780 
2781                         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2782                                 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2783                                 __func__, __LINE__);
2784                                 return FAILED;
2785                         }
2786                         dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2787                                 __func__, __LINE__);
2788 
2789                         for (sl = 0; sl < 10; sl++)
2790                                 msleep(500);
2791 
2792                         outstanding = atomic_read(&instance->fw_outstanding);
2793 
2794                         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2795                         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2796                                 goto no_outstanding;
2797                 }
2798                 i++;
2799         } while (i <= 3);
2800 
2801 no_outstanding:
2802 
2803         dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2804                 __func__, __LINE__);
2805         return SUCCESS;
2806 
2807 kill_hba_and_failed:
2808 
2809         /* Reset not supported, kill adapter */
2810         dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2811                 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2812                 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2813                 atomic_read(&instance->fw_outstanding));
2814         megasas_dump_pending_frames(instance);
2815         megaraid_sas_kill_hba(instance);
2816 
2817         return FAILED;
2818 }
2819 
2820 /**
2821  * megasas_generic_reset -      Generic reset routine
2822  * @scmd:                       Mid-layer SCSI command
2823  *
2824  * This routine implements a generic reset handler for device, bus and host
2825  * reset requests. Device, bus and host specific reset handlers can use this
2826  * function after they do their specific tasks.
2827  */
2828 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2829 {
2830         int ret_val;
2831         struct megasas_instance *instance;
2832 
2833         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2834 
2835         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2836                  scmd->cmnd[0], scmd->retries);
2837 
2838         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2839                 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2840                 return FAILED;
2841         }
2842 
2843         ret_val = megasas_wait_for_outstanding(instance);
2844         if (ret_val == SUCCESS)
2845                 dev_notice(&instance->pdev->dev, "reset successful\n");
2846         else
2847                 dev_err(&instance->pdev->dev, "failed to do reset\n");
2848 
2849         return ret_val;
2850 }
2851 
2852 /**
2853  * megasas_reset_timer - quiesce the adapter if required
2854  * @scmd:               scsi cmnd
2855  *
2856  * Sets the FW busy flag and reduces the host->can_queue if the
2857  * cmd has not been completed within the timeout period.
2858  */
2859 static enum
2860 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2861 {
2862         struct megasas_instance *instance;
2863         unsigned long flags;
2864 
2865         if (time_after(jiffies, scmd->jiffies_at_alloc +
2866                                 (scmd_timeout * 2) * HZ)) {
2867                 return BLK_EH_DONE;
2868         }
2869 
2870         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2871         if (!(instance->flag & MEGASAS_FW_BUSY)) {
2872                 /* FW is busy, throttle IO */
2873                 spin_lock_irqsave(instance->host->host_lock, flags);
2874 
2875                 instance->host->can_queue = instance->throttlequeuedepth;
2876                 instance->last_time = jiffies;
2877                 instance->flag |= MEGASAS_FW_BUSY;
2878 
2879                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2880         }
2881         return BLK_EH_RESET_TIMER;
2882 }
2883 
2884 /**
2885  * megasas_dump -       This function will print hexdump of provided buffer.
2886  * @buf:                Buffer to be dumped
2887  * @sz:         Size in bytes
2888  * @format:             Different formats of dumping e.g. format=n will
2889  *                      cause only 'n' 32 bit words to be dumped in a single
2890  *                      line.
2891  */
2892 inline void
2893 megasas_dump(void *buf, int sz, int format)
2894 {
2895         int i;
2896         __le32 *buf_loc = (__le32 *)buf;
2897 
2898         for (i = 0; i < (sz / sizeof(__le32)); i++) {
2899                 if ((i % format) == 0) {
2900                         if (i != 0)
2901                                 printk(KERN_CONT "\n");
2902                         printk(KERN_CONT "%08x: ", (i * 4));
2903                 }
2904                 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2905         }
2906         printk(KERN_CONT "\n");
2907 }
2908 
2909 /**
2910  * megasas_dump_reg_set -       This function will print hexdump of register set
2911  * @buf:                        Buffer to be dumped
2912  * @sz:                         Size in bytes
2913  * @format:                     Different formats of dumping e.g. format=n will
2914  *                              cause only 'n' 32 bit words to be dumped in a
2915  *                              single line.
2916  */
2917 inline void
2918 megasas_dump_reg_set(void __iomem *reg_set)
2919 {
2920         unsigned int i, sz = 256;
2921         u32 __iomem *reg = (u32 __iomem *)reg_set;
2922 
2923         for (i = 0; i < (sz / sizeof(u32)); i++)
2924                 printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2925 }
2926 
2927 /**
2928  * megasas_dump_fusion_io -     This function will print key details
2929  *                              of SCSI IO
2930  * @scmd:                       SCSI command pointer of SCSI IO
2931  */
2932 void
2933 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2934 {
2935         struct megasas_cmd_fusion *cmd;
2936         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2937         struct megasas_instance *instance;
2938 
2939         cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2940         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2941 
2942         scmd_printk(KERN_INFO, scmd,
2943                     "scmd: (0x%p)  retries: 0x%x  allowed: 0x%x\n",
2944                     scmd, scmd->retries, scmd->allowed);
2945         scsi_print_command(scmd);
2946 
2947         if (cmd) {
2948                 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2949                 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2950                 scmd_printk(KERN_INFO, scmd,
2951                             "RequestFlags:0x%x  MSIxIndex:0x%x  SMID:0x%x  LMID:0x%x  DevHandle:0x%x\n",
2952                             req_desc->SCSIIO.RequestFlags,
2953                             req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2954                             req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2955 
2956                 printk(KERN_INFO "IO request frame:\n");
2957                 megasas_dump(cmd->io_request,
2958                              MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
2959                 printk(KERN_INFO "Chain frame:\n");
2960                 megasas_dump(cmd->sg_frame,
2961                              instance->max_chain_frame_sz, 8);
2962         }
2963 
2964 }
2965 
2966 /*
2967  * megasas_dump_sys_regs - This function will dump system registers through
2968  *                          sysfs.
2969  * @reg_set:                Pointer to System register set.
2970  * @buf:                    Buffer to which output is to be written.
2971  * @return:                 Number of bytes written to buffer.
2972  */
2973 static inline ssize_t
2974 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2975 {
2976         unsigned int i, sz = 256;
2977         int bytes_wrote = 0;
2978         char *loc = (char *)buf;
2979         u32 __iomem *reg = (u32 __iomem *)reg_set;
2980 
2981         for (i = 0; i < sz / sizeof(u32); i++) {
2982                 bytes_wrote += snprintf(loc + bytes_wrote, PAGE_SIZE,
2983                                         "%08x: %08x\n", (i * 4),
2984                                         readl(&reg[i]));
2985         }
2986         return bytes_wrote;
2987 }
2988 
2989 /**
2990  * megasas_reset_bus_host -     Bus & host reset handler entry point
2991  */
2992 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2993 {
2994         int ret;
2995         struct megasas_instance *instance;
2996 
2997         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2998 
2999         scmd_printk(KERN_INFO, scmd,
3000                 "OCR is requested due to IO timeout!!\n");
3001 
3002         scmd_printk(KERN_INFO, scmd,
3003                 "SCSI host state: %d  SCSI host busy: %d  FW outstanding: %d\n",
3004                 scmd->device->host->shost_state,
3005                 scsi_host_busy(scmd->device->host),
3006                 atomic_read(&instance->fw_outstanding));
3007         /*
3008          * First wait for all commands to complete
3009          */
3010         if (instance->adapter_type == MFI_SERIES) {
3011                 ret = megasas_generic_reset(scmd);
3012         } else {
3013                 megasas_dump_fusion_io(scmd);
3014                 ret = megasas_reset_fusion(scmd->device->host,
3015                                 SCSIIO_TIMEOUT_OCR);
3016         }
3017 
3018         return ret;
3019 }
3020 
3021 /**
3022  * megasas_task_abort - Issues task abort request to firmware
3023  *                      (supported only for fusion adapters)
3024  * @scmd:               SCSI command pointer
3025  */
3026 static int megasas_task_abort(struct scsi_cmnd *scmd)
3027 {
3028         int ret;
3029         struct megasas_instance *instance;
3030 
3031         instance = (struct megasas_instance *)scmd->device->host->hostdata;
3032 
3033         if (instance->adapter_type != MFI_SERIES)
3034                 ret = megasas_task_abort_fusion(scmd);
3035         else {
3036                 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3037                 ret = FAILED;
3038         }
3039 
3040         return ret;
3041 }
3042 
3043 /**
3044  * megasas_reset_target:  Issues target reset request to firmware
3045  *                        (supported only for fusion adapters)
3046  * @scmd:                 SCSI command pointer
3047  */
3048 static int megasas_reset_target(struct scsi_cmnd *scmd)
3049 {
3050         int ret;
3051         struct megasas_instance *instance;
3052 
3053         instance = (struct megasas_instance *)scmd->device->host->hostdata;
3054 
3055         if (instance->adapter_type != MFI_SERIES)
3056                 ret = megasas_reset_target_fusion(scmd);
3057         else {
3058                 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3059                 ret = FAILED;
3060         }
3061 
3062         return ret;
3063 }
3064 
3065 /**
3066  * megasas_bios_param - Returns disk geometry for a disk
3067  * @sdev:               device handle
3068  * @bdev:               block device
3069  * @capacity:           drive capacity
3070  * @geom:               geometry parameters
3071  */
3072 static int
3073 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3074                  sector_t capacity, int geom[])
3075 {
3076         int heads;
3077         int sectors;
3078         sector_t cylinders;
3079         unsigned long tmp;
3080 
3081         /* Default heads (64) & sectors (32) */
3082         heads = 64;
3083         sectors = 32;
3084 
3085         tmp = heads * sectors;
3086         cylinders = capacity;
3087 
3088         sector_div(cylinders, tmp);
3089 
3090         /*
3091          * Handle extended translation size for logical drives > 1Gb
3092          */
3093 
3094         if (capacity >= 0x200000) {
3095                 heads = 255;
3096                 sectors = 63;
3097                 tmp = heads*sectors;
3098                 cylinders = capacity;
3099                 sector_div(cylinders, tmp);
3100         }
3101 
3102         geom[0] = heads;
3103         geom[1] = sectors;
3104         geom[2] = cylinders;
3105 
3106         return 0;
3107 }
3108 
3109 static void megasas_aen_polling(struct work_struct *work);
3110 
3111 /**
3112  * megasas_service_aen -        Processes an event notification
3113  * @instance:                   Adapter soft state
3114  * @cmd:                        AEN command completed by the ISR
3115  *
3116  * For AEN, driver sends a command down to FW that is held by the FW till an
3117  * event occurs. When an event of interest occurs, FW completes the command
3118  * that it was previously holding.
3119  *
3120  * This routines sends SIGIO signal to processes that have registered with the
3121  * driver for AEN.
3122  */
3123 static void
3124 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3125 {
3126         unsigned long flags;
3127 
3128         /*
3129          * Don't signal app if it is just an aborted previously registered aen
3130          */
3131         if ((!cmd->abort_aen) && (instance->unload == 0)) {
3132                 spin_lock_irqsave(&poll_aen_lock, flags);
3133                 megasas_poll_wait_aen = 1;
3134                 spin_unlock_irqrestore(&poll_aen_lock, flags);
3135                 wake_up(&megasas_poll_wait);
3136                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3137         }
3138         else
3139                 cmd->abort_aen = 0;
3140 
3141         instance->aen_cmd = NULL;
3142 
3143         megasas_return_cmd(instance, cmd);
3144 
3145         if ((instance->unload == 0) &&
3146                 ((instance->issuepend_done == 1))) {
3147                 struct megasas_aen_event *ev;
3148 
3149                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3150                 if (!ev) {
3151                         dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3152                 } else {
3153                         ev->instance = instance;
3154                         instance->ev = ev;
3155                         INIT_DELAYED_WORK(&ev->hotplug_work,
3156                                           megasas_aen_polling);
3157                         schedule_delayed_work(&ev->hotplug_work, 0);
3158                 }
3159         }
3160 }
3161 
3162 static ssize_t
3163 fw_crash_buffer_store(struct device *cdev,
3164         struct device_attribute *attr, const char *buf, size_t count)
3165 {
3166         struct Scsi_Host *shost = class_to_shost(cdev);
3167         struct megasas_instance *instance =
3168                 (struct megasas_instance *) shost->hostdata;
3169         int val = 0;
3170         unsigned long flags;
3171 
3172         if (kstrtoint(buf, 0, &val) != 0)
3173                 return -EINVAL;
3174 
3175         spin_lock_irqsave(&instance->crashdump_lock, flags);
3176         instance->fw_crash_buffer_offset = val;
3177         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3178         return strlen(buf);
3179 }
3180 
3181 static ssize_t
3182 fw_crash_buffer_show(struct device *cdev,
3183         struct device_attribute *attr, char *buf)
3184 {
3185         struct Scsi_Host *shost = class_to_shost(cdev);
3186         struct megasas_instance *instance =
3187                 (struct megasas_instance *) shost->hostdata;
3188         u32 size;
3189         unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3190         unsigned long chunk_left_bytes;
3191         unsigned long src_addr;
3192         unsigned long flags;
3193         u32 buff_offset;
3194 
3195         spin_lock_irqsave(&instance->crashdump_lock, flags);
3196         buff_offset = instance->fw_crash_buffer_offset;
3197         if (!instance->crash_dump_buf &&
3198                 !((instance->fw_crash_state == AVAILABLE) ||
3199                 (instance->fw_crash_state == COPYING))) {
3200                 dev_err(&instance->pdev->dev,
3201                         "Firmware crash dump is not available\n");
3202                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3203                 return -EINVAL;
3204         }
3205 
3206         if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3207                 dev_err(&instance->pdev->dev,
3208                         "Firmware crash dump offset is out of range\n");
3209                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3210                 return 0;
3211         }
3212 
3213         size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3214         chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3215         size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3216         size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3217 
3218         src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3219                 (buff_offset % dmachunk);
3220         memcpy(buf, (void *)src_addr, size);
3221         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3222 
3223         return size;
3224 }
3225 
3226 static ssize_t
3227 fw_crash_buffer_size_show(struct device *cdev,
3228         struct device_attribute *attr, char *buf)
3229 {
3230         struct Scsi_Host *shost = class_to_shost(cdev);
3231         struct megasas_instance *instance =
3232                 (struct megasas_instance *) shost->hostdata;
3233 
3234         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3235                 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3236 }
3237 
3238 static ssize_t
3239 fw_crash_state_store(struct device *cdev,
3240         struct device_attribute *attr, const char *buf, size_t count)
3241 {
3242         struct Scsi_Host *shost = class_to_shost(cdev);
3243         struct megasas_instance *instance =
3244                 (struct megasas_instance *) shost->hostdata;
3245         int val = 0;
3246         unsigned long flags;
3247 
3248         if (kstrtoint(buf, 0, &val) != 0)
3249                 return -EINVAL;
3250 
3251         if ((val <= AVAILABLE || val > COPY_ERROR)) {
3252                 dev_err(&instance->pdev->dev, "application updates invalid "
3253                         "firmware crash state\n");
3254                 return -EINVAL;
3255         }
3256 
3257         instance->fw_crash_state = val;
3258 
3259         if ((val == COPIED) || (val == COPY_ERROR)) {
3260                 spin_lock_irqsave(&instance->crashdump_lock, flags);
3261                 megasas_free_host_crash_buffer(instance);
3262                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3263                 if (val == COPY_ERROR)
3264                         dev_info(&instance->pdev->dev, "application failed to "
3265                                 "copy Firmware crash dump\n");
3266                 else
3267                         dev_info(&instance->pdev->dev, "Firmware crash dump "
3268                                 "copied successfully\n");
3269         }
3270         return strlen(buf);
3271 }
3272 
3273 static ssize_t
3274 fw_crash_state_show(struct device *cdev,
3275         struct device_attribute *attr, char *buf)
3276 {
3277         struct Scsi_Host *shost = class_to_shost(cdev);
3278         struct megasas_instance *instance =
3279                 (struct megasas_instance *) shost->hostdata;
3280 
3281         return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3282 }
3283 
3284 static ssize_t
3285 page_size_show(struct device *cdev,
3286         struct device_attribute *attr, char *buf)
3287 {
3288         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3289 }
3290 
3291 static ssize_t
3292 ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3293         char *buf)
3294 {
3295         struct Scsi_Host *shost = class_to_shost(cdev);
3296         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3297 
3298         return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3299 }
3300 
3301 static ssize_t
3302 fw_cmds_outstanding_show(struct device *cdev,
3303                                  struct device_attribute *attr, char *buf)
3304 {
3305         struct Scsi_Host *shost = class_to_shost(cdev);
3306         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3307 
3308         return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3309 }
3310 
3311 static ssize_t
3312 enable_sdev_max_qd_show(struct device *cdev,
3313         struct device_attribute *attr, char *buf)
3314 {
3315         struct Scsi_Host *shost = class_to_shost(cdev);
3316         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3317 
3318         return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3319 }
3320 
3321 static ssize_t
3322 enable_sdev_max_qd_store(struct device *cdev,
3323         struct device_attribute *attr, const char *buf, size_t count)
3324 {
3325         struct Scsi_Host *shost = class_to_shost(cdev);
3326         struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3327         u32 val = 0;
3328         bool is_target_prop;
3329         int ret_target_prop = DCMD_FAILED;
3330         struct scsi_device *sdev;
3331 
3332         if (kstrtou32(buf, 0, &val) != 0) {
3333                 pr_err("megasas: could not set enable_sdev_max_qd\n");
3334                 return -EINVAL;
3335         }
3336 
3337         mutex_lock(&instance->reset_mutex);
3338         if (val)
3339                 instance->enable_sdev_max_qd = true;
3340         else
3341                 instance->enable_sdev_max_qd = false;
3342 
3343         shost_for_each_device(sdev, shost) {
3344                 ret_target_prop = megasas_get_target_prop(instance, sdev);
3345                 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3346                 megasas_set_fw_assisted_qd(sdev, is_target_prop);
3347         }
3348         mutex_unlock(&instance->reset_mutex);
3349 
3350         return strlen(buf);
3351 }
3352 
3353 static ssize_t
3354 dump_system_regs_show(struct device *cdev,
3355                                struct device_attribute *attr, char *buf)
3356 {
3357         struct Scsi_Host *shost = class_to_shost(cdev);
3358         struct megasas_instance *instance =
3359                         (struct megasas_instance *)shost->hostdata;
3360 
3361         return megasas_dump_sys_regs(instance->reg_set, buf);
3362 }
3363 
3364 static ssize_t
3365 raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3366                           char *buf)
3367 {
3368         struct Scsi_Host *shost = class_to_shost(cdev);
3369         struct megasas_instance *instance =
3370                         (struct megasas_instance *)shost->hostdata;
3371 
3372         return snprintf(buf, PAGE_SIZE, "%ld\n",
3373                         (unsigned long)instance->map_id);
3374 }
3375 
3376 static DEVICE_ATTR_RW(fw_crash_buffer);
3377 static DEVICE_ATTR_RO(fw_crash_buffer_size);
3378 static DEVICE_ATTR_RW(fw_crash_state);
3379 static DEVICE_ATTR_RO(page_size);
3380 static DEVICE_ATTR_RO(ldio_outstanding);
3381 static DEVICE_ATTR_RO(fw_cmds_outstanding);
3382 static DEVICE_ATTR_RW(enable_sdev_max_qd);
3383 static DEVICE_ATTR_RO(dump_system_regs);
3384 static DEVICE_ATTR_RO(raid_map_id);
3385 
3386 static struct device_attribute *megaraid_host_attrs[] = {
3387         &dev_attr_fw_crash_buffer_size,
3388         &dev_attr_fw_crash_buffer,
3389         &dev_attr_fw_crash_state,
3390         &dev_attr_page_size,
3391         &dev_attr_ldio_outstanding,
3392         &dev_attr_fw_cmds_outstanding,
3393         &dev_attr_enable_sdev_max_qd,
3394         &dev_attr_dump_system_regs,
3395         &dev_attr_raid_map_id,
3396         NULL,
3397 };
3398 
3399 /*
3400  * Scsi host template for megaraid_sas driver
3401  */
3402 static struct scsi_host_template megasas_template = {
3403 
3404         .module = THIS_MODULE,
3405         .name = "Avago SAS based MegaRAID driver",
3406         .proc_name = "megaraid_sas",
3407         .slave_configure = megasas_slave_configure,
3408         .slave_alloc = megasas_slave_alloc,
3409         .slave_destroy = megasas_slave_destroy,
3410         .queuecommand = megasas_queue_command,
3411         .eh_target_reset_handler = megasas_reset_target,
3412         .eh_abort_handler = megasas_task_abort,
3413         .eh_host_reset_handler = megasas_reset_bus_host,
3414         .eh_timed_out = megasas_reset_timer,
3415         .shost_attrs = megaraid_host_attrs,
3416         .bios_param = megasas_bios_param,
3417         .change_queue_depth = scsi_change_queue_depth,
3418         .max_segment_size = 0xffffffff,
3419         .no_write_same = 1,
3420 };
3421 
3422 /**
3423  * megasas_complete_int_cmd -   Completes an internal command
3424  * @instance:                   Adapter soft state
3425  * @cmd:                        Command to be completed
3426  *
3427  * The megasas_issue_blocked_cmd() function waits for a command to complete
3428  * after it issues a command. This function wakes up that waiting routine by
3429  * calling wake_up() on the wait queue.
3430  */
3431 static void
3432 megasas_complete_int_cmd(struct megasas_instance *instance,
3433                          struct megasas_cmd *cmd)
3434 {
3435         cmd->cmd_status_drv = cmd->frame->io.cmd_status;
3436         wake_up(&instance->int_cmd_wait_q);
3437 }
3438 
3439 /**
3440  * megasas_complete_abort -     Completes aborting a command
3441  * @instance:                   Adapter soft state
3442  * @cmd:                        Cmd that was issued to abort another cmd
3443  *
3444  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3445  * after it issues an abort on a previously issued command. This function
3446  * wakes up all functions waiting on the same wait queue.
3447  */
3448 static void
3449 megasas_complete_abort(struct megasas_instance *instance,
3450                        struct megasas_cmd *cmd)
3451 {
3452         if (cmd->sync_cmd) {
3453                 cmd->sync_cmd = 0;
3454                 cmd->cmd_status_drv = 0;
3455                 wake_up(&instance->abort_cmd_wait_q);
3456         }
3457 }
3458 
3459 /**
3460  * megasas_complete_cmd -       Completes a command
3461  * @instance:                   Adapter soft state
3462  * @cmd:                        Command to be completed
3463  * @alt_status:                 If non-zero, use this value as status to
3464  *                              SCSI mid-layer instead of the value returned
3465  *                              by the FW. This should be used if caller wants
3466  *                              an alternate status (as in the case of aborted
3467  *                              commands)
3468  */
3469 void
3470 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3471                      u8 alt_status)
3472 {
3473         int exception = 0;
3474         struct megasas_header *hdr = &cmd->frame->hdr;
3475         unsigned long flags;
3476         struct fusion_context *fusion = instance->ctrl_context;
3477         u32 opcode, status;
3478 
3479         /* flag for the retry reset */
3480         cmd->retry_for_fw_reset = 0;
3481 
3482         if (cmd->scmd)
3483                 cmd->scmd->SCp.ptr = NULL;
3484 
3485         switch (hdr->cmd) {
3486         case MFI_CMD_INVALID:
3487                 /* Some older 1068 controller FW may keep a pended
3488                    MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3489                    when booting the kdump kernel.  Ignore this command to
3490                    prevent a kernel panic on shutdown of the kdump kernel. */
3491                 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3492                        "completed\n");
3493                 dev_warn(&instance->pdev->dev, "If you have a controller "
3494                        "other than PERC5, please upgrade your firmware\n");
3495                 break;
3496         case MFI_CMD_PD_SCSI_IO:
3497         case MFI_CMD_LD_SCSI_IO:
3498 
3499                 /*
3500                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3501                  * issued either through an IO path or an IOCTL path. If it
3502                  * was via IOCTL, we will send it to internal completion.
3503                  */
3504                 if (cmd->sync_cmd) {
3505                         cmd->sync_cmd = 0;
3506                         megasas_complete_int_cmd(instance, cmd);
3507                         break;
3508                 }
3509                 /* fall through */
3510 
3511         case MFI_CMD_LD_READ:
3512         case MFI_CMD_LD_WRITE:
3513 
3514                 if (alt_status) {
3515                         cmd->scmd->result = alt_status << 16;
3516                         exception = 1;
3517                 }
3518 
3519                 if (exception) {
3520 
3521                         atomic_dec(&instance->fw_outstanding);
3522 
3523                         scsi_dma_unmap(cmd->scmd);
3524                         cmd->scmd->scsi_done(cmd->scmd);
3525                         megasas_return_cmd(instance, cmd);
3526 
3527                         break;
3528                 }
3529 
3530                 switch (hdr->cmd_status) {
3531 
3532                 case MFI_STAT_OK:
3533                         cmd->scmd->result = DID_OK << 16;
3534                         break;
3535 
3536                 case MFI_STAT_SCSI_IO_FAILED:
3537                 case MFI_STAT_LD_INIT_IN_PROGRESS:
3538                         cmd->scmd->result =
3539                             (DID_ERROR << 16) | hdr->scsi_status;
3540                         break;
3541 
3542                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3543 
3544                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3545 
3546                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3547                                 memset(cmd->scmd->sense_buffer, 0,
3548                                        SCSI_SENSE_BUFFERSIZE);
3549                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3550                                        hdr->sense_len);
3551 
3552                                 cmd->scmd->result |= DRIVER_SENSE << 24;
3553                         }
3554 
3555                         break;
3556 
3557                 case MFI_STAT_LD_OFFLINE:
3558                 case MFI_STAT_DEVICE_NOT_FOUND:
3559                         cmd->scmd->result = DID_BAD_TARGET << 16;
3560                         break;
3561 
3562                 default:
3563                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3564                                hdr->cmd_status);
3565                         cmd->scmd->result = DID_ERROR << 16;
3566                         break;
3567                 }
3568 
3569                 atomic_dec(&instance->fw_outstanding);
3570 
3571                 scsi_dma_unmap(cmd->scmd);
3572                 cmd->scmd->scsi_done(cmd->scmd);
3573                 megasas_return_cmd(instance, cmd);
3574 
3575                 break;
3576 
3577         case MFI_CMD_SMP:
3578         case MFI_CMD_STP:
3579         case MFI_CMD_NVME:
3580         case MFI_CMD_TOOLBOX:
3581                 megasas_complete_int_cmd(instance, cmd);
3582                 break;
3583 
3584         case MFI_CMD_DCMD:
3585                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3586                 /* Check for LD map update */
3587                 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3588                         && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3589                         fusion->fast_path_io = 0;
3590                         spin_lock_irqsave(instance->host->host_lock, flags);
3591                         status = cmd->frame->hdr.cmd_status;
3592                         instance->map_update_cmd = NULL;
3593                         if (status != MFI_STAT_OK) {
3594                                 if (status != MFI_STAT_NOT_FOUND)
3595                                         dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3596                                                cmd->frame->hdr.cmd_status);
3597                                 else {
3598                                         megasas_return_cmd(instance, cmd);
3599                                         spin_unlock_irqrestore(
3600                                                 instance->host->host_lock,
3601                                                 flags);
3602                                         break;
3603                                 }
3604                         }
3605 
3606                         megasas_return_cmd(instance, cmd);
3607 
3608                         /*
3609                          * Set fast path IO to ZERO.
3610                          * Validate Map will set proper value.
3611                          * Meanwhile all IOs will go as LD IO.
3612                          */
3613                         if (status == MFI_STAT_OK &&
3614                             (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3615                                 instance->map_id++;
3616                                 fusion->fast_path_io = 1;
3617                         } else {
3618                                 fusion->fast_path_io = 0;
3619                         }
3620 
3621                         megasas_sync_map_info(instance);
3622                         spin_unlock_irqrestore(instance->host->host_lock,
3623                                                flags);
3624                         break;
3625                 }
3626                 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3627                     opcode == MR_DCMD_CTRL_EVENT_GET) {
3628                         spin_lock_irqsave(&poll_aen_lock, flags);
3629                         megasas_poll_wait_aen = 0;
3630                         spin_unlock_irqrestore(&poll_aen_lock, flags);
3631                 }
3632 
3633                 /* FW has an updated PD sequence */
3634                 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3635                         (cmd->frame->dcmd.mbox.b[0] == 1)) {
3636 
3637                         spin_lock_irqsave(instance->host->host_lock, flags);
3638                         status = cmd->frame->hdr.cmd_status;
3639                         instance->jbod_seq_cmd = NULL;
3640                         megasas_return_cmd(instance, cmd);
3641 
3642                         if (status == MFI_STAT_OK) {
3643                                 instance->pd_seq_map_id++;
3644                                 /* Re-register a pd sync seq num cmd */
3645                                 if (megasas_sync_pd_seq_num(instance, true))
3646                                         instance->use_seqnum_jbod_fp = false;
3647                         } else
3648                                 instance->use_seqnum_jbod_fp = false;
3649 
3650                         spin_unlock_irqrestore(instance->host->host_lock, flags);
3651                         break;
3652                 }
3653 
3654                 /*
3655                  * See if got an event notification
3656                  */
3657                 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3658                         megasas_service_aen(instance, cmd);
3659                 else
3660                         megasas_complete_int_cmd(instance, cmd);
3661 
3662                 break;
3663 
3664         case MFI_CMD_ABORT:
3665                 /*
3666                  * Cmd issued to abort another cmd returned
3667                  */
3668                 megasas_complete_abort(instance, cmd);
3669                 break;
3670 
3671         default:
3672                 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3673                        hdr->cmd);
3674                 megasas_complete_int_cmd(instance, cmd);
3675                 break;
3676         }
3677 }
3678 
3679 /**
3680  * megasas_issue_pending_cmds_again -   issue all pending cmds
3681  *                                      in FW again because of the fw reset
3682  * @instance:                           Adapter soft state
3683  */
3684 static inline void
3685 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3686 {
3687         struct megasas_cmd *cmd;
3688         struct list_head clist_local;
3689         union megasas_evt_class_locale class_locale;
3690         unsigned long flags;
3691         u32 seq_num;
3692 
3693         INIT_LIST_HEAD(&clist_local);
3694         spin_lock_irqsave(&instance->hba_lock, flags);
3695         list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3696         spin_unlock_irqrestore(&instance->hba_lock, flags);
3697 
3698         while (!list_empty(&clist_local)) {
3699                 cmd = list_entry((&clist_local)->next,
3700                                         struct megasas_cmd, list);
3701                 list_del_init(&cmd->list);
3702 
3703                 if (cmd->sync_cmd || cmd->scmd) {
3704                         dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3705                                 "detected to be pending while HBA reset\n",
3706                                         cmd, cmd->scmd, cmd->sync_cmd);
3707 
3708                         cmd->retry_for_fw_reset++;
3709 
3710                         if (cmd->retry_for_fw_reset == 3) {
3711                                 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3712                                         "was tried multiple times during reset."
3713                                         "Shutting down the HBA\n",
3714                                         cmd, cmd->scmd, cmd->sync_cmd);
3715                                 instance->instancet->disable_intr(instance);
3716                                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3717                                 megaraid_sas_kill_hba(instance);
3718                                 return;
3719                         }
3720                 }
3721 
3722                 if (cmd->sync_cmd == 1) {
3723                         if (cmd->scmd) {
3724                                 dev_notice(&instance->pdev->dev, "unexpected"
3725                                         "cmd attached to internal command!\n");
3726                         }
3727                         dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3728                                                 "on the internal reset queue,"
3729                                                 "issue it again.\n", cmd);
3730                         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3731                         instance->instancet->fire_cmd(instance,
3732                                                         cmd->frame_phys_addr,
3733                                                         0, instance->reg_set);
3734                 } else if (cmd->scmd) {
3735                         dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3736                         "detected on the internal queue, issue again.\n",
3737                         cmd, cmd->scmd->cmnd[0]);
3738 
3739                         atomic_inc(&instance->fw_outstanding);
3740                         instance->instancet->fire_cmd(instance,
3741                                         cmd->frame_phys_addr,
3742                                         cmd->frame_count-1, instance->reg_set);
3743                 } else {
3744                         dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3745                                 "internal reset defer list while re-issue!!\n",
3746                                 cmd);
3747                 }
3748         }
3749 
3750         if (instance->aen_cmd) {
3751                 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3752                 megasas_return_cmd(instance, instance->aen_cmd);
3753 
3754                 instance->aen_cmd = NULL;
3755         }
3756 
3757         /*
3758          * Initiate AEN (Asynchronous Event Notification)
3759          */
3760         seq_num = instance->last_seq_num;
3761         class_locale.members.reserved = 0;
3762         class_locale.members.locale = MR_EVT_LOCALE_ALL;
3763         class_locale.members.class = MR_EVT_CLASS_DEBUG;
3764 
3765         megasas_register_aen(instance, seq_num, class_locale.word);
3766 }
3767 
3768 /**
3769  * Move the internal reset pending commands to a deferred queue.
3770  *
3771  * We move the commands pending at internal reset time to a
3772  * pending queue. This queue would be flushed after successful
3773  * completion of the internal reset sequence. if the internal reset
3774  * did not complete in time, the kernel reset handler would flush
3775  * these commands.
3776  **/
3777 static void
3778 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3779 {
3780         struct megasas_cmd *cmd;
3781         int i;
3782         u16 max_cmd = instance->max_fw_cmds;
3783         u32 defer_index;
3784         unsigned long flags;
3785 
3786         defer_index = 0;
3787         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3788         for (i = 0; i < max_cmd; i++) {
3789                 cmd = instance->cmd_list[i];
3790                 if (cmd->sync_cmd == 1 || cmd->scmd) {
3791                         dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3792                                         "on the defer queue as internal\n",
3793                                 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3794 
3795                         if (!list_empty(&cmd->list)) {
3796                                 dev_notice(&instance->pdev->dev, "ERROR while"
3797                                         " moving this cmd:%p, %d %p, it was"
3798                                         "discovered on some list?\n",
3799                                         cmd, cmd->sync_cmd, cmd->scmd);
3800 
3801                                 list_del_init(&cmd->list);
3802                         }
3803                         defer_index++;
3804                         list_add_tail(&cmd->list,
3805                                 &instance->internal_reset_pending_q);
3806                 }
3807         }
3808         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3809 }
3810 
3811 
3812 static void
3813 process_fw_state_change_wq(struct work_struct *work)
3814 {
3815         struct megasas_instance *instance =
3816                 container_of(work, struct megasas_instance, work_init);
3817         u32 wait;
3818         unsigned long flags;
3819 
3820     if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3821                 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3822                                 atomic_read(&instance->adprecovery));
3823                 return ;
3824         }
3825 
3826         if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3827                 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3828                                         "state, restarting it...\n");
3829 
3830                 instance->instancet->disable_intr(instance);
3831                 atomic_set(&instance->fw_outstanding, 0);
3832 
3833                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3834                 instance->instancet->adp_reset(instance, instance->reg_set);
3835                 atomic_set(&instance->fw_reset_no_pci_access, 0);
3836 
3837                 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3838                                         "initiating next stage...\n");
3839 
3840                 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3841                                         "state 2 starting...\n");
3842 
3843                 /* waiting for about 20 second before start the second init */
3844                 for (wait = 0; wait < 30; wait++) {
3845                         msleep(1000);
3846                 }
3847 
3848                 if (megasas_transition_to_ready(instance, 1)) {
3849                         dev_notice(&instance->pdev->dev, "adapter not ready\n");
3850 
3851                         atomic_set(&instance->fw_reset_no_pci_access, 1);
3852                         megaraid_sas_kill_hba(instance);
3853                         return ;
3854                 }
3855 
3856                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3857                         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3858                         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3859                         ) {
3860                         *instance->consumer = *instance->producer;
3861                 } else {
3862                         *instance->consumer = 0;
3863                         *instance->producer = 0;
3864                 }
3865 
3866                 megasas_issue_init_mfi(instance);
3867 
3868                 spin_lock_irqsave(&instance->hba_lock, flags);
3869                 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3870                 spin_unlock_irqrestore(&instance->hba_lock, flags);
3871                 instance->instancet->enable_intr(instance);
3872 
3873                 megasas_issue_pending_cmds_again(instance);
3874                 instance->issuepend_done = 1;
3875         }
3876 }
3877 
3878 /**
3879  * megasas_deplete_reply_queue -        Processes all completed commands
3880  * @instance:                           Adapter soft state
3881  * @alt_status:                         Alternate status to be returned to
3882  *                                      SCSI mid-layer instead of the status
3883  *                                      returned by the FW
3884  * Note: this must be called with hba lock held
3885  */
3886 static int
3887 megasas_deplete_reply_queue(struct megasas_instance *instance,
3888                                         u8 alt_status)
3889 {
3890         u32 mfiStatus;
3891         u32 fw_state;
3892 
3893         if ((mfiStatus = instance->instancet->check_reset(instance,
3894                                         instance->reg_set)) == 1) {
3895                 return IRQ_HANDLED;
3896         }
3897 
3898         mfiStatus = instance->instancet->clear_intr(instance);
3899         if (mfiStatus == 0) {
3900                 /* Hardware may not set outbound_intr_status in MSI-X mode */
3901                 if (!instance->msix_vectors)
3902                         return IRQ_NONE;
3903         }
3904 
3905         instance->mfiStatus = mfiStatus;
3906 
3907         if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3908                 fw_state = instance->instancet->read_fw_status_reg(
3909                                 instance) & MFI_STATE_MASK;
3910 
3911                 if (fw_state != MFI_STATE_FAULT) {
3912                         dev_notice(&instance->pdev->dev, "fw state:%x\n",
3913                                                 fw_state);
3914                 }
3915 
3916                 if ((fw_state == MFI_STATE_FAULT) &&
3917                                 (instance->disableOnlineCtrlReset == 0)) {
3918                         dev_notice(&instance->pdev->dev, "wait adp restart\n");
3919 
3920                         if ((instance->pdev->device ==
3921                                         PCI_DEVICE_ID_LSI_SAS1064R) ||
3922                                 (instance->pdev->device ==
3923                                         PCI_DEVICE_ID_DELL_PERC5) ||
3924                                 (instance->pdev->device ==
3925                                         PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3926 
3927                                 *instance->consumer =
3928                                         cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3929                         }
3930 
3931 
3932                         instance->instancet->disable_intr(instance);
3933                         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
3934                         instance->issuepend_done = 0;
3935 
3936                         atomic_set(&instance->fw_outstanding, 0);
3937                         megasas_internal_reset_defer_cmds(instance);
3938 
3939                         dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3940                                         fw_state, atomic_read(&instance->adprecovery));
3941 
3942                         schedule_work(&instance->work_init);
3943                         return IRQ_HANDLED;
3944 
3945                 } else {
3946                         dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3947                                 fw_state, instance->disableOnlineCtrlReset);
3948                 }
3949         }
3950 
3951         tasklet_schedule(&instance->isr_tasklet);
3952         return IRQ_HANDLED;
3953 }
3954 /**
3955  * megasas_isr - isr entry point
3956  */
3957 static irqreturn_t megasas_isr(int irq, void *devp)
3958 {
3959         struct megasas_irq_context *irq_context = devp;
3960         struct megasas_instance *instance = irq_context->instance;
3961         unsigned long flags;
3962         irqreturn_t rc;
3963 
3964         if (atomic_read(&instance->fw_reset_no_pci_access))
3965                 return IRQ_HANDLED;
3966 
3967         spin_lock_irqsave(&instance->hba_lock, flags);
3968         rc = megasas_deplete_reply_queue(instance, DID_OK);
3969         spin_unlock_irqrestore(&instance->hba_lock, flags);
3970 
3971         return rc;
3972 }
3973 
3974 /**
3975  * megasas_transition_to_ready -        Move the FW to READY state
3976  * @instance:                           Adapter soft state
3977  *
3978  * During the initialization, FW passes can potentially be in any one of
3979  * several possible states. If the FW in operational, waiting-for-handshake
3980  * states, driver must take steps to bring it to ready state. Otherwise, it
3981  * has to wait for the ready state.
3982  */
3983 int
3984 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3985 {
3986         int i;
3987         u8 max_wait;
3988         u32 fw_state;
3989         u32 abs_state, curr_abs_state;
3990 
3991         abs_state = instance->instancet->read_fw_status_reg(instance);
3992         fw_state = abs_state & MFI_STATE_MASK;
3993 
3994         if (fw_state != MFI_STATE_READY)
3995                 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3996                        " state\n");
3997 
3998         while (fw_state != MFI_STATE_READY) {
3999 
4000                 switch (fw_state) {
4001 
4002                 case MFI_STATE_FAULT:
4003                         dev_printk(KERN_ERR, &instance->pdev->dev,
4004                                    "FW in FAULT state, Fault code:0x%x subcode:0x%x func:%s\n",
4005                                    abs_state & MFI_STATE_FAULT_CODE,
4006                                    abs_state & MFI_STATE_FAULT_SUBCODE, __func__);
4007                         if (ocr) {
4008                                 max_wait = MEGASAS_RESET_WAIT_TIME;
4009                                 break;
4010                         } else {
4011                                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4012                                 megasas_dump_reg_set(instance->reg_set);
4013                                 return -ENODEV;
4014                         }
4015 
4016                 case MFI_STATE_WAIT_HANDSHAKE:
4017                         /*
4018                          * Set the CLR bit in inbound doorbell
4019                          */
4020                         if ((instance->pdev->device ==
4021                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4022                                 (instance->pdev->device ==
4023                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4024                                 (instance->adapter_type != MFI_SERIES))
4025                                 writel(
4026                                   MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4027                                   &instance->reg_set->doorbell);
4028                         else
4029                                 writel(
4030                                     MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
4031                                         &instance->reg_set->inbound_doorbell);
4032 
4033                         max_wait = MEGASAS_RESET_WAIT_TIME;
4034                         break;
4035 
4036                 case MFI_STATE_BOOT_MESSAGE_PENDING:
4037                         if ((instance->pdev->device ==
4038                              PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4039                                 (instance->pdev->device ==
4040                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
4041                                 (instance->adapter_type != MFI_SERIES))
4042                                 writel(MFI_INIT_HOTPLUG,
4043                                        &instance->reg_set->doorbell);
4044                         else
4045                                 writel(MFI_INIT_HOTPLUG,
4046                                         &instance->reg_set->inbound_doorbell);
4047 
4048                         max_wait = MEGASAS_RESET_WAIT_TIME;
4049                         break;
4050 
4051                 case MFI_STATE_OPERATIONAL:
4052                         /*
4053                          * Bring it to READY state; assuming max wait 10 secs
4054                          */
4055                         instance->instancet->disable_intr(instance);
4056                         if ((instance->pdev->device ==
4057                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4058                                 (instance->pdev->device ==
4059                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
4060                                 (instance->adapter_type != MFI_SERIES)) {
4061                                 writel(MFI_RESET_FLAGS,
4062                                         &instance->reg_set->doorbell);
4063 
4064                                 if (instance->adapter_type != MFI_SERIES) {
4065                                         for (i = 0; i < (10 * 1000); i += 20) {
4066                                                 if (megasas_readl(
4067                                                             instance,
4068                                                             &instance->
4069                                                             reg_set->
4070                                                             doorbell) & 1)
4071                                                         msleep(20);
4072                                                 else
4073                                                         break;
4074                                         }
4075                                 }
4076                         } else
4077                                 writel(MFI_RESET_FLAGS,
4078                                         &instance->reg_set->inbound_doorbell);
4079 
4080                         max_wait = MEGASAS_RESET_WAIT_TIME;
4081                         break;
4082 
4083                 case MFI_STATE_UNDEFINED:
4084                         /*
4085                          * This state should not last for more than 2 seconds
4086                          */
4087                         max_wait = MEGASAS_RESET_WAIT_TIME;
4088                         break;
4089 
4090                 case MFI_STATE_BB_INIT:
4091                         max_wait = MEGASAS_RESET_WAIT_TIME;
4092                         break;
4093 
4094                 case MFI_STATE_FW_INIT:
4095                         max_wait = MEGASAS_RESET_WAIT_TIME;
4096                         break;
4097 
4098                 case MFI_STATE_FW_INIT_2:
4099                         max_wait = MEGASAS_RESET_WAIT_TIME;
4100                         break;
4101 
4102                 case MFI_STATE_DEVICE_SCAN:
4103                         max_wait = MEGASAS_RESET_WAIT_TIME;
4104                         break;
4105 
4106                 case MFI_STATE_FLUSH_CACHE:
4107                         max_wait = MEGASAS_RESET_WAIT_TIME;
4108                         break;
4109 
4110                 default:
4111                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
4112                                fw_state);
4113                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4114                         megasas_dump_reg_set(instance->reg_set);
4115                         return -ENODEV;
4116                 }
4117 
4118                 /*
4119                  * The cur_state should not last for more than max_wait secs
4120                  */
4121                 for (i = 0; i < max_wait * 50; i++) {
4122                         curr_abs_state = instance->instancet->
4123                                 read_fw_status_reg(instance);
4124 
4125                         if (abs_state == curr_abs_state) {
4126                                 msleep(20);
4127                         } else
4128                                 break;
4129                 }
4130 
4131                 /*
4132                  * Return error if fw_state hasn't changed after max_wait
4133                  */
4134                 if (curr_abs_state == abs_state) {
4135                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
4136                                "in %d secs\n", fw_state, max_wait);
4137                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "System Register set:\n");
4138                         megasas_dump_reg_set(instance->reg_set);
4139                         return -ENODEV;
4140                 }
4141 
4142                 abs_state = curr_abs_state;
4143                 fw_state = curr_abs_state & MFI_STATE_MASK;
4144         }
4145         dev_info(&instance->pdev->dev, "FW now in Ready state\n");
4146 
4147         return 0;
4148 }
4149 
4150 /**
4151  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
4152  * @instance:                           Adapter soft state
4153  */
4154 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
4155 {
4156         int i;
4157         u16 max_cmd = instance->max_mfi_cmds;
4158         struct megasas_cmd *cmd;
4159 
4160         if (!instance->frame_dma_pool)
4161                 return;
4162 
4163         /*
4164          * Return all frames to pool
4165          */
4166         for (i = 0; i < max_cmd; i++) {
4167 
4168                 cmd = instance->cmd_list[i];
4169 
4170                 if (cmd->frame)
4171                         dma_pool_free(instance->frame_dma_pool, cmd->frame,
4172                                       cmd->frame_phys_addr);
4173 
4174                 if (cmd->sense)
4175                         dma_pool_free(instance->sense_dma_pool, cmd->sense,
4176                                       cmd->sense_phys_addr);
4177         }
4178 
4179         /*
4180          * Now destroy the pool itself
4181          */
4182         dma_pool_destroy(instance->frame_dma_pool);
4183         dma_pool_destroy(instance->sense_dma_pool);
4184 
4185         instance->frame_dma_pool = NULL;
4186         instance->sense_dma_pool = NULL;
4187 }
4188 
4189 /**
4190  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
4191  * @instance:                   Adapter soft state
4192  *
4193  * Each command packet has an embedded DMA memory buffer that is used for
4194  * filling MFI frame and the SG list that immediately follows the frame. This
4195  * function creates those DMA memory buffers for each command packet by using
4196  * PCI pool facility.
4197  */
4198 static int megasas_create_frame_pool(struct megasas_instance *instance)
4199 {
4200         int i;
4201         u16 max_cmd;
4202         u32 frame_count;
4203         struct megasas_cmd *cmd;
4204 
4205         max_cmd = instance->max_mfi_cmds;
4206 
4207         /*
4208          * For MFI controllers.
4209          * max_num_sge = 60
4210          * max_sge_sz  = 16 byte (sizeof megasas_sge_skinny)
4211          * Total 960 byte (15 MFI frame of 64 byte)
4212          *
4213          * Fusion adapter require only 3 extra frame.
4214          * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
4215          * max_sge_sz  = 12 byte (sizeof  megasas_sge64)
4216          * Total 192 byte (3 MFI frame of 64 byte)
4217          */
4218         frame_count = (instance->adapter_type == MFI_SERIES) ?
4219                         (15 + 1) : (3 + 1);
4220         instance->mfi_frame_size = MEGAMFI_FRAME_SIZE * frame_count;
4221         /*
4222          * Use DMA pool facility provided by PCI layer
4223          */
4224         instance->frame_dma_pool = dma_pool_create("megasas frame pool",
4225                                         &instance->pdev->dev,
4226                                         instance->mfi_frame_size, 256, 0);
4227 
4228         if (!instance->frame_dma_pool) {
4229                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
4230                 return -ENOMEM;
4231         }
4232 
4233         instance->sense_dma_pool = dma_pool_create("megasas sense pool",
4234                                                    &instance->pdev->dev, 128,
4235                                                    4, 0);
4236 
4237         if (!instance->sense_dma_pool) {
4238                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
4239 
4240                 dma_pool_destroy(instance->frame_dma_pool);
4241                 instance->frame_dma_pool = NULL;
4242 
4243                 return -ENOMEM;
4244         }
4245 
4246         /*
4247          * Allocate and attach a frame to each of the commands in cmd_list.
4248          * By making cmd->index as the context instead of the &cmd, we can
4249          * always use 32bit context regardless of the architecture
4250          */
4251         for (i = 0; i < max_cmd; i++) {
4252 
4253                 cmd = instance->cmd_list[i];
4254 
4255                 cmd->frame = dma_pool_zalloc(instance->frame_dma_pool,
4256                                             GFP_KERNEL, &cmd->frame_phys_addr);
4257 
4258                 cmd->sense = dma_pool_alloc(instance->sense_dma_pool,
4259                                             GFP_KERNEL, &cmd->sense_phys_addr);
4260 
4261                 /*
4262                  * megasas_teardown_frame_pool() takes care of freeing
4263                  * whatever has been allocated
4264                  */
4265                 if (!cmd->frame || !cmd->sense) {
4266                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "dma_pool_alloc failed\n");
4267                         megasas_teardown_frame_pool(instance);
4268                         return -ENOMEM;
4269                 }
4270 
4271                 cmd->frame->io.context = cpu_to_le32(cmd->index);
4272                 cmd->frame->io.pad_0 = 0;
4273                 if ((instance->adapter_type == MFI_SERIES) && reset_devices)
4274                         cmd->frame->hdr.cmd = MFI_CMD_INVALID;
4275         }
4276 
4277         return 0;
4278 }
4279 
4280 /**
4281  * megasas_free_cmds -  Free all the cmds in the free cmd pool
4282  * @instance:           Adapter soft state
4283  */
4284 void megasas_free_cmds(struct megasas_instance *instance)
4285 {
4286         int i;
4287 
4288         /* First free the MFI frame pool */
4289         megasas_teardown_frame_pool(instance);
4290 
4291         /* Free all the commands in the cmd_list */
4292         for (i = 0; i < instance->max_mfi_cmds; i++)
4293 
4294                 kfree(instance->cmd_list[i]);
4295 
4296         /* Free the cmd_list buffer itself */
4297         kfree(instance->cmd_list);
4298         instance->cmd_list = NULL;
4299 
4300         INIT_LIST_HEAD(&instance->cmd_pool);
4301 }
4302 
4303 /**
4304  * megasas_alloc_cmds - Allocates the command packets
4305  * @instance:           Adapter soft state
4306  *
4307  * Each command that is issued to the FW, whether IO commands from the OS or
4308  * internal commands like IOCTLs, are wrapped in local data structure called
4309  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
4310  * the FW.
4311  *
4312  * Each frame has a 32-bit field called context (tag). This context is used
4313  * to get back the megasas_cmd from the frame when a frame gets completed in
4314  * the ISR. Typically the address of the megasas_cmd itself would be used as
4315  * the context. But we wanted to keep the differences between 32 and 64 bit
4316  * systems to the mininum. We always use 32 bit integers for the context. In
4317  * this driver, the 32 bit values are the indices into an array cmd_list.
4318  * This array is used only to look up the megasas_cmd given the context. The
4319  * free commands themselves are maintained in a linked list called cmd_pool.
4320  */
4321 int megasas_alloc_cmds(struct megasas_instance *instance)
4322 {
4323         int i;
4324         int j;
4325         u16 max_cmd;
4326         struct megasas_cmd *cmd;
4327 
4328         max_cmd = instance->max_mfi_cmds;
4329 
4330         /*
4331          * instance->cmd_list is an array of struct megasas_cmd pointers.
4332          * Allocate the dynamic array first and then allocate individual
4333          * commands.
4334          */
4335         instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
4336 
4337         if (!instance->cmd_list) {
4338                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
4339                 return -ENOMEM;
4340         }
4341 
4342         memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
4343 
4344         for (i = 0; i < max_cmd; i++) {
4345                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
4346                                                 GFP_KERNEL);
4347 
4348                 if (!instance->cmd_list[i]) {
4349 
4350                         for (j = 0; j < i; j++)
4351                                 kfree(instance->cmd_list[j]);
4352 
4353                         kfree(instance->cmd_list);
4354                         instance->cmd_list = NULL;
4355 
4356                         return -ENOMEM;
4357                 }
4358         }
4359 
4360         for (i = 0; i < max_cmd; i++) {
4361                 cmd = instance->cmd_list[i];
4362                 memset(cmd, 0, sizeof(struct megasas_cmd));
4363                 cmd->index = i;
4364                 cmd->scmd = NULL;
4365                 cmd->instance = instance;
4366 
4367                 list_add_tail(&cmd->list, &instance->cmd_pool);
4368         }
4369 
4370         /*
4371          * Create a frame pool and assign one frame to each cmd
4372          */
4373         if (megasas_create_frame_pool(instance)) {
4374                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
4375                 megasas_free_cmds(instance);
4376                 return -ENOMEM;
4377         }
4378 
4379         return 0;
4380 }
4381 
4382 /*
4383  * dcmd_timeout_ocr_possible -  Check if OCR is possible based on Driver/FW state.
4384  * @instance:                           Adapter soft state
4385  *
4386  * Return 0 for only Fusion adapter, if driver load/unload is not in progress
4387  * or FW is not under OCR.
4388  */
4389 inline int
4390 dcmd_timeout_ocr_possible(struct megasas_instance *instance) {
4391 
4392         if (instance->adapter_type == MFI_SERIES)
4393                 return KILL_ADAPTER;
4394         else if (instance->unload ||
4395                         test_bit(MEGASAS_FUSION_OCR_NOT_POSSIBLE,
4396                                  &instance->reset_flags))
4397                 return IGNORE_TIMEOUT;
4398         else
4399                 return INITIATE_OCR;
4400 }
4401 
4402 static void
4403 megasas_get_pd_info(struct megasas_instance *instance, struct scsi_device *sdev)
4404 {
4405         int ret;
4406         struct megasas_cmd *cmd;
4407         struct megasas_dcmd_frame *dcmd;
4408 
4409         struct MR_PRIV_DEVICE *mr_device_priv_data;
4410         u16 device_id = 0;
4411 
4412         device_id = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + sdev->id;
4413         cmd = megasas_get_cmd(instance);
4414 
4415         if (!cmd) {
4416                 dev_err(&instance->pdev->dev, "Failed to get cmd %s\n", __func__);
4417                 return;
4418         }
4419 
4420         dcmd = &cmd->frame->dcmd;
4421 
4422         memset(instance->pd_info, 0, sizeof(*instance->pd_info));
4423         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4424 
4425         dcmd->mbox.s[0] = cpu_to_le16(device_id);
4426         dcmd->cmd = MFI_CMD_DCMD;
4427         dcmd->cmd_status = 0xFF;
4428         dcmd->sge_count = 1;
4429         dcmd->flags = MFI_FRAME_DIR_READ;
4430         dcmd->timeout = 0;
4431         dcmd->pad_0 = 0;
4432         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_PD_INFO));
4433         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_GET_INFO);
4434 
4435         megasas_set_dma_settings(instance, dcmd, instance->pd_info_h,
4436                                  sizeof(struct MR_PD_INFO));
4437 
4438         if ((instance->adapter_type != MFI_SERIES) &&
4439             !instance->mask_interrupts)
4440                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4441         else
4442                 ret = megasas_issue_polled(instance, cmd);
4443 
4444         switch (ret) {
4445         case DCMD_SUCCESS:
4446                 mr_device_priv_data = sdev->hostdata;
4447                 le16_to_cpus((u16 *)&instance->pd_info->state.ddf.pdType);
4448                 mr_device_priv_data->interface_type =
4449                                 instance->pd_info->state.ddf.pdType.intf;
4450                 break;
4451 
4452         case DCMD_TIMEOUT:
4453 
4454                 switch (dcmd_timeout_ocr_possible(instance)) {
4455                 case INITIATE_OCR:
4456                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4457                         mutex_unlock(&instance->reset_mutex);
4458                         megasas_reset_fusion(instance->host,
4459                                 MFI_IO_TIMEOUT_OCR);
4460                         mutex_lock(&instance->reset_mutex);
4461                         break;
4462                 case KILL_ADAPTER:
4463                         megaraid_sas_kill_hba(instance);
4464                         break;
4465                 case IGNORE_TIMEOUT:
4466                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4467                                 __func__, __LINE__);
4468                         break;
4469                 }
4470 
4471                 break;
4472         }
4473 
4474         if (ret != DCMD_TIMEOUT)
4475                 megasas_return_cmd(instance, cmd);
4476 
4477         return;
4478 }
4479 /*
4480  * megasas_get_pd_list_info -   Returns FW's pd_list structure
4481  * @instance:                           Adapter soft state
4482  * @pd_list:                            pd_list structure
4483  *
4484  * Issues an internal command (DCMD) to get the FW's controller PD
4485  * list structure.  This information is mainly used to find out SYSTEM
4486  * supported by the FW.
4487  */
4488 static int
4489 megasas_get_pd_list(struct megasas_instance *instance)
4490 {
4491         int ret = 0, pd_index = 0;
4492         struct megasas_cmd *cmd;
4493         struct megasas_dcmd_frame *dcmd;
4494         struct MR_PD_LIST *ci;
4495         struct MR_PD_ADDRESS *pd_addr;
4496 
4497         if (instance->pd_list_not_supported) {
4498                 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4499                 "not supported by firmware\n");
4500                 return ret;
4501         }
4502 
4503         ci = instance->pd_list_buf;
4504 
4505         cmd = megasas_get_cmd(instance);
4506 
4507         if (!cmd) {
4508                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
4509                 return -ENOMEM;
4510         }
4511 
4512         dcmd = &cmd->frame->dcmd;
4513 
4514         memset(ci, 0, sizeof(*ci));
4515         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4516 
4517         dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
4518         dcmd->mbox.b[1] = 0;
4519         dcmd->cmd = MFI_CMD_DCMD;
4520         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4521         dcmd->sge_count = 1;
4522         dcmd->flags = MFI_FRAME_DIR_READ;
4523         dcmd->timeout = 0;
4524         dcmd->pad_0 = 0;
4525         dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
4526         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
4527 
4528         megasas_set_dma_settings(instance, dcmd, instance->pd_list_buf_h,
4529                                  (MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST)));
4530 
4531         if ((instance->adapter_type != MFI_SERIES) &&
4532             !instance->mask_interrupts)
4533                 ret = megasas_issue_blocked_cmd(instance, cmd,
4534                         MFI_IO_TIMEOUT_SECS);
4535         else
4536                 ret = megasas_issue_polled(instance, cmd);
4537 
4538         switch (ret) {
4539         case DCMD_FAILED:
4540                 dev_info(&instance->pdev->dev, "MR_DCMD_PD_LIST_QUERY "
4541                         "failed/not supported by firmware\n");
4542 
4543                 if (instance->adapter_type != MFI_SERIES)
4544                         megaraid_sas_kill_hba(instance);
4545                 else
4546                         instance->pd_list_not_supported = 1;
4547                 break;
4548         case DCMD_TIMEOUT:
4549 
4550                 switch (dcmd_timeout_ocr_possible(instance)) {
4551                 case INITIATE_OCR:
4552                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4553                         /*
4554                          * DCMD failed from AEN path.
4555                          * AEN path already hold reset_mutex to avoid PCI access
4556                          * while OCR is in progress.
4557                          */
4558                         mutex_unlock(&instance->reset_mutex);
4559                         megasas_reset_fusion(instance->host,
4560                                                 MFI_IO_TIMEOUT_OCR);
4561                         mutex_lock(&instance->reset_mutex);
4562                         break;
4563                 case KILL_ADAPTER:
4564                         megaraid_sas_kill_hba(instance);
4565                         break;
4566                 case IGNORE_TIMEOUT:
4567                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d \n",
4568                                 __func__, __LINE__);
4569                         break;
4570                 }
4571 
4572                 break;
4573 
4574         case DCMD_SUCCESS:
4575                 pd_addr = ci->addr;
4576                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4577                         dev_info(&instance->pdev->dev, "%s, sysPD count: 0x%x\n",
4578                                  __func__, le32_to_cpu(ci->count));
4579 
4580                 if ((le32_to_cpu(ci->count) >
4581                         (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL)))
4582                         break;
4583 
4584                 memset(instance->local_pd_list, 0,
4585                                 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4586 
4587                 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
4588                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid     =
4589                                         le16_to_cpu(pd_addr->deviceId);
4590                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType       =
4591                                         pd_addr->scsiDevType;
4592                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState      =
4593                                         MR_PD_STATE_SYSTEM;
4594                         if (megasas_dbg_lvl & LD_PD_DEBUG)
4595                                 dev_info(&instance->pdev->dev,
4596                                          "PD%d: targetID: 0x%03x deviceType:0x%x\n",
4597                                          pd_index, le16_to_cpu(pd_addr->deviceId),
4598                                          pd_addr->scsiDevType);
4599                         pd_addr++;
4600                 }
4601 
4602                 memcpy(instance->pd_list, instance->local_pd_list,
4603                         sizeof(instance->pd_list));
4604                 break;
4605 
4606         }
4607 
4608         if (ret != DCMD_TIMEOUT)
4609                 megasas_return_cmd(instance, cmd);
4610 
4611         return ret;
4612 }
4613 
4614 /*
4615  * megasas_get_ld_list_info -   Returns FW's ld_list structure
4616  * @instance:                           Adapter soft state
4617  * @ld_list:                            ld_list structure
4618  *
4619  * Issues an internal command (DCMD) to get the FW's controller PD
4620  * list structure.  This information is mainly used to find out SYSTEM
4621  * supported by the FW.
4622  */
4623 static int
4624 megasas_get_ld_list(struct megasas_instance *instance)
4625 {
4626         int ret = 0, ld_index = 0, ids = 0;
4627         struct megasas_cmd *cmd;
4628         struct megasas_dcmd_frame *dcmd;
4629         struct MR_LD_LIST *ci;
4630         dma_addr_t ci_h = 0;
4631         u32 ld_count;
4632 
4633         ci = instance->ld_list_buf;
4634         ci_h = instance->ld_list_buf_h;
4635 
4636         cmd = megasas_get_cmd(instance);
4637 
4638         if (!cmd) {
4639                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
4640                 return -ENOMEM;
4641         }
4642 
4643         dcmd = &cmd->frame->dcmd;
4644 
4645         memset(ci, 0, sizeof(*ci));
4646         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4647 
4648         if (instance->supportmax256vd)
4649                 dcmd->mbox.b[0] = 1;
4650         dcmd->cmd = MFI_CMD_DCMD;
4651         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4652         dcmd->sge_count = 1;
4653         dcmd->flags = MFI_FRAME_DIR_READ;
4654         dcmd->timeout = 0;
4655         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
4656         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
4657         dcmd->pad_0  = 0;
4658 
4659         megasas_set_dma_settings(instance, dcmd, ci_h,
4660                                  sizeof(struct MR_LD_LIST));
4661 
4662         if ((instance->adapter_type != MFI_SERIES) &&
4663             !instance->mask_interrupts)
4664                 ret = megasas_issue_blocked_cmd(instance, cmd,
4665                         MFI_IO_TIMEOUT_SECS);
4666         else
4667                 ret = megasas_issue_polled(instance, cmd);
4668 
4669         ld_count = le32_to_cpu(ci->ldCount);
4670 
4671         switch (ret) {
4672         case DCMD_FAILED:
4673                 megaraid_sas_kill_hba(instance);
4674                 break;
4675         case DCMD_TIMEOUT:
4676 
4677                 switch (dcmd_timeout_ocr_possible(instance)) {
4678                 case INITIATE_OCR:
4679                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4680                         /*
4681                          * DCMD failed from AEN path.
4682                          * AEN path already hold reset_mutex to avoid PCI access
4683                          * while OCR is in progress.
4684                          */
4685                         mutex_unlock(&instance->reset_mutex);
4686                         megasas_reset_fusion(instance->host,
4687                                                 MFI_IO_TIMEOUT_OCR);
4688                         mutex_lock(&instance->reset_mutex);
4689                         break;
4690                 case KILL_ADAPTER:
4691                         megaraid_sas_kill_hba(instance);
4692                         break;
4693                 case IGNORE_TIMEOUT:
4694                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4695                                 __func__, __LINE__);
4696                         break;
4697                 }
4698 
4699                 break;
4700 
4701         case DCMD_SUCCESS:
4702                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4703                         dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4704                                  __func__, ld_count);
4705 
4706                 if (ld_count > instance->fw_supported_vd_count)
4707                         break;
4708 
4709                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4710 
4711                 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4712                         if (ci->ldList[ld_index].state != 0) {
4713                                 ids = ci->ldList[ld_index].ref.targetId;
4714                                 instance->ld_ids[ids] = ci->ldList[ld_index].ref.targetId;
4715                                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4716                                         dev_info(&instance->pdev->dev,
4717                                                  "LD%d: targetID: 0x%03x\n",
4718                                                  ld_index, ids);
4719                         }
4720                 }
4721 
4722                 break;
4723         }
4724 
4725         if (ret != DCMD_TIMEOUT)
4726                 megasas_return_cmd(instance, cmd);
4727 
4728         return ret;
4729 }
4730 
4731 /**
4732  * megasas_ld_list_query -      Returns FW's ld_list structure
4733  * @instance:                           Adapter soft state
4734  * @ld_list:                            ld_list structure
4735  *
4736  * Issues an internal command (DCMD) to get the FW's controller PD
4737  * list structure.  This information is mainly used to find out SYSTEM
4738  * supported by the FW.
4739  */
4740 static int
4741 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4742 {
4743         int ret = 0, ld_index = 0, ids = 0;
4744         struct megasas_cmd *cmd;
4745         struct megasas_dcmd_frame *dcmd;
4746         struct MR_LD_TARGETID_LIST *ci;
4747         dma_addr_t ci_h = 0;
4748         u32 tgtid_count;
4749 
4750         ci = instance->ld_targetid_list_buf;
4751         ci_h = instance->ld_targetid_list_buf_h;
4752 
4753         cmd = megasas_get_cmd(instance);
4754 
4755         if (!cmd) {
4756                 dev_warn(&instance->pdev->dev,
4757                          "megasas_ld_list_query: Failed to get cmd\n");
4758                 return -ENOMEM;
4759         }
4760 
4761         dcmd = &cmd->frame->dcmd;
4762 
4763         memset(ci, 0, sizeof(*ci));
4764         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4765 
4766         dcmd->mbox.b[0] = query_type;
4767         if (instance->supportmax256vd)
4768                 dcmd->mbox.b[2] = 1;
4769 
4770         dcmd->cmd = MFI_CMD_DCMD;
4771         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4772         dcmd->sge_count = 1;
4773         dcmd->flags = MFI_FRAME_DIR_READ;
4774         dcmd->timeout = 0;
4775         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4776         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4777         dcmd->pad_0  = 0;
4778 
4779         megasas_set_dma_settings(instance, dcmd, ci_h,
4780                                  sizeof(struct MR_LD_TARGETID_LIST));
4781 
4782         if ((instance->adapter_type != MFI_SERIES) &&
4783             !instance->mask_interrupts)
4784                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
4785         else
4786                 ret = megasas_issue_polled(instance, cmd);
4787 
4788         switch (ret) {
4789         case DCMD_FAILED:
4790                 dev_info(&instance->pdev->dev,
4791                         "DCMD not supported by firmware - %s %d\n",
4792                                 __func__, __LINE__);
4793                 ret = megasas_get_ld_list(instance);
4794                 break;
4795         case DCMD_TIMEOUT:
4796                 switch (dcmd_timeout_ocr_possible(instance)) {
4797                 case INITIATE_OCR:
4798                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4799                         /*
4800                          * DCMD failed from AEN path.
4801                          * AEN path already hold reset_mutex to avoid PCI access
4802                          * while OCR is in progress.
4803                          */
4804                         mutex_unlock(&instance->reset_mutex);
4805                         megasas_reset_fusion(instance->host,
4806                                                 MFI_IO_TIMEOUT_OCR);
4807                         mutex_lock(&instance->reset_mutex);
4808                         break;
4809                 case KILL_ADAPTER:
4810                         megaraid_sas_kill_hba(instance);
4811                         break;
4812                 case IGNORE_TIMEOUT:
4813                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4814                                 __func__, __LINE__);
4815                         break;
4816                 }
4817 
4818                 break;
4819         case DCMD_SUCCESS:
4820                 tgtid_count = le32_to_cpu(ci->count);
4821 
4822                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4823                         dev_info(&instance->pdev->dev, "%s, LD count: 0x%x\n",
4824                                  __func__, tgtid_count);
4825 
4826                 if ((tgtid_count > (instance->fw_supported_vd_count)))
4827                         break;
4828 
4829                 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4830                 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4831                         ids = ci->targetId[ld_index];
4832                         instance->ld_ids[ids] = ci->targetId[ld_index];
4833                         if (megasas_dbg_lvl & LD_PD_DEBUG)
4834                                 dev_info(&instance->pdev->dev, "LD%d: targetID: 0x%03x\n",
4835                                          ld_index, ci->targetId[ld_index]);
4836                 }
4837 
4838                 break;
4839         }
4840 
4841         if (ret != DCMD_TIMEOUT)
4842                 megasas_return_cmd(instance, cmd);
4843 
4844         return ret;
4845 }
4846 
4847 /**
4848  * dcmd.opcode            - MR_DCMD_CTRL_DEVICE_LIST_GET
4849  * dcmd.mbox              - reserved
4850  * dcmd.sge IN            - ptr to return MR_HOST_DEVICE_LIST structure
4851  * Desc:    This DCMD will return the combined device list
4852  * Status:  MFI_STAT_OK - List returned successfully
4853  *          MFI_STAT_INVALID_CMD - Firmware support for the feature has been
4854  *                                 disabled
4855  * @instance:                   Adapter soft state
4856  * @is_probe:                   Driver probe check
4857  * Return:                      0 if DCMD succeeded
4858  *                               non-zero if failed
4859  */
4860 static int
4861 megasas_host_device_list_query(struct megasas_instance *instance,
4862                                bool is_probe)
4863 {
4864         int ret, i, target_id;
4865         struct megasas_cmd *cmd;
4866         struct megasas_dcmd_frame *dcmd;
4867         struct MR_HOST_DEVICE_LIST *ci;
4868         u32 count;
4869         dma_addr_t ci_h;
4870 
4871         ci = instance->host_device_list_buf;
4872         ci_h = instance->host_device_list_buf_h;
4873 
4874         cmd = megasas_get_cmd(instance);
4875 
4876         if (!cmd) {
4877                 dev_warn(&instance->pdev->dev,
4878                          "%s: failed to get cmd\n",
4879                          __func__);
4880                 return -ENOMEM;
4881         }
4882 
4883         dcmd = &cmd->frame->dcmd;
4884 
4885         memset(ci, 0, sizeof(*ci));
4886         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4887 
4888         dcmd->mbox.b[0] = is_probe ? 0 : 1;
4889         dcmd->cmd = MFI_CMD_DCMD;
4890         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4891         dcmd->sge_count = 1;
4892         dcmd->flags = MFI_FRAME_DIR_READ;
4893         dcmd->timeout = 0;
4894         dcmd->pad_0 = 0;
4895         dcmd->data_xfer_len = cpu_to_le32(HOST_DEVICE_LIST_SZ);
4896         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_DEVICE_LIST_GET);
4897 
4898         megasas_set_dma_settings(instance, dcmd, ci_h, HOST_DEVICE_LIST_SZ);
4899 
4900         if (!instance->mask_interrupts) {
4901                 ret = megasas_issue_blocked_cmd(instance, cmd,
4902                                                 MFI_IO_TIMEOUT_SECS);
4903         } else {
4904                 ret = megasas_issue_polled(instance, cmd);
4905                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4906         }
4907 
4908         switch (ret) {
4909         case DCMD_SUCCESS:
4910                 /* Fill the internal pd_list and ld_ids array based on
4911                  * targetIds returned by FW
4912                  */
4913                 count = le32_to_cpu(ci->count);
4914 
4915                 if (count > (MEGASAS_MAX_PD + MAX_LOGICAL_DRIVES_EXT))
4916                         break;
4917 
4918                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4919                         dev_info(&instance->pdev->dev, "%s, Device count: 0x%x\n",
4920                                  __func__, count);
4921 
4922                 memset(instance->local_pd_list, 0,
4923                        MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
4924                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4925                 for (i = 0; i < count; i++) {
4926                         target_id = le16_to_cpu(ci->host_device_list[i].target_id);
4927                         if (ci->host_device_list[i].flags.u.bits.is_sys_pd) {
4928                                 instance->local_pd_list[target_id].tid = target_id;
4929                                 instance->local_pd_list[target_id].driveType =
4930                                                 ci->host_device_list[i].scsi_type;
4931                                 instance->local_pd_list[target_id].driveState =
4932                                                 MR_PD_STATE_SYSTEM;
4933                                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4934                                         dev_info(&instance->pdev->dev,
4935                                                  "Device %d: PD targetID: 0x%03x deviceType:0x%x\n",
4936                                                  i, target_id, ci->host_device_list[i].scsi_type);
4937                         } else {
4938                                 instance->ld_ids[target_id] = target_id;
4939                                 if (megasas_dbg_lvl & LD_PD_DEBUG)
4940                                         dev_info(&instance->pdev->dev,
4941                                                  "Device %d: LD targetID: 0x%03x\n",
4942                                                  i, target_id);
4943                         }
4944                 }
4945 
4946                 memcpy(instance->pd_list, instance->local_pd_list,
4947                        sizeof(instance->pd_list));
4948                 break;
4949 
4950         case DCMD_TIMEOUT:
4951                 switch (dcmd_timeout_ocr_possible(instance)) {
4952                 case INITIATE_OCR:
4953                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
4954                         mutex_unlock(&instance->reset_mutex);
4955                         megasas_reset_fusion(instance->host,
4956                                 MFI_IO_TIMEOUT_OCR);
4957                         mutex_lock(&instance->reset_mutex);
4958                         break;
4959                 case KILL_ADAPTER:
4960                         megaraid_sas_kill_hba(instance);
4961                         break;
4962                 case IGNORE_TIMEOUT:
4963                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
4964                                  __func__, __LINE__);
4965                         break;
4966                 }
4967                 break;
4968         case DCMD_FAILED:
4969                 dev_err(&instance->pdev->dev,
4970                         "%s: MR_DCMD_CTRL_DEVICE_LIST_GET failed\n",
4971                         __func__);
4972                 break;
4973         }
4974 
4975         if (ret != DCMD_TIMEOUT)
4976                 megasas_return_cmd(instance, cmd);
4977 
4978         return ret;
4979 }
4980 
4981 /*
4982  * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4983  * instance                      : Controller's instance
4984 */
4985 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4986 {
4987         struct fusion_context *fusion;
4988         u32 ventura_map_sz = 0;
4989 
4990         fusion = instance->ctrl_context;
4991         /* For MFI based controllers return dummy success */
4992         if (!fusion)
4993                 return;
4994 
4995         instance->supportmax256vd =
4996                 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs;
4997         /* Below is additional check to address future FW enhancement */
4998         if (instance->ctrl_info_buf->max_lds > 64)
4999                 instance->supportmax256vd = 1;
5000 
5001         instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
5002                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
5003         instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
5004                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
5005         if (instance->supportmax256vd) {
5006                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
5007                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5008         } else {
5009                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
5010                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
5011         }
5012 
5013         dev_info(&instance->pdev->dev,
5014                 "FW provided supportMaxExtLDs: %d\tmax_lds: %d\n",
5015                 instance->ctrl_info_buf->adapterOperations3.supportMaxExtLDs ? 1 : 0,
5016                 instance->ctrl_info_buf->max_lds);
5017 
5018         if (instance->max_raid_mapsize) {
5019                 ventura_map_sz = instance->max_raid_mapsize *
5020                                                 MR_MIN_MAP_SIZE; /* 64k */
5021                 fusion->current_map_sz = ventura_map_sz;
5022                 fusion->max_map_sz = ventura_map_sz;
5023         } else {
5024                 fusion->old_map_sz =  sizeof(struct MR_FW_RAID_MAP) +
5025                                         (sizeof(struct MR_LD_SPAN_MAP) *
5026                                         (instance->fw_supported_vd_count - 1));
5027                 fusion->new_map_sz =  sizeof(struct MR_FW_RAID_MAP_EXT);
5028 
5029                 fusion->max_map_sz =
5030                         max(fusion->old_map_sz, fusion->new_map_sz);
5031 
5032                 if (instance->supportmax256vd)
5033                         fusion->current_map_sz = fusion->new_map_sz;
5034                 else
5035                         fusion->current_map_sz = fusion->old_map_sz;
5036         }
5037         /* irrespective of FW raid maps, driver raid map is constant */
5038         fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP_ALL);
5039 }
5040 
5041 /*
5042  * dcmd.opcode                - MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES
5043  * dcmd.hdr.length            - number of bytes to read
5044  * dcmd.sge                   - Ptr to MR_SNAPDUMP_PROPERTIES
5045  * Desc:                         Fill in snapdump properties
5046  * Status:                       MFI_STAT_OK- Command successful
5047  */
5048 void megasas_get_snapdump_properties(struct megasas_instance *instance)
5049 {
5050         int ret = 0;
5051         struct megasas_cmd *cmd;
5052         struct megasas_dcmd_frame *dcmd;
5053         struct MR_SNAPDUMP_PROPERTIES *ci;
5054         dma_addr_t ci_h = 0;
5055 
5056         ci = instance->snapdump_prop;
5057         ci_h = instance->snapdump_prop_h;
5058 
5059         if (!ci)
5060                 return;
5061 
5062         cmd = megasas_get_cmd(instance);
5063 
5064         if (!cmd) {
5065                 dev_dbg(&instance->pdev->dev, "Failed to get a free cmd\n");
5066                 return;
5067         }
5068 
5069         dcmd = &cmd->frame->dcmd;
5070 
5071         memset(ci, 0, sizeof(*ci));
5072         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5073 
5074         dcmd->cmd = MFI_CMD_DCMD;
5075         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5076         dcmd->sge_count = 1;
5077         dcmd->flags = MFI_FRAME_DIR_READ;
5078         dcmd->timeout = 0;
5079         dcmd->pad_0 = 0;
5080         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_SNAPDUMP_PROPERTIES));
5081         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SNAPDUMP_GET_PROPERTIES);
5082 
5083         megasas_set_dma_settings(instance, dcmd, ci_h,
5084                                  sizeof(struct MR_SNAPDUMP_PROPERTIES));
5085 
5086         if (!instance->mask_interrupts) {
5087                 ret = megasas_issue_blocked_cmd(instance, cmd,
5088                                                 MFI_IO_TIMEOUT_SECS);
5089         } else {
5090                 ret = megasas_issue_polled(instance, cmd);
5091                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5092         }
5093 
5094         switch (ret) {
5095         case DCMD_SUCCESS:
5096                 instance->snapdump_wait_time =
5097                         min_t(u8, ci->trigger_min_num_sec_before_ocr,
5098                                 MEGASAS_MAX_SNAP_DUMP_WAIT_TIME);
5099                 break;
5100 
5101         case DCMD_TIMEOUT:
5102                 switch (dcmd_timeout_ocr_possible(instance)) {
5103                 case INITIATE_OCR:
5104                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5105                         mutex_unlock(&instance->reset_mutex);
5106                         megasas_reset_fusion(instance->host,
5107                                 MFI_IO_TIMEOUT_OCR);
5108                         mutex_lock(&instance->reset_mutex);
5109                         break;
5110                 case KILL_ADAPTER:
5111                         megaraid_sas_kill_hba(instance);
5112                         break;
5113                 case IGNORE_TIMEOUT:
5114                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5115                                 __func__, __LINE__);
5116                         break;
5117                 }
5118         }
5119 
5120         if (ret != DCMD_TIMEOUT)
5121                 megasas_return_cmd(instance, cmd);
5122 }
5123 
5124 /**
5125  * megasas_get_controller_info -        Returns FW's controller structure
5126  * @instance:                           Adapter soft state
5127  *
5128  * Issues an internal command (DCMD) to get the FW's controller structure.
5129  * This information is mainly used to find out the maximum IO transfer per
5130  * command supported by the FW.
5131  */
5132 int
5133 megasas_get_ctrl_info(struct megasas_instance *instance)
5134 {
5135         int ret = 0;
5136         struct megasas_cmd *cmd;
5137         struct megasas_dcmd_frame *dcmd;
5138         struct megasas_ctrl_info *ci;
5139         dma_addr_t ci_h = 0;
5140 
5141         ci = instance->ctrl_info_buf;
5142         ci_h = instance->ctrl_info_buf_h;
5143 
5144         cmd = megasas_get_cmd(instance);
5145 
5146         if (!cmd) {
5147                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
5148                 return -ENOMEM;
5149         }
5150 
5151         dcmd = &cmd->frame->dcmd;
5152 
5153         memset(ci, 0, sizeof(*ci));
5154         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5155 
5156         dcmd->cmd = MFI_CMD_DCMD;
5157         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5158         dcmd->sge_count = 1;
5159         dcmd->flags = MFI_FRAME_DIR_READ;
5160         dcmd->timeout = 0;
5161         dcmd->pad_0 = 0;
5162         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
5163         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
5164         dcmd->mbox.b[0] = 1;
5165 
5166         megasas_set_dma_settings(instance, dcmd, ci_h,
5167                                  sizeof(struct megasas_ctrl_info));
5168 
5169         if ((instance->adapter_type != MFI_SERIES) &&
5170             !instance->mask_interrupts) {
5171                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5172         } else {
5173                 ret = megasas_issue_polled(instance, cmd);
5174                 cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5175         }
5176 
5177         switch (ret) {
5178         case DCMD_SUCCESS:
5179                 /* Save required controller information in
5180                  * CPU endianness format.
5181                  */
5182                 le32_to_cpus((u32 *)&ci->properties.OnOffProperties);
5183                 le16_to_cpus((u16 *)&ci->properties.on_off_properties2);
5184                 le32_to_cpus((u32 *)&ci->adapterOperations2);
5185                 le32_to_cpus((u32 *)&ci->adapterOperations3);
5186                 le16_to_cpus((u16 *)&ci->adapter_operations4);
5187                 le32_to_cpus((u32 *)&ci->adapter_operations5);
5188 
5189                 /* Update the latest Ext VD info.
5190                  * From Init path, store current firmware details.
5191                  * From OCR path, detect any firmware properties changes.
5192                  * in case of Firmware upgrade without system reboot.
5193                  */
5194                 megasas_update_ext_vd_details(instance);
5195                 instance->support_seqnum_jbod_fp =
5196                         ci->adapterOperations3.useSeqNumJbodFP;
5197                 instance->support_morethan256jbod =
5198                         ci->adapter_operations4.support_pd_map_target_id;
5199                 instance->support_nvme_passthru =
5200                         ci->adapter_operations4.support_nvme_passthru;
5201                 instance->support_pci_lane_margining =
5202                         ci->adapter_operations5.support_pci_lane_margining;
5203                 instance->task_abort_tmo = ci->TaskAbortTO;
5204                 instance->max_reset_tmo = ci->MaxResetTO;
5205 
5206                 /*Check whether controller is iMR or MR */
5207                 instance->is_imr = (ci->memory_size ? 0 : 1);
5208 
5209                 instance->snapdump_wait_time =
5210                         (ci->properties.on_off_properties2.enable_snap_dump ?
5211                          MEGASAS_DEFAULT_SNAP_DUMP_WAIT_TIME : 0);
5212 
5213                 instance->enable_fw_dev_list =
5214                         ci->properties.on_off_properties2.enable_fw_dev_list;
5215 
5216                 dev_info(&instance->pdev->dev,
5217                         "controller type\t: %s(%dMB)\n",
5218                         instance->is_imr ? "iMR" : "MR",
5219                         le16_to_cpu(ci->memory_size));
5220 
5221                 instance->disableOnlineCtrlReset =
5222                         ci->properties.OnOffProperties.disableOnlineCtrlReset;
5223                 instance->secure_jbod_support =
5224                         ci->adapterOperations3.supportSecurityonJBOD;
5225                 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
5226                         instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
5227                 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
5228                         instance->secure_jbod_support ? "Yes" : "No");
5229                 dev_info(&instance->pdev->dev, "NVMe passthru support\t: %s\n",
5230                          instance->support_nvme_passthru ? "Yes" : "No");
5231                 dev_info(&instance->pdev->dev,
5232                          "FW provided TM TaskAbort/Reset timeout\t: %d secs/%d secs\n",
5233                          instance->task_abort_tmo, instance->max_reset_tmo);
5234                 dev_info(&instance->pdev->dev, "JBOD sequence map support\t: %s\n",
5235                          instance->support_seqnum_jbod_fp ? "Yes" : "No");
5236                 dev_info(&instance->pdev->dev, "PCI Lane Margining support\t: %s\n",
5237                          instance->support_pci_lane_margining ? "Yes" : "No");
5238 
5239                 break;
5240 
5241         case DCMD_TIMEOUT:
5242                 switch (dcmd_timeout_ocr_possible(instance)) {
5243                 case INITIATE_OCR:
5244                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5245                         mutex_unlock(&instance->reset_mutex);
5246                         megasas_reset_fusion(instance->host,
5247                                 MFI_IO_TIMEOUT_OCR);
5248                         mutex_lock(&instance->reset_mutex);
5249                         break;
5250                 case KILL_ADAPTER:
5251                         megaraid_sas_kill_hba(instance);
5252                         break;
5253                 case IGNORE_TIMEOUT:
5254                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5255                                 __func__, __LINE__);
5256                         break;
5257                 }
5258                 break;
5259         case DCMD_FAILED:
5260                 megaraid_sas_kill_hba(instance);
5261                 break;
5262 
5263         }
5264 
5265         if (ret != DCMD_TIMEOUT)
5266                 megasas_return_cmd(instance, cmd);
5267 
5268         return ret;
5269 }
5270 
5271 /*
5272  * megasas_set_crash_dump_params -      Sends address of crash dump DMA buffer
5273  *                                      to firmware
5274  *
5275  * @instance:                           Adapter soft state
5276  * @crash_buf_state             -       tell FW to turn ON/OFF crash dump feature
5277                                         MR_CRASH_BUF_TURN_OFF = 0
5278                                         MR_CRASH_BUF_TURN_ON = 1
5279  * @return 0 on success non-zero on failure.
5280  * Issues an internal command (DCMD) to set parameters for crash dump feature.
5281  * Driver will send address of crash dump DMA buffer and set mbox to tell FW
5282  * that driver supports crash dump feature. This DCMD will be sent only if
5283  * crash dump feature is supported by the FW.
5284  *
5285  */
5286 int megasas_set_crash_dump_params(struct megasas_instance *instance,
5287         u8 crash_buf_state)
5288 {
5289         int ret = 0;
5290         struct megasas_cmd *cmd;
5291         struct megasas_dcmd_frame *dcmd;
5292 
5293         cmd = megasas_get_cmd(instance);
5294 
5295         if (!cmd) {
5296                 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
5297                 return -ENOMEM;
5298         }
5299 
5300 
5301         dcmd = &cmd->frame->dcmd;
5302 
5303         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5304         dcmd->mbox.b[0] = crash_buf_state;
5305         dcmd->cmd = MFI_CMD_DCMD;
5306         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
5307         dcmd->sge_count = 1;
5308         dcmd->flags = MFI_FRAME_DIR_NONE;
5309         dcmd->timeout = 0;
5310         dcmd->pad_0 = 0;
5311         dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
5312         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
5313 
5314         megasas_set_dma_settings(instance, dcmd, instance->crash_dump_h,
5315                                  CRASH_DMA_BUF_SIZE);
5316 
5317         if ((instance->adapter_type != MFI_SERIES) &&
5318             !instance->mask_interrupts)
5319                 ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
5320         else
5321                 ret = megasas_issue_polled(instance, cmd);
5322 
5323         if (ret == DCMD_TIMEOUT) {
5324                 switch (dcmd_timeout_ocr_possible(instance)) {
5325                 case INITIATE_OCR:
5326                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
5327                         megasas_reset_fusion(instance->host,
5328                                         MFI_IO_TIMEOUT_OCR);
5329                         break;
5330                 case KILL_ADAPTER:
5331                         megaraid_sas_kill_hba(instance);
5332                         break;
5333                 case IGNORE_TIMEOUT:
5334                         dev_info(&instance->pdev->dev, "Ignore DCMD timeout: %s %d\n",
5335                                 __func__, __LINE__);
5336                         break;
5337                 }
5338         } else
5339                 megasas_return_cmd(instance, cmd);
5340 
5341         return ret;
5342 }
5343 
5344 /**
5345  * megasas_issue_init_mfi -     Initializes the FW
5346  * @instance:           Adapter soft state
5347  *
5348  * Issues the INIT MFI cmd
5349  */
5350 static int
5351 megasas_issue_init_mfi(struct megasas_instance *instance)
5352 {
5353         __le32 context;
5354         struct megasas_cmd *cmd;
5355         struct megasas_init_frame *init_frame;
5356         struct megasas_init_queue_info *initq_info;
5357         dma_addr_t init_frame_h;
5358         dma_addr_t initq_info_h;
5359 
5360         /*
5361          * Prepare a init frame. Note the init frame points to queue info
5362          * structure. Each frame has SGL allocated after first 64 bytes. For
5363          * this frame - since we don't need any SGL - we use SGL's space as
5364          * queue info structure
5365          *
5366          * We will not get a NULL command below. We just created the pool.
5367          */
5368         cmd = megasas_get_cmd(instance);
5369 
5370         init_frame = (struct megasas_init_frame *)cmd->frame;
5371         initq_info = (struct megasas_init_queue_info *)
5372                 ((unsigned long)init_frame + 64);
5373 
5374         init_frame_h = cmd->frame_phys_addr;
5375         initq_info_h = init_frame_h + 64;
5376 
5377         context = init_frame->context;
5378         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
5379         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
5380         init_frame->context = context;
5381 
5382         initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
5383         initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
5384 
5385         initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
5386         initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
5387 
5388         init_frame->cmd = MFI_CMD_INIT;
5389         init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
5390         init_frame->queue_info_new_phys_addr_lo =
5391                 cpu_to_le32(lower_32_bits(initq_info_h));
5392         init_frame->queue_info_new_phys_addr_hi =
5393                 cpu_to_le32(upper_32_bits(initq_info_h));
5394 
5395         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
5396 
5397         /*
5398          * disable the intr before firing the init frame to FW
5399          */
5400         instance->instancet->disable_intr(instance);
5401 
5402         /*
5403          * Issue the init frame in polled mode
5404          */
5405 
5406         if (megasas_issue_polled(instance, cmd)) {
5407                 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
5408                 megasas_return_cmd(instance, cmd);
5409                 goto fail_fw_init;
5410         }
5411 
5412         megasas_return_cmd(instance, cmd);
5413 
5414         return 0;
5415 
5416 fail_fw_init:
5417         return -EINVAL;
5418 }
5419 
5420 static u32
5421 megasas_init_adapter_mfi(struct megasas_instance *instance)
5422 {
5423         u32 context_sz;
5424         u32 reply_q_sz;
5425 
5426         /*
5427          * Get various operational parameters from status register
5428          */
5429         instance->max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF;
5430         /*
5431          * Reduce the max supported cmds by 1. This is to ensure that the
5432          * reply_q_sz (1 more than the max cmd that driver may send)
5433          * does not exceed max cmds that the FW can support
5434          */
5435         instance->max_fw_cmds = instance->max_fw_cmds-1;
5436         instance->max_mfi_cmds = instance->max_fw_cmds;
5437         instance->max_num_sge = (instance->instancet->read_fw_status_reg(instance) & 0xFF0000) >>
5438                                         0x10;
5439         /*
5440          * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
5441          * are reserved for IOCTL + driver's internal DCMDs.
5442          */
5443         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5444                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
5445                 instance->max_scsi_cmds = (instance->max_fw_cmds -
5446                         MEGASAS_SKINNY_INT_CMDS);
5447                 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
5448         } else {
5449                 instance->max_scsi_cmds = (instance->max_fw_cmds -
5450                         MEGASAS_INT_CMDS);
5451                 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
5452         }
5453 
5454         instance->cur_can_queue = instance->max_scsi_cmds;
5455         /*
5456          * Create a pool of commands
5457          */
5458         if (megasas_alloc_cmds(instance))
5459                 goto fail_alloc_cmds;
5460 
5461         /*
5462          * Allocate memory for reply queue. Length of reply queue should
5463          * be _one_ more than the maximum commands handled by the firmware.
5464          *
5465          * Note: When FW completes commands, it places corresponding contex
5466          * values in this circular reply queue. This circular queue is a fairly
5467          * typical producer-consumer queue. FW is the producer (of completed
5468          * commands) and the driver is the consumer.
5469          */
5470         context_sz = sizeof(u32);
5471         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
5472 
5473         instance->reply_queue = dma_alloc_coherent(&instance->pdev->dev,
5474                         reply_q_sz, &instance->reply_queue_h, GFP_KERNEL);
5475 
5476         if (!instance->reply_queue) {
5477                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
5478                 goto fail_reply_queue;
5479         }
5480 
5481         if (megasas_issue_init_mfi(instance))
5482                 goto fail_fw_init;
5483 
5484         if (megasas_get_ctrl_info(instance)) {
5485                 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
5486                         "Fail from %s %d\n", instance->unique_id,
5487                         __func__, __LINE__);
5488                 goto fail_fw_init;
5489         }
5490 
5491         instance->fw_support_ieee = 0;
5492         instance->fw_support_ieee =
5493                 (instance->instancet->read_fw_status_reg(instance) &
5494                 0x04000000);
5495 
5496         dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
5497                         instance->fw_support_ieee);
5498 
5499         if (instance->fw_support_ieee)
5500                 instance->flag_ieee = 1;
5501 
5502         return 0;
5503 
5504 fail_fw_init:
5505 
5506         dma_free_coherent(&instance->pdev->dev, reply_q_sz,
5507                             instance->reply_queue, instance->reply_queue_h);
5508 fail_reply_queue:
5509         megasas_free_cmds(instance);
5510 
5511 fail_alloc_cmds:
5512         return 1;
5513 }
5514 
5515 static
5516 void megasas_setup_irq_poll(struct megasas_instance *instance)
5517 {
5518         struct megasas_irq_context *irq_ctx;
5519         u32 count, i;
5520 
5521         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5522 
5523         /* Initialize IRQ poll */
5524         for (i = 0; i < count; i++) {
5525                 irq_ctx = &instance->irq_context[i];
5526                 irq_ctx->os_irq = pci_irq_vector(instance->pdev, i);
5527                 irq_ctx->irq_poll_scheduled = false;
5528                 irq_poll_init(&irq_ctx->irqpoll,
5529                               instance->threshold_reply_count,
5530                               megasas_irqpoll);
5531         }
5532 }
5533 
5534 /*
5535  * megasas_setup_irqs_ioapic -          register legacy interrupts.
5536  * @instance:                           Adapter soft state
5537  *
5538  * Do not enable interrupt, only setup ISRs.
5539  *
5540  * Return 0 on success.
5541  */
5542 static int
5543 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
5544 {
5545         struct pci_dev *pdev;
5546 
5547         pdev = instance->pdev;
5548         instance->irq_context[0].instance = instance;
5549         instance->irq_context[0].MSIxIndex = 0;
5550         if (request_irq(pci_irq_vector(pdev, 0),
5551                         instance->instancet->service_isr, IRQF_SHARED,
5552                         "megasas", &instance->irq_context[0])) {
5553                 dev_err(&instance->pdev->dev,
5554                                 "Failed to register IRQ from %s %d\n",
5555                                 __func__, __LINE__);
5556                 return -1;
5557         }
5558         instance->perf_mode = MR_LATENCY_PERF_MODE;
5559         instance->low_latency_index_start = 0;
5560         return 0;
5561 }
5562 
5563 /**
5564  * megasas_setup_irqs_msix -            register MSI-x interrupts.
5565  * @instance:                           Adapter soft state
5566  * @is_probe:                           Driver probe check
5567  *
5568  * Do not enable interrupt, only setup ISRs.
5569  *
5570  * Return 0 on success.
5571  */
5572 static int
5573 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
5574 {
5575         int i, j;
5576         struct pci_dev *pdev;
5577 
5578         pdev = instance->pdev;
5579 
5580         /* Try MSI-x */
5581         for (i = 0; i < instance->msix_vectors; i++) {
5582                 instance->irq_context[i].instance = instance;
5583                 instance->irq_context[i].MSIxIndex = i;
5584                 if (request_irq(pci_irq_vector(pdev, i),
5585                         instance->instancet->service_isr, 0, "megasas",
5586                         &instance->irq_context[i])) {
5587                         dev_err(&instance->pdev->dev,
5588                                 "Failed to register IRQ for vector %d.\n", i);
5589                         for (j = 0; j < i; j++)
5590                                 free_irq(pci_irq_vector(pdev, j),
5591                                          &instance->irq_context[j]);
5592                         /* Retry irq register for IO_APIC*/
5593                         instance->msix_vectors = 0;
5594                         instance->msix_load_balance = false;
5595                         if (is_probe) {
5596                                 pci_free_irq_vectors(instance->pdev);
5597                                 return megasas_setup_irqs_ioapic(instance);
5598                         } else {
5599                                 return -1;
5600                         }
5601                 }
5602         }
5603 
5604         return 0;
5605 }
5606 
5607 /*
5608  * megasas_destroy_irqs-                unregister interrupts.
5609  * @instance:                           Adapter soft state
5610  * return:                              void
5611  */
5612 static void
5613 megasas_destroy_irqs(struct megasas_instance *instance) {
5614 
5615         int i;
5616         int count;
5617         struct megasas_irq_context *irq_ctx;
5618 
5619         count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
5620         if (instance->adapter_type != MFI_SERIES) {
5621                 for (i = 0; i < count; i++) {
5622                         irq_ctx = &instance->irq_context[i];
5623                         irq_poll_disable(&irq_ctx->irqpoll);
5624                 }
5625         }
5626 
5627         if (instance->msix_vectors)
5628                 for (i = 0; i < instance->msix_vectors; i++) {
5629                         free_irq(pci_irq_vector(instance->pdev, i),
5630                                  &instance->irq_context[i]);
5631                 }
5632         else
5633                 free_irq(pci_irq_vector(instance->pdev, 0),
5634                          &instance->irq_context[0]);
5635 }
5636 
5637 /**
5638  * megasas_setup_jbod_map -     setup jbod map for FP seq_number.
5639  * @instance:                           Adapter soft state
5640  * @is_probe:                           Driver probe check
5641  *
5642  * Return 0 on success.
5643  */
5644 void
5645 megasas_setup_jbod_map(struct megasas_instance *instance)
5646 {
5647         int i;
5648         struct fusion_context *fusion = instance->ctrl_context;
5649         u32 pd_seq_map_sz;
5650 
5651         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5652                 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
5653 
5654         instance->use_seqnum_jbod_fp =
5655                 instance->support_seqnum_jbod_fp;
5656         if (reset_devices || !fusion ||
5657                 !instance->support_seqnum_jbod_fp) {
5658                 dev_info(&instance->pdev->dev,
5659                         "JBOD sequence map is disabled %s %d\n",
5660                         __func__, __LINE__);
5661                 instance->use_seqnum_jbod_fp = false;
5662                 return;
5663         }
5664 
5665         if (fusion->pd_seq_sync[0])
5666                 goto skip_alloc;
5667 
5668         for (i = 0; i < JBOD_MAPS_COUNT; i++) {
5669                 fusion->pd_seq_sync[i] = dma_alloc_coherent
5670                         (&instance->pdev->dev, pd_seq_map_sz,
5671                         &fusion->pd_seq_phys[i], GFP_KERNEL);
5672                 if (!fusion->pd_seq_sync[i]) {
5673                         dev_err(&instance->pdev->dev,
5674                                 "Failed to allocate memory from %s %d\n",
5675                                 __func__, __LINE__);
5676                         if (i == 1) {
5677                                 dma_free_coherent(&instance->pdev->dev,
5678                                         pd_seq_map_sz, fusion->pd_seq_sync[0],
5679                                         fusion->pd_seq_phys[0]);
5680                                 fusion->pd_seq_sync[0] = NULL;
5681                         }
5682                         instance->use_seqnum_jbod_fp = false;
5683                         return;
5684                 }
5685         }
5686 
5687 skip_alloc:
5688         if (!megasas_sync_pd_seq_num(instance, false) &&
5689                 !megasas_sync_pd_seq_num(instance, true))
5690                 instance->use_seqnum_jbod_fp = true;
5691         else
5692                 instance->use_seqnum_jbod_fp = false;
5693 }
5694 
5695 static void megasas_setup_reply_map(struct megasas_instance *instance)
5696 {
5697         const struct cpumask *mask;
5698         unsigned int queue, cpu, low_latency_index_start;
5699 
5700         low_latency_index_start = instance->low_latency_index_start;
5701 
5702         for (queue = low_latency_index_start; queue < instance->msix_vectors; queue++) {
5703                 mask = pci_irq_get_affinity(instance->pdev, queue);
5704                 if (!mask)
5705                         goto fallback;
5706 
5707                 for_each_cpu(cpu, mask)
5708                         instance->reply_map[cpu] = queue;
5709         }
5710         return;
5711 
5712 fallback:
5713         queue = low_latency_index_start;
5714         for_each_possible_cpu(cpu) {
5715                 instance->reply_map[cpu] = queue;
5716                 if (queue == (instance->msix_vectors - 1))
5717                         queue = low_latency_index_start;
5718                 else
5719                         queue++;
5720         }
5721 }
5722 
5723 /**
5724  * megasas_get_device_list -    Get the PD and LD device list from FW.
5725  * @instance:                   Adapter soft state
5726  * @return:                     Success or failure
5727  *
5728  * Issue DCMDs to Firmware to get the PD and LD list.
5729  * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
5730  * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
5731  */
5732 static
5733 int megasas_get_device_list(struct megasas_instance *instance)
5734 {
5735         memset(instance->pd_list, 0,
5736                (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
5737         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
5738 
5739         if (instance->enable_fw_dev_list) {
5740                 if (megasas_host_device_list_query(instance, true))
5741                         return FAILED;
5742         } else {
5743                 if (megasas_get_pd_list(instance) < 0) {
5744                         dev_err(&instance->pdev->dev, "failed to get PD list\n");
5745                         return FAILED;
5746                 }
5747 
5748                 if (megasas_ld_list_query(instance,
5749                                           MR_LD_QUERY_TYPE_EXPOSED_TO_HOST)) {
5750                         dev_err(&instance->pdev->dev, "failed to get LD list\n");
5751                         return FAILED;
5752                 }
5753         }
5754 
5755         return SUCCESS;
5756 }
5757 
5758 /**
5759  * megasas_set_high_iops_queue_affinity_hint -  Set affinity hint for high IOPS queues
5760  * @instance:                                   Adapter soft state
5761  * return:                                      void
5762  */
5763 static inline void
5764 megasas_set_high_iops_queue_affinity_hint(struct megasas_instance *instance)
5765 {
5766         int i;
5767         int local_numa_node;
5768 
5769         if (instance->perf_mode == MR_BALANCED_PERF_MODE) {
5770                 local_numa_node = dev_to_node(&instance->pdev->dev);
5771 
5772                 for (i = 0; i < instance->low_latency_index_start; i++)
5773                         irq_set_affinity_hint(pci_irq_vector(instance->pdev, i),
5774                                 cpumask_of_node(local_numa_node));
5775         }
5776 }
5777 
5778 static int
5779 __megasas_alloc_irq_vectors(struct megasas_instance *instance)
5780 {
5781         int i, irq_flags;
5782         struct irq_affinity desc = { .pre_vectors = instance->low_latency_index_start };
5783         struct irq_affinity *descp = &desc;
5784 
5785         irq_flags = PCI_IRQ_MSIX;
5786 
5787         if (instance->smp_affinity_enable)
5788                 irq_flags |= PCI_IRQ_AFFINITY;
5789         else
5790                 descp = NULL;
5791 
5792         i = pci_alloc_irq_vectors_affinity(instance->pdev,
5793                 instance->low_latency_index_start,
5794                 instance->msix_vectors, irq_flags, descp);
5795 
5796         return i;
5797 }
5798 
5799 /**
5800  * megasas_alloc_irq_vectors -  Allocate IRQ vectors/enable MSI-x vectors
5801  * @instance:                   Adapter soft state
5802  * return:                      void
5803  */
5804 static void
5805 megasas_alloc_irq_vectors(struct megasas_instance *instance)
5806 {
5807         int i;
5808         unsigned int num_msix_req;
5809 
5810         i = __megasas_alloc_irq_vectors(instance);
5811 
5812         if ((instance->perf_mode == MR_BALANCED_PERF_MODE) &&
5813             (i != instance->msix_vectors)) {
5814                 if (instance->msix_vectors)
5815                         pci_free_irq_vectors(instance->pdev);
5816                 /* Disable Balanced IOPS mode and try realloc vectors */
5817                 instance->perf_mode = MR_LATENCY_PERF_MODE;
5818                 instance->low_latency_index_start = 1;
5819                 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
5820 
5821                 instance->msix_vectors = min(num_msix_req,
5822                                 instance->msix_vectors);
5823 
5824                 i = __megasas_alloc_irq_vectors(instance);
5825 
5826         }
5827 
5828         dev_info(&instance->pdev->dev,
5829                 "requested/available msix %d/%d\n", instance->msix_vectors, i);
5830 
5831         if (i > 0)
5832                 instance->msix_vectors = i;
5833         else
5834                 instance->msix_vectors = 0;
5835 
5836         if (instance->smp_affinity_enable)
5837                 megasas_set_high_iops_queue_affinity_hint(instance);
5838 }
5839 
5840 /**
5841  * megasas_init_fw -    Initializes the FW
5842  * @instance:           Adapter soft state
5843  *
5844  * This is the main function for initializing firmware
5845  */
5846 
5847 static int megasas_init_fw(struct megasas_instance *instance)
5848 {
5849         u32 max_sectors_1;
5850         u32 max_sectors_2, tmp_sectors, msix_enable;
5851         u32 scratch_pad_1, scratch_pad_2, scratch_pad_3, status_reg;
5852         resource_size_t base_addr;
5853         void *base_addr_phys;
5854         struct megasas_ctrl_info *ctrl_info = NULL;
5855         unsigned long bar_list;
5856         int i, j, loop;
5857         struct IOV_111 *iovPtr;
5858         struct fusion_context *fusion;
5859         bool intr_coalescing;
5860         unsigned int num_msix_req;
5861         u16 lnksta, speed;
5862 
5863         fusion = instance->ctrl_context;
5864 
5865         /* Find first memory bar */
5866         bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
5867         instance->bar = find_first_bit(&bar_list, BITS_PER_LONG);
5868         if (pci_request_selected_regions(instance->pdev, 1<<instance->bar,
5869                                          "megasas: LSI")) {
5870                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
5871                 return -EBUSY;
5872         }
5873 
5874         base_addr = pci_resource_start(instance->pdev, instance->bar);
5875         instance->reg_set = ioremap_nocache(base_addr, 8192);
5876 
5877         if (!instance->reg_set) {
5878                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
5879                 goto fail_ioremap;
5880         }
5881 
5882         base_addr_phys = &base_addr;
5883         dev_printk(KERN_DEBUG, &instance->pdev->dev,
5884                    "BAR:0x%lx  BAR's base_addr(phys):%pa  mapped virt_addr:0x%p\n",
5885                    instance->bar, base_addr_phys, instance->reg_set);
5886 
5887         if (instance->adapter_type != MFI_SERIES)
5888                 instance->instancet = &megasas_instance_template_fusion;
5889         else {
5890                 switch (instance->pdev->device) {
5891                 case PCI_DEVICE_ID_LSI_SAS1078R:
5892                 case PCI_DEVICE_ID_LSI_SAS1078DE:
5893                         instance->instancet = &megasas_instance_template_ppc;
5894                         break;
5895                 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
5896                 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
5897                         instance->instancet = &megasas_instance_template_gen2;
5898                         break;
5899                 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
5900                 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
5901                         instance->instancet = &megasas_instance_template_skinny;
5902                         break;
5903                 case PCI_DEVICE_ID_LSI_SAS1064R:
5904                 case PCI_DEVICE_ID_DELL_PERC5:
5905                 default:
5906                         instance->instancet = &megasas_instance_template_xscale;
5907                         instance->pd_list_not_supported = 1;
5908                         break;
5909                 }
5910         }
5911 
5912         if (megasas_transition_to_ready(instance, 0)) {
5913                 dev_info(&instance->pdev->dev,
5914                          "Failed to transition controller to ready from %s!\n",
5915                          __func__);
5916                 if (instance->adapter_type != MFI_SERIES) {
5917                         status_reg = instance->instancet->read_fw_status_reg(
5918                                         instance);
5919                         if (status_reg & MFI_RESET_ADAPTER) {
5920                                 if (megasas_adp_reset_wait_for_ready
5921                                         (instance, true, 0) == FAILED)
5922                                         goto fail_ready_state;
5923                         } else {
5924                                 goto fail_ready_state;
5925                         }
5926                 } else {
5927                         atomic_set(&instance->fw_reset_no_pci_access, 1);
5928                         instance->instancet->adp_reset
5929                                 (instance, instance->reg_set);
5930                         atomic_set(&instance->fw_reset_no_pci_access, 0);
5931 
5932                         /*waiting for about 30 second before retry*/
5933                         ssleep(30);
5934 
5935                         if (megasas_transition_to_ready(instance, 0))
5936                                 goto fail_ready_state;
5937                 }
5938 
5939                 dev_info(&instance->pdev->dev,
5940                          "FW restarted successfully from %s!\n",
5941                          __func__);
5942         }
5943 
5944         megasas_init_ctrl_params(instance);
5945 
5946         if (megasas_set_dma_mask(instance))
5947                 goto fail_ready_state;
5948 
5949         if (megasas_alloc_ctrl_mem(instance))
5950                 goto fail_alloc_dma_buf;
5951 
5952         if (megasas_alloc_ctrl_dma_buffers(instance))
5953                 goto fail_alloc_dma_buf;
5954 
5955         fusion = instance->ctrl_context;
5956 
5957         if (instance->adapter_type >= VENTURA_SERIES) {
5958                 scratch_pad_2 =
5959                         megasas_readl(instance,
5960                                       &instance->reg_set->outbound_scratch_pad_2);
5961                 instance->max_raid_mapsize = ((scratch_pad_2 >>
5962                         MR_MAX_RAID_MAP_SIZE_OFFSET_SHIFT) &
5963                         MR_MAX_RAID_MAP_SIZE_MASK);
5964         }
5965 
5966         instance->enable_sdev_max_qd = enable_sdev_max_qd;
5967 
5968         switch (instance->adapter_type) {
5969         case VENTURA_SERIES:
5970                 fusion->pcie_bw_limitation = true;
5971                 break;
5972         case AERO_SERIES:
5973                 fusion->r56_div_offload = true;
5974                 break;
5975         default:
5976                 break;
5977         }
5978 
5979         /* Check if MSI-X is supported while in ready state */
5980         msix_enable = (instance->instancet->read_fw_status_reg(instance) &
5981                        0x4000000) >> 0x1a;
5982         if (msix_enable && !msix_disable) {
5983 
5984                 scratch_pad_1 = megasas_readl
5985                         (instance, &instance->reg_set->outbound_scratch_pad_1);
5986                 /* Check max MSI-X vectors */
5987                 if (fusion) {
5988                         if (instance->adapter_type == THUNDERBOLT_SERIES) {
5989                                 /* Thunderbolt Series*/
5990                                 instance->msix_vectors = (scratch_pad_1
5991                                         & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
5992                         } else {
5993                                 instance->msix_vectors = ((scratch_pad_1
5994                                         & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
5995                                         >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
5996 
5997                                 /*
5998                                  * For Invader series, > 8 MSI-x vectors
5999                                  * supported by FW/HW implies combined
6000                                  * reply queue mode is enabled.
6001                                  * For Ventura series, > 16 MSI-x vectors
6002                                  * supported by FW/HW implies combined
6003                                  * reply queue mode is enabled.
6004                                  */
6005                                 switch (instance->adapter_type) {
6006                                 case INVADER_SERIES:
6007                                         if (instance->msix_vectors > 8)
6008                                                 instance->msix_combined = true;
6009                                         break;
6010                                 case AERO_SERIES:
6011                                 case VENTURA_SERIES:
6012                                         if (instance->msix_vectors > 16)
6013                                                 instance->msix_combined = true;
6014                                         break;
6015                                 }
6016 
6017                                 if (rdpq_enable)
6018                                         instance->is_rdpq = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ?
6019                                                                 1 : 0;
6020 
6021                                 if (instance->adapter_type >= INVADER_SERIES &&
6022                                     !instance->msix_combined) {
6023                                         instance->msix_load_balance = true;
6024                                         instance->smp_affinity_enable = false;
6025                                 }
6026 
6027                                 /* Save 1-15 reply post index address to local memory
6028                                  * Index 0 is already saved from reg offset
6029                                  * MPI2_REPLY_POST_HOST_INDEX_OFFSET
6030                                  */
6031                                 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
6032                                         instance->reply_post_host_index_addr[loop] =
6033                                                 (u32 __iomem *)
6034                                                 ((u8 __iomem *)instance->reg_set +
6035                                                 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
6036                                                 + (loop * 0x10));
6037                                 }
6038                         }
6039 
6040                         dev_info(&instance->pdev->dev,
6041                                  "firmware supports msix\t: (%d)",
6042                                  instance->msix_vectors);
6043                         if (msix_vectors)
6044                                 instance->msix_vectors = min(msix_vectors,
6045                                         instance->msix_vectors);
6046                 } else /* MFI adapters */
6047                         instance->msix_vectors = 1;
6048 
6049 
6050                 /*
6051                  * For Aero (if some conditions are met), driver will configure a
6052                  * few additional reply queues with interrupt coalescing enabled.
6053                  * These queues with interrupt coalescing enabled are called
6054                  * High IOPS queues and rest of reply queues (based on number of
6055                  * logical CPUs) are termed as Low latency queues.
6056                  *
6057                  * Total Number of reply queues = High IOPS queues + low latency queues
6058                  *
6059                  * For rest of fusion adapters, 1 additional reply queue will be
6060                  * reserved for management commands, rest of reply queues
6061                  * (based on number of logical CPUs) will be used for IOs and
6062                  * referenced as IO queues.
6063                  * Total Number of reply queues = 1 + IO queues
6064                  *
6065                  * MFI adapters supports single MSI-x so single reply queue
6066                  * will be used for IO and management commands.
6067                  */
6068 
6069                 intr_coalescing = (scratch_pad_1 & MR_INTR_COALESCING_SUPPORT_OFFSET) ?
6070                                                                 true : false;
6071                 if (intr_coalescing &&
6072                         (num_online_cpus() >= MR_HIGH_IOPS_QUEUE_COUNT) &&
6073                         (instance->msix_vectors == MEGASAS_MAX_MSIX_QUEUES))
6074                         instance->perf_mode = MR_BALANCED_PERF_MODE;
6075                 else
6076                         instance->perf_mode = MR_LATENCY_PERF_MODE;
6077 
6078 
6079                 if (instance->adapter_type == AERO_SERIES) {
6080                         pcie_capability_read_word(instance->pdev, PCI_EXP_LNKSTA, &lnksta);
6081                         speed = lnksta & PCI_EXP_LNKSTA_CLS;
6082 
6083                         /*
6084                          * For Aero, if PCIe link speed is <16 GT/s, then driver should operate
6085                          * in latency perf mode and enable R1 PCI bandwidth algorithm
6086                          */
6087                         if (speed < 0x4) {
6088                                 instance->perf_mode = MR_LATENCY_PERF_MODE;
6089                                 fusion->pcie_bw_limitation = true;
6090                         }
6091 
6092                         /*
6093                          * Performance mode settings provided through module parameter-perf_mode will
6094                          * take affect only for:
6095                          * 1. Aero family of adapters.
6096                          * 2. When user sets module parameter- perf_mode in range of 0-2.
6097                          */
6098                         if ((perf_mode >= MR_BALANCED_PERF_MODE) &&
6099                                 (perf_mode <= MR_LATENCY_PERF_MODE))
6100                                 instance->perf_mode = perf_mode;
6101                         /*
6102                          * If intr coalescing is not supported by controller FW, then IOPS
6103                          * and Balanced modes are not feasible.
6104                          */
6105                         if (!intr_coalescing)
6106                                 instance->perf_mode = MR_LATENCY_PERF_MODE;
6107 
6108                 }
6109 
6110                 if (instance->perf_mode == MR_BALANCED_PERF_MODE)
6111                         instance->low_latency_index_start =
6112                                 MR_HIGH_IOPS_QUEUE_COUNT;
6113                 else
6114                         instance->low_latency_index_start = 1;
6115 
6116                 num_msix_req = num_online_cpus() + instance->low_latency_index_start;
6117 
6118                 instance->msix_vectors = min(num_msix_req,
6119                                 instance->msix_vectors);
6120 
6121                 megasas_alloc_irq_vectors(instance);
6122                 if (!instance->msix_vectors)
6123                         instance->msix_load_balance = false;
6124         }
6125         /*
6126          * MSI-X host index 0 is common for all adapter.
6127          * It is used for all MPT based Adapters.
6128          */
6129         if (instance->msix_combined) {
6130                 instance->reply_post_host_index_addr[0] =
6131                                 (u32 *)((u8 *)instance->reg_set +
6132                                 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET);
6133         } else {
6134                 instance->reply_post_host_index_addr[0] =
6135                         (u32 *)((u8 *)instance->reg_set +
6136                         MPI2_REPLY_POST_HOST_INDEX_OFFSET);
6137         }
6138 
6139         if (!instance->msix_vectors) {
6140                 i = pci_alloc_irq_vectors(instance->pdev, 1, 1, PCI_IRQ_LEGACY);
6141                 if (i < 0)
6142                         goto fail_init_adapter;
6143         }
6144 
6145         megasas_setup_reply_map(instance);
6146 
6147         dev_info(&instance->pdev->dev,
6148                 "current msix/online cpus\t: (%d/%d)\n",
6149                 instance->msix_vectors, (unsigned int)num_online_cpus());
6150         dev_info(&instance->pdev->dev,
6151                 "RDPQ mode\t: (%s)\n", instance->is_rdpq ? "enabled" : "disabled");
6152 
6153         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
6154                 (unsigned long)instance);
6155 
6156         /*
6157          * Below are default value for legacy Firmware.
6158          * non-fusion based controllers
6159          */
6160         instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
6161         instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
6162         /* Get operational params, sge flags, send init cmd to controller */
6163         if (instance->instancet->init_adapter(instance))
6164                 goto fail_init_adapter;
6165 
6166         if (instance->adapter_type >= VENTURA_SERIES) {
6167                 scratch_pad_3 =
6168                         megasas_readl(instance,
6169                                       &instance->reg_set->outbound_scratch_pad_3);
6170                 if ((scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK) >=
6171                         MR_DEFAULT_NVME_PAGE_SHIFT)
6172                         instance->nvme_page_size =
6173                                 (1 << (scratch_pad_3 & MR_NVME_PAGE_SIZE_MASK));
6174 
6175                 dev_info(&instance->pdev->dev,
6176                          "NVME page size\t: (%d)\n", instance->nvme_page_size);
6177         }
6178 
6179         if (instance->msix_vectors ?
6180                 megasas_setup_irqs_msix(instance, 1) :
6181                 megasas_setup_irqs_ioapic(instance))
6182                 goto fail_init_adapter;
6183 
6184         if (instance->adapter_type != MFI_SERIES)
6185                 megasas_setup_irq_poll(instance);
6186 
6187         instance->instancet->enable_intr(instance);
6188 
6189         dev_info(&instance->pdev->dev, "INIT adapter done\n");
6190 
6191         megasas_setup_jbod_map(instance);
6192 
6193         if (megasas_get_device_list(instance) != SUCCESS) {
6194                 dev_err(&instance->pdev->dev,
6195                         "%s: megasas_get_device_list failed\n",
6196                         __func__);
6197                 goto fail_get_ld_pd_list;
6198         }
6199 
6200         /* stream detection initialization */
6201         if (instance->adapter_type >= VENTURA_SERIES) {
6202                 fusion->stream_detect_by_ld =
6203                         kcalloc(MAX_LOGICAL_DRIVES_EXT,
6204                                 sizeof(struct LD_STREAM_DETECT *),
6205                                 GFP_KERNEL);
6206                 if (!fusion->stream_detect_by_ld) {
6207                         dev_err(&instance->pdev->dev,
6208                                 "unable to allocate stream detection for pool of LDs\n");
6209                         goto fail_get_ld_pd_list;
6210                 }
6211                 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i) {
6212                         fusion->stream_detect_by_ld[i] =
6213                                 kzalloc(sizeof(struct LD_STREAM_DETECT),
6214                                 GFP_KERNEL);
6215                         if (!fusion->stream_detect_by_ld[i]) {
6216                                 dev_err(&instance->pdev->dev,
6217                                         "unable to allocate stream detect by LD\n ");
6218                                 for (j = 0; j < i; ++j)
6219                                         kfree(fusion->stream_detect_by_ld[j]);
6220                                 kfree(fusion->stream_detect_by_ld);
6221                                 fusion->stream_detect_by_ld = NULL;
6222                                 goto fail_get_ld_pd_list;
6223                         }
6224                         fusion->stream_detect_by_ld[i]->mru_bit_map
6225                                 = MR_STREAM_BITMAP;
6226                 }
6227         }
6228 
6229         /*
6230          * Compute the max allowed sectors per IO: The controller info has two
6231          * limits on max sectors. Driver should use the minimum of these two.
6232          *
6233          * 1 << stripe_sz_ops.min = max sectors per strip
6234          *
6235          * Note that older firmwares ( < FW ver 30) didn't report information
6236          * to calculate max_sectors_1. So the number ended up as zero always.
6237          */
6238         tmp_sectors = 0;
6239         ctrl_info = instance->ctrl_info_buf;
6240 
6241         max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
6242                 le16_to_cpu(ctrl_info->max_strips_per_io);
6243         max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
6244 
6245         tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
6246 
6247         instance->peerIsPresent = ctrl_info->cluster.peerIsPresent;
6248         instance->passive = ctrl_info->cluster.passive;
6249         memcpy(instance->clusterId, ctrl_info->clusterId, sizeof(instance->clusterId));
6250         instance->UnevenSpanSupport =
6251                 ctrl_info->adapterOperations2.supportUnevenSpans;
6252         if (instance->UnevenSpanSupport) {
6253                 struct fusion_context *fusion = instance->ctrl_context;
6254                 if (MR_ValidateMapInfo(instance, instance->map_id))
6255                         fusion->fast_path_io = 1;
6256                 else
6257                         fusion->fast_path_io = 0;
6258 
6259         }
6260         if (ctrl_info->host_interface.SRIOV) {
6261                 instance->requestorId = ctrl_info->iov.requestorId;
6262                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
6263                         if (!ctrl_info->adapterOperations2.activePassive)
6264                             instance->PlasmaFW111 = 1;
6265 
6266                         dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
6267                             instance->PlasmaFW111 ? "1.11" : "new");
6268 
6269                         if (instance->PlasmaFW111) {
6270                             iovPtr = (struct IOV_111 *)
6271                                 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
6272                             instance->requestorId = iovPtr->requestorId;
6273                         }
6274                 }
6275                 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
6276                         instance->requestorId);
6277         }
6278 
6279         instance->crash_dump_fw_support =
6280                 ctrl_info->adapterOperations3.supportCrashDump;
6281         instance->crash_dump_drv_support =
6282                 (instance->crash_dump_fw_support &&
6283                 instance->crash_dump_buf);
6284         if (instance->crash_dump_drv_support)
6285                 megasas_set_crash_dump_params(instance,
6286                         MR_CRASH_BUF_TURN_OFF);
6287 
6288         else {
6289                 if (instance->crash_dump_buf)
6290                         dma_free_coherent(&instance->pdev->dev,
6291                                 CRASH_DMA_BUF_SIZE,
6292                                 instance->crash_dump_buf,
6293                                 instance->crash_dump_h);
6294                 instance->crash_dump_buf = NULL;
6295         }
6296 
6297         if (instance->snapdump_wait_time) {
6298                 megasas_get_snapdump_properties(instance);
6299                 dev_info(&instance->pdev->dev, "Snap dump wait time\t: %d\n",
6300                          instance->snapdump_wait_time);
6301         }
6302 
6303         dev_info(&instance->pdev->dev,
6304                 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
6305                 le16_to_cpu(ctrl_info->pci.vendor_id),
6306                 le16_to_cpu(ctrl_info->pci.device_id),
6307                 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
6308                 le16_to_cpu(ctrl_info->pci.sub_device_id));
6309         dev_info(&instance->pdev->dev, "unevenspan support      : %s\n",
6310                 instance->UnevenSpanSupport ? "yes" : "no");
6311         dev_info(&instance->pdev->dev, "firmware crash dump     : %s\n",
6312                 instance->crash_dump_drv_support ? "yes" : "no");
6313         dev_info(&instance->pdev->dev, "JBOD sequence map       : %s\n",
6314                 instance->use_seqnum_jbod_fp ? "enabled" : "disabled");
6315 
6316         instance->max_sectors_per_req = instance->max_num_sge *
6317                                                 SGE_BUFFER_SIZE / 512;
6318         if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
6319                 instance->max_sectors_per_req = tmp_sectors;
6320 
6321         /* Check for valid throttlequeuedepth module parameter */
6322         if (throttlequeuedepth &&
6323                         throttlequeuedepth <= instance->max_scsi_cmds)
6324                 instance->throttlequeuedepth = throttlequeuedepth;
6325         else
6326                 instance->throttlequeuedepth =
6327                                 MEGASAS_THROTTLE_QUEUE_DEPTH;
6328 
6329         if ((resetwaittime < 1) ||
6330             (resetwaittime > MEGASAS_RESET_WAIT_TIME))
6331                 resetwaittime = MEGASAS_RESET_WAIT_TIME;
6332 
6333         if ((scmd_timeout < 10) || (scmd_timeout > MEGASAS_DEFAULT_CMD_TIMEOUT))
6334                 scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
6335 
6336         /* Launch SR-IOV heartbeat timer */
6337         if (instance->requestorId) {
6338                 if (!megasas_sriov_start_heartbeat(instance, 1)) {
6339                         megasas_start_timer(instance);
6340                 } else {
6341                         instance->skip_heartbeat_timer_del = 1;
6342                         goto fail_get_ld_pd_list;
6343                 }
6344         }
6345 
6346         /*
6347          * Create and start watchdog thread which will monitor
6348          * controller state every 1 sec and trigger OCR when
6349          * it enters fault state
6350          */
6351         if (instance->adapter_type != MFI_SERIES)
6352                 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
6353                         goto fail_start_watchdog;
6354 
6355         return 0;
6356 
6357 fail_start_watchdog:
6358         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
6359                 del_timer_sync(&instance->sriov_heartbeat_timer);
6360 fail_get_ld_pd_list:
6361         instance->instancet->disable_intr(instance);
6362         megasas_destroy_irqs(instance);
6363 fail_init_adapter:
6364         if (instance->msix_vectors)
6365                 pci_free_irq_vectors(instance->pdev);
6366         instance->msix_vectors = 0;
6367 fail_alloc_dma_buf:
6368         megasas_free_ctrl_dma_buffers(instance);
6369         megasas_free_ctrl_mem(instance);
6370 fail_ready_state:
6371         iounmap(instance->reg_set);
6372 
6373 fail_ioremap:
6374         pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6375 
6376         dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6377                 __func__, __LINE__);
6378         return -EINVAL;
6379 }
6380 
6381 /**
6382  * megasas_release_mfi -        Reverses the FW initialization
6383  * @instance:                   Adapter soft state
6384  */
6385 static void megasas_release_mfi(struct megasas_instance *instance)
6386 {
6387         u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
6388 
6389         if (instance->reply_queue)
6390                 dma_free_coherent(&instance->pdev->dev, reply_q_sz,
6391                             instance->reply_queue, instance->reply_queue_h);
6392 
6393         megasas_free_cmds(instance);
6394 
6395         iounmap(instance->reg_set);
6396 
6397         pci_release_selected_regions(instance->pdev, 1<<instance->bar);
6398 }
6399 
6400 /**
6401  * megasas_get_seq_num -        Gets latest event sequence numbers
6402  * @instance:                   Adapter soft state
6403  * @eli:                        FW event log sequence numbers information
6404  *
6405  * FW maintains a log of all events in a non-volatile area. Upper layers would
6406  * usually find out the latest sequence number of the events, the seq number at
6407  * the boot etc. They would "read" all the events below the latest seq number
6408  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
6409  * number), they would subsribe to AEN (asynchronous event notification) and
6410  * wait for the events to happen.
6411  */
6412 static int
6413 megasas_get_seq_num(struct megasas_instance *instance,
6414                     struct megasas_evt_log_info *eli)
6415 {
6416         struct megasas_cmd *cmd;
6417         struct megasas_dcmd_frame *dcmd;
6418         struct megasas_evt_log_info *el_info;
6419         dma_addr_t el_info_h = 0;
6420         int ret;
6421 
6422         cmd = megasas_get_cmd(instance);
6423 
6424         if (!cmd) {
6425                 return -ENOMEM;
6426         }
6427 
6428         dcmd = &cmd->frame->dcmd;
6429         el_info = dma_alloc_coherent(&instance->pdev->dev,
6430                                      sizeof(struct megasas_evt_log_info),
6431                                      &el_info_h, GFP_KERNEL);
6432         if (!el_info) {
6433                 megasas_return_cmd(instance, cmd);
6434                 return -ENOMEM;
6435         }
6436 
6437         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6438 
6439         dcmd->cmd = MFI_CMD_DCMD;
6440         dcmd->cmd_status = 0x0;
6441         dcmd->sge_count = 1;
6442         dcmd->flags = MFI_FRAME_DIR_READ;
6443         dcmd->timeout = 0;
6444         dcmd->pad_0 = 0;
6445         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
6446         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
6447 
6448         megasas_set_dma_settings(instance, dcmd, el_info_h,
6449                                  sizeof(struct megasas_evt_log_info));
6450 
6451         ret = megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS);
6452         if (ret != DCMD_SUCCESS) {
6453                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
6454                         __func__, __LINE__);
6455                 goto dcmd_failed;
6456         }
6457 
6458         /*
6459          * Copy the data back into callers buffer
6460          */
6461         eli->newest_seq_num = el_info->newest_seq_num;
6462         eli->oldest_seq_num = el_info->oldest_seq_num;
6463         eli->clear_seq_num = el_info->clear_seq_num;
6464         eli->shutdown_seq_num = el_info->shutdown_seq_num;
6465         eli->boot_seq_num = el_info->boot_seq_num;
6466 
6467 dcmd_failed:
6468         dma_free_coherent(&instance->pdev->dev,
6469                         sizeof(struct megasas_evt_log_info),
6470                         el_info, el_info_h);
6471 
6472         megasas_return_cmd(instance, cmd);
6473 
6474         return ret;
6475 }
6476 
6477 /**
6478  * megasas_register_aen -       Registers for asynchronous event notification
6479  * @instance:                   Adapter soft state
6480  * @seq_num:                    The starting sequence number
6481  * @class_locale:               Class of the event
6482  *
6483  * This function subscribes for AEN for events beyond the @seq_num. It requests
6484  * to be notified if and only if the event is of type @class_locale
6485  */
6486 static int
6487 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
6488                      u32 class_locale_word)
6489 {
6490         int ret_val;
6491         struct megasas_cmd *cmd;
6492         struct megasas_dcmd_frame *dcmd;
6493         union megasas_evt_class_locale curr_aen;
6494         union megasas_evt_class_locale prev_aen;
6495 
6496         /*
6497          * If there an AEN pending already (aen_cmd), check if the
6498          * class_locale of that pending AEN is inclusive of the new
6499          * AEN request we currently have. If it is, then we don't have
6500          * to do anything. In other words, whichever events the current
6501          * AEN request is subscribing to, have already been subscribed
6502          * to.
6503          *
6504          * If the old_cmd is _not_ inclusive, then we have to abort
6505          * that command, form a class_locale that is superset of both
6506          * old and current and re-issue to the FW
6507          */
6508 
6509         curr_aen.word = class_locale_word;
6510 
6511         if (instance->aen_cmd) {
6512 
6513                 prev_aen.word =
6514                         le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
6515 
6516                 if ((curr_aen.members.class < MFI_EVT_CLASS_DEBUG) ||
6517                     (curr_aen.members.class > MFI_EVT_CLASS_DEAD)) {
6518                         dev_info(&instance->pdev->dev,
6519                                  "%s %d out of range class %d send by application\n",
6520                                  __func__, __LINE__, curr_aen.members.class);
6521                         return 0;
6522                 }
6523 
6524                 /*
6525                  * A class whose enum value is smaller is inclusive of all
6526                  * higher values. If a PROGRESS (= -1) was previously
6527                  * registered, then a new registration requests for higher
6528                  * classes need not be sent to FW. They are automatically
6529                  * included.
6530                  *
6531                  * Locale numbers don't have such hierarchy. They are bitmap
6532                  * values
6533                  */
6534                 if ((prev_aen.members.class <= curr_aen.members.class) &&
6535                     !((prev_aen.members.locale & curr_aen.members.locale) ^
6536                       curr_aen.members.locale)) {
6537                         /*
6538                          * Previously issued event registration includes
6539                          * current request. Nothing to do.
6540                          */
6541                         return 0;
6542                 } else {
6543                         curr_aen.members.locale |= prev_aen.members.locale;
6544 
6545                         if (prev_aen.members.class < curr_aen.members.class)
6546                                 curr_aen.members.class = prev_aen.members.class;
6547 
6548                         instance->aen_cmd->abort_aen = 1;
6549                         ret_val = megasas_issue_blocked_abort_cmd(instance,
6550                                                                   instance->
6551                                                                   aen_cmd, 30);
6552 
6553                         if (ret_val) {
6554                                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
6555                                        "previous AEN command\n");
6556                                 return ret_val;
6557                         }
6558                 }
6559         }
6560 
6561         cmd = megasas_get_cmd(instance);
6562 
6563         if (!cmd)
6564                 return -ENOMEM;
6565 
6566         dcmd = &cmd->frame->dcmd;
6567 
6568         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
6569 
6570         /*
6571          * Prepare DCMD for aen registration
6572          */
6573         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6574 
6575         dcmd->cmd = MFI_CMD_DCMD;
6576         dcmd->cmd_status = 0x0;
6577         dcmd->sge_count = 1;
6578         dcmd->flags = MFI_FRAME_DIR_READ;
6579         dcmd->timeout = 0;
6580         dcmd->pad_0 = 0;
6581         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
6582         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
6583         dcmd->mbox.w[0] = cpu_to_le32(seq_num);
6584         instance->last_seq_num = seq_num;
6585         dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
6586 
6587         megasas_set_dma_settings(instance, dcmd, instance->evt_detail_h,
6588                                  sizeof(struct megasas_evt_detail));
6589 
6590         if (instance->aen_cmd != NULL) {
6591                 megasas_return_cmd(instance, cmd);
6592                 return 0;
6593         }
6594 
6595         /*
6596          * Store reference to the cmd used to register for AEN. When an
6597          * application wants us to register for AEN, we have to abort this
6598          * cmd and re-register with a new EVENT LOCALE supplied by that app
6599          */
6600         instance->aen_cmd = cmd;
6601 
6602         /*
6603          * Issue the aen registration frame
6604          */
6605         instance->instancet->issue_dcmd(instance, cmd);
6606 
6607         return 0;
6608 }
6609 
6610 /* megasas_get_target_prop - Send DCMD with below details to firmware.
6611  *
6612  * This DCMD will fetch few properties of LD/system PD defined
6613  * in MR_TARGET_DEV_PROPERTIES. eg. Queue Depth, MDTS value.
6614  *
6615  * DCMD send by drivers whenever new target is added to the OS.
6616  *
6617  * dcmd.opcode         - MR_DCMD_DEV_GET_TARGET_PROP
6618  * dcmd.mbox.b[0]      - DCMD is to be fired for LD or system PD.
6619  *                       0 = system PD, 1 = LD.
6620  * dcmd.mbox.s[1]      - TargetID for LD/system PD.
6621  * dcmd.sge IN         - Pointer to return MR_TARGET_DEV_PROPERTIES.
6622  *
6623  * @instance:           Adapter soft state
6624  * @sdev:               OS provided scsi device
6625  *
6626  * Returns 0 on success non-zero on failure.
6627  */
6628 int
6629 megasas_get_target_prop(struct megasas_instance *instance,
6630                         struct scsi_device *sdev)
6631 {
6632         int ret;
6633         struct megasas_cmd *cmd;
6634         struct megasas_dcmd_frame *dcmd;
6635         u16 targetId = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) +
6636                         sdev->id;
6637 
6638         cmd = megasas_get_cmd(instance);
6639 
6640         if (!cmd) {
6641                 dev_err(&instance->pdev->dev,
6642                         "Failed to get cmd %s\n", __func__);
6643                 return -ENOMEM;
6644         }
6645 
6646         dcmd = &cmd->frame->dcmd;
6647 
6648         memset(instance->tgt_prop, 0, sizeof(*instance->tgt_prop));
6649         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
6650         dcmd->mbox.b[0] = MEGASAS_IS_LOGICAL(sdev);
6651 
6652         dcmd->mbox.s[1] = cpu_to_le16(targetId);
6653         dcmd->cmd = MFI_CMD_DCMD;
6654         dcmd->cmd_status = 0xFF;
6655         dcmd->sge_count = 1;
6656         dcmd->flags = MFI_FRAME_DIR_READ;
6657         dcmd->timeout = 0;
6658         dcmd->pad_0 = 0;
6659         dcmd->data_xfer_len =
6660                 cpu_to_le32(sizeof(struct MR_TARGET_PROPERTIES));
6661         dcmd->opcode = cpu_to_le32(MR_DCMD_DRV_GET_TARGET_PROP);
6662 
6663         megasas_set_dma_settings(instance, dcmd, instance->tgt_prop_h,
6664                                  sizeof(struct MR_TARGET_PROPERTIES));
6665 
6666         if ((instance->adapter_type != MFI_SERIES) &&
6667             !instance->mask_interrupts)
6668                 ret = megasas_issue_blocked_cmd(instance,
6669                                                 cmd, MFI_IO_TIMEOUT_SECS);
6670         else
6671                 ret = megasas_issue_polled(instance, cmd);
6672 
6673         switch (ret) {
6674         case DCMD_TIMEOUT:
6675                 switch (dcmd_timeout_ocr_possible(instance)) {
6676                 case INITIATE_OCR:
6677                         cmd->flags |= DRV_DCMD_SKIP_REFIRE;
6678                         mutex_unlock(&instance->reset_mutex);
6679                         megasas_reset_fusion(instance->host,
6680                                              MFI_IO_TIMEOUT_OCR);
6681                         mutex_lock(&instance->reset_mutex);
6682                         break;
6683                 case KILL_ADAPTER:
6684                         megaraid_sas_kill_hba(instance);
6685                         break;
6686                 case IGNORE_TIMEOUT:
6687                         dev_info(&instance->pdev->dev,
6688                                  "Ignore DCMD timeout: %s %d\n",
6689                                  __func__, __LINE__);
6690                         break;
6691                 }
6692                 break;
6693 
6694         default:
6695                 megasas_return_cmd(instance, cmd);
6696         }
6697         if (ret != DCMD_SUCCESS)
6698                 dev_err(&instance->pdev->dev,
6699                         "return from %s %d return value %d\n",
6700                         __func__, __LINE__, ret);
6701 
6702         return ret;
6703 }
6704 
6705 /**
6706  * megasas_start_aen -  Subscribes to AEN during driver load time
6707  * @instance:           Adapter soft state
6708  */
6709 static int megasas_start_aen(struct megasas_instance *instance)
6710 {
6711         struct megasas_evt_log_info eli;
6712         union megasas_evt_class_locale class_locale;
6713 
6714         /*
6715          * Get the latest sequence number from FW
6716          */
6717         memset(&eli, 0, sizeof(eli));
6718 
6719         if (megasas_get_seq_num(instance, &eli))
6720                 return -1;
6721 
6722         /*
6723          * Register AEN with FW for latest sequence number plus 1
6724          */
6725         class_locale.members.reserved = 0;
6726         class_locale.members.locale = MR_EVT_LOCALE_ALL;
6727         class_locale.members.class = MR_EVT_CLASS_DEBUG;
6728 
6729         return megasas_register_aen(instance,
6730                         le32_to_cpu(eli.newest_seq_num) + 1,
6731                         class_locale.word);
6732 }
6733 
6734 /**
6735  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
6736  * @instance:           Adapter soft state
6737  */
6738 static int megasas_io_attach(struct megasas_instance *instance)
6739 {
6740         struct Scsi_Host *host = instance->host;
6741 
6742         /*
6743          * Export parameters required by SCSI mid-layer
6744          */
6745         host->unique_id = instance->unique_id;
6746         host->can_queue = instance->max_scsi_cmds;
6747         host->this_id = instance->init_id;
6748         host->sg_tablesize = instance->max_num_sge;
6749 
6750         if (instance->fw_support_ieee)
6751                 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
6752 
6753         /*
6754          * Check if the module parameter value for max_sectors can be used
6755          */
6756         if (max_sectors && max_sectors < instance->max_sectors_per_req)
6757                 instance->max_sectors_per_req = max_sectors;
6758         else {
6759                 if (max_sectors) {
6760                         if (((instance->pdev->device ==
6761                                 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
6762                                 (instance->pdev->device ==
6763                                 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
6764                                 (max_sectors <= MEGASAS_MAX_SECTORS)) {
6765                                 instance->max_sectors_per_req = max_sectors;
6766                         } else {
6767                         dev_info(&instance->pdev->dev, "max_sectors should be > 0"
6768                                 "and <= %d (or < 1MB for GEN2 controller)\n",
6769                                 instance->max_sectors_per_req);
6770                         }
6771                 }
6772         }
6773 
6774         host->max_sectors = instance->max_sectors_per_req;
6775         host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
6776         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
6777         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
6778         host->max_lun = MEGASAS_MAX_LUN;
6779         host->max_cmd_len = 16;
6780 
6781         /*
6782          * Notify the mid-layer about the new controller
6783          */
6784         if (scsi_add_host(host, &instance->pdev->dev)) {
6785                 dev_err(&instance->pdev->dev,
6786                         "Failed to add host from %s %d\n",
6787                         __func__, __LINE__);
6788                 return -ENODEV;
6789         }
6790 
6791         return 0;
6792 }
6793 
6794 /**
6795  * megasas_set_dma_mask -       Set DMA mask for supported controllers
6796  *
6797  * @instance:           Adapter soft state
6798  * Description:
6799  *
6800  * For Ventura, driver/FW will operate in 63bit DMA addresses.
6801  *
6802  * For invader-
6803  *      By default, driver/FW will operate in 32bit DMA addresses
6804  *      for consistent DMA mapping but if 32 bit consistent
6805  *      DMA mask fails, driver will try with 63 bit consistent
6806  *      mask provided FW is true 63bit DMA capable
6807  *
6808  * For older controllers(Thunderbolt and MFI based adapters)-
6809  *      driver/FW will operate in 32 bit consistent DMA addresses.
6810  */
6811 static int
6812 megasas_set_dma_mask(struct megasas_instance *instance)
6813 {
6814         u64 consistent_mask;
6815         struct pci_dev *pdev;
6816         u32 scratch_pad_1;
6817 
6818         pdev = instance->pdev;
6819         consistent_mask = (instance->adapter_type >= VENTURA_SERIES) ?
6820                                 DMA_BIT_MASK(63) : DMA_BIT_MASK(32);
6821 
6822         if (IS_DMA64) {
6823                 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(63)) &&
6824                     dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6825                         goto fail_set_dma_mask;
6826 
6827                 if ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) &&
6828                     (dma_set_coherent_mask(&pdev->dev, consistent_mask) &&
6829                      dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))) {
6830                         /*
6831                          * If 32 bit DMA mask fails, then try for 64 bit mask
6832                          * for FW capable of handling 64 bit DMA.
6833                          */
6834                         scratch_pad_1 = megasas_readl
6835                                 (instance, &instance->reg_set->outbound_scratch_pad_1);
6836 
6837                         if (!(scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET))
6838                                 goto fail_set_dma_mask;
6839                         else if (dma_set_mask_and_coherent(&pdev->dev,
6840                                                            DMA_BIT_MASK(63)))
6841                                 goto fail_set_dma_mask;
6842                 }
6843         } else if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32)))
6844                 goto fail_set_dma_mask;
6845 
6846         if (pdev->dev.coherent_dma_mask == DMA_BIT_MASK(32))
6847                 instance->consistent_mask_64bit = false;
6848         else
6849                 instance->consistent_mask_64bit = true;
6850 
6851         dev_info(&pdev->dev, "%s bit DMA mask and %s bit consistent mask\n",
6852                  ((*pdev->dev.dma_mask == DMA_BIT_MASK(63)) ? "63" : "32"),
6853                  (instance->consistent_mask_64bit ? "63" : "32"));
6854 
6855         return 0;
6856 
6857 fail_set_dma_mask:
6858         dev_err(&pdev->dev, "Failed to set DMA mask\n");
6859         return -1;
6860 
6861 }
6862 
6863 /*
6864  * megasas_set_adapter_type -   Set adapter type.
6865  *                              Supported controllers can be divided in
6866  *                              different categories-
6867  *                                      enum MR_ADAPTER_TYPE {
6868  *                                              MFI_SERIES = 1,
6869  *                                              THUNDERBOLT_SERIES = 2,
6870  *                                              INVADER_SERIES = 3,
6871  *                                              VENTURA_SERIES = 4,
6872  *                                              AERO_SERIES = 5,
6873  *                                      };
6874  * @instance:                   Adapter soft state
6875  * return:                      void
6876  */
6877 static inline void megasas_set_adapter_type(struct megasas_instance *instance)
6878 {
6879         if ((instance->pdev->vendor == PCI_VENDOR_ID_DELL) &&
6880             (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5)) {
6881                 instance->adapter_type = MFI_SERIES;
6882         } else {
6883                 switch (instance->pdev->device) {
6884                 case PCI_DEVICE_ID_LSI_AERO_10E1:
6885                 case PCI_DEVICE_ID_LSI_AERO_10E2:
6886                 case PCI_DEVICE_ID_LSI_AERO_10E5:
6887                 case PCI_DEVICE_ID_LSI_AERO_10E6:
6888                         instance->adapter_type = AERO_SERIES;
6889                         break;
6890                 case PCI_DEVICE_ID_LSI_VENTURA:
6891                 case PCI_DEVICE_ID_LSI_CRUSADER:
6892                 case PCI_DEVICE_ID_LSI_HARPOON:
6893                 case PCI_DEVICE_ID_LSI_TOMCAT:
6894                 case PCI_DEVICE_ID_LSI_VENTURA_4PORT:
6895                 case PCI_DEVICE_ID_LSI_CRUSADER_4PORT:
6896                         instance->adapter_type = VENTURA_SERIES;
6897                         break;
6898                 case PCI_DEVICE_ID_LSI_FUSION:
6899                 case PCI_DEVICE_ID_LSI_PLASMA:
6900                         instance->adapter_type = THUNDERBOLT_SERIES;
6901                         break;
6902                 case PCI_DEVICE_ID_LSI_INVADER:
6903                 case PCI_DEVICE_ID_LSI_INTRUDER:
6904                 case PCI_DEVICE_ID_LSI_INTRUDER_24:
6905                 case PCI_DEVICE_ID_LSI_CUTLASS_52:
6906                 case PCI_DEVICE_ID_LSI_CUTLASS_53:
6907                 case PCI_DEVICE_ID_LSI_FURY:
6908                         instance->adapter_type = INVADER_SERIES;
6909                         break;
6910                 default: /* For all other supported controllers */
6911                         instance->adapter_type = MFI_SERIES;
6912                         break;
6913                 }
6914         }
6915 }
6916 
6917 static inline int megasas_alloc_mfi_ctrl_mem(struct megasas_instance *instance)
6918 {
6919         instance->producer = dma_alloc_coherent(&instance->pdev->dev,
6920                         sizeof(u32), &instance->producer_h, GFP_KERNEL);
6921         instance->consumer = dma_alloc_coherent(&instance->pdev->dev,
6922                         sizeof(u32), &instance->consumer_h, GFP_KERNEL);
6923 
6924         if (!instance->producer || !instance->consumer) {
6925                 dev_err(&instance->pdev->dev,
6926                         "Failed to allocate memory for producer, consumer\n");
6927                 return -1;
6928         }
6929 
6930         *instance->producer = 0;
6931         *instance->consumer = 0;
6932         return 0;
6933 }
6934 
6935 /**
6936  * megasas_alloc_ctrl_mem -     Allocate per controller memory for core data
6937  *                              structures which are not common across MFI
6938  *                              adapters and fusion adapters.
6939  *                              For MFI based adapters, allocate producer and
6940  *                              consumer buffers. For fusion adapters, allocate
6941  *                              memory for fusion context.
6942  * @instance:                   Adapter soft state
6943  * return:                      0 for SUCCESS
6944  */
6945 static int megasas_alloc_ctrl_mem(struct megasas_instance *instance)
6946 {
6947         instance->reply_map = kcalloc(nr_cpu_ids, sizeof(unsigned int),
6948                                       GFP_KERNEL);
6949         if (!instance->reply_map)
6950                 return -ENOMEM;
6951 
6952         switch (instance->adapter_type) {
6953         case MFI_SERIES:
6954                 if (megasas_alloc_mfi_ctrl_mem(instance))
6955                         goto fail;
6956                 break;
6957         case AERO_SERIES:
6958         case VENTURA_SERIES:
6959         case THUNDERBOLT_SERIES:
6960         case INVADER_SERIES:
6961                 if (megasas_alloc_fusion_context(instance))
6962                         goto fail;
6963                 break;
6964         }
6965 
6966         return 0;
6967  fail:
6968         kfree(instance->reply_map);
6969         instance->reply_map = NULL;
6970         return -ENOMEM;
6971 }
6972 
6973 /*
6974  * megasas_free_ctrl_mem -      Free fusion context for fusion adapters and
6975  *                              producer, consumer buffers for MFI adapters
6976  *
6977  * @instance -                  Adapter soft instance
6978  *
6979  */
6980 static inline void megasas_free_ctrl_mem(struct megasas_instance *instance)
6981 {
6982         kfree(instance->reply_map);
6983         if (instance->adapter_type == MFI_SERIES) {
6984                 if (instance->producer)
6985                         dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6986                                             instance->producer,
6987                                             instance->producer_h);
6988                 if (instance->consumer)
6989                         dma_free_coherent(&instance->pdev->dev, sizeof(u32),
6990                                             instance->consumer,
6991                                             instance->consumer_h);
6992         } else {
6993                 megasas_free_fusion_context(instance);
6994         }
6995 }
6996 
6997 /**
6998  * megasas_alloc_ctrl_dma_buffers -     Allocate consistent DMA buffers during
6999  *                                      driver load time
7000  *
7001  * @instance-                           Adapter soft instance
7002  * @return-                             O for SUCCESS
7003  */
7004 static inline
7005 int megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance)
7006 {
7007         struct pci_dev *pdev = instance->pdev;
7008         struct fusion_context *fusion = instance->ctrl_context;
7009 
7010         instance->evt_detail = dma_alloc_coherent(&pdev->dev,
7011                         sizeof(struct megasas_evt_detail),
7012                         &instance->evt_detail_h, GFP_KERNEL);
7013 
7014         if (!instance->evt_detail) {
7015                 dev_err(&instance->pdev->dev,
7016                         "Failed to allocate event detail buffer\n");
7017                 return -ENOMEM;
7018         }
7019 
7020         if (fusion) {
7021                 fusion->ioc_init_request =
7022                         dma_alloc_coherent(&pdev->dev,
7023                                            sizeof(struct MPI2_IOC_INIT_REQUEST),
7024                                            &fusion->ioc_init_request_phys,
7025                                            GFP_KERNEL);
7026 
7027                 if (!fusion->ioc_init_request) {
7028                         dev_err(&pdev->dev,
7029                                 "Failed to allocate PD list buffer\n");
7030                         return -ENOMEM;
7031                 }
7032 
7033                 instance->snapdump_prop = dma_alloc_coherent(&pdev->dev,
7034                                 sizeof(struct MR_SNAPDUMP_PROPERTIES),
7035                                 &instance->snapdump_prop_h, GFP_KERNEL);
7036 
7037                 if (!instance->snapdump_prop)
7038                         dev_err(&pdev->dev,
7039                                 "Failed to allocate snapdump properties buffer\n");
7040 
7041                 instance->host_device_list_buf = dma_alloc_coherent(&pdev->dev,
7042                                                         HOST_DEVICE_LIST_SZ,
7043                                                         &instance->host_device_list_buf_h,
7044                                                         GFP_KERNEL);
7045 
7046                 if (!instance->host_device_list_buf) {
7047                         dev_err(&pdev->dev,
7048                                 "Failed to allocate targetid list buffer\n");
7049                         return -ENOMEM;
7050                 }
7051 
7052         }
7053 
7054         instance->pd_list_buf =
7055                 dma_alloc_coherent(&pdev->dev,
7056                                      MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7057                                      &instance->pd_list_buf_h, GFP_KERNEL);
7058 
7059         if (!instance->pd_list_buf) {
7060                 dev_err(&pdev->dev, "Failed to allocate PD list buffer\n");
7061                 return -ENOMEM;
7062         }
7063 
7064         instance->ctrl_info_buf =
7065                 dma_alloc_coherent(&pdev->dev,
7066                                      sizeof(struct megasas_ctrl_info),
7067                                      &instance->ctrl_info_buf_h, GFP_KERNEL);
7068 
7069         if (!instance->ctrl_info_buf) {
7070                 dev_err(&pdev->dev,
7071                         "Failed to allocate controller info buffer\n");
7072                 return -ENOMEM;
7073         }
7074 
7075         instance->ld_list_buf =
7076                 dma_alloc_coherent(&pdev->dev,
7077                                      sizeof(struct MR_LD_LIST),
7078                                      &instance->ld_list_buf_h, GFP_KERNEL);
7079 
7080         if (!instance->ld_list_buf) {
7081                 dev_err(&pdev->dev, "Failed to allocate LD list buffer\n");
7082                 return -ENOMEM;
7083         }
7084 
7085         instance->ld_targetid_list_buf =
7086                 dma_alloc_coherent(&pdev->dev,
7087                                 sizeof(struct MR_LD_TARGETID_LIST),
7088                                 &instance->ld_targetid_list_buf_h, GFP_KERNEL);
7089 
7090         if (!instance->ld_targetid_list_buf) {
7091                 dev_err(&pdev->dev,
7092                         "Failed to allocate LD targetid list buffer\n");
7093                 return -ENOMEM;
7094         }
7095 
7096         if (!reset_devices) {
7097                 instance->system_info_buf =
7098                         dma_alloc_coherent(&pdev->dev,
7099                                         sizeof(struct MR_DRV_SYSTEM_INFO),
7100                                         &instance->system_info_h, GFP_KERNEL);
7101                 instance->pd_info =
7102                         dma_alloc_coherent(&pdev->dev,
7103                                         sizeof(struct MR_PD_INFO),
7104                                         &instance->pd_info_h, GFP_KERNEL);
7105                 instance->tgt_prop =
7106                         dma_alloc_coherent(&pdev->dev,
7107                                         sizeof(struct MR_TARGET_PROPERTIES),
7108                                         &instance->tgt_prop_h, GFP_KERNEL);
7109                 instance->crash_dump_buf =
7110                         dma_alloc_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7111                                         &instance->crash_dump_h, GFP_KERNEL);
7112 
7113                 if (!instance->system_info_buf)
7114                         dev_err(&instance->pdev->dev,
7115                                 "Failed to allocate system info buffer\n");
7116 
7117                 if (!instance->pd_info)
7118                         dev_err(&instance->pdev->dev,
7119                                 "Failed to allocate pd_info buffer\n");
7120 
7121                 if (!instance->tgt_prop)
7122                         dev_err(&instance->pdev->dev,
7123                                 "Failed to allocate tgt_prop buffer\n");
7124 
7125                 if (!instance->crash_dump_buf)
7126                         dev_err(&instance->pdev->dev,
7127                                 "Failed to allocate crash dump buffer\n");
7128         }
7129 
7130         return 0;
7131 }
7132 
7133 /*
7134  * megasas_free_ctrl_dma_buffers -      Free consistent DMA buffers allocated
7135  *                                      during driver load time
7136  *
7137  * @instance-                           Adapter soft instance
7138  *
7139  */
7140 static inline
7141 void megasas_free_ctrl_dma_buffers(struct megasas_instance *instance)
7142 {
7143         struct pci_dev *pdev = instance->pdev;
7144         struct fusion_context *fusion = instance->ctrl_context;
7145 
7146         if (instance->evt_detail)
7147                 dma_free_coherent(&pdev->dev, sizeof(struct megasas_evt_detail),
7148                                     instance->evt_detail,
7149                                     instance->evt_detail_h);
7150 
7151         if (fusion && fusion->ioc_init_request)
7152                 dma_free_coherent(&pdev->dev,
7153                                   sizeof(struct MPI2_IOC_INIT_REQUEST),
7154                                   fusion->ioc_init_request,
7155                                   fusion->ioc_init_request_phys);
7156 
7157         if (instance->pd_list_buf)
7158                 dma_free_coherent(&pdev->dev,
7159                                     MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
7160                                     instance->pd_list_buf,
7161                                     instance->pd_list_buf_h);
7162 
7163         if (instance->ld_list_buf)
7164                 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_LIST),
7165                                     instance->ld_list_buf,
7166                                     instance->ld_list_buf_h);
7167 
7168         if (instance->ld_targetid_list_buf)
7169                 dma_free_coherent(&pdev->dev, sizeof(struct MR_LD_TARGETID_LIST),
7170                                     instance->ld_targetid_list_buf,
7171                                     instance->ld_targetid_list_buf_h);
7172 
7173         if (instance->ctrl_info_buf)
7174                 dma_free_coherent(&pdev->dev, sizeof(struct megasas_ctrl_info),
7175                                     instance->ctrl_info_buf,
7176                                     instance->ctrl_info_buf_h);
7177 
7178         if (instance->system_info_buf)
7179                 dma_free_coherent(&pdev->dev, sizeof(struct MR_DRV_SYSTEM_INFO),
7180                                     instance->system_info_buf,
7181                                     instance->system_info_h);
7182 
7183         if (instance->pd_info)
7184                 dma_free_coherent(&pdev->dev, sizeof(struct MR_PD_INFO),
7185                                     instance->pd_info, instance->pd_info_h);
7186 
7187         if (instance->tgt_prop)
7188                 dma_free_coherent(&pdev->dev, sizeof(struct MR_TARGET_PROPERTIES),
7189                                     instance->tgt_prop, instance->tgt_prop_h);
7190 
7191         if (instance->crash_dump_buf)
7192                 dma_free_coherent(&pdev->dev, CRASH_DMA_BUF_SIZE,
7193                                     instance->crash_dump_buf,
7194                                     instance->crash_dump_h);
7195 
7196         if (instance->snapdump_prop)
7197                 dma_free_coherent(&pdev->dev,
7198                                   sizeof(struct MR_SNAPDUMP_PROPERTIES),
7199                                   instance->snapdump_prop,
7200                                   instance->snapdump_prop_h);
7201 
7202         if (instance->host_device_list_buf)
7203                 dma_free_coherent(&pdev->dev,
7204                                   HOST_DEVICE_LIST_SZ,
7205                                   instance->host_device_list_buf,
7206                                   instance->host_device_list_buf_h);
7207 
7208 }
7209 
7210 /*
7211  * megasas_init_ctrl_params -           Initialize controller's instance
7212  *                                      parameters before FW init
7213  * @instance -                          Adapter soft instance
7214  * @return -                            void
7215  */
7216 static inline void megasas_init_ctrl_params(struct megasas_instance *instance)
7217 {
7218         instance->fw_crash_state = UNAVAILABLE;
7219 
7220         megasas_poll_wait_aen = 0;
7221         instance->issuepend_done = 1;
7222         atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
7223 
7224         /*
7225          * Initialize locks and queues
7226          */
7227         INIT_LIST_HEAD(&instance->cmd_pool);
7228         INIT_LIST_HEAD(&instance->internal_reset_pending_q);
7229 
7230         atomic_set(&instance->fw_outstanding, 0);
7231         atomic64_set(&instance->total_io_count, 0);
7232 
7233         init_waitqueue_head(&instance->int_cmd_wait_q);
7234         init_waitqueue_head(&instance->abort_cmd_wait_q);
7235 
7236         spin_lock_init(&instance->crashdump_lock);
7237         spin_lock_init(&instance->mfi_pool_lock);
7238         spin_lock_init(&instance->hba_lock);
7239         spin_lock_init(&instance->stream_lock);
7240         spin_lock_init(&instance->completion_lock);
7241 
7242         mutex_init(&instance->reset_mutex);
7243 
7244         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
7245             (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
7246                 instance->flag_ieee = 1;
7247 
7248         megasas_dbg_lvl = 0;
7249         instance->flag = 0;
7250         instance->unload = 1;
7251         instance->last_time = 0;
7252         instance->disableOnlineCtrlReset = 1;
7253         instance->UnevenSpanSupport = 0;
7254         instance->smp_affinity_enable = smp_affinity_enable ? true : false;
7255         instance->msix_load_balance = false;
7256 
7257         if (instance->adapter_type != MFI_SERIES)
7258                 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
7259         else
7260                 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
7261 }
7262 
7263 /**
7264  * megasas_probe_one -  PCI hotplug entry point
7265  * @pdev:               PCI device structure
7266  * @id:                 PCI ids of supported hotplugged adapter
7267  */
7268 static int megasas_probe_one(struct pci_dev *pdev,
7269                              const struct pci_device_id *id)
7270 {
7271         int rval, pos;
7272         struct Scsi_Host *host;
7273         struct megasas_instance *instance;
7274         u16 control = 0;
7275 
7276         switch (pdev->device) {
7277         case PCI_DEVICE_ID_LSI_AERO_10E0:
7278         case PCI_DEVICE_ID_LSI_AERO_10E3:
7279         case PCI_DEVICE_ID_LSI_AERO_10E4:
7280         case PCI_DEVICE_ID_LSI_AERO_10E7:
7281                 dev_err(&pdev->dev, "Adapter is in non secure mode\n");
7282                 return 1;
7283         case PCI_DEVICE_ID_LSI_AERO_10E1:
7284         case PCI_DEVICE_ID_LSI_AERO_10E5:
7285                 dev_info(&pdev->dev, "Adapter is in configurable secure mode\n");
7286                 break;
7287         }
7288 
7289         /* Reset MSI-X in the kdump kernel */
7290         if (reset_devices) {
7291                 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
7292                 if (pos) {
7293                         pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
7294                                              &control);
7295                         if (control & PCI_MSIX_FLAGS_ENABLE) {
7296                                 dev_info(&pdev->dev, "resetting MSI-X\n");
7297                                 pci_write_config_word(pdev,
7298                                                       pos + PCI_MSIX_FLAGS,
7299                                                       control &
7300                                                       ~PCI_MSIX_FLAGS_ENABLE);
7301                         }
7302                 }
7303         }
7304 
7305         /*
7306          * PCI prepping: enable device set bus mastering and dma mask
7307          */
7308         rval = pci_enable_device_mem(pdev);
7309 
7310         if (rval) {
7311                 return rval;
7312         }
7313 
7314         pci_set_master(pdev);
7315 
7316         host = scsi_host_alloc(&megasas_template,
7317                                sizeof(struct megasas_instance));
7318 
7319         if (!host) {
7320                 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
7321                 goto fail_alloc_instance;
7322         }
7323 
7324         instance = (struct megasas_instance *)host->hostdata;
7325         memset(instance, 0, sizeof(*instance));
7326         atomic_set(&instance->fw_reset_no_pci_access, 0);
7327 
7328         /*
7329          * Initialize PCI related and misc parameters
7330          */
7331         instance->pdev = pdev;
7332         instance->host = host;
7333         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
7334         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
7335 
7336         megasas_set_adapter_type(instance);
7337 
7338         /*
7339          * Initialize MFI Firmware
7340          */
7341         if (megasas_init_fw(instance))
7342                 goto fail_init_mfi;
7343 
7344         if (instance->requestorId) {
7345                 if (instance->PlasmaFW111) {
7346                         instance->vf_affiliation_111 =
7347                                 dma_alloc_coherent(&pdev->dev,
7348                                         sizeof(struct MR_LD_VF_AFFILIATION_111),
7349                                         &instance->vf_affiliation_111_h,
7350                                         GFP_KERNEL);
7351                         if (!instance->vf_affiliation_111)
7352                                 dev_warn(&pdev->dev, "Can't allocate "
7353                                        "memory for VF affiliation buffer\n");
7354                 } else {
7355                         instance->vf_affiliation =
7356                                 dma_alloc_coherent(&pdev->dev,
7357                                         (MAX_LOGICAL_DRIVES + 1) *
7358                                         sizeof(struct MR_LD_VF_AFFILIATION),
7359                                         &instance->vf_affiliation_h,
7360                                         GFP_KERNEL);
7361                         if (!instance->vf_affiliation)
7362                                 dev_warn(&pdev->dev, "Can't allocate "
7363                                        "memory for VF affiliation buffer\n");
7364                 }
7365         }
7366 
7367         /*
7368          * Store instance in PCI softstate
7369          */
7370         pci_set_drvdata(pdev, instance);
7371 
7372         /*
7373          * Add this controller to megasas_mgmt_info structure so that it
7374          * can be exported to management applications
7375          */
7376         megasas_mgmt_info.count++;
7377         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
7378         megasas_mgmt_info.max_index++;
7379 
7380         /*
7381          * Register with SCSI mid-layer
7382          */
7383         if (megasas_io_attach(instance))
7384                 goto fail_io_attach;
7385 
7386         instance->unload = 0;
7387         /*
7388          * Trigger SCSI to scan our drives
7389          */
7390         if (!instance->enable_fw_dev_list ||
7391             (instance->host_device_list_buf->count > 0))
7392                 scsi_scan_host(host);
7393 
7394         /*
7395          * Initiate AEN (Asynchronous Event Notification)
7396          */
7397         if (megasas_start_aen(instance)) {
7398                 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
7399                 goto fail_start_aen;
7400         }
7401 
7402         megasas_setup_debugfs(instance);
7403 
7404         /* Get current SR-IOV LD/VF affiliation */
7405         if (instance->requestorId)
7406                 megasas_get_ld_vf_affiliation(instance, 1);
7407 
7408         return 0;
7409 
7410 fail_start_aen:
7411 fail_io_attach:
7412         megasas_mgmt_info.count--;
7413         megasas_mgmt_info.max_index--;
7414         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
7415 
7416         instance->instancet->disable_intr(instance);
7417         megasas_destroy_irqs(instance);
7418 
7419         if (instance->adapter_type != MFI_SERIES)
7420                 megasas_release_fusion(instance);
7421         else
7422                 megasas_release_mfi(instance);
7423         if (instance->msix_vectors)
7424                 pci_free_irq_vectors(instance->pdev);
7425 fail_init_mfi:
7426         scsi_host_put(host);
7427 fail_alloc_instance:
7428         pci_disable_device(pdev);
7429 
7430         return -ENODEV;
7431 }
7432 
7433 /**
7434  * megasas_flush_cache -        Requests FW to flush all its caches
7435  * @instance:                   Adapter soft state
7436  */
7437 static void megasas_flush_cache(struct megasas_instance *instance)
7438 {
7439         struct megasas_cmd *cmd;
7440         struct megasas_dcmd_frame *dcmd;
7441 
7442         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7443                 return;
7444 
7445         cmd = megasas_get_cmd(instance);
7446 
7447         if (!cmd)
7448                 return;
7449 
7450         dcmd = &cmd->frame->dcmd;
7451 
7452         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7453 
7454         dcmd->cmd = MFI_CMD_DCMD;
7455         dcmd->cmd_status = 0x0;
7456         dcmd->sge_count = 0;
7457         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7458         dcmd->timeout = 0;
7459         dcmd->pad_0 = 0;
7460         dcmd->data_xfer_len = 0;
7461         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
7462         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
7463 
7464         if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7465                         != DCMD_SUCCESS) {
7466                 dev_err(&instance->pdev->dev,
7467                         "return from %s %d\n", __func__, __LINE__);
7468                 return;
7469         }
7470 
7471         megasas_return_cmd(instance, cmd);
7472 }
7473 
7474 /**
7475  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
7476  * @instance:                           Adapter soft state
7477  * @opcode:                             Shutdown/Hibernate
7478  */
7479 static void megasas_shutdown_controller(struct megasas_instance *instance,
7480                                         u32 opcode)
7481 {
7482         struct megasas_cmd *cmd;
7483         struct megasas_dcmd_frame *dcmd;
7484 
7485         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
7486                 return;
7487 
7488         cmd = megasas_get_cmd(instance);
7489 
7490         if (!cmd)
7491                 return;
7492 
7493         if (instance->aen_cmd)
7494                 megasas_issue_blocked_abort_cmd(instance,
7495                         instance->aen_cmd, MFI_IO_TIMEOUT_SECS);
7496         if (instance->map_update_cmd)
7497                 megasas_issue_blocked_abort_cmd(instance,
7498                         instance->map_update_cmd, MFI_IO_TIMEOUT_SECS);
7499         if (instance->jbod_seq_cmd)
7500                 megasas_issue_blocked_abort_cmd(instance,
7501                         instance->jbod_seq_cmd, MFI_IO_TIMEOUT_SECS);
7502 
7503         dcmd = &cmd->frame->dcmd;
7504 
7505         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
7506 
7507         dcmd->cmd = MFI_CMD_DCMD;
7508         dcmd->cmd_status = 0x0;
7509         dcmd->sge_count = 0;
7510         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
7511         dcmd->timeout = 0;
7512         dcmd->pad_0 = 0;
7513         dcmd->data_xfer_len = 0;
7514         dcmd->opcode = cpu_to_le32(opcode);
7515 
7516         if (megasas_issue_blocked_cmd(instance, cmd, MFI_IO_TIMEOUT_SECS)
7517                         != DCMD_SUCCESS) {
7518                 dev_err(&instance->pdev->dev,
7519                         "return from %s %d\n", __func__, __LINE__);
7520                 return;
7521         }
7522 
7523         megasas_return_cmd(instance, cmd);
7524 }
7525 
7526 #ifdef CONFIG_PM
7527 /**
7528  * megasas_suspend -    driver suspend entry point
7529  * @pdev:               PCI device structure
7530  * @state:              PCI power state to suspend routine
7531  */
7532 static int
7533 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
7534 {
7535         struct megasas_instance *instance;
7536 
7537         instance = pci_get_drvdata(pdev);
7538 
7539         if (!instance)
7540                 return 0;
7541 
7542         instance->unload = 1;
7543 
7544         dev_info(&pdev->dev, "%s is called\n", __func__);
7545 
7546         /* Shutdown SR-IOV heartbeat timer */
7547         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7548                 del_timer_sync(&instance->sriov_heartbeat_timer);
7549 
7550         /* Stop the FW fault detection watchdog */
7551         if (instance->adapter_type != MFI_SERIES)
7552                 megasas_fusion_stop_watchdog(instance);
7553 
7554         megasas_flush_cache(instance);
7555         megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
7556 
7557         /* cancel the delayed work if this work still in queue */
7558         if (instance->ev != NULL) {
7559                 struct megasas_aen_event *ev = instance->ev;
7560                 cancel_delayed_work_sync(&ev->hotplug_work);
7561                 instance->ev = NULL;
7562         }
7563 
7564         tasklet_kill(&instance->isr_tasklet);
7565 
7566         pci_set_drvdata(instance->pdev, instance);
7567         instance->instancet->disable_intr(instance);
7568 
7569         megasas_destroy_irqs(instance);
7570 
7571         if (instance->msix_vectors)
7572                 pci_free_irq_vectors(instance->pdev);
7573 
7574         pci_save_state(pdev);
7575         pci_disable_device(pdev);
7576 
7577         pci_set_power_state(pdev, pci_choose_state(pdev, state));
7578 
7579         return 0;
7580 }
7581 
7582 /**
7583  * megasas_resume-      driver resume entry point
7584  * @pdev:               PCI device structure
7585  */
7586 static int
7587 megasas_resume(struct pci_dev *pdev)
7588 {
7589         int rval;
7590         struct Scsi_Host *host;
7591         struct megasas_instance *instance;
7592         int irq_flags = PCI_IRQ_LEGACY;
7593 
7594         instance = pci_get_drvdata(pdev);
7595 
7596         if (!instance)
7597                 return 0;
7598 
7599         host = instance->host;
7600         pci_set_power_state(pdev, PCI_D0);
7601         pci_enable_wake(pdev, PCI_D0, 0);
7602         pci_restore_state(pdev);
7603 
7604         dev_info(&pdev->dev, "%s is called\n", __func__);
7605         /*
7606          * PCI prepping: enable device set bus mastering and dma mask
7607          */
7608         rval = pci_enable_device_mem(pdev);
7609 
7610         if (rval) {
7611                 dev_err(&pdev->dev, "Enable device failed\n");
7612                 return rval;
7613         }
7614 
7615         pci_set_master(pdev);
7616 
7617         /*
7618          * We expect the FW state to be READY
7619          */
7620         if (megasas_transition_to_ready(instance, 0))
7621                 goto fail_ready_state;
7622 
7623         if (megasas_set_dma_mask(instance))
7624                 goto fail_set_dma_mask;
7625 
7626         /*
7627          * Initialize MFI Firmware
7628          */
7629 
7630         atomic_set(&instance->fw_outstanding, 0);
7631         atomic_set(&instance->ldio_outstanding, 0);
7632 
7633         /* Now re-enable MSI-X */
7634         if (instance->msix_vectors) {
7635                 irq_flags = PCI_IRQ_MSIX;
7636                 if (instance->smp_affinity_enable)
7637                         irq_flags |= PCI_IRQ_AFFINITY;
7638         }
7639         rval = pci_alloc_irq_vectors(instance->pdev, 1,
7640                                      instance->msix_vectors ?
7641                                      instance->msix_vectors : 1, irq_flags);
7642         if (rval < 0)
7643                 goto fail_reenable_msix;
7644 
7645         megasas_setup_reply_map(instance);
7646 
7647         if (instance->adapter_type != MFI_SERIES) {
7648                 megasas_reset_reply_desc(instance);
7649                 if (megasas_ioc_init_fusion(instance)) {
7650                         megasas_free_cmds(instance);
7651                         megasas_free_cmds_fusion(instance);
7652                         goto fail_init_mfi;
7653                 }
7654                 if (!megasas_get_map_info(instance))
7655                         megasas_sync_map_info(instance);
7656         } else {
7657                 *instance->producer = 0;
7658                 *instance->consumer = 0;
7659                 if (megasas_issue_init_mfi(instance))
7660                         goto fail_init_mfi;
7661         }
7662 
7663         if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS)
7664                 goto fail_init_mfi;
7665 
7666         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
7667                      (unsigned long)instance);
7668 
7669         if (instance->msix_vectors ?
7670                         megasas_setup_irqs_msix(instance, 0) :
7671                         megasas_setup_irqs_ioapic(instance))
7672                 goto fail_init_mfi;
7673 
7674         if (instance->adapter_type != MFI_SERIES)
7675                 megasas_setup_irq_poll(instance);
7676 
7677         /* Re-launch SR-IOV heartbeat timer */
7678         if (instance->requestorId) {
7679                 if (!megasas_sriov_start_heartbeat(instance, 0))
7680                         megasas_start_timer(instance);
7681                 else {
7682                         instance->skip_heartbeat_timer_del = 1;
7683                         goto fail_init_mfi;
7684                 }
7685         }
7686 
7687         instance->instancet->enable_intr(instance);
7688         megasas_setup_jbod_map(instance);
7689         instance->unload = 0;
7690 
7691         /*
7692          * Initiate AEN (Asynchronous Event Notification)
7693          */
7694         if (megasas_start_aen(instance))
7695                 dev_err(&instance->pdev->dev, "Start AEN failed\n");
7696 
7697         /* Re-launch FW fault watchdog */
7698         if (instance->adapter_type != MFI_SERIES)
7699                 if (megasas_fusion_start_watchdog(instance) != SUCCESS)
7700                         goto fail_start_watchdog;
7701 
7702         return 0;
7703 
7704 fail_start_watchdog:
7705         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7706                 del_timer_sync(&instance->sriov_heartbeat_timer);
7707 fail_init_mfi:
7708         megasas_free_ctrl_dma_buffers(instance);
7709         megasas_free_ctrl_mem(instance);
7710         scsi_host_put(host);
7711 
7712 fail_reenable_msix:
7713 fail_set_dma_mask:
7714 fail_ready_state:
7715 
7716         pci_disable_device(pdev);
7717 
7718         return -ENODEV;
7719 }
7720 #else
7721 #define megasas_suspend NULL
7722 #define megasas_resume  NULL
7723 #endif
7724 
7725 static inline int
7726 megasas_wait_for_adapter_operational(struct megasas_instance *instance)
7727 {
7728         int wait_time = MEGASAS_RESET_WAIT_TIME * 2;
7729         int i;
7730         u8 adp_state;
7731 
7732         for (i = 0; i < wait_time; i++) {
7733                 adp_state = atomic_read(&instance->adprecovery);
7734                 if ((adp_state == MEGASAS_HBA_OPERATIONAL) ||
7735                     (adp_state == MEGASAS_HW_CRITICAL_ERROR))
7736                         break;
7737 
7738                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL))
7739                         dev_notice(&instance->pdev->dev, "waiting for controller reset to finish\n");
7740 
7741                 msleep(1000);
7742         }
7743 
7744         if (adp_state != MEGASAS_HBA_OPERATIONAL) {
7745                 dev_info(&instance->pdev->dev,
7746                          "%s HBA failed to become operational, adp_state %d\n",
7747                          __func__, adp_state);
7748                 return 1;
7749         }
7750 
7751         return 0;
7752 }
7753 
7754 /**
7755  * megasas_detach_one - PCI hot"un"plug entry point
7756  * @pdev:               PCI device structure
7757  */
7758 static void megasas_detach_one(struct pci_dev *pdev)
7759 {
7760         int i;
7761         struct Scsi_Host *host;
7762         struct megasas_instance *instance;
7763         struct fusion_context *fusion;
7764         u32 pd_seq_map_sz;
7765 
7766         instance = pci_get_drvdata(pdev);
7767 
7768         if (!instance)
7769                 return;
7770 
7771         host = instance->host;
7772         fusion = instance->ctrl_context;
7773 
7774         /* Shutdown SR-IOV heartbeat timer */
7775         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
7776                 del_timer_sync(&instance->sriov_heartbeat_timer);
7777 
7778         /* Stop the FW fault detection watchdog */
7779         if (instance->adapter_type != MFI_SERIES)
7780                 megasas_fusion_stop_watchdog(instance);
7781 
7782         if (instance->fw_crash_state != UNAVAILABLE)
7783                 megasas_free_host_crash_buffer(instance);
7784         scsi_remove_host(instance->host);
7785         instance->unload = 1;
7786 
7787         if (megasas_wait_for_adapter_operational(instance))
7788                 goto skip_firing_dcmds;
7789 
7790         megasas_flush_cache(instance);
7791         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7792 
7793 skip_firing_dcmds:
7794         /* cancel the delayed work if this work still in queue*/
7795         if (instance->ev != NULL) {
7796                 struct megasas_aen_event *ev = instance->ev;
7797                 cancel_delayed_work_sync(&ev->hotplug_work);
7798                 instance->ev = NULL;
7799         }
7800 
7801         /* cancel all wait events */
7802         wake_up_all(&instance->int_cmd_wait_q);
7803 
7804         tasklet_kill(&instance->isr_tasklet);
7805 
7806         /*
7807          * Take the instance off the instance array. Note that we will not
7808          * decrement the max_index. We let this array be sparse array
7809          */
7810         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7811                 if (megasas_mgmt_info.instance[i] == instance) {
7812                         megasas_mgmt_info.count--;
7813                         megasas_mgmt_info.instance[i] = NULL;
7814 
7815                         break;
7816                 }
7817         }
7818 
7819         instance->instancet->disable_intr(instance);
7820 
7821         megasas_destroy_irqs(instance);
7822 
7823         if (instance->msix_vectors)
7824                 pci_free_irq_vectors(instance->pdev);
7825 
7826         if (instance->adapter_type >= VENTURA_SERIES) {
7827                 for (i = 0; i < MAX_LOGICAL_DRIVES_EXT; ++i)
7828                         kfree(fusion->stream_detect_by_ld[i]);
7829                 kfree(fusion->stream_detect_by_ld);
7830                 fusion->stream_detect_by_ld = NULL;
7831         }
7832 
7833 
7834         if (instance->adapter_type != MFI_SERIES) {
7835                 megasas_release_fusion(instance);
7836                         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
7837                                 (sizeof(struct MR_PD_CFG_SEQ) *
7838                                         (MAX_PHYSICAL_DEVICES - 1));
7839                 for (i = 0; i < 2 ; i++) {
7840                         if (fusion->ld_map[i])
7841                                 dma_free_coherent(&instance->pdev->dev,
7842                                                   fusion->max_map_sz,
7843                                                   fusion->ld_map[i],
7844                                                   fusion->ld_map_phys[i]);
7845                         if (fusion->ld_drv_map[i]) {
7846                                 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
7847                                         vfree(fusion->ld_drv_map[i]);
7848                                 else
7849                                         free_pages((ulong)fusion->ld_drv_map[i],
7850                                                    fusion->drv_map_pages);
7851                         }
7852 
7853                         if (fusion->pd_seq_sync[i])
7854                                 dma_free_coherent(&instance->pdev->dev,
7855                                         pd_seq_map_sz,
7856                                         fusion->pd_seq_sync[i],
7857                                         fusion->pd_seq_phys[i]);
7858                 }
7859         } else {
7860                 megasas_release_mfi(instance);
7861         }
7862 
7863         if (instance->vf_affiliation)
7864                 dma_free_coherent(&pdev->dev, (MAX_LOGICAL_DRIVES + 1) *
7865                                     sizeof(struct MR_LD_VF_AFFILIATION),
7866                                     instance->vf_affiliation,
7867                                     instance->vf_affiliation_h);
7868 
7869         if (instance->vf_affiliation_111)
7870                 dma_free_coherent(&pdev->dev,
7871                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
7872                                     instance->vf_affiliation_111,
7873                                     instance->vf_affiliation_111_h);
7874 
7875         if (instance->hb_host_mem)
7876                 dma_free_coherent(&pdev->dev, sizeof(struct MR_CTRL_HB_HOST_MEM),
7877                                     instance->hb_host_mem,
7878                                     instance->hb_host_mem_h);
7879 
7880         megasas_free_ctrl_dma_buffers(instance);
7881 
7882         megasas_free_ctrl_mem(instance);
7883 
7884         megasas_destroy_debugfs(instance);
7885 
7886         scsi_host_put(host);
7887 
7888         pci_disable_device(pdev);
7889 }
7890 
7891 /**
7892  * megasas_shutdown -   Shutdown entry point
7893  * @device:             Generic device structure
7894  */
7895 static void megasas_shutdown(struct pci_dev *pdev)
7896 {
7897         struct megasas_instance *instance = pci_get_drvdata(pdev);
7898 
7899         if (!instance)
7900                 return;
7901 
7902         instance->unload = 1;
7903 
7904         if (megasas_wait_for_adapter_operational(instance))
7905                 goto skip_firing_dcmds;
7906 
7907         megasas_flush_cache(instance);
7908         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
7909 
7910 skip_firing_dcmds:
7911         instance->instancet->disable_intr(instance);
7912         megasas_destroy_irqs(instance);
7913 
7914         if (instance->msix_vectors)
7915                 pci_free_irq_vectors(instance->pdev);
7916 }
7917 
7918 /**
7919  * megasas_mgmt_open -  char node "open" entry point
7920  */
7921 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
7922 {
7923         /*
7924          * Allow only those users with admin rights
7925          */
7926         if (!capable(CAP_SYS_ADMIN))
7927                 return -EACCES;
7928 
7929         return 0;
7930 }
7931 
7932 /**
7933  * megasas_mgmt_fasync -        Async notifier registration from applications
7934  *
7935  * This function adds the calling process to a driver global queue. When an
7936  * event occurs, SIGIO will be sent to all processes in this queue.
7937  */
7938 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
7939 {
7940         int rc;
7941 
7942         mutex_lock(&megasas_async_queue_mutex);
7943 
7944         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
7945 
7946         mutex_unlock(&megasas_async_queue_mutex);
7947 
7948         if (rc >= 0) {
7949                 /* For sanity check when we get ioctl */
7950                 filep->private_data = filep;
7951                 return 0;
7952         }
7953 
7954         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
7955 
7956         return rc;
7957 }
7958 
7959 /**
7960  * megasas_mgmt_poll -  char node "poll" entry point
7961  * */
7962 static __poll_t megasas_mgmt_poll(struct file *file, poll_table *wait)
7963 {
7964         __poll_t mask;
7965         unsigned long flags;
7966 
7967         poll_wait(file, &megasas_poll_wait, wait);
7968         spin_lock_irqsave(&poll_aen_lock, flags);
7969         if (megasas_poll_wait_aen)
7970                 mask = (EPOLLIN | EPOLLRDNORM);
7971         else
7972                 mask = 0;
7973         megasas_poll_wait_aen = 0;
7974         spin_unlock_irqrestore(&poll_aen_lock, flags);
7975         return mask;
7976 }
7977 
7978 /*
7979  * megasas_set_crash_dump_params_ioctl:
7980  *              Send CRASH_DUMP_MODE DCMD to all controllers
7981  * @cmd:        MFI command frame
7982  */
7983 
7984 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
7985 {
7986         struct megasas_instance *local_instance;
7987         int i, error = 0;
7988         int crash_support;
7989 
7990         crash_support = cmd->frame->dcmd.mbox.w[0];
7991 
7992         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
7993                 local_instance = megasas_mgmt_info.instance[i];
7994                 if (local_instance && local_instance->crash_dump_drv_support) {
7995                         if ((atomic_read(&local_instance->adprecovery) ==
7996                                 MEGASAS_HBA_OPERATIONAL) &&
7997                                 !megasas_set_crash_dump_params(local_instance,
7998                                         crash_support)) {
7999                                 local_instance->crash_dump_app_support =
8000                                         crash_support;
8001                                 dev_info(&local_instance->pdev->dev,
8002                                         "Application firmware crash "
8003                                         "dump mode set success\n");
8004                                 error = 0;
8005                         } else {
8006                                 dev_info(&local_instance->pdev->dev,
8007                                         "Application firmware crash "
8008                                         "dump mode set failed\n");
8009                                 error = -1;
8010                         }
8011                 }
8012         }
8013         return error;
8014 }
8015 
8016 /**
8017  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
8018  * @instance:                   Adapter soft state
8019  * @argp:                       User's ioctl packet
8020  */
8021 static int
8022 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
8023                       struct megasas_iocpacket __user * user_ioc,
8024                       struct megasas_iocpacket *ioc)
8025 {
8026         struct megasas_sge64 *kern_sge64 = NULL;
8027         struct megasas_sge32 *kern_sge32 = NULL;
8028         struct megasas_cmd *cmd;
8029         void *kbuff_arr[MAX_IOCTL_SGE];
8030         dma_addr_t buf_handle = 0;
8031         int error = 0, i;
8032         void *sense = NULL;
8033         dma_addr_t sense_handle;
8034         unsigned long *sense_ptr;
8035         u32 opcode = 0;
8036 
8037         memset(kbuff_arr, 0, sizeof(kbuff_arr));
8038 
8039         if (ioc->sge_count > MAX_IOCTL_SGE) {
8040                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] >  max limit [%d]\n",
8041                        ioc->sge_count, MAX_IOCTL_SGE);
8042                 return -EINVAL;
8043         }
8044 
8045         if ((ioc->frame.hdr.cmd >= MFI_CMD_OP_COUNT) ||
8046             ((ioc->frame.hdr.cmd == MFI_CMD_NVME) &&
8047             !instance->support_nvme_passthru) ||
8048             ((ioc->frame.hdr.cmd == MFI_CMD_TOOLBOX) &&
8049             !instance->support_pci_lane_margining)) {
8050                 dev_err(&instance->pdev->dev,
8051                         "Received invalid ioctl command 0x%x\n",
8052                         ioc->frame.hdr.cmd);
8053                 return -ENOTSUPP;
8054         }
8055 
8056         cmd = megasas_get_cmd(instance);
8057         if (!cmd) {
8058                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
8059                 return -ENOMEM;
8060         }
8061 
8062         /*
8063          * User's IOCTL packet has 2 frames (maximum). Copy those two
8064          * frames into our cmd's frames. cmd->frame's context will get
8065          * overwritten when we copy from user's frames. So set that value
8066          * alone separately
8067          */
8068         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
8069         cmd->frame->hdr.context = cpu_to_le32(cmd->index);
8070         cmd->frame->hdr.pad_0 = 0;
8071 
8072         cmd->frame->hdr.flags &= (~MFI_FRAME_IEEE);
8073 
8074         if (instance->consistent_mask_64bit)
8075                 cmd->frame->hdr.flags |= cpu_to_le16((MFI_FRAME_SGL64 |
8076                                        MFI_FRAME_SENSE64));
8077         else
8078                 cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_SGL64 |
8079                                                MFI_FRAME_SENSE64));
8080 
8081         if (cmd->frame->hdr.cmd == MFI_CMD_DCMD)
8082                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
8083 
8084         if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
8085                 mutex_lock(&instance->reset_mutex);
8086                 if (megasas_get_ctrl_info(instance) != DCMD_SUCCESS) {
8087                         megasas_return_cmd(instance, cmd);
8088                         mutex_unlock(&instance->reset_mutex);
8089                         return -1;
8090                 }
8091                 mutex_unlock(&instance->reset_mutex);
8092         }
8093 
8094         if (opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
8095                 error = megasas_set_crash_dump_params_ioctl(cmd);
8096                 megasas_return_cmd(instance, cmd);
8097                 return error;
8098         }
8099 
8100         /*
8101          * The management interface between applications and the fw uses
8102          * MFI frames. E.g, RAID configuration changes, LD property changes
8103          * etc are accomplishes through different kinds of MFI frames. The
8104          * driver needs to care only about substituting user buffers with
8105          * kernel buffers in SGLs. The location of SGL is embedded in the
8106          * struct iocpacket itself.
8107          */
8108         if (instance->consistent_mask_64bit)
8109                 kern_sge64 = (struct megasas_sge64 *)
8110                         ((unsigned long)cmd->frame + ioc->sgl_off);
8111         else
8112                 kern_sge32 = (struct megasas_sge32 *)
8113                         ((unsigned long)cmd->frame + ioc->sgl_off);
8114 
8115         /*
8116          * For each user buffer, create a mirror buffer and copy in
8117          */
8118         for (i = 0; i < ioc->sge_count; i++) {
8119                 if (!ioc->sgl[i].iov_len)
8120                         continue;
8121 
8122                 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
8123                                                     ioc->sgl[i].iov_len,
8124                                                     &buf_handle, GFP_KERNEL);
8125                 if (!kbuff_arr[i]) {
8126                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
8127                                "kernel SGL buffer for IOCTL\n");
8128                         error = -ENOMEM;
8129                         goto out;
8130                 }
8131 
8132                 /*
8133                  * We don't change the dma_coherent_mask, so
8134                  * dma_alloc_coherent only returns 32bit addresses
8135                  */
8136                 if (instance->consistent_mask_64bit) {
8137                         kern_sge64[i].phys_addr = cpu_to_le64(buf_handle);
8138                         kern_sge64[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8139                 } else {
8140                         kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
8141                         kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
8142                 }
8143 
8144                 /*
8145                  * We created a kernel buffer corresponding to the
8146                  * user buffer. Now copy in from the user buffer
8147                  */
8148                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
8149                                    (u32) (ioc->sgl[i].iov_len))) {
8150                         error = -EFAULT;
8151                         goto out;
8152                 }
8153         }
8154 
8155         if (ioc->sense_len) {
8156                 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
8157                                              &sense_handle, GFP_KERNEL);
8158                 if (!sense) {
8159                         error = -ENOMEM;
8160                         goto out;
8161                 }
8162 
8163                 sense_ptr =
8164                 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
8165                 if (instance->consistent_mask_64bit)
8166                         *sense_ptr = cpu_to_le64(sense_handle);
8167                 else
8168                         *sense_ptr = cpu_to_le32(sense_handle);
8169         }
8170 
8171         /*
8172          * Set the sync_cmd flag so that the ISR knows not to complete this
8173          * cmd to the SCSI mid-layer
8174          */
8175         cmd->sync_cmd = 1;
8176         if (megasas_issue_blocked_cmd(instance, cmd, 0) == DCMD_NOT_FIRED) {
8177                 cmd->sync_cmd = 0;
8178                 dev_err(&instance->pdev->dev,
8179                         "return -EBUSY from %s %d cmd 0x%x opcode 0x%x cmd->cmd_status_drv 0x%x\n",
8180                         __func__, __LINE__, cmd->frame->hdr.cmd, opcode,
8181                         cmd->cmd_status_drv);
8182                 return -EBUSY;
8183         }
8184 
8185         cmd->sync_cmd = 0;
8186 
8187         if (instance->unload == 1) {
8188                 dev_info(&instance->pdev->dev, "Driver unload is in progress "
8189                         "don't submit data to application\n");
8190                 goto out;
8191         }
8192         /*
8193          * copy out the kernel buffers to user buffers
8194          */
8195         for (i = 0; i < ioc->sge_count; i++) {
8196                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
8197                                  ioc->sgl[i].iov_len)) {
8198                         error = -EFAULT;
8199                         goto out;
8200                 }
8201         }
8202 
8203         /*
8204          * copy out the sense
8205          */
8206         if (ioc->sense_len) {
8207                 /*
8208                  * sense_ptr points to the location that has the user
8209                  * sense buffer address
8210                  */
8211                 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
8212                                 ioc->sense_off);
8213 
8214                 if (copy_to_user((void __user *)((unsigned long)
8215                                  get_unaligned((unsigned long *)sense_ptr)),
8216                                  sense, ioc->sense_len)) {
8217                         dev_err(&instance->pdev->dev, "Failed to copy out to user "
8218                                         "sense data\n");
8219                         error = -EFAULT;
8220                         goto out;
8221                 }
8222         }
8223 
8224         /*
8225          * copy the status codes returned by the fw
8226          */
8227         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
8228                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
8229                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
8230                 error = -EFAULT;
8231         }
8232 
8233 out:
8234         if (sense) {
8235                 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
8236                                     sense, sense_handle);
8237         }
8238 
8239         for (i = 0; i < ioc->sge_count; i++) {
8240                 if (kbuff_arr[i]) {
8241                         if (instance->consistent_mask_64bit)
8242                                 dma_free_coherent(&instance->pdev->dev,
8243                                         le32_to_cpu(kern_sge64[i].length),
8244                                         kbuff_arr[i],
8245                                         le64_to_cpu(kern_sge64[i].phys_addr));
8246                         else
8247                                 dma_free_coherent(&instance->pdev->dev,
8248                                         le32_to_cpu(kern_sge32[i].length),
8249                                         kbuff_arr[i],
8250                                         le32_to_cpu(kern_sge32[i].phys_addr));
8251                         kbuff_arr[i] = NULL;
8252                 }
8253         }
8254 
8255         megasas_return_cmd(instance, cmd);
8256         return error;
8257 }
8258 
8259 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
8260 {
8261         struct megasas_iocpacket __user *user_ioc =
8262             (struct megasas_iocpacket __user *)arg;
8263         struct megasas_iocpacket *ioc;
8264         struct megasas_instance *instance;
8265         int error;
8266 
8267         ioc = memdup_user(user_ioc, sizeof(*ioc));
8268         if (IS_ERR(ioc))
8269                 return PTR_ERR(ioc);
8270 
8271         instance = megasas_lookup_instance(ioc->host_no);
8272         if (!instance) {
8273                 error = -ENODEV;
8274                 goto out_kfree_ioc;
8275         }
8276 
8277         /* Block ioctls in VF mode */
8278         if (instance->requestorId && !allow_vf_ioctls) {
8279                 error = -ENODEV;
8280                 goto out_kfree_ioc;
8281         }
8282 
8283         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8284                 dev_err(&instance->pdev->dev, "Controller in crit error\n");
8285                 error = -ENODEV;
8286                 goto out_kfree_ioc;
8287         }
8288 
8289         if (instance->unload == 1) {
8290                 error = -ENODEV;
8291                 goto out_kfree_ioc;
8292         }
8293 
8294         if (down_interruptible(&instance->ioctl_sem)) {
8295                 error = -ERESTARTSYS;
8296                 goto out_kfree_ioc;
8297         }
8298 
8299         if  (megasas_wait_for_adapter_operational(instance)) {
8300                 error = -ENODEV;
8301                 goto out_up;
8302         }
8303 
8304         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
8305 out_up:
8306         up(&instance->ioctl_sem);
8307 
8308 out_kfree_ioc:
8309         kfree(ioc);
8310         return error;
8311 }
8312 
8313 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
8314 {
8315         struct megasas_instance *instance;
8316         struct megasas_aen aen;
8317         int error;
8318 
8319         if (file->private_data != file) {
8320                 printk(KERN_DEBUG "megasas: fasync_helper was not "
8321                        "called first\n");
8322                 return -EINVAL;
8323         }
8324 
8325         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
8326                 return -EFAULT;
8327 
8328         instance = megasas_lookup_instance(aen.host_no);
8329 
8330         if (!instance)
8331                 return -ENODEV;
8332 
8333         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
8334                 return -ENODEV;
8335         }
8336 
8337         if (instance->unload == 1) {
8338                 return -ENODEV;
8339         }
8340 
8341         if  (megasas_wait_for_adapter_operational(instance))
8342                 return -ENODEV;
8343 
8344         mutex_lock(&instance->reset_mutex);
8345         error = megasas_register_aen(instance, aen.seq_num,
8346                                      aen.class_locale_word);
8347         mutex_unlock(&instance->reset_mutex);
8348         return error;
8349 }
8350 
8351 /**
8352  * megasas_mgmt_ioctl - char node ioctl entry point
8353  */
8354 static long
8355 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
8356 {
8357         switch (cmd) {
8358         case MEGASAS_IOC_FIRMWARE:
8359                 return megasas_mgmt_ioctl_fw(file, arg);
8360 
8361         case MEGASAS_IOC_GET_AEN:
8362                 return megasas_mgmt_ioctl_aen(file, arg);
8363         }
8364 
8365         return -ENOTTY;
8366 }
8367 
8368 #ifdef CONFIG_COMPAT
8369 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
8370 {
8371         struct compat_megasas_iocpacket __user *cioc =
8372             (struct compat_megasas_iocpacket __user *)arg;
8373         struct megasas_iocpacket __user *ioc =
8374             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
8375         int i;
8376         int error = 0;
8377         compat_uptr_t ptr;
8378         u32 local_sense_off;
8379         u32 local_sense_len;
8380         u32 user_sense_off;
8381 
8382         if (clear_user(ioc, sizeof(*ioc)))
8383                 return -EFAULT;
8384 
8385         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
8386             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
8387             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
8388             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
8389             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
8390             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
8391                 return -EFAULT;
8392 
8393         /*
8394          * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
8395          * sense_len is not null, so prepare the 64bit value under
8396          * the same condition.
8397          */
8398         if (get_user(local_sense_off, &ioc->sense_off) ||
8399                 get_user(local_sense_len, &ioc->sense_len) ||
8400                 get_user(user_sense_off, &cioc->sense_off))
8401                 return -EFAULT;
8402 
8403         if (local_sense_off != user_sense_off)
8404                 return -EINVAL;
8405 
8406         if (local_sense_len) {
8407                 void __user **sense_ioc_ptr =
8408                         (void __user **)((u8 *)((unsigned long)&ioc->frame.raw) + local_sense_off);
8409                 compat_uptr_t *sense_cioc_ptr =
8410                         (compat_uptr_t *)(((unsigned long)&cioc->frame.raw) + user_sense_off);
8411                 if (get_user(ptr, sense_cioc_ptr) ||
8412                     put_user(compat_ptr(ptr), sense_ioc_ptr))
8413                         return -EFAULT;
8414         }
8415 
8416         for (i = 0; i < MAX_IOCTL_SGE; i++) {
8417                 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
8418                     put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
8419                     copy_in_user(&ioc->sgl[i].iov_len,
8420                                  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
8421                         return -EFAULT;
8422         }
8423 
8424         error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
8425 
8426         if (copy_in_user(&cioc->frame.hdr.cmd_status,
8427                          &ioc->frame.hdr.cmd_status, sizeof(u8))) {
8428                 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
8429                 return -EFAULT;
8430         }
8431         return error;
8432 }
8433 
8434 static long
8435 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
8436                           unsigned long arg)
8437 {
8438         switch (cmd) {
8439         case MEGASAS_IOC_FIRMWARE32:
8440                 return megasas_mgmt_compat_ioctl_fw(file, arg);
8441         case MEGASAS_IOC_GET_AEN:
8442                 return megasas_mgmt_ioctl_aen(file, arg);
8443         }
8444 
8445         return -ENOTTY;
8446 }
8447 #endif
8448 
8449 /*
8450  * File operations structure for management interface
8451  */
8452 static const struct file_operations megasas_mgmt_fops = {
8453         .owner = THIS_MODULE,
8454         .open = megasas_mgmt_open,
8455         .fasync = megasas_mgmt_fasync,
8456         .unlocked_ioctl = megasas_mgmt_ioctl,
8457         .poll = megasas_mgmt_poll,
8458 #ifdef CONFIG_COMPAT
8459         .compat_ioctl = megasas_mgmt_compat_ioctl,
8460 #endif
8461         .llseek = noop_llseek,
8462 };
8463 
8464 /*
8465  * PCI hotplug support registration structure
8466  */
8467 static struct pci_driver megasas_pci_driver = {
8468 
8469         .name = "megaraid_sas",
8470         .id_table = megasas_pci_table,
8471         .probe = megasas_probe_one,
8472         .remove = megasas_detach_one,
8473         .suspend = megasas_suspend,
8474         .resume = megasas_resume,
8475         .shutdown = megasas_shutdown,
8476 };
8477 
8478 /*
8479  * Sysfs driver attributes
8480  */
8481 static ssize_t version_show(struct device_driver *dd, char *buf)
8482 {
8483         return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
8484                         MEGASAS_VERSION);
8485 }
8486 static DRIVER_ATTR_RO(version);
8487 
8488 static ssize_t release_date_show(struct device_driver *dd, char *buf)
8489 {
8490         return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
8491                 MEGASAS_RELDATE);
8492 }
8493 static DRIVER_ATTR_RO(release_date);
8494 
8495 static ssize_t support_poll_for_event_show(struct device_driver *dd, char *buf)
8496 {
8497         return sprintf(buf, "%u\n", support_poll_for_event);
8498 }
8499 static DRIVER_ATTR_RO(support_poll_for_event);
8500 
8501 static ssize_t support_device_change_show(struct device_driver *dd, char *buf)
8502 {
8503         return sprintf(buf, "%u\n", support_device_change);
8504 }
8505 static DRIVER_ATTR_RO(support_device_change);
8506 
8507 static ssize_t dbg_lvl_show(struct device_driver *dd, char *buf)
8508 {
8509         return sprintf(buf, "%u\n", megasas_dbg_lvl);
8510 }
8511 
8512 static ssize_t dbg_lvl_store(struct device_driver *dd, const char *buf,
8513                              size_t count)
8514 {
8515         int retval = count;
8516 
8517         if (sscanf(buf, "%u", &megasas_dbg_lvl) < 1) {
8518                 printk(KERN_ERR "megasas: could not set dbg_lvl\n");
8519                 retval = -EINVAL;
8520         }
8521         return retval;
8522 }
8523 static DRIVER_ATTR_RW(dbg_lvl);
8524 
8525 static ssize_t
8526 support_nvme_encapsulation_show(struct device_driver *dd, char *buf)
8527 {
8528         return sprintf(buf, "%u\n", support_nvme_encapsulation);
8529 }
8530 
8531 static DRIVER_ATTR_RO(support_nvme_encapsulation);
8532 
8533 static ssize_t
8534 support_pci_lane_margining_show(struct device_driver *dd, char *buf)
8535 {
8536         return sprintf(buf, "%u\n", support_pci_lane_margining);
8537 }
8538 
8539 static DRIVER_ATTR_RO(support_pci_lane_margining);
8540 
8541 static inline void megasas_remove_scsi_device(struct scsi_device *sdev)
8542 {
8543         sdev_printk(KERN_INFO, sdev, "SCSI device is removed\n");
8544         scsi_remove_device(sdev);
8545         scsi_device_put(sdev);
8546 }
8547 
8548 /**
8549  * megasas_update_device_list - Update the PD and LD device list from FW
8550  *                              after an AEN event notification
8551  * @instance:                   Adapter soft state
8552  * @event_type:                 Indicates type of event (PD or LD event)
8553  *
8554  * @return:                     Success or failure
8555  *
8556  * Issue DCMDs to Firmware to update the internal device list in driver.
8557  * Based on the FW support, driver sends the HOST_DEVICE_LIST or combination
8558  * of PD_LIST/LD_LIST_QUERY DCMDs to get the device list.
8559  */
8560 static
8561 int megasas_update_device_list(struct megasas_instance *instance,
8562                                int event_type)
8563 {
8564         int dcmd_ret = DCMD_SUCCESS;
8565 
8566         if (instance->enable_fw_dev_list) {
8567                 dcmd_ret = megasas_host_device_list_query(instance, false);
8568                 if (dcmd_ret != DCMD_SUCCESS)
8569                         goto out;
8570         } else {
8571                 if (event_type & SCAN_PD_CHANNEL) {
8572                         dcmd_ret = megasas_get_pd_list(instance);
8573 
8574                         if (dcmd_ret != DCMD_SUCCESS)
8575                                 goto out;
8576                 }
8577 
8578                 if (event_type & SCAN_VD_CHANNEL) {
8579                         if (!instance->requestorId ||
8580                             (instance->requestorId &&
8581                              megasas_get_ld_vf_affiliation(instance, 0))) {
8582                                 dcmd_ret = megasas_ld_list_query(instance,
8583                                                 MR_LD_QUERY_TYPE_EXPOSED_TO_HOST);
8584                                 if (dcmd_ret != DCMD_SUCCESS)
8585                                         goto out;
8586                         }
8587                 }
8588         }
8589 
8590 out:
8591         return dcmd_ret;
8592 }
8593 
8594 /**
8595  * megasas_add_remove_devices - Add/remove devices to SCSI mid-layer
8596  *                              after an AEN event notification
8597  * @instance:                   Adapter soft state
8598  * @scan_type:                  Indicates type of devices (PD/LD) to add
8599  * @return                      void
8600  */
8601 static
8602 void megasas_add_remove_devices(struct megasas_instance *instance,
8603                                 int scan_type)
8604 {
8605         int i, j;
8606         u16 pd_index = 0;
8607         u16 ld_index = 0;
8608         u16 channel = 0, id = 0;
8609         struct Scsi_Host *host;
8610         struct scsi_device *sdev1;
8611         struct MR_HOST_DEVICE_LIST *targetid_list = NULL;
8612         struct MR_HOST_DEVICE_LIST_ENTRY *targetid_entry = NULL;
8613 
8614         host = instance->host;
8615 
8616         if (instance->enable_fw_dev_list) {
8617                 targetid_list = instance->host_device_list_buf;
8618                 for (i = 0; i < targetid_list->count; i++) {
8619                         targetid_entry = &targetid_list->host_device_list[i];
8620                         if (targetid_entry->flags.u.bits.is_sys_pd) {
8621                                 channel = le16_to_cpu(targetid_entry->target_id) /
8622                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8623                                 id = le16_to_cpu(targetid_entry->target_id) %
8624                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8625                         } else {
8626                                 channel = MEGASAS_MAX_PD_CHANNELS +
8627                                           (le16_to_cpu(targetid_entry->target_id) /
8628                                            MEGASAS_MAX_DEV_PER_CHANNEL);
8629                                 id = le16_to_cpu(targetid_entry->target_id) %
8630                                                 MEGASAS_MAX_DEV_PER_CHANNEL;
8631                         }
8632                         sdev1 = scsi_device_lookup(host, channel, id, 0);
8633                         if (!sdev1) {
8634                                 scsi_add_device(host, channel, id, 0);
8635                         } else {
8636                                 scsi_device_put(sdev1);
8637                         }
8638                 }
8639         }
8640 
8641         if (scan_type & SCAN_PD_CHANNEL) {
8642                 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
8643                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8644                                 pd_index = i * MEGASAS_MAX_DEV_PER_CHANNEL + j;
8645                                 sdev1 = scsi_device_lookup(host, i, j, 0);
8646                                 if (instance->pd_list[pd_index].driveState ==
8647                                                         MR_PD_STATE_SYSTEM) {
8648                                         if (!sdev1)
8649                                                 scsi_add_device(host, i, j, 0);
8650                                         else
8651                                                 scsi_device_put(sdev1);
8652                                 } else {
8653                                         if (sdev1)
8654                                                 megasas_remove_scsi_device(sdev1);
8655                                 }
8656                         }
8657                 }
8658         }
8659 
8660         if (scan_type & SCAN_VD_CHANNEL) {
8661                 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
8662                         for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
8663                                 ld_index = (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
8664                                 sdev1 = scsi_device_lookup(host,
8665                                                 MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8666                                 if (instance->ld_ids[ld_index] != 0xff) {
8667                                         if (!sdev1)
8668                                                 scsi_add_device(host, MEGASAS_MAX_PD_CHANNELS + i, j, 0);
8669                                         else
8670                                                 scsi_device_put(sdev1);
8671                                 } else {
8672                                         if (sdev1)
8673                                                 megasas_remove_scsi_device(sdev1);
8674                                 }
8675                         }
8676                 }
8677         }
8678 
8679 }
8680 
8681 static void
8682 megasas_aen_polling(struct work_struct *work)
8683 {
8684         struct megasas_aen_event *ev =
8685                 container_of(work, struct megasas_aen_event, hotplug_work.work);
8686         struct megasas_instance *instance = ev->instance;
8687         union megasas_evt_class_locale class_locale;
8688         int event_type = 0;
8689         u32 seq_num;
8690         int error;
8691         u8  dcmd_ret = DCMD_SUCCESS;
8692 
8693         if (!instance) {
8694                 printk(KERN_ERR "invalid instance!\n");
8695                 kfree(ev);
8696                 return;
8697         }
8698 
8699         /* Don't run the event workqueue thread if OCR is running */
8700         mutex_lock(&instance->reset_mutex);
8701 
8702         instance->ev = NULL;
8703         if (instance->evt_detail) {
8704                 megasas_decode_evt(instance);
8705 
8706                 switch (le32_to_cpu(instance->evt_detail->code)) {
8707 
8708                 case MR_EVT_PD_INSERTED:
8709                 case MR_EVT_PD_REMOVED:
8710                         event_type = SCAN_PD_CHANNEL;
8711                         break;
8712 
8713                 case MR_EVT_LD_OFFLINE:
8714                 case MR_EVT_CFG_CLEARED:
8715                 case MR_EVT_LD_DELETED:
8716                 case MR_EVT_LD_CREATED:
8717                         event_type = SCAN_VD_CHANNEL;
8718                         break;
8719 
8720                 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED:
8721                 case MR_EVT_FOREIGN_CFG_IMPORTED:
8722                 case MR_EVT_LD_STATE_CHANGE:
8723                         event_type = SCAN_PD_CHANNEL | SCAN_VD_CHANNEL;
8724                         dev_info(&instance->pdev->dev, "scanning for scsi%d...\n",
8725                                 instance->host->host_no);
8726                         break;
8727 
8728                 case MR_EVT_CTRL_PROP_CHANGED:
8729                         dcmd_ret = megasas_get_ctrl_info(instance);
8730                         if (dcmd_ret == DCMD_SUCCESS &&
8731                             instance->snapdump_wait_time) {
8732                                 megasas_get_snapdump_properties(instance);
8733                                 dev_info(&instance->pdev->dev,
8734                                          "Snap dump wait time\t: %d\n",
8735                                          instance->snapdump_wait_time);
8736                         }
8737                         break;
8738                 default:
8739                         event_type = 0;
8740                         break;
8741                 }
8742         } else {
8743                 dev_err(&instance->pdev->dev, "invalid evt_detail!\n");
8744                 mutex_unlock(&instance->reset_mutex);
8745                 kfree(ev);
8746                 return;
8747         }
8748 
8749         if (event_type)
8750                 dcmd_ret = megasas_update_device_list(instance, event_type);
8751 
8752         mutex_unlock(&instance->reset_mutex);
8753 
8754         if (event_type && dcmd_ret == DCMD_SUCCESS)
8755                 megasas_add_remove_devices(instance, event_type);
8756 
8757         if (dcmd_ret == DCMD_SUCCESS)
8758                 seq_num = le32_to_cpu(instance->evt_detail->seq_num) + 1;
8759         else
8760                 seq_num = instance->last_seq_num;
8761 
8762         /* Register AEN with FW for latest sequence number plus 1 */
8763         class_locale.members.reserved = 0;
8764         class_locale.members.locale = MR_EVT_LOCALE_ALL;
8765         class_locale.members.class = MR_EVT_CLASS_DEBUG;
8766 
8767         if (instance->aen_cmd != NULL) {
8768                 kfree(ev);
8769                 return;
8770         }
8771 
8772         mutex_lock(&instance->reset_mutex);
8773         error = megasas_register_aen(instance, seq_num,
8774                                         class_locale.word);
8775         if (error)
8776                 dev_err(&instance->pdev->dev,
8777                         "register aen failed error %x\n", error);
8778 
8779         mutex_unlock(&instance->reset_mutex);
8780         kfree(ev);
8781 }
8782 
8783 /**
8784  * megasas_init - Driver load entry point
8785  */
8786 static int __init megasas_init(void)
8787 {
8788         int rval;
8789 
8790         /*
8791          * Booted in kdump kernel, minimize memory footprints by
8792          * disabling few features
8793          */
8794         if (reset_devices) {
8795                 msix_vectors = 1;
8796                 rdpq_enable = 0;
8797                 dual_qdepth_disable = 1;
8798         }
8799 
8800         /*
8801          * Announce driver version and other information
8802          */
8803         pr_info("megasas: %s\n", MEGASAS_VERSION);
8804 
8805         spin_lock_init(&poll_aen_lock);
8806 
8807         support_poll_for_event = 2;
8808         support_device_change = 1;
8809         support_nvme_encapsulation = true;
8810         support_pci_lane_margining = true;
8811 
8812         memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
8813 
8814         /*
8815          * Register character device node
8816          */
8817         rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
8818 
8819         if (rval < 0) {
8820                 printk(KERN_DEBUG "megasas: failed to open device node\n");
8821                 return rval;
8822         }
8823 
8824         megasas_mgmt_majorno = rval;
8825 
8826         megasas_init_debugfs();
8827 
8828         /*
8829          * Register ourselves as PCI hotplug module
8830          */
8831         rval = pci_register_driver(&megasas_pci_driver);
8832 
8833         if (rval) {
8834                 printk(KERN_DEBUG "megasas: PCI hotplug registration failed \n");
8835                 goto err_pcidrv;
8836         }
8837 
8838         if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
8839             (event_log_level > MFI_EVT_CLASS_DEAD)) {
8840                 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");
8841                 event_log_level = MFI_EVT_CLASS_CRITICAL;
8842         }
8843 
8844         rval = driver_create_file(&megasas_pci_driver.driver,
8845                                   &driver_attr_version);
8846         if (rval)
8847                 goto err_dcf_attr_ver;
8848 
8849         rval = driver_create_file(&megasas_pci_driver.driver,
8850                                   &driver_attr_release_date);
8851         if (rval)
8852                 goto err_dcf_rel_date;
8853 
8854         rval = driver_create_file(&megasas_pci_driver.driver,
8855                                 &driver_attr_support_poll_for_event);
8856         if (rval)
8857                 goto err_dcf_support_poll_for_event;
8858 
8859         rval = driver_create_file(&megasas_pci_driver.driver,
8860                                   &driver_attr_dbg_lvl);
8861         if (rval)
8862                 goto err_dcf_dbg_lvl;
8863         rval = driver_create_file(&megasas_pci_driver.driver,
8864                                 &driver_attr_support_device_change);
8865         if (rval)
8866                 goto err_dcf_support_device_change;
8867 
8868         rval = driver_create_file(&megasas_pci_driver.driver,
8869                                   &driver_attr_support_nvme_encapsulation);
8870         if (rval)
8871                 goto err_dcf_support_nvme_encapsulation;
8872 
8873         rval = driver_create_file(&megasas_pci_driver.driver,
8874                                   &driver_attr_support_pci_lane_margining);
8875         if (rval)
8876                 goto err_dcf_support_pci_lane_margining;
8877 
8878         return rval;
8879 
8880 err_dcf_support_pci_lane_margining:
8881         driver_remove_file(&megasas_pci_driver.driver,
8882                            &driver_attr_support_nvme_encapsulation);
8883 
8884 err_dcf_support_nvme_encapsulation:
8885         driver_remove_file(&megasas_pci_driver.driver,
8886                            &driver_attr_support_device_change);
8887 
8888 err_dcf_support_device_change:
8889         driver_remove_file(&megasas_pci_driver.driver,
8890                            &driver_attr_dbg_lvl);
8891 err_dcf_dbg_lvl:
8892         driver_remove_file(&megasas_pci_driver.driver,
8893                         &driver_attr_support_poll_for_event);
8894 err_dcf_support_poll_for_event:
8895         driver_remove_file(&megasas_pci_driver.driver,
8896                            &driver_attr_release_date);
8897 err_dcf_rel_date:
8898         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8899 err_dcf_attr_ver:
8900         pci_unregister_driver(&megasas_pci_driver);
8901 err_pcidrv:
8902         megasas_exit_debugfs();
8903         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8904         return rval;
8905 }
8906 
8907 /**
8908  * megasas_exit - Driver unload entry point
8909  */
8910 static void __exit megasas_exit(void)
8911 {
8912         driver_remove_file(&megasas_pci_driver.driver,
8913                            &driver_attr_dbg_lvl);
8914         driver_remove_file(&megasas_pci_driver.driver,
8915                         &driver_attr_support_poll_for_event);
8916         driver_remove_file(&megasas_pci_driver.driver,
8917                         &driver_attr_support_device_change);
8918         driver_remove_file(&megasas_pci_driver.driver,
8919                            &driver_attr_release_date);
8920         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
8921         driver_remove_file(&megasas_pci_driver.driver,
8922                            &driver_attr_support_nvme_encapsulation);
8923         driver_remove_file(&megasas_pci_driver.driver,
8924                            &driver_attr_support_pci_lane_margining);
8925 
8926         pci_unregister_driver(&megasas_pci_driver);
8927         megasas_exit_debugfs();
8928         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
8929 }
8930 
8931 module_init(megasas_init);
8932 module_exit(megasas_exit);

/* [<][>][^][v][top][bottom][index][help] */