linux/drivers/scsi/megaraid/megaraid_sas_base.c
<<
>>
Prefs
   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#include <linux/blk-mq-pci.h>
  41
  42#include <scsi/scsi.h>
  43#include <scsi/scsi_cmnd.h>
  44#include <scsi/scsi_device.h>
  45#include <scsi/scsi_host.h>
  46#include <scsi/scsi_tcq.h>
  47#include <scsi/scsi_dbg.h>
  48#include "megaraid_sas_fusion.h"
  49#include "megaraid_sas.h"
  50
  51/*
  52 * Number of sectors per IO command
  53 * Will be set in megasas_init_mfi if user does not provide
  54 */
  55static unsigned int max_sectors;
  56module_param_named(max_sectors, max_sectors, int, 0444);
  57MODULE_PARM_DESC(max_sectors,
  58        "Maximum number of sectors per IO command");
  59
  60static int msix_disable;
  61module_param(msix_disable, int, 0444);
  62MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
  63
  64static unsigned int msix_vectors;
  65module_param(msix_vectors, int, 0444);
  66MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
  67
  68static int allow_vf_ioctls;
  69module_param(allow_vf_ioctls, int, 0444);
  70MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
  71
  72static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
  73module_param(throttlequeuedepth, int, 0444);
  74MODULE_PARM_DESC(throttlequeuedepth,
  75        "Adapter queue depth when throttled due to I/O timeout. Default: 16");
  76
  77unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
  78module_param(resetwaittime, int, 0444);
  79MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
  80
  81static int smp_affinity_enable = 1;
  82module_param(smp_affinity_enable, int, 0444);
  83MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
  84
  85static int rdpq_enable = 1;
  86module_param(rdpq_enable, int, 0444);
  87MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
  88
  89unsigned int dual_qdepth_disable;
  90module_param(dual_qdepth_disable, int, 0444);
  91MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
  92
  93static unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
  94module_param(scmd_timeout, int, 0444);
  95MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
  96
  97int perf_mode = -1;
  98module_param(perf_mode, int, 0444);
  99MODULE_PARM_DESC(perf_mode, "Performance mode (only for Aero adapters), options:\n\t\t"
 100                "0 - balanced: High iops and low latency queues are allocated &\n\t\t"
 101                "interrupt coalescing is enabled only on high iops queues\n\t\t"
 102                "1 - iops: High iops queues are not allocated &\n\t\t"
 103                "interrupt coalescing is enabled on all queues\n\t\t"
 104                "2 - latency: High iops queues are not allocated &\n\t\t"
 105                "interrupt coalescing is disabled on all queues\n\t\t"
 106                "default mode is 'balanced'"
 107                );
 108
 109int event_log_level = MFI_EVT_CLASS_CRITICAL;
 110module_param(event_log_level, int, 0644);
 111MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
 112
 113unsigned int enable_sdev_max_qd;
 114module_param(enable_sdev_max_qd, int, 0444);
 115MODULE_PARM_DESC(enable_sdev_max_qd, "Enable sdev max qd as can_queue. Default: 0");
 116
 117int poll_queues;
 118module_param(poll_queues, int, 0444);
 119MODULE_PARM_DESC(poll_queues, "Number of queues to be use for io_uring poll mode.\n\t\t"
 120                "This parameter is effective only if host_tagset_enable=1 &\n\t\t"
 121                "It is not applicable for MFI_SERIES. &\n\t\t"
 122                "Driver will work in latency mode. &\n\t\t"
 123                "High iops queues are not allocated &\n\t\t"
 124                );
 125
 126int host_tagset_enable = 1;
 127module_param(host_tagset_enable, int, 0444);
 128MODULE_PARM_DESC(host_tagset_enable, "Shared host tagset enable/disable Default: enable(1)");
 129
 130MODULE_LICENSE("GPL");
 131MODULE_VERSION(MEGASAS_VERSION);
 132MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
 133MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
 134
 135int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
 136static int megasas_get_pd_list(struct megasas_instance *instance);
 137static int megasas_ld_list_query(struct megasas_instance *instance,
 138                                 u8 query_type);
 139static int megasas_issue_init_mfi(struct megasas_instance *instance);
 140static int megasas_register_aen(struct megasas_instance *instance,
 141                                u32 seq_num, u32 class_locale_word);
 142static void megasas_get_pd_info(struct megasas_instance *instance,
 143                                struct scsi_device *sdev);
 144static void
 145megasas_set_ld_removed_by_fw(struct megasas_instance *instance);
 146
 147/*
 148 * PCI ID table for all supported controllers
 149 */
 150static struct pci_device_id megasas_pci_table[] = {
 151
 152        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
 153        /* xscale IOP */
 154        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
 155        /* ppc IOP */
 156        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
 157        /* ppc IOP */
 158        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
 159        /* gen2*/
 160        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
 161        /* gen2*/
 162        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
 163        /* skinny*/
 164        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
 165        /* skinny*/
 166        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
 167        /* xscale IOP, vega */
 168        {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
 169        /* xscale IOP */
 170        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
 171        /* Fusion */
 172        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
 173        /* Plasma */
 174        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
 175        /* Invader */
 176        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
 177        /* Fury */
 178        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
 179        /* Intruder */
 180        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
 181        /* Intruder 24 port*/
 182        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
 183        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
 184        /* VENTURA */
 185        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
 186        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
 187        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
 188        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
 189        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
 190        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
 191        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
 192        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
 193        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
 194        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
 195        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
 196        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
 197        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
 198        {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
 199        {}
 200};
 201
 202MODULE_DEVICE_TABLE(pci, megasas_pci_table);
 203
 204static int megasas_mgmt_majorno;
 205struct megasas_mgmt_info megasas_mgmt_info;
 206static struct fasync_struct *megasas_async_queue;
 207static DEFINE_MUTEX(megasas_async_queue_mutex);
 208
 209static int megasas_poll_wait_aen;
 210static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
 211static u32 support_poll_for_event;
 212u32 megasas_dbg_lvl;
 213static u32 support_device_change;
 214static bool support_nvme_encapsulation;
 215static bool support_pci_lane_margining;
 216
 217/* define lock for aen poll */
 218static DEFINE_SPINLOCK(poll_aen_lock);
 219
 220extern struct dentry *megasas_debugfs_root;
 221extern int megasas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num);
 222
 223void
 224megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
 225                     u8 alt_status);
 226static u32
 227megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
 228static int
 229megasas_adp_reset_gen2(struct megasas_instance *instance,
 230                       struct megasas_register_set __iomem *reg_set);
 231static irqreturn_t megasas_isr(int irq, void *devp);
 232static u32
 233megasas_init_adapter_mfi(struct megasas_instance *instance);
 234u32
 235megasas_build_and_issue_cmd(struct megasas_instance *instance,
 236                            struct scsi_cmnd *scmd);
 237static void megasas_complete_cmd_dpc(unsigned long instance_addr);
 238int
 239wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
 240        int seconds);
 241void megasas_fusion_ocr_wq(struct work_struct *work);
 242static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
 243                                         int initial);
 244static int
 245megasas_set_dma_mask(struct megasas_instance *instance);
 246static int
 247megasas_alloc_ctrl_mem(struct megasas_instance *instance);
 248static inline void
 249megasas_free_ctrl_mem(struct megasas_instance *instance);
 250static inline int
 251megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
 252static inline void
 253megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
 254static inline void
 255megasas_init_ctrl_params(struct megasas_instance *instance);
 256
 257u32 megasas_readl(struct megasas_instance *instance,
 258                  const volatile void __iomem *addr)
 259{
 260        u32 i = 0, ret_val;
 261        /*
 262         * Due to a HW errata in Aero controllers, reads to certain
 263         * Fusion registers could intermittently return all zeroes.
 264         * This behavior is transient in nature and subsequent reads will
 265         * return valid value. As a workaround in driver, retry readl for
 266         * upto three times until a non-zero value is read.
 267         */
 268        if (instance->adapter_type == AERO_SERIES) {
 269                do {
 270                        ret_val = readl(addr);
 271                        i++;
 272                } while (ret_val == 0 && i < 3);
 273                return ret_val;
 274        } else {
 275                return readl(addr);
 276        }
 277}
 278
 279/**
 280 * megasas_set_dma_settings -   Populate DMA address, length and flags for DCMDs
 281 * @instance:                   Adapter soft state
 282 * @dcmd:                       DCMD frame inside MFI command
 283 * @dma_addr:                   DMA address of buffer to be passed to FW
 284 * @dma_len:                    Length of DMA buffer to be passed to FW
 285 * @return:                     void
 286 */
 287void megasas_set_dma_settings(struct megasas_instance *instance,
 288                              struct megasas_dcmd_frame *dcmd,
 289                              dma_addr_t dma_addr, u32 dma_len)
 290{
 291        if (instance->consistent_mask_64bit) {
 292                dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
 293                dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
 294                dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
 295
 296        } else {
 297                dcmd->sgl.sge32[0].phys_addr =
 298                                cpu_to_le32(lower_32_bits(dma_addr));
 299                dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
 300                dcmd->flags = cpu_to_le16(dcmd->flags);
 301        }
 302}
 303
 304static void
 305megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 306{
 307        instance->instancet->fire_cmd(instance,
 308                cmd->frame_phys_addr, 0, instance->reg_set);
 309        return;
 310}
 311
 312/**
 313 * megasas_get_cmd -    Get a command from the free pool
 314 * @instance:           Adapter soft state
 315 *
 316 * Returns a free command from the pool
 317 */
 318struct megasas_cmd *megasas_get_cmd(struct megasas_instance
 319                                                  *instance)
 320{
 321        unsigned long flags;
 322        struct megasas_cmd *cmd = NULL;
 323
 324        spin_lock_irqsave(&instance->mfi_pool_lock, flags);
 325
 326        if (!list_empty(&instance->cmd_pool)) {
 327                cmd = list_entry((&instance->cmd_pool)->next,
 328                                 struct megasas_cmd, list);
 329                list_del_init(&cmd->list);
 330        } else {
 331                dev_err(&instance->pdev->dev, "Command pool empty!\n");
 332        }
 333
 334        spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
 335        return cmd;
 336}
 337
 338/**
 339 * megasas_return_cmd - Return a cmd to free command pool
 340 * @instance:           Adapter soft state
 341 * @cmd:                Command packet to be returned to free command pool
 342 */
 343void
 344megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
 345{
 346        unsigned long flags;
 347        u32 blk_tags;
 348        struct megasas_cmd_fusion *cmd_fusion;
 349        struct fusion_context *fusion = instance->ctrl_context;
 350
 351        /* This flag is used only for fusion adapter.
 352         * Wait for Interrupt for Polled mode DCMD
 353         */
 354        if (cmd->flags & DRV_DCMD_POLLED_MODE)
 355                return;
 356
 357        spin_lock_irqsave(&instance->mfi_pool_lock, flags);
 358
 359        if (fusion) {
 360                blk_tags = instance->max_scsi_cmds + cmd->index;
 361                cmd_fusion = fusion->cmd_list[blk_tags];
 362                megasas_return_cmd_fusion(instance, cmd_fusion);
 363        }
 364        cmd->scmd = NULL;
 365        cmd->frame_count = 0;
 366        cmd->flags = 0;
 367        memset(cmd->frame, 0, instance->mfi_frame_size);
 368        cmd->frame->io.context = cpu_to_le32(cmd->index);
 369        if (!fusion && reset_devices)
 370                cmd->frame->hdr.cmd = MFI_CMD_INVALID;
 371        list_add(&cmd->list, (&instance->cmd_pool)->next);
 372
 373        spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
 374
 375}
 376
 377static const char *
 378format_timestamp(uint32_t timestamp)
 379{
 380        static char buffer[32];
 381
 382        if ((timestamp & 0xff000000) == 0xff000000)
 383                snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
 384                0x00ffffff);
 385        else
 386                snprintf(buffer, sizeof(buffer), "%us", timestamp);
 387        return buffer;
 388}
 389
 390static const char *
 391format_class(int8_t class)
 392{
 393        static char buffer[6];
 394
 395        switch (class) {
 396        case MFI_EVT_CLASS_DEBUG:
 397                return "debug";
 398        case MFI_EVT_CLASS_PROGRESS:
 399                return "progress";
 400        case MFI_EVT_CLASS_INFO:
 401                return "info";
 402        case MFI_EVT_CLASS_WARNING:
 403                return "WARN";
 404        case MFI_EVT_CLASS_CRITICAL:
 405                return "CRIT";
 406        case MFI_EVT_CLASS_FATAL:
 407                return "FATAL";
 408        case MFI_EVT_CLASS_DEAD:
 409                return "DEAD";
 410        default:
 411                snprintf(buffer, sizeof(buffer), "%d", class);
 412                return buffer;
 413        }
 414}
 415
 416/**
 417  * megasas_decode_evt: Decode FW AEN event and print critical event
 418  * for information.
 419  * @instance:                  Adapter soft state
 420  */
 421static void
 422megasas_decode_evt(struct megasas_instance *instance)
 423{
 424        struct megasas_evt_detail *evt_detail = instance->evt_detail;
 425        union megasas_evt_class_locale class_locale;
 426        class_locale.word = le32_to_cpu(evt_detail->cl.word);
 427
 428        if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
 429            (event_log_level > MFI_EVT_CLASS_DEAD)) {
 430                printk(KERN_WARNING "megaraid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
 431                event_log_level = MFI_EVT_CLASS_CRITICAL;
 432        }
 433
 434        if (class_locale.members.class >= event_log_level)
 435                dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
 436                        le32_to_cpu(evt_detail->seq_num),
 437                        format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
 438                        (class_locale.members.locale),
 439                        format_class(class_locale.members.class),
 440                        evt_detail->description);
 441
 442        if (megasas_dbg_lvl & LD_PD_DEBUG)
 443                dev_info(&instance->pdev->dev,
 444                         "evt_detail.args.ld.target_id/index %d/%d\n",
 445                         evt_detail->args.ld.target_id, evt_detail->args.ld.ld_index);
 446
 447}
 448
 449/*
 450 * The following functions are defined for xscale
 451 * (deviceid : 1064R, PERC5) controllers
 452 */
 453
 454/**
 455 * megasas_enable_intr_xscale - Enables interrupts
 456 * @instance:   Adapter soft state
 457 */
 458static inline void
 459megasas_enable_intr_xscale(struct megasas_instance *instance)
 460{
 461        struct megasas_register_set __iomem *regs;
 462
 463        regs = instance->reg_set;
 464        writel(0, &(regs)->outbound_intr_mask);
 465
 466        /* Dummy readl to force pci flush */
 467        readl(&regs->outbound_intr_mask);
 468}
 469
 470/**
 471 * megasas_disable_intr_xscale -Disables interrupt
 472 * @instance:   Adapter soft state
 473 */
 474static inline void
 475megasas_disable_intr_xscale(struct megasas_instance *instance)
 476{
 477        struct megasas_register_set __iomem *regs;
 478        u32 mask = 0x1f;
 479
 480        regs = instance->reg_set;
 481        writel(mask, &regs->outbound_intr_mask);
 482        /* Dummy readl to force pci flush */
 483        readl(&regs->outbound_intr_mask);
 484}
 485
 486/**
 487 * megasas_read_fw_status_reg_xscale - returns the current FW status value
 488 * @instance:   Adapter soft state
 489 */
 490static u32
 491megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
 492{
 493        return readl(&instance->reg_set->outbound_msg_0);
 494}
 495/**
 496 * megasas_clear_intr_xscale -  Check & clear interrupt
 497 * @instance:   Adapter soft state
 498 */
 499static int
 500megasas_clear_intr_xscale(struct megasas_instance *instance)
 501{
 502        u32 status;
 503        u32 mfiStatus = 0;
 504        struct megasas_register_set __iomem *regs;
 505        regs = instance->reg_set;
 506
 507        /*
 508         * Check if it is our interrupt
 509         */
 510        status = readl(&regs->outbound_intr_status);
 511
 512        if (status & MFI_OB_INTR_STATUS_MASK)
 513                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 514        if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
 515                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 516
 517        /*
 518         * Clear the interrupt by writing back the same value
 519         */
 520        if (mfiStatus)
 521                writel(status, &regs->outbound_intr_status);
 522
 523        /* Dummy readl to force pci flush */
 524        readl(&regs->outbound_intr_status);
 525
 526        return mfiStatus;
 527}
 528
 529/**
 530 * megasas_fire_cmd_xscale -    Sends command to the FW
 531 * @instance:           Adapter soft state
 532 * @frame_phys_addr :   Physical address of cmd
 533 * @frame_count :       Number of frames for the command
 534 * @regs :              MFI register set
 535 */
 536static inline void
 537megasas_fire_cmd_xscale(struct megasas_instance *instance,
 538                dma_addr_t frame_phys_addr,
 539                u32 frame_count,
 540                struct megasas_register_set __iomem *regs)
 541{
 542        unsigned long flags;
 543
 544        spin_lock_irqsave(&instance->hba_lock, flags);
 545        writel((frame_phys_addr >> 3)|(frame_count),
 546               &(regs)->inbound_queue_port);
 547        spin_unlock_irqrestore(&instance->hba_lock, flags);
 548}
 549
 550/**
 551 * megasas_adp_reset_xscale -  For controller reset
 552 * @instance:   Adapter soft state
 553 * @regs:       MFI register set
 554 */
 555static int
 556megasas_adp_reset_xscale(struct megasas_instance *instance,
 557        struct megasas_register_set __iomem *regs)
 558{
 559        u32 i;
 560        u32 pcidata;
 561
 562        writel(MFI_ADP_RESET, &regs->inbound_doorbell);
 563
 564        for (i = 0; i < 3; i++)
 565                msleep(1000); /* sleep for 3 secs */
 566        pcidata  = 0;
 567        pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
 568        dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
 569        if (pcidata & 0x2) {
 570                dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
 571                pcidata &= ~0x2;
 572                pci_write_config_dword(instance->pdev,
 573                                MFI_1068_PCSR_OFFSET, pcidata);
 574
 575                for (i = 0; i < 2; i++)
 576                        msleep(1000); /* need to wait 2 secs again */
 577
 578                pcidata  = 0;
 579                pci_read_config_dword(instance->pdev,
 580                                MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
 581                dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
 582                if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
 583                        dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
 584                        pcidata = 0;
 585                        pci_write_config_dword(instance->pdev,
 586                                MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
 587                }
 588        }
 589        return 0;
 590}
 591
 592/**
 593 * megasas_check_reset_xscale - For controller reset check
 594 * @instance:   Adapter soft state
 595 * @regs:       MFI register set
 596 */
 597static int
 598megasas_check_reset_xscale(struct megasas_instance *instance,
 599                struct megasas_register_set __iomem *regs)
 600{
 601        if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
 602            (le32_to_cpu(*instance->consumer) ==
 603                MEGASAS_ADPRESET_INPROG_SIGN))
 604                return 1;
 605        return 0;
 606}
 607
 608static struct megasas_instance_template megasas_instance_template_xscale = {
 609
 610        .fire_cmd = megasas_fire_cmd_xscale,
 611        .enable_intr = megasas_enable_intr_xscale,
 612        .disable_intr = megasas_disable_intr_xscale,
 613        .clear_intr = megasas_clear_intr_xscale,
 614        .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
 615        .adp_reset = megasas_adp_reset_xscale,
 616        .check_reset = megasas_check_reset_xscale,
 617        .service_isr = megasas_isr,
 618        .tasklet = megasas_complete_cmd_dpc,
 619        .init_adapter = megasas_init_adapter_mfi,
 620        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 621        .issue_dcmd = megasas_issue_dcmd,
 622};
 623
 624/*
 625 * This is the end of set of functions & definitions specific
 626 * to xscale (deviceid : 1064R, PERC5) controllers
 627 */
 628
 629/*
 630 * The following functions are defined for ppc (deviceid : 0x60)
 631 * controllers
 632 */
 633
 634/**
 635 * megasas_enable_intr_ppc -    Enables interrupts
 636 * @instance:   Adapter soft state
 637 */
 638static inline void
 639megasas_enable_intr_ppc(struct megasas_instance *instance)
 640{
 641        struct megasas_register_set __iomem *regs;
 642
 643        regs = instance->reg_set;
 644        writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 645
 646        writel(~0x80000000, &(regs)->outbound_intr_mask);
 647
 648        /* Dummy readl to force pci flush */
 649        readl(&regs->outbound_intr_mask);
 650}
 651
 652/**
 653 * megasas_disable_intr_ppc -   Disable interrupt
 654 * @instance:   Adapter soft state
 655 */
 656static inline void
 657megasas_disable_intr_ppc(struct megasas_instance *instance)
 658{
 659        struct megasas_register_set __iomem *regs;
 660        u32 mask = 0xFFFFFFFF;
 661
 662        regs = instance->reg_set;
 663        writel(mask, &regs->outbound_intr_mask);
 664        /* Dummy readl to force pci flush */
 665        readl(&regs->outbound_intr_mask);
 666}
 667
 668/**
 669 * megasas_read_fw_status_reg_ppc - returns the current FW status value
 670 * @instance:   Adapter soft state
 671 */
 672static u32
 673megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
 674{
 675        return readl(&instance->reg_set->outbound_scratch_pad_0);
 676}
 677
 678/**
 679 * megasas_clear_intr_ppc -     Check & clear interrupt
 680 * @instance:   Adapter soft state
 681 */
 682static int
 683megasas_clear_intr_ppc(struct megasas_instance *instance)
 684{
 685        u32 status, mfiStatus = 0;
 686        struct megasas_register_set __iomem *regs;
 687        regs = instance->reg_set;
 688
 689        /*
 690         * Check if it is our interrupt
 691         */
 692        status = readl(&regs->outbound_intr_status);
 693
 694        if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
 695                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 696
 697        if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
 698                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 699
 700        /*
 701         * Clear the interrupt by writing back the same value
 702         */
 703        writel(status, &regs->outbound_doorbell_clear);
 704
 705        /* Dummy readl to force pci flush */
 706        readl(&regs->outbound_doorbell_clear);
 707
 708        return mfiStatus;
 709}
 710
 711/**
 712 * megasas_fire_cmd_ppc -       Sends command to the FW
 713 * @instance:           Adapter soft state
 714 * @frame_phys_addr:    Physical address of cmd
 715 * @frame_count:        Number of frames for the command
 716 * @regs:               MFI register set
 717 */
 718static inline void
 719megasas_fire_cmd_ppc(struct megasas_instance *instance,
 720                dma_addr_t frame_phys_addr,
 721                u32 frame_count,
 722                struct megasas_register_set __iomem *regs)
 723{
 724        unsigned long flags;
 725
 726        spin_lock_irqsave(&instance->hba_lock, flags);
 727        writel((frame_phys_addr | (frame_count<<1))|1,
 728                        &(regs)->inbound_queue_port);
 729        spin_unlock_irqrestore(&instance->hba_lock, flags);
 730}
 731
 732/**
 733 * megasas_check_reset_ppc -    For controller reset check
 734 * @instance:   Adapter soft state
 735 * @regs:       MFI register set
 736 */
 737static int
 738megasas_check_reset_ppc(struct megasas_instance *instance,
 739                        struct megasas_register_set __iomem *regs)
 740{
 741        if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
 742                return 1;
 743
 744        return 0;
 745}
 746
 747static struct megasas_instance_template megasas_instance_template_ppc = {
 748
 749        .fire_cmd = megasas_fire_cmd_ppc,
 750        .enable_intr = megasas_enable_intr_ppc,
 751        .disable_intr = megasas_disable_intr_ppc,
 752        .clear_intr = megasas_clear_intr_ppc,
 753        .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
 754        .adp_reset = megasas_adp_reset_xscale,
 755        .check_reset = megasas_check_reset_ppc,
 756        .service_isr = megasas_isr,
 757        .tasklet = megasas_complete_cmd_dpc,
 758        .init_adapter = megasas_init_adapter_mfi,
 759        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 760        .issue_dcmd = megasas_issue_dcmd,
 761};
 762
 763/**
 764 * megasas_enable_intr_skinny - Enables interrupts
 765 * @instance:   Adapter soft state
 766 */
 767static inline void
 768megasas_enable_intr_skinny(struct megasas_instance *instance)
 769{
 770        struct megasas_register_set __iomem *regs;
 771
 772        regs = instance->reg_set;
 773        writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
 774
 775        writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 776
 777        /* Dummy readl to force pci flush */
 778        readl(&regs->outbound_intr_mask);
 779}
 780
 781/**
 782 * megasas_disable_intr_skinny -        Disables interrupt
 783 * @instance:   Adapter soft state
 784 */
 785static inline void
 786megasas_disable_intr_skinny(struct megasas_instance *instance)
 787{
 788        struct megasas_register_set __iomem *regs;
 789        u32 mask = 0xFFFFFFFF;
 790
 791        regs = instance->reg_set;
 792        writel(mask, &regs->outbound_intr_mask);
 793        /* Dummy readl to force pci flush */
 794        readl(&regs->outbound_intr_mask);
 795}
 796
 797/**
 798 * megasas_read_fw_status_reg_skinny - returns the current FW status value
 799 * @instance:   Adapter soft state
 800 */
 801static u32
 802megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
 803{
 804        return readl(&instance->reg_set->outbound_scratch_pad_0);
 805}
 806
 807/**
 808 * megasas_clear_intr_skinny -  Check & clear interrupt
 809 * @instance:   Adapter soft state
 810 */
 811static int
 812megasas_clear_intr_skinny(struct megasas_instance *instance)
 813{
 814        u32 status;
 815        u32 mfiStatus = 0;
 816        struct megasas_register_set __iomem *regs;
 817        regs = instance->reg_set;
 818
 819        /*
 820         * Check if it is our interrupt
 821         */
 822        status = readl(&regs->outbound_intr_status);
 823
 824        if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
 825                return 0;
 826        }
 827
 828        /*
 829         * Check if it is our interrupt
 830         */
 831        if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
 832            MFI_STATE_FAULT) {
 833                mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 834        } else
 835                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 836
 837        /*
 838         * Clear the interrupt by writing back the same value
 839         */
 840        writel(status, &regs->outbound_intr_status);
 841
 842        /*
 843         * dummy read to flush PCI
 844         */
 845        readl(&regs->outbound_intr_status);
 846
 847        return mfiStatus;
 848}
 849
 850/**
 851 * megasas_fire_cmd_skinny -    Sends command to the FW
 852 * @instance:           Adapter soft state
 853 * @frame_phys_addr:    Physical address of cmd
 854 * @frame_count:        Number of frames for the command
 855 * @regs:               MFI register set
 856 */
 857static inline void
 858megasas_fire_cmd_skinny(struct megasas_instance *instance,
 859                        dma_addr_t frame_phys_addr,
 860                        u32 frame_count,
 861                        struct megasas_register_set __iomem *regs)
 862{
 863        unsigned long flags;
 864
 865        spin_lock_irqsave(&instance->hba_lock, flags);
 866        writel(upper_32_bits(frame_phys_addr),
 867               &(regs)->inbound_high_queue_port);
 868        writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
 869               &(regs)->inbound_low_queue_port);
 870        spin_unlock_irqrestore(&instance->hba_lock, flags);
 871}
 872
 873/**
 874 * megasas_check_reset_skinny - For controller reset check
 875 * @instance:   Adapter soft state
 876 * @regs:       MFI register set
 877 */
 878static int
 879megasas_check_reset_skinny(struct megasas_instance *instance,
 880                                struct megasas_register_set __iomem *regs)
 881{
 882        if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
 883                return 1;
 884
 885        return 0;
 886}
 887
 888static struct megasas_instance_template megasas_instance_template_skinny = {
 889
 890        .fire_cmd = megasas_fire_cmd_skinny,
 891        .enable_intr = megasas_enable_intr_skinny,
 892        .disable_intr = megasas_disable_intr_skinny,
 893        .clear_intr = megasas_clear_intr_skinny,
 894        .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
 895        .adp_reset = megasas_adp_reset_gen2,
 896        .check_reset = megasas_check_reset_skinny,
 897        .service_isr = megasas_isr,
 898        .tasklet = megasas_complete_cmd_dpc,
 899        .init_adapter = megasas_init_adapter_mfi,
 900        .build_and_issue_cmd = megasas_build_and_issue_cmd,
 901        .issue_dcmd = megasas_issue_dcmd,
 902};
 903
 904
 905/*
 906 * The following functions are defined for gen2 (deviceid : 0x78 0x79)
 907 * controllers
 908 */
 909
 910/**
 911 * megasas_enable_intr_gen2 -  Enables interrupts
 912 * @instance:   Adapter soft state
 913 */
 914static inline void
 915megasas_enable_intr_gen2(struct megasas_instance *instance)
 916{
 917        struct megasas_register_set __iomem *regs;
 918
 919        regs = instance->reg_set;
 920        writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
 921
 922        /* write ~0x00000005 (4 & 1) to the intr mask*/
 923        writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
 924
 925        /* Dummy readl to force pci flush */
 926        readl(&regs->outbound_intr_mask);
 927}
 928
 929/**
 930 * megasas_disable_intr_gen2 - Disables interrupt
 931 * @instance:   Adapter soft state
 932 */
 933static inline void
 934megasas_disable_intr_gen2(struct megasas_instance *instance)
 935{
 936        struct megasas_register_set __iomem *regs;
 937        u32 mask = 0xFFFFFFFF;
 938
 939        regs = instance->reg_set;
 940        writel(mask, &regs->outbound_intr_mask);
 941        /* Dummy readl to force pci flush */
 942        readl(&regs->outbound_intr_mask);
 943}
 944
 945/**
 946 * megasas_read_fw_status_reg_gen2 - returns the current FW status value
 947 * @instance:   Adapter soft state
 948 */
 949static u32
 950megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
 951{
 952        return readl(&instance->reg_set->outbound_scratch_pad_0);
 953}
 954
 955/**
 956 * megasas_clear_intr_gen2 -      Check & clear interrupt
 957 * @instance:   Adapter soft state
 958 */
 959static int
 960megasas_clear_intr_gen2(struct megasas_instance *instance)
 961{
 962        u32 status;
 963        u32 mfiStatus = 0;
 964        struct megasas_register_set __iomem *regs;
 965        regs = instance->reg_set;
 966
 967        /*
 968         * Check if it is our interrupt
 969         */
 970        status = readl(&regs->outbound_intr_status);
 971
 972        if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
 973                mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
 974        }
 975        if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
 976                mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
 977        }
 978
 979        /*
 980         * Clear the interrupt by writing back the same value
 981         */
 982        if (mfiStatus)
 983                writel(status, &regs->outbound_doorbell_clear);
 984
 985        /* Dummy readl to force pci flush */
 986        readl(&regs->outbound_intr_status);
 987
 988        return mfiStatus;
 989}
 990
 991/**
 992 * megasas_fire_cmd_gen2 -     Sends command to the FW
 993 * @instance:           Adapter soft state
 994 * @frame_phys_addr:    Physical address of cmd
 995 * @frame_count:        Number of frames for the command
 996 * @regs:               MFI register set
 997 */
 998static inline void
 999megasas_fire_cmd_gen2(struct megasas_instance *instance,
1000                        dma_addr_t frame_phys_addr,
1001                        u32 frame_count,
1002                        struct megasas_register_set __iomem *regs)
1003{
1004        unsigned long flags;
1005
1006        spin_lock_irqsave(&instance->hba_lock, flags);
1007        writel((frame_phys_addr | (frame_count<<1))|1,
1008                        &(regs)->inbound_queue_port);
1009        spin_unlock_irqrestore(&instance->hba_lock, flags);
1010}
1011
1012/**
1013 * megasas_adp_reset_gen2 -     For controller reset
1014 * @instance:   Adapter soft state
1015 * @reg_set:    MFI register set
1016 */
1017static int
1018megasas_adp_reset_gen2(struct megasas_instance *instance,
1019                        struct megasas_register_set __iomem *reg_set)
1020{
1021        u32 retry = 0 ;
1022        u32 HostDiag;
1023        u32 __iomem *seq_offset = &reg_set->seq_offset;
1024        u32 __iomem *hostdiag_offset = &reg_set->host_diag;
1025
1026        if (instance->instancet == &megasas_instance_template_skinny) {
1027                seq_offset = &reg_set->fusion_seq_offset;
1028                hostdiag_offset = &reg_set->fusion_host_diag;
1029        }
1030
1031        writel(0, seq_offset);
1032        writel(4, seq_offset);
1033        writel(0xb, seq_offset);
1034        writel(2, seq_offset);
1035        writel(7, seq_offset);
1036        writel(0xd, seq_offset);
1037
1038        msleep(1000);
1039
1040        HostDiag = (u32)readl(hostdiag_offset);
1041
1042        while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1043                msleep(100);
1044                HostDiag = (u32)readl(hostdiag_offset);
1045                dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1046                                        retry, HostDiag);
1047
1048                if (retry++ >= 100)
1049                        return 1;
1050
1051        }
1052
1053        dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1054
1055        writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1056
1057        ssleep(10);
1058
1059        HostDiag = (u32)readl(hostdiag_offset);
1060        while (HostDiag & DIAG_RESET_ADAPTER) {
1061                msleep(100);
1062                HostDiag = (u32)readl(hostdiag_offset);
1063                dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1064                                retry, HostDiag);
1065
1066                if (retry++ >= 1000)
1067                        return 1;
1068
1069        }
1070        return 0;
1071}
1072
1073/**
1074 * megasas_check_reset_gen2 -   For controller reset check
1075 * @instance:   Adapter soft state
1076 * @regs:       MFI register set
1077 */
1078static int
1079megasas_check_reset_gen2(struct megasas_instance *instance,
1080                struct megasas_register_set __iomem *regs)
1081{
1082        if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1083                return 1;
1084
1085        return 0;
1086}
1087
1088static struct megasas_instance_template megasas_instance_template_gen2 = {
1089
1090        .fire_cmd = megasas_fire_cmd_gen2,
1091        .enable_intr = megasas_enable_intr_gen2,
1092        .disable_intr = megasas_disable_intr_gen2,
1093        .clear_intr = megasas_clear_intr_gen2,
1094        .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1095        .adp_reset = megasas_adp_reset_gen2,
1096        .check_reset = megasas_check_reset_gen2,
1097        .service_isr = megasas_isr,
1098        .tasklet = megasas_complete_cmd_dpc,
1099        .init_adapter = megasas_init_adapter_mfi,
1100        .build_and_issue_cmd = megasas_build_and_issue_cmd,
1101        .issue_dcmd = megasas_issue_dcmd,
1102};
1103
1104/*
1105 * This is the end of set of functions & definitions
1106 * specific to gen2 (deviceid : 0x78, 0x79) controllers
1107 */
1108
1109/*
1110 * Template added for TB (Fusion)
1111 */
1112extern struct megasas_instance_template megasas_instance_template_fusion;
1113
1114/**
1115 * megasas_issue_polled -       Issues a polling command
1116 * @instance:                   Adapter soft state
1117 * @cmd:                        Command packet to be issued
1118 *
1119 * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1120 */
1121int
1122megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1123{
1124        struct megasas_header *frame_hdr = &cmd->frame->hdr;
1125
1126        frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1127        frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1128
1129        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1130                dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1131                        __func__, __LINE__);
1132                return DCMD_INIT;
1133        }
1134
1135        instance->instancet->issue_dcmd(instance, cmd);
1136
1137        return wait_and_poll(instance, cmd, instance->requestorId ?
1138                        MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1139}
1140
1141/**
1142 * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
1143 * @instance:                   Adapter soft state
1144 * @cmd:                        Command to be issued
1145 * @timeout:                    Timeout in seconds
1146 *
1147 * This function waits on an event for the command to be returned from ISR.
1148 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1149 * Used to issue ioctl commands.
1150 */
1151int
1152megasas_issue_blocked_cmd(struct megasas_instance *instance,
1153                          struct megasas_cmd *cmd, int timeout)
1154{
1155        int ret = 0;
1156        cmd->cmd_status_drv = DCMD_INIT;
1157
1158        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1159                dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1160                        __func__, __LINE__);
1161                return DCMD_INIT;
1162        }
1163
1164        instance->instancet->issue_dcmd(instance, cmd);
1165
1166        if (timeout) {
1167                ret = wait_event_timeout(instance->int_cmd_wait_q,
1168                cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1169                if (!ret) {
1170                        dev_err(&instance->pdev->dev,
1171                                "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1172                                cmd->frame->dcmd.opcode, __func__);
1173                        return DCMD_TIMEOUT;
1174                }
1175        } else
1176                wait_event(instance->int_cmd_wait_q,
1177                                cmd->cmd_status_drv != DCMD_INIT);
1178
1179        return cmd->cmd_status_drv;
1180}
1181
1182/**
1183 * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
1184 * @instance:                           Adapter soft state
1185 * @cmd_to_abort:                       Previously issued cmd to be aborted
1186 * @timeout:                            Timeout in seconds
1187 *
1188 * MFI firmware can abort previously issued AEN comamnd (automatic event
1189 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1190 * cmd and waits for return status.
1191 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1192 */
1193static int
1194megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1195                                struct megasas_cmd *cmd_to_abort, int timeout)
1196{
1197        struct megasas_cmd *cmd;
1198        struct megasas_abort_frame *abort_fr;
1199        int ret = 0;
1200        u32 opcode;
1201
1202        cmd = megasas_get_cmd(instance);
1203
1204        if (!cmd)
1205                return -1;
1206
1207        abort_fr = &cmd->frame->abort;
1208
1209        /*
1210         * Prepare and issue the abort frame
1211         */
1212        abort_fr->cmd = MFI_CMD_ABORT;
1213        abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1214        abort_fr->flags = cpu_to_le16(0);
1215        abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1216        abort_fr->abort_mfi_phys_addr_lo =
1217                cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1218        abort_fr->abort_mfi_phys_addr_hi =
1219                cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1220
1221        cmd->sync_cmd = 1;
1222        cmd->cmd_status_drv = DCMD_INIT;
1223
1224        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1225                dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1226                        __func__, __LINE__);
1227                return DCMD_INIT;
1228        }
1229
1230        instance->instancet->issue_dcmd(instance, cmd);
1231
1232        if (timeout) {
1233                ret = wait_event_timeout(instance->abort_cmd_wait_q,
1234                cmd->cmd_status_drv != DCMD_INIT, timeout * HZ);
1235                if (!ret) {
1236                        opcode = cmd_to_abort->frame->dcmd.opcode;
1237                        dev_err(&instance->pdev->dev,
1238                                "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1239                                opcode,  __func__);
1240                        return DCMD_TIMEOUT;
1241                }
1242        } else
1243                wait_event(instance->abort_cmd_wait_q,
1244                cmd->cmd_status_drv != DCMD_INIT);
1245
1246        cmd->sync_cmd = 0;
1247
1248        megasas_return_cmd(instance, cmd);
1249        return cmd->cmd_status_drv;
1250}
1251
1252/**
1253 * megasas_make_sgl32 - Prepares 32-bit SGL
1254 * @instance:           Adapter soft state
1255 * @scp:                SCSI command from the mid-layer
1256 * @mfi_sgl:            SGL to be filled in
1257 *
1258 * If successful, this function returns the number of SG elements. Otherwise,
1259 * it returnes -1.
1260 */
1261static int
1262megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1263                   union megasas_sgl *mfi_sgl)
1264{
1265        int i;
1266        int sge_count;
1267        struct scatterlist *os_sgl;
1268
1269        sge_count = scsi_dma_map(scp);
1270        BUG_ON(sge_count < 0);
1271
1272        if (sge_count) {
1273                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1274                        mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1275                        mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1276                }
1277        }
1278        return sge_count;
1279}
1280
1281/**
1282 * megasas_make_sgl64 - Prepares 64-bit SGL
1283 * @instance:           Adapter soft state
1284 * @scp:                SCSI command from the mid-layer
1285 * @mfi_sgl:            SGL to be filled in
1286 *
1287 * If successful, this function returns the number of SG elements. Otherwise,
1288 * it returnes -1.
1289 */
1290static int
1291megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1292                   union megasas_sgl *mfi_sgl)
1293{
1294        int i;
1295        int sge_count;
1296        struct scatterlist *os_sgl;
1297
1298        sge_count = scsi_dma_map(scp);
1299        BUG_ON(sge_count < 0);
1300
1301        if (sge_count) {
1302                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1303                        mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1304                        mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1305                }
1306        }
1307        return sge_count;
1308}
1309
1310/**
1311 * megasas_make_sgl_skinny - Prepares IEEE SGL
1312 * @instance:           Adapter soft state
1313 * @scp:                SCSI command from the mid-layer
1314 * @mfi_sgl:            SGL to be filled in
1315 *
1316 * If successful, this function returns the number of SG elements. Otherwise,
1317 * it returnes -1.
1318 */
1319static int
1320megasas_make_sgl_skinny(struct megasas_instance *instance,
1321                struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1322{
1323        int i;
1324        int sge_count;
1325        struct scatterlist *os_sgl;
1326
1327        sge_count = scsi_dma_map(scp);
1328
1329        if (sge_count) {
1330                scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1331                        mfi_sgl->sge_skinny[i].length =
1332                                cpu_to_le32(sg_dma_len(os_sgl));
1333                        mfi_sgl->sge_skinny[i].phys_addr =
1334                                cpu_to_le64(sg_dma_address(os_sgl));
1335                        mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1336                }
1337        }
1338        return sge_count;
1339}
1340
1341 /**
1342 * megasas_get_frame_count - Computes the number of frames
1343 * @frame_type          : type of frame- io or pthru frame
1344 * @sge_count           : number of sg elements
1345 *
1346 * Returns the number of frames required for numnber of sge's (sge_count)
1347 */
1348
1349static u32 megasas_get_frame_count(struct megasas_instance *instance,
1350                        u8 sge_count, u8 frame_type)
1351{
1352        int num_cnt;
1353        int sge_bytes;
1354        u32 sge_sz;
1355        u32 frame_count = 0;
1356
1357        sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1358            sizeof(struct megasas_sge32);
1359
1360        if (instance->flag_ieee) {
1361                sge_sz = sizeof(struct megasas_sge_skinny);
1362        }
1363
1364        /*
1365         * Main frame can contain 2 SGEs for 64-bit SGLs and
1366         * 3 SGEs for 32-bit SGLs for ldio &
1367         * 1 SGEs for 64-bit SGLs and
1368         * 2 SGEs for 32-bit SGLs for pthru frame
1369         */
1370        if (unlikely(frame_type == PTHRU_FRAME)) {
1371                if (instance->flag_ieee == 1) {
1372                        num_cnt = sge_count - 1;
1373                } else if (IS_DMA64)
1374                        num_cnt = sge_count - 1;
1375                else
1376                        num_cnt = sge_count - 2;
1377        } else {
1378                if (instance->flag_ieee == 1) {
1379                        num_cnt = sge_count - 1;
1380                } else if (IS_DMA64)
1381                        num_cnt = sge_count - 2;
1382                else
1383                        num_cnt = sge_count - 3;
1384        }
1385
1386        if (num_cnt > 0) {
1387                sge_bytes = sge_sz * num_cnt;
1388
1389                frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1390                    ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1391        }
1392        /* Main frame */
1393        frame_count += 1;
1394
1395        if (frame_count > 7)
1396                frame_count = 8;
1397        return frame_count;
1398}
1399
1400/**
1401 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1402 * @instance:           Adapter soft state
1403 * @scp:                SCSI command
1404 * @cmd:                Command to be prepared in
1405 *
1406 * This function prepares CDB commands. These are typcially pass-through
1407 * commands to the devices.
1408 */
1409static int
1410megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1411                   struct megasas_cmd *cmd)
1412{
1413        u32 is_logical;
1414        u32 device_id;
1415        u16 flags = 0;
1416        struct megasas_pthru_frame *pthru;
1417
1418        is_logical = MEGASAS_IS_LOGICAL(scp->device);
1419        device_id = MEGASAS_DEV_INDEX(scp);
1420        pthru = (struct megasas_pthru_frame *)cmd->frame;
1421
1422        if (scp->sc_data_direction == DMA_TO_DEVICE)
1423                flags = MFI_FRAME_DIR_WRITE;
1424        else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1425                flags = MFI_FRAME_DIR_READ;
1426        else if (scp->sc_data_direction == DMA_NONE)
1427                flags = MFI_FRAME_DIR_NONE;
1428
1429        if (instance->flag_ieee == 1) {
1430                flags |= MFI_FRAME_IEEE;
1431        }
1432
1433        /*
1434         * Prepare the DCDB frame
1435         */
1436        pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1437        pthru->cmd_status = 0x0;
1438        pthru->scsi_status = 0x0;
1439        pthru->target_id = device_id;
1440        pthru->lun = scp->device->lun;
1441        pthru->cdb_len = scp->cmd_len;
1442        pthru->timeout = 0;
1443        pthru->pad_0 = 0;
1444        pthru->flags = cpu_to_le16(flags);
1445        pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1446
1447        memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1448
1449        /*
1450         * If the command is for the tape device, set the
1451         * pthru timeout to the os layer timeout value.
1452         */
1453        if (scp->device->type == TYPE_TAPE) {
1454                if ((scp->request->timeout / HZ) > 0xFFFF)
1455                        pthru->timeout = cpu_to_le16(0xFFFF);
1456                else
1457                        pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1458        }
1459
1460        /*
1461         * Construct SGL
1462         */
1463        if (instance->flag_ieee == 1) {
1464                pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1465                pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1466                                                      &pthru->sgl);
1467        } else if (IS_DMA64) {
1468                pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1469                pthru->sge_count = megasas_make_sgl64(instance, scp,
1470                                                      &pthru->sgl);
1471        } else
1472                pthru->sge_count = megasas_make_sgl32(instance, scp,
1473                                                      &pthru->sgl);
1474
1475        if (pthru->sge_count > instance->max_num_sge) {
1476                dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1477                        pthru->sge_count);
1478                return 0;
1479        }
1480
1481        /*
1482         * Sense info specific
1483         */
1484        pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1485        pthru->sense_buf_phys_addr_hi =
1486                cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1487        pthru->sense_buf_phys_addr_lo =
1488                cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1489
1490        /*
1491         * Compute the total number of frames this command consumes. FW uses
1492         * this number to pull sufficient number of frames from host memory.
1493         */
1494        cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1495                                                        PTHRU_FRAME);
1496
1497        return cmd->frame_count;
1498}
1499
1500/**
1501 * megasas_build_ldio - Prepares IOs to logical devices
1502 * @instance:           Adapter soft state
1503 * @scp:                SCSI command
1504 * @cmd:                Command to be prepared
1505 *
1506 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1507 */
1508static int
1509megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1510                   struct megasas_cmd *cmd)
1511{
1512        u32 device_id;
1513        u8 sc = scp->cmnd[0];
1514        u16 flags = 0;
1515        struct megasas_io_frame *ldio;
1516
1517        device_id = MEGASAS_DEV_INDEX(scp);
1518        ldio = (struct megasas_io_frame *)cmd->frame;
1519
1520        if (scp->sc_data_direction == DMA_TO_DEVICE)
1521                flags = MFI_FRAME_DIR_WRITE;
1522        else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1523                flags = MFI_FRAME_DIR_READ;
1524
1525        if (instance->flag_ieee == 1) {
1526                flags |= MFI_FRAME_IEEE;
1527        }
1528
1529        /*
1530         * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1531         */
1532        ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1533        ldio->cmd_status = 0x0;
1534        ldio->scsi_status = 0x0;
1535        ldio->target_id = device_id;
1536        ldio->timeout = 0;
1537        ldio->reserved_0 = 0;
1538        ldio->pad_0 = 0;
1539        ldio->flags = cpu_to_le16(flags);
1540        ldio->start_lba_hi = 0;
1541        ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1542
1543        /*
1544         * 6-byte READ(0x08) or WRITE(0x0A) cdb
1545         */
1546        if (scp->cmd_len == 6) {
1547                ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1548                ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1549                                                 ((u32) scp->cmnd[2] << 8) |
1550                                                 (u32) scp->cmnd[3]);
1551
1552                ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1553        }
1554
1555        /*
1556         * 10-byte READ(0x28) or WRITE(0x2A) cdb
1557         */
1558        else if (scp->cmd_len == 10) {
1559                ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1560                                              ((u32) scp->cmnd[7] << 8));
1561                ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1562                                                 ((u32) scp->cmnd[3] << 16) |
1563                                                 ((u32) scp->cmnd[4] << 8) |
1564                                                 (u32) scp->cmnd[5]);
1565        }
1566
1567        /*
1568         * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1569         */
1570        else if (scp->cmd_len == 12) {
1571                ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1572                                              ((u32) scp->cmnd[7] << 16) |
1573                                              ((u32) scp->cmnd[8] << 8) |
1574                                              (u32) scp->cmnd[9]);
1575
1576                ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1577                                                 ((u32) scp->cmnd[3] << 16) |
1578                                                 ((u32) scp->cmnd[4] << 8) |
1579                                                 (u32) scp->cmnd[5]);
1580        }
1581
1582        /*
1583         * 16-byte READ(0x88) or WRITE(0x8A) cdb
1584         */
1585        else if (scp->cmd_len == 16) {
1586                ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1587                                              ((u32) scp->cmnd[11] << 16) |
1588                                              ((u32) scp->cmnd[12] << 8) |
1589                                              (u32) scp->cmnd[13]);
1590
1591                ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1592                                                 ((u32) scp->cmnd[7] << 16) |
1593                                                 ((u32) scp->cmnd[8] << 8) |
1594                                                 (u32) scp->cmnd[9]);
1595
1596                ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1597                                                 ((u32) scp->cmnd[3] << 16) |
1598                                                 ((u32) scp->cmnd[4] << 8) |
1599                                                 (u32) scp->cmnd[5]);
1600
1601        }
1602
1603        /*
1604         * Construct SGL
1605         */
1606        if (instance->flag_ieee) {
1607                ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1608                ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1609                                              &ldio->sgl);
1610        } else if (IS_DMA64) {
1611                ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1612                ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1613        } else
1614                ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1615
1616        if (ldio->sge_count > instance->max_num_sge) {
1617                dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1618                        ldio->sge_count);
1619                return 0;
1620        }
1621
1622        /*
1623         * Sense info specific
1624         */
1625        ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1626        ldio->sense_buf_phys_addr_hi = 0;
1627        ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1628
1629        /*
1630         * Compute the total number of frames this command consumes. FW uses
1631         * this number to pull sufficient number of frames from host memory.
1632         */
1633        cmd->frame_count = megasas_get_frame_count(instance,
1634                        ldio->sge_count, IO_FRAME);
1635
1636        return cmd->frame_count;
1637}
1638
1639/**
1640 * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1641 *                              and whether it's RW or non RW
1642 * @cmd:                        SCSI command
1643 *
1644 */
1645inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1646{
1647        int ret;
1648
1649        switch (cmd->cmnd[0]) {
1650        case READ_10:
1651        case WRITE_10:
1652        case READ_12:
1653        case WRITE_12:
1654        case READ_6:
1655        case WRITE_6:
1656        case READ_16:
1657        case WRITE_16:
1658                ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1659                        READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1660                break;
1661        default:
1662                ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1663                        NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1664        }
1665        return ret;
1666}
1667
1668 /**
1669 * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1670 *                                      in FW
1671 * @instance:                           Adapter soft state
1672 */
1673static inline void
1674megasas_dump_pending_frames(struct megasas_instance *instance)
1675{
1676        struct megasas_cmd *cmd;
1677        int i,n;
1678        union megasas_sgl *mfi_sgl;
1679        struct megasas_io_frame *ldio;
1680        struct megasas_pthru_frame *pthru;
1681        u32 sgcount;
1682        u16 max_cmd = instance->max_fw_cmds;
1683
1684        dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1685        dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1686        if (IS_DMA64)
1687                dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1688        else
1689                dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1690
1691        dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1692        for (i = 0; i < max_cmd; i++) {
1693                cmd = instance->cmd_list[i];
1694                if (!cmd->scmd)
1695                        continue;
1696                dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1697                if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1698                        ldio = (struct megasas_io_frame *)cmd->frame;
1699                        mfi_sgl = &ldio->sgl;
1700                        sgcount = ldio->sge_count;
1701                        dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1702                        " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1703                        instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1704                        le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1705                        le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1706                } else {
1707                        pthru = (struct megasas_pthru_frame *) cmd->frame;
1708                        mfi_sgl = &pthru->sgl;
1709                        sgcount = pthru->sge_count;
1710                        dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1711                        "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1712                        instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1713                        pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1714                        le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1715                }
1716                if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1717                        for (n = 0; n < sgcount; n++) {
1718                                if (IS_DMA64)
1719                                        dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1720                                                le32_to_cpu(mfi_sgl->sge64[n].length),
1721                                                le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1722                                else
1723                                        dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1724                                                le32_to_cpu(mfi_sgl->sge32[n].length),
1725                                                le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1726                        }
1727                }
1728        } /*for max_cmd*/
1729        dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1730        for (i = 0; i < max_cmd; i++) {
1731
1732                cmd = instance->cmd_list[i];
1733
1734                if (cmd->sync_cmd == 1)
1735                        dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1736        }
1737        dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1738}
1739
1740u32
1741megasas_build_and_issue_cmd(struct megasas_instance *instance,
1742                            struct scsi_cmnd *scmd)
1743{
1744        struct megasas_cmd *cmd;
1745        u32 frame_count;
1746
1747        cmd = megasas_get_cmd(instance);
1748        if (!cmd)
1749                return SCSI_MLQUEUE_HOST_BUSY;
1750
1751        /*
1752         * Logical drive command
1753         */
1754        if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1755                frame_count = megasas_build_ldio(instance, scmd, cmd);
1756        else
1757                frame_count = megasas_build_dcdb(instance, scmd, cmd);
1758
1759        if (!frame_count)
1760                goto out_return_cmd;
1761
1762        cmd->scmd = scmd;
1763        scmd->SCp.ptr = (char *)cmd;
1764
1765        /*
1766         * Issue the command to the FW
1767         */
1768        atomic_inc(&instance->fw_outstanding);
1769
1770        instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1771                                cmd->frame_count-1, instance->reg_set);
1772
1773        return 0;
1774out_return_cmd:
1775        megasas_return_cmd(instance, cmd);
1776        return SCSI_MLQUEUE_HOST_BUSY;
1777}
1778
1779
1780/**
1781 * megasas_queue_command -      Queue entry point
1782 * @shost:                      adapter SCSI host
1783 * @scmd:                       SCSI command to be queued
1784 */
1785static int
1786megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1787{
1788        struct megasas_instance *instance;
1789        struct MR_PRIV_DEVICE *mr_device_priv_data;
1790        u32 ld_tgt_id;
1791
1792        instance = (struct megasas_instance *)
1793            scmd->device->host->hostdata;
1794
1795        if (instance->unload == 1) {
1796                scmd->result = DID_NO_CONNECT << 16;
1797                scmd->scsi_done(scmd);
1798                return 0;
1799        }
1800
1801        if (instance->issuepend_done == 0)
1802                return SCSI_MLQUEUE_HOST_BUSY;
1803
1804
1805        /* Check for an mpio path and adjust behavior */
1806        if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1807                if (megasas_check_mpio_paths(instance, scmd) ==
1808                    (DID_REQUEUE << 16)) {
1809                        return SCSI_MLQUEUE_HOST_BUSY;
1810                } else {
1811                        scmd->result = DID_NO_CONNECT << 16;
1812                        scmd->scsi_done(scmd);
1813                        return 0;
1814                }
1815        }
1816
1817        mr_device_priv_data = scmd->device->hostdata;
1818        if (!mr_device_priv_data ||
1819            (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)) {
1820                scmd->result = DID_NO_CONNECT << 16;
1821                scmd->scsi_done(scmd);
1822                return 0;
1823        }
1824
1825        if (MEGASAS_IS_LOGICAL(scmd->device)) {
1826                ld_tgt_id = MEGASAS_TARGET_ID(scmd->device);
1827                if (instance->ld_tgtid_status[ld_tgt_id] == LD_TARGET_ID_DELETED) {
1828                        scmd->result = DID_NO_CONNECT << 16;
1829                        scmd->scsi_done(scmd);
1830                        return 0;
1831                }
1832        }
1833
1834        if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1835                return SCSI_MLQUEUE_HOST_BUSY;
1836
1837        if (mr_device_priv_data->tm_busy)
1838                return SCSI_MLQUEUE_DEVICE_BUSY;
1839
1840
1841        scmd->result = 0;
1842
1843        if (MEGASAS_IS_LOGICAL(scmd->device) &&
1844            (scmd->device->id >= instance->fw_supported_vd_count ||
1845                scmd->device->lun)) {
1846                scmd->result = DID_BAD_TARGET << 16;
1847                goto out_done;
1848        }
1849
1850        if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1851            MEGASAS_IS_LOGICAL(scmd->device) &&
1852            (!instance->fw_sync_cache_support)) {
1853                scmd->result = DID_OK << 16;
1854                goto out_done;
1855        }
1856
1857        return instance->instancet->build_and_issue_cmd(instance, scmd);
1858
1859 out_done:
1860        scmd->scsi_done(scmd);
1861        return 0;
1862}
1863
1864static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1865{
1866        int i;
1867
1868        for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1869
1870                if ((megasas_mgmt_info.instance[i]) &&
1871                    (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1872                        return megasas_mgmt_info.instance[i];
1873        }
1874
1875        return NULL;
1876}
1877
1878/*
1879* megasas_set_dynamic_target_properties -
1880* Device property set by driver may not be static and it is required to be
1881* updated after OCR
1882*
1883* set tm_capable.
1884* set dma alignment (only for eedp protection enable vd).
1885*
1886* @sdev: OS provided scsi device
1887*
1888* Returns void
1889*/
1890void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1891                                           bool is_target_prop)
1892{
1893        u16 pd_index = 0, ld;
1894        u32 device_id;
1895        struct megasas_instance *instance;
1896        struct fusion_context *fusion;
1897        struct MR_PRIV_DEVICE *mr_device_priv_data;
1898        struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1899        struct MR_LD_RAID *raid;
1900        struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1901
1902        instance = megasas_lookup_instance(sdev->host->host_no);
1903        fusion = instance->ctrl_context;
1904        mr_device_priv_data = sdev->hostdata;
1905
1906        if (!fusion || !mr_device_priv_data)
1907                return;
1908
1909        if (MEGASAS_IS_LOGICAL(sdev)) {
1910                device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1911                                        + sdev->id;
1912                local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1913                ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1914                if (ld >= instance->fw_supported_vd_count)
1915                        return;
1916                raid = MR_LdRaidGet(ld, local_map_ptr);
1917
1918                if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1919                blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1920
1921                mr_device_priv_data->is_tm_capable =
1922                        raid->capability.tmCapable;
1923
1924                if (!raid->flags.isEPD)
1925                        sdev->no_write_same = 1;
1926
1927        } else if (instance->use_seqnum_jbod_fp) {
1928                pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1929                        sdev->id;
1930                pd_sync = (void *)fusion->pd_seq_sync
1931                                [(instance->pd_seq_map_id - 1) & 1];
1932                mr_device_priv_data->is_tm_capable =
1933                        pd_sync->seq[pd_index].capability.tmCapable;
1934        }
1935
1936        if (is_target_prop && instance->tgt_prop->reset_tmo) {
1937                /*
1938                 * If FW provides a target reset timeout value, driver will use
1939                 * it. If not set, fallback to default values.
1940                 */
1941                mr_device_priv_data->target_reset_tmo =
1942                        min_t(u8, instance->max_reset_tmo,
1943                              instance->tgt_prop->reset_tmo);
1944                mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1945        } else {
1946                mr_device_priv_data->target_reset_tmo =
1947                                                MEGASAS_DEFAULT_TM_TIMEOUT;
1948                mr_device_priv_data->task_abort_tmo =
1949                                                MEGASAS_DEFAULT_TM_TIMEOUT;
1950        }
1951}
1952
1953/*
1954 * megasas_set_nvme_device_properties -
1955 * set nomerges=2
1956 * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1957 * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1958 *
1959 * MR firmware provides value in KB. Caller of this function converts
1960 * kb into bytes.
1961 *
1962 * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1963 * MR firmware provides value 128 as (32 * 4K) = 128K.
1964 *
1965 * @sdev:                               scsi device
1966 * @max_io_size:                                maximum io transfer size
1967 *
1968 */
1969static inline void
1970megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1971{
1972        struct megasas_instance *instance;
1973        u32 mr_nvme_pg_size;
1974
1975        instance = (struct megasas_instance *)sdev->host->hostdata;
1976        mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1977                                MR_DEFAULT_NVME_PAGE_SIZE);
1978
1979        blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1980
1981        blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1982        blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1983}
1984
1985/*
1986 * megasas_set_fw_assisted_qd -
1987 * set device queue depth to can_queue
1988 * set device queue depth to fw assisted qd
1989 *
1990 * @sdev:                               scsi device
1991 * @is_target_prop                      true, if fw provided target properties.
1992 */
1993static void megasas_set_fw_assisted_qd(struct scsi_device *sdev,
1994                                                 bool is_target_prop)
1995{
1996        u8 interface_type;
1997        u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1998        u32 tgt_device_qd;
1999        struct megasas_instance *instance;
2000        struct MR_PRIV_DEVICE *mr_device_priv_data;
2001
2002        instance = megasas_lookup_instance(sdev->host->host_no);
2003        mr_device_priv_data = sdev->hostdata;
2004        interface_type  = mr_device_priv_data->interface_type;
2005
2006        switch (interface_type) {
2007        case SAS_PD:
2008                device_qd = MEGASAS_SAS_QD;
2009                break;
2010        case SATA_PD:
2011                device_qd = MEGASAS_SATA_QD;
2012                break;
2013        case NVME_PD:
2014                device_qd = MEGASAS_NVME_QD;
2015                break;
2016        }
2017
2018        if (is_target_prop) {
2019                tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
2020                if (tgt_device_qd)
2021                        device_qd = min(instance->host->can_queue,
2022                                        (int)tgt_device_qd);
2023        }
2024
2025        if (instance->enable_sdev_max_qd && interface_type != UNKNOWN_DRIVE)
2026                device_qd = instance->host->can_queue;
2027
2028        scsi_change_queue_depth(sdev, device_qd);
2029}
2030
2031/*
2032 * megasas_set_static_target_properties -
2033 * Device property set by driver are static and it is not required to be
2034 * updated after OCR.
2035 *
2036 * set io timeout
2037 * set device queue depth
2038 * set nvme device properties. see - megasas_set_nvme_device_properties
2039 *
2040 * @sdev:                               scsi device
2041 * @is_target_prop                      true, if fw provided target properties.
2042 */
2043static void megasas_set_static_target_properties(struct scsi_device *sdev,
2044                                                 bool is_target_prop)
2045{
2046        u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
2047        struct megasas_instance *instance;
2048
2049        instance = megasas_lookup_instance(sdev->host->host_no);
2050
2051        /*
2052         * The RAID firmware may require extended timeouts.
2053         */
2054        blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
2055
2056        /* max_io_size_kb will be set to non zero for
2057         * nvme based vd and syspd.
2058         */
2059        if (is_target_prop)
2060                max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
2061
2062        if (instance->nvme_page_size && max_io_size_kb)
2063                megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2064
2065        megasas_set_fw_assisted_qd(sdev, is_target_prop);
2066}
2067
2068
2069static int megasas_slave_configure(struct scsi_device *sdev)
2070{
2071        u16 pd_index = 0;
2072        struct megasas_instance *instance;
2073        int ret_target_prop = DCMD_FAILED;
2074        bool is_target_prop = false;
2075
2076        instance = megasas_lookup_instance(sdev->host->host_no);
2077        if (instance->pd_list_not_supported) {
2078                if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2079                        pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2080                                sdev->id;
2081                        if (instance->pd_list[pd_index].driveState !=
2082                                MR_PD_STATE_SYSTEM)
2083                                return -ENXIO;
2084                }
2085        }
2086
2087        mutex_lock(&instance->reset_mutex);
2088        /* Send DCMD to Firmware and cache the information */
2089        if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2090                megasas_get_pd_info(instance, sdev);
2091
2092        /* Some ventura firmware may not have instance->nvme_page_size set.
2093         * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2094         */
2095        if ((instance->tgt_prop) && (instance->nvme_page_size))
2096                ret_target_prop = megasas_get_target_prop(instance, sdev);
2097
2098        is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2099        megasas_set_static_target_properties(sdev, is_target_prop);
2100
2101        /* This sdev property may change post OCR */
2102        megasas_set_dynamic_target_properties(sdev, is_target_prop);
2103
2104        mutex_unlock(&instance->reset_mutex);
2105
2106        return 0;
2107}
2108
2109static int megasas_slave_alloc(struct scsi_device *sdev)
2110{
2111        u16 pd_index = 0, ld_tgt_id;
2112        struct megasas_instance *instance ;
2113        struct MR_PRIV_DEVICE *mr_device_priv_data;
2114
2115        instance = megasas_lookup_instance(sdev->host->host_no);
2116        if (!MEGASAS_IS_LOGICAL(sdev)) {
2117                /*
2118                 * Open the OS scan to the SYSTEM PD
2119                 */
2120                pd_index =
2121                        (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2122                        sdev->id;
2123                if ((instance->pd_list_not_supported ||
2124                        instance->pd_list[pd_index].driveState ==
2125                        MR_PD_STATE_SYSTEM)) {
2126                        goto scan_target;
2127                }
2128                return -ENXIO;
2129        }
2130
2131scan_target:
2132        mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2133                                        GFP_KERNEL);
2134        if (!mr_device_priv_data)
2135                return -ENOMEM;
2136
2137        if (MEGASAS_IS_LOGICAL(sdev)) {
2138                ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2139                instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_ACTIVE;
2140                if (megasas_dbg_lvl & LD_PD_DEBUG)
2141                        sdev_printk(KERN_INFO, sdev, "LD target ID %d created.\n", ld_tgt_id);
2142        }
2143
2144        sdev->hostdata = mr_device_priv_data;
2145
2146        atomic_set(&mr_device_priv_data->r1_ldio_hint,
2147                   instance->r1_ldio_hint_default);
2148        return 0;
2149}
2150
2151static void megasas_slave_destroy(struct scsi_device *sdev)
2152{
2153        u16 ld_tgt_id;
2154        struct megasas_instance *instance;
2155
2156        instance = megasas_lookup_instance(sdev->host->host_no);
2157
2158        if (MEGASAS_IS_LOGICAL(sdev)) {
2159                ld_tgt_id = MEGASAS_TARGET_ID(sdev);
2160                instance->ld_tgtid_status[ld_tgt_id] = LD_TARGET_ID_DELETED;
2161                if (megasas_dbg_lvl & LD_PD_DEBUG)
2162                        sdev_printk(KERN_INFO, sdev,
2163                                    "LD target ID %d removed from OS stack\n", ld_tgt_id);
2164        }
2165
2166        kfree(sdev->hostdata);
2167        sdev->hostdata = NULL;
2168}
2169
2170/*
2171* megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2172*                                       kill adapter
2173* @instance:                            Adapter soft state
2174*
2175*/
2176static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2177{
2178        int i;
2179        struct megasas_cmd *cmd_mfi;
2180        struct megasas_cmd_fusion *cmd_fusion;
2181        struct fusion_context *fusion = instance->ctrl_context;
2182
2183        /* Find all outstanding ioctls */
2184        if (fusion) {
2185                for (i = 0; i < instance->max_fw_cmds; i++) {
2186                        cmd_fusion = fusion->cmd_list[i];
2187                        if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2188                                cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2189                                if (cmd_mfi->sync_cmd &&
2190                                    (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2191                                        cmd_mfi->frame->hdr.cmd_status =
2192                                                        MFI_STAT_WRONG_STATE;
2193                                        megasas_complete_cmd(instance,
2194                                                             cmd_mfi, DID_OK);
2195                                }
2196                        }
2197                }
2198        } else {
2199                for (i = 0; i < instance->max_fw_cmds; i++) {
2200                        cmd_mfi = instance->cmd_list[i];
2201                        if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2202                                MFI_CMD_ABORT)
2203                                megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2204                }
2205        }
2206}
2207
2208
2209void megaraid_sas_kill_hba(struct megasas_instance *instance)
2210{
2211        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2212                dev_warn(&instance->pdev->dev,
2213                         "Adapter already dead, skipping kill HBA\n");
2214                return;
2215        }
2216
2217        /* Set critical error to block I/O & ioctls in case caller didn't */
2218        atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2219        /* Wait 1 second to ensure IO or ioctls in build have posted */
2220        msleep(1000);
2221        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2222                (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2223                (instance->adapter_type != MFI_SERIES)) {
2224                if (!instance->requestorId) {
2225                        writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2226                        /* Flush */
2227                        readl(&instance->reg_set->doorbell);
2228                }
2229                if (instance->requestorId && instance->peerIsPresent)
2230                        memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2231        } else {
2232                writel(MFI_STOP_ADP,
2233                        &instance->reg_set->inbound_doorbell);
2234        }
2235        /* Complete outstanding ioctls when adapter is killed */
2236        megasas_complete_outstanding_ioctls(instance);
2237}
2238
2239 /**
2240  * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2241  *                                     restored to max value
2242  * @instance:                  Adapter soft state
2243  *
2244  */
2245void
2246megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2247{
2248        unsigned long flags;
2249
2250        if (instance->flag & MEGASAS_FW_BUSY
2251            && time_after(jiffies, instance->last_time + 5 * HZ)
2252            && atomic_read(&instance->fw_outstanding) <
2253            instance->throttlequeuedepth + 1) {
2254
2255                spin_lock_irqsave(instance->host->host_lock, flags);
2256                instance->flag &= ~MEGASAS_FW_BUSY;
2257
2258                instance->host->can_queue = instance->cur_can_queue;
2259                spin_unlock_irqrestore(instance->host->host_lock, flags);
2260        }
2261}
2262
2263/**
2264 * megasas_complete_cmd_dpc      -      Returns FW's controller structure
2265 * @instance_addr:                      Address of adapter soft state
2266 *
2267 * Tasklet to complete cmds
2268 */
2269static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2270{
2271        u32 producer;
2272        u32 consumer;
2273        u32 context;
2274        struct megasas_cmd *cmd;
2275        struct megasas_instance *instance =
2276                                (struct megasas_instance *)instance_addr;
2277        unsigned long flags;
2278
2279        /* If we have already declared adapter dead, donot complete cmds */
2280        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2281                return;
2282
2283        spin_lock_irqsave(&instance->completion_lock, flags);
2284
2285        producer = le32_to_cpu(*instance->producer);
2286        consumer = le32_to_cpu(*instance->consumer);
2287
2288        while (consumer != producer) {
2289                context = le32_to_cpu(instance->reply_queue[consumer]);
2290                if (context >= instance->max_fw_cmds) {
2291                        dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2292                                context);
2293                        BUG();
2294                }
2295
2296                cmd = instance->cmd_list[context];
2297
2298                megasas_complete_cmd(instance, cmd, DID_OK);
2299
2300                consumer++;
2301                if (consumer == (instance->max_fw_cmds + 1)) {
2302                        consumer = 0;
2303                }
2304        }
2305
2306        *instance->consumer = cpu_to_le32(producer);
2307
2308        spin_unlock_irqrestore(&instance->completion_lock, flags);
2309
2310        /*
2311         * Check if we can restore can_queue
2312         */
2313        megasas_check_and_restore_queue_depth(instance);
2314}
2315
2316static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2317
2318/**
2319 * megasas_start_timer - Initializes sriov heartbeat timer object
2320 * @instance:           Adapter soft state
2321 *
2322 */
2323void megasas_start_timer(struct megasas_instance *instance)
2324{
2325        struct timer_list *timer = &instance->sriov_heartbeat_timer;
2326
2327        timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2328        timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2329        add_timer(timer);
2330}
2331
2332static void
2333megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2334
2335static void
2336process_fw_state_change_wq(struct work_struct *work);
2337
2338static void megasas_do_ocr(struct megasas_instance *instance)
2339{
2340        if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2341        (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2342        (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2343                *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2344        }
2345        instance->instancet->disable_intr(instance);
2346        atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2347        instance->issuepend_done = 0;
2348
2349        atomic_set(&instance->fw_outstanding, 0);
2350        megasas_internal_reset_defer_cmds(instance);
2351        process_fw_state_change_wq(&instance->work_init);
2352}
2353
2354static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2355                                            int initial)
2356{
2357        struct megasas_cmd *cmd;
2358        struct megasas_dcmd_frame *dcmd;
2359        struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2360        dma_addr_t new_affiliation_111_h;
2361        int ld, retval = 0;
2362        u8 thisVf;
2363
2364        cmd = megasas_get_cmd(instance);
2365
2366        if (!cmd) {
2367                dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2368                       "Failed to get cmd for scsi%d\n",
2369                        instance->host->host_no);
2370                return -ENOMEM;
2371        }
2372
2373        dcmd = &cmd->frame->dcmd;
2374
2375        if (!instance->vf_affiliation_111) {
2376                dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2377                       "affiliation for scsi%d\n", instance->host->host_no);
2378                megasas_return_cmd(instance, cmd);
2379                return -ENOMEM;
2380        }
2381
2382        if (initial)
2383                        memset(instance->vf_affiliation_111, 0,
2384                               sizeof(struct MR_LD_VF_AFFILIATION_111));
2385        else {
2386                new_affiliation_111 =
2387                        dma_alloc_coherent(&instance->pdev->dev,
2388                                           sizeof(struct MR_LD_VF_AFFILIATION_111),
2389                                           &new_affiliation_111_h, GFP_KERNEL);
2390                if (!new_affiliation_111) {
2391                        dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2392                               "memory for new affiliation for scsi%d\n",
2393                               instance->host->host_no);
2394                        megasas_return_cmd(instance, cmd);
2395                        return -ENOMEM;
2396                }
2397        }
2398
2399        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2400
2401        dcmd->cmd = MFI_CMD_DCMD;
2402        dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2403        dcmd->sge_count = 1;
2404        dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2405        dcmd->timeout = 0;
2406        dcmd->pad_0 = 0;
2407        dcmd->data_xfer_len =
2408                cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2409        dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2410
2411        if (initial)
2412                dcmd->sgl.sge32[0].phys_addr =
2413                        cpu_to_le32(instance->vf_affiliation_111_h);
2414        else
2415                dcmd->sgl.sge32[0].phys_addr =
2416                        cpu_to_le32(new_affiliation_111_h);
2417
2418        dcmd->sgl.sge32[0].length = cpu_to_le32(
2419                sizeof(struct MR_LD_VF_AFFILIATION_111));
2420
2421        dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2422               "scsi%d\n", instance->host->host_no);
2423
2424        if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2425                dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2426                       " failed with status 0x%x for scsi%d\n",
2427                       dcmd->cmd_status, instance->host->host_no);
2428                retval = 1; /* Do a scan if we couldn't get affiliation */
2429                goto out;
2430        }
2431
2432        if (!initial) {
2433                thisVf = new_affiliation_111->thisVf;
2434                for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2435                        if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2436                            new_affiliation_111->map[ld].policy[thisVf]) {
2437                                dev_warn(&instance->pdev->dev, "SR-IOV: "
2438                                       "Got new LD/VF affiliation for scsi%d\n",
2439                                       instance->host->host_no);
2440                                memcpy(instance->vf_affiliation_111,
2441                                       new_affiliation_111,
2442                                       sizeof(struct MR_LD_VF_AFFILIATION_111));
2443                                retval = 1;
2444                                goto out;
2445                        }
2446        }
2447out:
2448        if (new_affiliation_111) {
2449                dma_free_coherent(&instance->pdev->dev,
2450                                    sizeof(struct MR_LD_VF_AFFILIATION_111),
2451                                    new_affiliation_111,
2452                                    new_affiliation_111_h);
2453        }
2454
2455        megasas_return_cmd(instance, cmd);
2456
2457        return retval;
2458}
2459
2460static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2461                                            int initial)
2462{
2463        struct megasas_cmd *cmd;
2464        struct megasas_dcmd_frame *dcmd;
2465        struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2466        struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2467        dma_addr_t new_affiliation_h;
2468        int i, j, retval = 0, found = 0, doscan = 0;
2469        u8 thisVf;
2470
2471        cmd = megasas_get_cmd(instance);
2472
2473        if (!cmd) {
2474                dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2475                       "Failed to get cmd for scsi%d\n",
2476                       instance->host->host_no);
2477                return -ENOMEM;
2478        }
2479
2480        dcmd = &cmd->frame->dcmd;
2481
2482        if (!instance->vf_affiliation) {
2483                dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2484                       "affiliation for scsi%d\n", instance->host->host_no);
2485                megasas_return_cmd(instance, cmd);
2486                return -ENOMEM;
2487        }
2488
2489        if (initial)
2490                memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2491                       sizeof(struct MR_LD_VF_AFFILIATION));
2492        else {
2493                new_affiliation =
2494                        dma_alloc_coherent(&instance->pdev->dev,
2495                                           (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2496                                           &new_affiliation_h, GFP_KERNEL);
2497                if (!new_affiliation) {
2498                        dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2499                               "memory for new affiliation for scsi%d\n",
2500                               instance->host->host_no);
2501                        megasas_return_cmd(instance, cmd);
2502                        return -ENOMEM;
2503                }
2504        }
2505
2506        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2507
2508        dcmd->cmd = MFI_CMD_DCMD;
2509        dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2510        dcmd->sge_count = 1;
2511        dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2512        dcmd->timeout = 0;
2513        dcmd->pad_0 = 0;
2514        dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2515                sizeof(struct MR_LD_VF_AFFILIATION));
2516        dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2517
2518        if (initial)
2519                dcmd->sgl.sge32[0].phys_addr =
2520                        cpu_to_le32(instance->vf_affiliation_h);
2521        else
2522                dcmd->sgl.sge32[0].phys_addr =
2523                        cpu_to_le32(new_affiliation_h);
2524
2525        dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2526                sizeof(struct MR_LD_VF_AFFILIATION));
2527
2528        dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2529               "scsi%d\n", instance->host->host_no);
2530
2531
2532        if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2533                dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2534                       " failed with status 0x%x for scsi%d\n",
2535                       dcmd->cmd_status, instance->host->host_no);
2536                retval = 1; /* Do a scan if we couldn't get affiliation */
2537                goto out;
2538        }
2539
2540        if (!initial) {
2541                if (!new_affiliation->ldCount) {
2542                        dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2543                               "affiliation for passive path for scsi%d\n",
2544                               instance->host->host_no);
2545                        retval = 1;
2546                        goto out;
2547                }
2548                newmap = new_affiliation->map;
2549                savedmap = instance->vf_affiliation->map;
2550                thisVf = new_affiliation->thisVf;
2551                for (i = 0 ; i < new_affiliation->ldCount; i++) {
2552                        found = 0;
2553                        for (j = 0; j < instance->vf_affiliation->ldCount;
2554                             j++) {
2555                                if (newmap->ref.targetId ==
2556                                    savedmap->ref.targetId) {
2557                                        found = 1;
2558                                        if (newmap->policy[thisVf] !=
2559                                            savedmap->policy[thisVf]) {
2560                                                doscan = 1;
2561                                                goto out;
2562                                        }
2563                                }
2564                                savedmap = (struct MR_LD_VF_MAP *)
2565                                        ((unsigned char *)savedmap +
2566                                         savedmap->size);
2567                        }
2568                        if (!found && newmap->policy[thisVf] !=
2569                            MR_LD_ACCESS_HIDDEN) {
2570                                doscan = 1;
2571                                goto out;
2572                        }
2573                        newmap = (struct MR_LD_VF_MAP *)
2574                                ((unsigned char *)newmap + newmap->size);
2575                }
2576
2577                newmap = new_affiliation->map;
2578                savedmap = instance->vf_affiliation->map;
2579
2580                for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2581                        found = 0;
2582                        for (j = 0 ; j < new_affiliation->ldCount; j++) {
2583                                if (savedmap->ref.targetId ==
2584                                    newmap->ref.targetId) {
2585                                        found = 1;
2586                                        if (savedmap->policy[thisVf] !=
2587                                            newmap->policy[thisVf]) {
2588                                                doscan = 1;
2589                                                goto out;
2590                                        }
2591                                }
2592                                newmap = (struct MR_LD_VF_MAP *)
2593                                        ((unsigned char *)newmap +
2594                                         newmap->size);
2595                        }
2596                        if (!found && savedmap->policy[thisVf] !=
2597                            MR_LD_ACCESS_HIDDEN) {
2598                                doscan = 1;
2599                                goto out;
2600                        }
2601                        savedmap = (struct MR_LD_VF_MAP *)
2602                                ((unsigned char *)savedmap +
2603                                 savedmap->size);
2604                }
2605        }
2606out:
2607        if (doscan) {
2608                dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2609                       "affiliation for scsi%d\n", instance->host->host_no);
2610                memcpy(instance->vf_affiliation, new_affiliation,
2611                       new_affiliation->size);
2612                retval = 1;
2613        }
2614
2615        if (new_affiliation)
2616                dma_free_coherent(&instance->pdev->dev,
2617                                    (MAX_LOGICAL_DRIVES + 1) *
2618                                    sizeof(struct MR_LD_VF_AFFILIATION),
2619                                    new_affiliation, new_affiliation_h);
2620        megasas_return_cmd(instance, cmd);
2621
2622        return retval;
2623}
2624
2625/* This function will get the current SR-IOV LD/VF affiliation */
2626static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2627        int initial)
2628{
2629        int retval;
2630
2631        if (instance->PlasmaFW111)
2632                retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2633        else
2634                retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2635        return retval;
2636}
2637
2638/* This function will tell FW to start the SR-IOV heartbeat */
2639int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2640                                         int initial)
2641{
2642        struct megasas_cmd *cmd;
2643        struct megasas_dcmd_frame *dcmd;
2644        int retval = 0;
2645
2646        cmd = megasas_get_cmd(instance);
2647
2648        if (!cmd) {
2649                dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2650                       "Failed to get cmd for scsi%d\n",
2651                       instance->host->host_no);
2652                return -ENOMEM;
2653        }
2654
2655        dcmd = &cmd->frame->dcmd;
2656
2657        if (initial) {
2658                instance->hb_host_mem =
2659                        dma_alloc_coherent(&instance->pdev->dev,
2660                                           sizeof(struct MR_CTRL_HB_HOST_MEM),
2661                                           &instance->hb_host_mem_h,
2662                                           GFP_KERNEL);
2663                if (!instance->hb_host_mem) {
2664                        dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2665                               " memory for heartbeat host memory for scsi%d\n",
2666                               instance->host->host_no);
2667                        retval = -ENOMEM;
2668                        goto out;
2669                }
2670        }
2671
2672        memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2673
2674        dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2675        dcmd->cmd = MFI_CMD_DCMD;
2676        dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2677        dcmd->sge_count = 1;
2678        dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2679        dcmd->timeout = 0;
2680        dcmd->pad_0 = 0;
2681        dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2682        dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2683
2684        megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2685                                 sizeof(struct MR_CTRL_HB_HOST_MEM));
2686
2687        dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2688               instance->host->host_no);
2689
2690        if ((instance->adapter_type != MFI_SERIES) &&
2691            !instance->mask_interrupts)
2692                retval = megasas_issue_blocked_cmd(instance, cmd,
2693                        MEGASAS_ROUTINE_WAIT_TIME_VF);
2694        else
2695                retval = megasas_issue_polled(instance, cmd);
2696
2697        if (retval) {
2698                dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2699                        "_MEM_ALLOC DCMD %s for scsi%d\n",
2700                        (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2701                        "timed out" : "failed", instance->host->host_no);
2702                retval = 1;
2703        }
2704
2705out:
2706        megasas_return_cmd(instance, cmd);
2707
2708        return retval;
2709}
2710
2711/* Handler for SR-IOV heartbeat */
2712static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2713{
2714        struct megasas_instance *instance =
2715                from_timer(instance, t, sriov_heartbeat_timer);
2716
2717        if (instance->hb_host_mem->HB.fwCounter !=
2718            instance->hb_host_mem->HB.driverCounter) {
2719                instance->hb_host_mem->HB.driverCounter =
2720                        instance->hb_host_mem->HB.fwCounter;
2721                mod_timer(&instance->sriov_heartbeat_timer,
2722                          jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2723        } else {
2724                dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2725                       "completed for scsi%d\n", instance->host->host_no);
2726                schedule_work(&instance->work_init);
2727        }
2728}
2729
2730/**
2731 * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2732 * @instance:                           Adapter soft state
2733 *
2734 * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2735 * complete all its outstanding commands. Returns error if one or more IOs
2736 * are pending after this time period. It also marks the controller dead.
2737 */
2738static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2739{
2740        int i, sl, outstanding;
2741        u32 reset_index;
2742        u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2743        unsigned long flags;
2744        struct list_head clist_local;
2745        struct megasas_cmd *reset_cmd;
2746        u32 fw_state;
2747
2748        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2749                dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2750                __func__, __LINE__);
2751                return FAILED;
2752        }
2753
2754        if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2755
2756                INIT_LIST_HEAD(&clist_local);
2757                spin_lock_irqsave(&instance->hba_lock, flags);
2758                list_splice_init(&instance->internal_reset_pending_q,
2759                                &clist_local);
2760                spin_unlock_irqrestore(&instance->hba_lock, flags);
2761
2762                dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2763                for (i = 0; i < wait_time; i++) {
2764                        msleep(1000);
2765                        if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2766                                break;
2767                }
2768
2769                if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2770                        dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2771                        atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2772                        return FAILED;
2773                }
2774
2775                reset_index = 0;
2776                while (!list_empty(&clist_local)) {
2777                        reset_cmd = list_entry((&clist_local)->next,
2778                                                struct megasas_cmd, list);
2779                        list_del_init(&reset_cmd->list);
2780                        if (reset_cmd->scmd) {
2781                                reset_cmd->scmd->result = DID_REQUEUE << 16;
2782                                dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2783                                        reset_index, reset_cmd,
2784                                        reset_cmd->scmd->cmnd[0]);
2785
2786                                reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2787                                megasas_return_cmd(instance, reset_cmd);
2788                        } else if (reset_cmd->sync_cmd) {
2789                                dev_notice(&instance->pdev->dev, "%p synch cmds"
2790                                                "reset queue\n",
2791                                                reset_cmd);
2792
2793                                reset_cmd->cmd_status_drv = DCMD_INIT;
2794                                instance->instancet->fire_cmd(instance,
2795                                                reset_cmd->frame_phys_addr,
2796                                                0, instance->reg_set);
2797                        } else {
2798                                dev_notice(&instance->pdev->dev, "%p unexpected"
2799                                        "cmds lst\n",
2800                                        reset_cmd);
2801                        }
2802                        reset_index++;
2803                }
2804
2805                return SUCCESS;
2806        }
2807
2808        for (i = 0; i < resetwaittime; i++) {
2809                outstanding = atomic_read(&instance->fw_outstanding);
2810
2811                if (!outstanding)
2812                        break;
2813
2814                if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2815                        dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2816                               "commands to complete\n",i,outstanding);
2817                        /*
2818                         * Call cmd completion routine. Cmd to be
2819                         * be completed directly without depending on isr.
2820                         */
2821                        megasas_complete_cmd_dpc((unsigned long)instance);
2822                }
2823
2824                msleep(1000);
2825        }
2826
2827        i = 0;
2828        outstanding = atomic_read(&instance->fw_outstanding);
2829        fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2830
2831        if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2832                goto no_outstanding;
2833
2834        if (instance->disableOnlineCtrlReset)
2835                goto kill_hba_and_failed;
2836        do {
2837                if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2838                        dev_info(&instance->pdev->dev,
2839                                "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2840                                __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2841                        if (i == 3)
2842                                goto kill_hba_and_failed;
2843                        megasas_do_ocr(instance);
2844
2845                        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2846                                dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2847                                __func__, __LINE__);
2848                                return FAILED;
2849                        }
2850                        dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2851                                __func__, __LINE__);
2852
2853                        for (sl = 0; sl < 10; sl++)
2854                                msleep(500);
2855
2856                        outstanding = atomic_read(&instance->fw_outstanding);
2857
2858                        fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2859                        if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2860                                goto no_outstanding;
2861                }
2862                i++;
2863        } while (i <= 3);
2864
2865no_outstanding:
2866
2867        dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2868                __func__, __LINE__);
2869        return SUCCESS;
2870
2871kill_hba_and_failed:
2872
2873        /* Reset not supported, kill adapter */
2874        dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2875                " disableOnlineCtrlReset %d fw_outstanding %d \n",
2876                __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2877                atomic_read(&instance->fw_outstanding));
2878        megasas_dump_pending_frames(instance);
2879        megaraid_sas_kill_hba(instance);
2880
2881        return FAILED;
2882}
2883
2884/**
2885 * megasas_generic_reset -      Generic reset routine
2886 * @scmd:                       Mid-layer SCSI command
2887 *
2888 * This routine implements a generic reset handler for device, bus and host
2889 * reset requests. Device, bus and host specific reset handlers can use this
2890 * function after they do their specific tasks.
2891 */
2892static int megasas_generic_reset(struct scsi_cmnd *scmd)
2893{
2894        int ret_val;
2895        struct megasas_instance *instance;
2896
2897        instance = (struct megasas_instance *)scmd->device->host->hostdata;
2898
2899        scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2900                 scmd->cmnd[0], scmd->retries);
2901
2902        if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2903                dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2904                return FAILED;
2905        }
2906
2907        ret_val = megasas_wait_for_outstanding(instance);
2908        if (ret_val == SUCCESS)
2909                dev_notice(&instance->pdev->dev, "reset successful\n");
2910        else
2911                dev_err(&instance->pdev->dev, "failed to do reset\n");
2912
2913        return ret_val;
2914}
2915
2916/**
2917 * megasas_reset_timer - quiesce the adapter if required
2918 * @scmd:               scsi cmnd
2919 *
2920 * Sets the FW busy flag and reduces the host->can_queue if the
2921 * cmd has not been completed within the timeout period.
2922 */
2923static enum
2924blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2925{
2926        struct megasas_instance *instance;
2927        unsigned long flags;
2928
2929        if (time_after(jiffies, scmd->jiffies_at_alloc +
2930                                (scmd_timeout * 2) * HZ)) {
2931                return BLK_EH_DONE;
2932        }
2933
2934        instance = (struct megasas_instance *)scmd->device->host->hostdata;
2935        if (!(instance->flag & MEGASAS_FW_BUSY)) {
2936                /* FW is busy, throttle IO */
2937                spin_lock_irqsave(instance->host->host_lock, flags);
2938
2939                instance->host->can_queue = instance->throttlequeuedepth;
2940                instance->last_time = jiffies;
2941                instance->flag |= MEGASAS_FW_BUSY;
2942
2943                spin_unlock_irqrestore(instance->host->host_lock, flags);
2944        }
2945        return BLK_EH_RESET_TIMER;
2946}
2947
2948/**
2949 * megasas_dump -       This function will print hexdump of provided buffer.
2950 * @buf:                Buffer to be dumped
2951 * @sz:         Size in bytes
2952 * @format:             Different formats of dumping e.g. format=n will
2953 *                      cause only 'n' 32 bit words to be dumped in a single
2954 *                      line.
2955 */
2956inline void
2957megasas_dump(void *buf, int sz, int format)
2958{
2959        int i;
2960        __le32 *buf_loc = (__le32 *)buf;
2961
2962        for (i = 0; i < (sz / sizeof(__le32)); i++) {
2963                if ((i % format) == 0) {
2964                        if (i != 0)
2965                                printk(KERN_CONT "\n");
2966                        printk(KERN_CONT "%08x: ", (i * 4));
2967                }
2968                printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2969        }
2970        printk(KERN_CONT "\n");
2971}
2972
2973/**
2974 * megasas_dump_reg_set -       This function will print hexdump of register set
2975 * @reg_set:    Register set to be dumped
2976 */
2977inline void
2978megasas_dump_reg_set(void __iomem *reg_set)
2979{
2980        unsigned int i, sz = 256;
2981        u32 __iomem *reg = (u32 __iomem *)reg_set;
2982
2983        for (i = 0; i < (sz / sizeof(u32)); i++)
2984                printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2985}
2986
2987/**
2988 * megasas_dump_fusion_io -     This function will print key details
2989 *                              of SCSI IO
2990 * @scmd:                       SCSI command pointer of SCSI IO
2991 */
2992void
2993megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2994{
2995        struct megasas_cmd_fusion *cmd;
2996        union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2997        struct megasas_instance *instance;
2998
2999        cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
3000        instance = (struct megasas_instance *)scmd->device->host->hostdata;
3001
3002        scmd_printk(KERN_INFO, scmd,
3003                    "scmd: (0x%p)  retries: 0x%x  allowed: 0x%x\n",
3004                    scmd, scmd->retries, scmd->allowed);
3005        scsi_print_command(scmd);
3006
3007        if (cmd) {
3008                req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
3009                scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
3010                scmd_printk(KERN_INFO, scmd,
3011                            "RequestFlags:0x%x  MSIxIndex:0x%x  SMID:0x%x  LMID:0x%x  DevHandle:0x%x\n",
3012                            req_desc->SCSIIO.RequestFlags,
3013                            req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
3014                            req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
3015
3016                printk(KERN_INFO "IO request frame:\n");
3017                megasas_dump(cmd->io_request,
3018                             MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
3019                printk(KERN_INFO "Chain frame:\n");
3020                megasas_dump(cmd->sg_frame,
3021                             instance->max_chain_frame_sz, 8);
3022        }
3023
3024}
3025
3026/*
3027 * megasas_dump_sys_regs - This function will dump system registers through
3028 *                          sysfs.
3029 * @reg_set:                Pointer to System register set.
3030 * @buf:                    Buffer to which output is to be written.
3031 * @return:                 Number of bytes written to buffer.
3032 */
3033static inline ssize_t
3034megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
3035{
3036        unsigned int i, sz = 256;
3037        int bytes_wrote = 0;
3038        char *loc = (char *)buf;
3039        u32 __iomem *reg = (u32 __iomem *)reg_set;
3040
3041        for (i = 0; i < sz / sizeof(u32); i++) {
3042                bytes_wrote += scnprintf(loc + bytes_wrote,
3043                                         PAGE_SIZE - bytes_wrote,
3044                                         "%08x: %08x\n", (i * 4),
3045                                         readl(&reg[i]));
3046        }
3047        return bytes_wrote;
3048}
3049
3050/**
3051 * megasas_reset_bus_host -     Bus & host reset handler entry point
3052 * @scmd:                       Mid-layer SCSI command
3053 */
3054static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
3055{
3056        int ret;
3057        struct megasas_instance *instance;
3058
3059        instance = (struct megasas_instance *)scmd->device->host->hostdata;
3060
3061        scmd_printk(KERN_INFO, scmd,
3062                "OCR is requested due to IO timeout!!\n");
3063
3064        scmd_printk(KERN_INFO, scmd,
3065                "SCSI host state: %d  SCSI host busy: %d  FW outstanding: %d\n",
3066                scmd->device->host->shost_state,
3067                scsi_host_busy(scmd->device->host),
3068                atomic_read(&instance->fw_outstanding));
3069        /*
3070         * First wait for all commands to complete
3071         */
3072        if (instance->adapter_type == MFI_SERIES) {
3073                ret = megasas_generic_reset(scmd);
3074        } else {
3075                megasas_dump_fusion_io(scmd);
3076                ret = megasas_reset_fusion(scmd->device->host,
3077                                SCSIIO_TIMEOUT_OCR);
3078        }
3079
3080        return ret;
3081}
3082
3083/**
3084 * megasas_task_abort - Issues task abort request to firmware
3085 *                      (supported only for fusion adapters)
3086 * @scmd:               SCSI command pointer
3087 */
3088static int megasas_task_abort(struct scsi_cmnd *scmd)
3089{
3090        int ret;
3091        struct megasas_instance *instance;
3092
3093        instance = (struct megasas_instance *)scmd->device->host->hostdata;
3094
3095        if (instance->adapter_type != MFI_SERIES)
3096                ret = megasas_task_abort_fusion(scmd);
3097        else {
3098                sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3099                ret = FAILED;
3100        }
3101
3102        return ret;
3103}
3104
3105/**
3106 * megasas_reset_target:  Issues target reset request to firmware
3107 *                        (supported only for fusion adapters)
3108 * @scmd:                 SCSI command pointer
3109 */
3110static int megasas_reset_target(struct scsi_cmnd *scmd)
3111{
3112        int ret;
3113        struct megasas_instance *instance;
3114
3115        instance = (struct megasas_instance *)scmd->device->host->hostdata;
3116
3117        if (instance->adapter_type != MFI_SERIES)
3118                ret = megasas_reset_target_fusion(scmd);
3119        else {
3120                sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3121                ret = FAILED;
3122        }
3123
3124        return ret;
3125}
3126
3127/**
3128 * megasas_bios_param - Returns disk geometry for a disk
3129 * @sdev:               device handle
3130 * @bdev:               block device
3131 * @capacity:           drive capacity
3132 * @geom:               geometry parameters
3133 */
3134static int
3135megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
3136                 sector_t capacity, int geom[])
3137{
3138        int heads;
3139        int sectors;
3140        sector_t cylinders;
3141        unsigned long tmp;
3142
3143        /* Default heads (64) & sectors (32) */
3144        heads = 64;
3145        sectors = 32;
3146
3147        tmp = heads * sectors;
3148        cylinders = capacity;
3149
3150        sector_div(cylinders, tmp);
3151
3152        /*
3153         * Handle extended translation size for logical drives > 1Gb
3154         */
3155
3156        if (capacity >= 0x200000) {
3157                heads = 255;
3158                sectors = 63;
3159                tmp = heads*sectors;
3160                cylinders = capacity;
3161                sector_div(cylinders, tmp);
3162        }
3163
3164        geom[0] = heads;
3165        geom[1] = sectors;
3166        geom[2] = cylinders;
3167
3168        return 0;
3169}
3170
3171static int megasas_map_queues(struct Scsi_Host *shost)
3172{
3173        struct megasas_instance *instance;
3174        int qoff = 0, offset;
3175        struct blk_mq_queue_map *map;
3176
3177        instance = (struct megasas_instance *)shost->hostdata;
3178
3179        if (shost->nr_hw_queues == 1)
3180                return 0;
3181
3182        offset = instance->low_latency_index_start;
3183
3184        /* Setup Default hctx */
3185        map = &shost->tag_set.map[HCTX_TYPE_DEFAULT];
3186        map->nr_queues = instance->msix_vectors - offset;
3187        map->queue_offset = 0;
3188        blk_mq_pci_map_queues(map, instance->pdev, offset);
3189        qoff += map->nr_queues;
3190        offset += map->nr_queues;
3191
3192        /* Setup Poll hctx */
3193        map = &shost->tag_set.map[HCTX_TYPE_POLL];
3194        map->nr_queues = instance->iopoll_q_count;
3195        if (map->nr_queues) {
3196                /*
3197                 * The poll queue(s) doesn't have an IRQ (and hence IRQ
3198                 * affinity), so use the regular blk-mq cpu mapping
3199                 */
3200                map->queue_offset = qoff;
3201                blk_mq_map_queues(map);
3202        }
3203
3204        return 0;
3205}
3206
3207static void megasas_aen_polling(struct work_struct *work);
3208
3209/**
3210 * megasas_service_aen -        Processes an event notification
3211 * @instance:                   Adapter soft state
3212 * @cmd:                        AEN command completed by the ISR
3213 *
3214 * For AEN, driver sends a command down to FW that is held by the FW till an
3215 * event occurs. When an event of interest occurs, FW completes the command
3216 * that it was previously holding.
3217 *
3218 * This routines sends SIGIO signal to processes that have registered with the
3219 * driver for AEN.
3220 */
3221static void
3222megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
3223{
3224        unsigned long flags;
3225
3226        /*
3227         * Don't signal app if it is just an aborted previously registered aen
3228         */
3229        if ((!cmd->abort_aen) && (instance->unload == 0)) {
3230                spin_lock_irqsave(&poll_aen_lock, flags);
3231                megasas_poll_wait_aen = 1;
3232                spin_unlock_irqrestore(&poll_aen_lock, flags);
3233                wake_up(&megasas_poll_wait);
3234                kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
3235        }
3236        else
3237                cmd->abort_aen = 0;
3238
3239        instance->aen_cmd = NULL;
3240
3241        megasas_return_cmd(instance, cmd);
3242
3243        if ((instance->unload == 0) &&
3244                ((instance->issuepend_done == 1))) {
3245                struct megasas_aen_event *ev;
3246
3247                ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
3248                if (!ev) {
3249                        dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
3250                } else {
3251                        ev->instance = instance;
3252                        instance->ev = ev;
3253                        INIT_DELAYED_WORK(&ev->hotplug_work,
3254                                          megasas_aen_polling);
3255                        schedule_delayed_work(&ev->hotplug_work, 0);
3256                }
3257        }
3258}
3259
3260static ssize_t
3261fw_crash_buffer_store(struct device *cdev,
3262        struct device_attribute *attr, const char *buf, size_t count)
3263{
3264        struct Scsi_Host *shost = class_to_shost(cdev);
3265        struct megasas_instance *instance =
3266                (struct megasas_instance *) shost->hostdata;
3267        int val = 0;
3268        unsigned long flags;
3269
3270        if (kstrtoint(buf, 0, &val) != 0)
3271                return -EINVAL;
3272
3273        spin_lock_irqsave(&instance->crashdump_lock, flags);
3274        instance->fw_crash_buffer_offset = val;
3275        spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3276        return strlen(buf);
3277}
3278
3279static ssize_t
3280fw_crash_buffer_show(struct device *cdev,
3281        struct device_attribute *attr, char *buf)
3282{
3283        struct Scsi_Host *shost = class_to_shost(cdev);
3284        struct megasas_instance *instance =
3285                (struct megasas_instance *) shost->hostdata;
3286        u32 size;
3287        unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
3288        unsigned long chunk_left_bytes;
3289        unsigned long src_addr;
3290        unsigned long flags;
3291        u32 buff_offset;
3292
3293        spin_lock_irqsave(&instance->crashdump_lock, flags);
3294        buff_offset = instance->fw_crash_buffer_offset;
3295        if (!instance->crash_dump_buf &&
3296                !((instance->fw_crash_state == AVAILABLE) ||
3297                (instance->fw_crash_state == COPYING))) {
3298                dev_err(&instance->pdev->dev,
3299                        "Firmware crash dump is not available\n");
3300                spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3301                return -EINVAL;
3302        }
3303
3304        if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
3305                dev_err(&instance->pdev->dev,
3306                        "Firmware crash dump offset is out of range\n");
3307                spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3308                return 0;
3309        }
3310
3311        size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
3312        chunk_left_bytes = dmachunk - (buff_offset % dmachunk);
3313        size = (size > chunk_left_bytes) ? chunk_left_bytes : size;
3314        size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
3315
3316        src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
3317                (buff_offset % dmachunk);
3318        memcpy(buf, (void *)src_addr, size);
3319        spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3320
3321        return size;
3322}
3323
3324static ssize_t
3325fw_crash_buffer_size_show(struct device *cdev,
3326        struct device_attribute *attr, char *buf)
3327{
3328        struct Scsi_Host *shost = class_to_shost(cdev);
3329        struct megasas_instance *instance =
3330                (struct megasas_instance *) shost->hostdata;
3331
3332        return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
3333                ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
3334}
3335
3336static ssize_t
3337fw_crash_state_store(struct device *cdev,
3338        struct device_attribute *attr, const char *buf, size_t count)
3339{
3340        struct Scsi_Host *shost = class_to_shost(cdev);
3341        struct megasas_instance *instance =
3342                (struct megasas_instance *) shost->hostdata;
3343        int val = 0;
3344        unsigned long flags;
3345
3346        if (kstrtoint(buf, 0, &val) != 0)
3347                return -EINVAL;
3348
3349        if ((val <= AVAILABLE || val > COPY_ERROR)) {
3350                dev_err(&instance->pdev->dev, "application updates invalid "
3351                        "firmware crash state\n");
3352                return -EINVAL;
3353        }
3354
3355        instance->fw_crash_state = val;
3356
3357        if ((val == COPIED) || (val == COPY_ERROR)) {
3358                spin_lock_irqsave(&instance->crashdump_lock, flags);
3359                megasas_free_host_crash_buffer(instance);
3360                spin_unlock_irqrestore(&instance->crashdump_lock, flags);
3361                if (val == COPY_ERROR)
3362                        dev_info(&instance->pdev->dev, "application failed to "
3363                                "copy Firmware crash dump\n");
3364                else
3365                        dev_info(&instance->pdev->dev, "Firmware crash dump "
3366                                "copied successfully\n");
3367        }
3368        return strlen(buf);
3369}
3370
3371static ssize_t
3372fw_crash_state_show(struct device *cdev,
3373        struct device_attribute *attr, char *buf)
3374{
3375        struct Scsi_Host *shost = class_to_shost(cdev);
3376        struct megasas_instance *instance =
3377                (struct megasas_instance *) shost->hostdata;
3378
3379        return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
3380}
3381
3382static ssize_t
3383page_size_show(struct device *cdev,
3384        struct device_attribute *attr, char *buf)
3385{
3386        return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
3387}
3388
3389static ssize_t
3390ldio_outstanding_show(struct device *cdev, struct device_attribute *attr,
3391        char *buf)
3392{
3393        struct Scsi_Host *shost = class_to_shost(cdev);
3394        struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3395
3396        return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->ldio_outstanding));
3397}
3398
3399static ssize_t
3400fw_cmds_outstanding_show(struct device *cdev,
3401                                 struct device_attribute *attr, char *buf)
3402{
3403        struct Scsi_Host *shost = class_to_shost(cdev);
3404        struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3405
3406        return snprintf(buf, PAGE_SIZE, "%d\n", atomic_read(&instance->fw_outstanding));
3407}
3408
3409static ssize_t
3410enable_sdev_max_qd_show(struct device *cdev,
3411        struct device_attribute *attr, char *buf)
3412{
3413        struct Scsi_Host *shost = class_to_shost(cdev);
3414        struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3415
3416        return snprintf(buf, PAGE_SIZE, "%d\n", instance->enable_sdev_max_qd);
3417}
3418
3419static ssize_t
3420enable_sdev_max_qd_store(struct device *cdev,
3421        struct device_attribute *attr, const char *buf, size_t count)
3422{
3423        struct Scsi_Host *shost = class_to_shost(cdev);
3424        struct megasas_instance *instance = (struct megasas_instance *)shost->hostdata;
3425        u32 val = 0;
3426        bool is_target_prop;
3427        int ret_target_prop = DCMD_FAILED;
3428        struct scsi_device *sdev;
3429
3430        if (kstrtou32(buf, 0, &val) != 0) {
3431                pr_err("megasas: could not set enable_sdev_max_qd\n");
3432                return -EINVAL;
3433        }
3434
3435        mutex_lock(&instance->reset_mutex);
3436        if (val)
3437                instance->enable_sdev_max_qd = true;
3438        else
3439                instance->enable_sdev_max_qd = false;
3440
3441        shost_for_each_device(sdev, shost) {
3442                ret_target_prop = megasas_get_target_prop(instance, sdev);
3443                is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
3444                megasas_set_fw_assisted_qd(sdev, is_target_prop);
3445        }
3446        mutex_unlock(&instance->reset_mutex);
3447
3448        return strlen(buf);
3449}
3450
3451static ssize_t
3452dump_system_regs_show(struct device *cdev,
3453                               struct device_attribute *attr, char *buf)
3454{
3455        struct Scsi_Host *shost = class_to_shost(cdev);
3456        struct megasas_instance *instance =
3457                        (struct megasas_instance *)shost->hostdata;
3458
3459        return megasas_dump_sys_regs(instance->reg_set, buf);
3460}
3461
3462static ssize_t
3463raid_map_id_show(struct device *cdev, struct device_attribute *attr,
3464                          char *buf)
3465{
3466        struct Scsi_Host *shost = class_to_shost(cdev);
3467        struct megasas_instance *instance =
3468                        (struct megasas_instance *)shost->hostdata;
3469
3470        return snprintf(buf, PAGE_SIZE, "%ld\n",
3471                        (unsigned long)instance->map_id);
3472}
3473
3474static DEVICE_ATTR_RW(fw_crash_buffer);
3475static DEVICE_ATTR_RO(fw_crash_buffer_size);
3476static DEVICE_ATTR_RW(fw_crash_state);
3477static DEVICE_ATTR_RO(page_size);
3478static DEVICE_ATTR_RO(ldio_outstanding);
3479static DEVICE_ATTR_RO(fw_cmds_outstanding);
3480static DEVICE_ATTR_RW(enable_sdev_max_qd);
3481static DEVICE_ATTR_RO(dump_system_regs);
3482static DEVICE_ATTR_RO(raid_map_id);
3483
3484static struct device_attribute *megaraid_host_attrs[] = {
3485        &dev_attr_fw_crash_buffer_size,
3486        &dev_attr_fw_crash_buffer,
3487        &dev_attr_fw_crash_state,
3488        &dev_attr_page_size,
3489        &dev_attr_ldio_outstanding,
3490        &dev_attr_fw_cmds_outstanding,
3491        &dev_attr_enable_sdev_max_qd,
3492        &dev_attr_dump_system_regs,
3493        &dev_attr_raid_map_id,
3494        NULL,
3495};
3496
3497/*
3498 * Scsi host template for megaraid_sas driver
3499 */
3500static struct scsi_host_template megasas_template = {
3501
3502        .module = THIS_MODULE,
3503        .name = "Avago SAS based MegaRAID driver",
3504        .proc_name = "megaraid_sas",
3505        .slave_configure = megasas_slave_configure,
3506        .slave_alloc = megasas_slave_alloc,
3507        .slave_destroy = megasas_slave_destroy,
3508        .queuecommand = megasas_queue_command,
3509        .eh_target_reset_handler = megasas_reset_target,
3510        .eh_abort_handler = megasas_task_abort,
3511        .eh_host_reset_handler = megasas_reset_bus_host,
3512        .eh_timed_out = megasas_reset_timer,
3513        .shost_attrs = megaraid_host_attrs,
3514        .bios_param = megasas_bios_param,
3515        .map_queues = megasas_map_queues,
3516        .mq_poll = megasas_blk_mq_poll,
3517        .change_queue_depth = scsi_change_queue_depth,
3518        .max_segment_size = 0xffffffff,
3519};
3520
3521/**
3522 * megasas_complete_int_cmd -   Completes an internal command
3523 * @instance:                   Adapter soft state
3524 * @cmd:                        Command to be completed
3525 *
3526 * The megasas_issue_blocked_cmd() function waits for a command to complete
3527 * after it issues a command. This function wakes up that waiting routine by
3528 * calling wake_up() on the wait queue.
3529 */
3530static void
3531megasas_complete_int_cmd(struct megasas_instance *instance,
3532                         struct megasas_cmd *cmd)
3533{
3534        if (cmd->cmd_status_drv == DCMD_INIT)
3535                cmd->cmd_status_drv =
3536                (cmd->frame->io.cmd_status == MFI_STAT_OK) ?
3537                DCMD_SUCCESS : DCMD_FAILED;
3538
3539        wake_up(&instance->int_cmd_wait_q);
3540}
3541
3542/**
3543 * megasas_complete_abort -     Completes aborting a command
3544 * @instance:                   Adapter soft state
3545 * @cmd:                        Cmd that was issued to abort another cmd
3546 *
3547 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
3548 * after it issues an abort on a previously issued command. This function
3549 * wakes up all functions waiting on the same wait queue.
3550 */
3551static void
3552megasas_complete_abort(struct megasas_instance *instance,
3553                       struct megasas_cmd *cmd)
3554{
3555        if (cmd->sync_cmd) {
3556                cmd->sync_cmd = 0;
3557                cmd->cmd_status_drv = DCMD_SUCCESS;
3558                wake_up(&instance->abort_cmd_wait_q);
3559        }
3560}
3561
3562static void
3563megasas_set_ld_removed_by_fw(struct megasas_instance *instance)
3564{
3565        uint i;
3566
3567        for (i = 0; (i < MEGASAS_MAX_LD_IDS); i++) {
3568                if (instance->ld_ids_prev[i] != 0xff &&
3569                    instance->ld_ids_from_raidmap[i] == 0xff) {
3570                        if (megasas_dbg_lvl & LD_PD_DEBUG)
3571                                dev_info(&instance->pdev->dev,
3572                                         "LD target ID %d removed from RAID map\n", i);
3573                        instance->ld_tgtid_status[i] = LD_TARGET_ID_DELETED;
3574                }
3575        }
3576}
3577
3578/**
3579 * megasas_complete_cmd -       Completes a command
3580 * @instance:                   Adapter soft state
3581 * @cmd:                        Command to be completed
3582 * @alt_status:                 If non-zero, use this value as status to
3583 *                              SCSI mid-layer instead of the value returned
3584 *                              by the FW. This should be used if caller wants
3585 *                              an alternate status (as in the case of aborted
3586 *                              commands)
3587 */
3588void
3589megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
3590                     u8 alt_status)
3591{
3592        int exception = 0;
3593        struct megasas_header *hdr = &cmd->frame->hdr;
3594        unsigned long flags;
3595        struct fusion_context *fusion = instance->ctrl_context;
3596        u32 opcode, status;
3597
3598        /* flag for the retry reset */
3599        cmd->retry_for_fw_reset = 0;
3600
3601        if (cmd->scmd)
3602                cmd->scmd->SCp.ptr = NULL;
3603
3604        switch (hdr->cmd) {
3605        case MFI_CMD_INVALID:
3606                /* Some older 1068 controller FW may keep a pended
3607                   MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
3608                   when booting the kdump kernel.  Ignore this command to
3609                   prevent a kernel panic on shutdown of the kdump kernel. */
3610                dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
3611                       "completed\n");
3612                dev_warn(&instance->pdev->dev, "If you have a controller "
3613                       "other than PERC5, please upgrade your firmware\n");
3614                break;
3615        case MFI_CMD_PD_SCSI_IO:
3616        case MFI_CMD_LD_SCSI_IO:
3617
3618                /*
3619                 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
3620                 * issued either through an IO path or an IOCTL path. If it
3621                 * was via IOCTL, we will send it to internal completion.
3622                 */
3623                if (cmd->sync_cmd) {
3624                        cmd->sync_cmd = 0;
3625                        megasas_complete_int_cmd(instance, cmd);
3626                        break;
3627                }
3628                fallthrough;
3629
3630        case MFI_CMD_LD_READ:
3631        case MFI_CMD_LD_WRITE:
3632
3633                if (alt_status) {
3634                        cmd->scmd->result = alt_status << 16;
3635                        exception = 1;
3636                }
3637
3638                if (exception) {
3639
3640                        atomic_dec(&instance->fw_outstanding);
3641
3642                        scsi_dma_unmap(cmd->scmd);
3643                        cmd->scmd->scsi_done(cmd->scmd);
3644                        megasas_return_cmd(instance, cmd);
3645
3646                        break;
3647                }
3648
3649                switch (hdr->cmd_status) {
3650
3651                case MFI_STAT_OK:
3652                        cmd->scmd->result = DID_OK << 16;
3653                        break;
3654
3655                case MFI_STAT_SCSI_IO_FAILED:
3656                case MFI_STAT_LD_INIT_IN_PROGRESS:
3657                        cmd->scmd->result =
3658                            (DID_ERROR << 16) | hdr->scsi_status;
3659                        break;
3660
3661                case MFI_STAT_SCSI_DONE_WITH_ERROR:
3662
3663                        cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3664
3665                        if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3666                                memset(cmd->scmd->sense_buffer, 0,
3667                                       SCSI_SENSE_BUFFERSIZE);
3668                                memcpy(cmd->scmd->sense_buffer, cmd->sense,
3669                                       hdr->sense_len);
3670                        }
3671
3672                        break;
3673
3674                case MFI_STAT_LD_OFFLINE:
3675                case MFI_STAT_DEVICE_NOT_FOUND:
3676                        cmd->scmd->result = DID_BAD_TARGET << 16;
3677                        break;
3678
3679                default:
3680                        dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3681                               hdr->cmd_status);
3682                        cmd->scmd->result = DID_ERROR << 16;
3683                        break;
3684                }
3685
3686                atomic_dec(&instance->fw_outstanding);
3687
3688                scsi_dma_unmap(cmd->scmd);
3689                cmd->scmd->scsi_done(cmd->scmd);
3690                megasas_return_cmd(instance, cmd);
3691
3692                break;
3693
3694        case MFI_CMD_SMP:
3695        case MFI_CMD_STP:
3696        case MFI_CMD_NVME:
3697        case MFI_CMD_TOOLBOX:
3698                megasas_complete_int_cmd(instance, cmd);
3699                break;
3700
3701        case MFI_CMD_DCMD:
3702                opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3703                /* Check for LD map update */
3704                if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3705                        && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3706                        fusion->fast_path_io = 0;
3707                        spin_lock_irqsave(instance->host->host_lock, flags);
3708                        status = cmd->frame->hdr.cmd_status;
3709                        instance->map_update_cmd = NULL;
3710                        if (status != MFI_STAT_OK) {
3711                                if (status != MFI_STAT_NOT_FOUND)
3712                                        dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3713                                               cmd->frame->hdr.cmd_status);
3714                                else {
3715                                        megasas_return_cmd(instance, cmd);
3716                                        spin_unlock_irqrestore(
3717                                                instance->host->host_lock,
3718                                                flags);
3719                                        break;
3720                                }
3721                        }
3722
3723                        megasas_return_cmd(instance, cmd);
3724
3725                        /*
3726                         * Set fast path IO to ZERO.
3727                         * Validate Map will set proper value.
3728                         * Meanwhile all IOs will go as LD IO.
3729                         */
3730                        if (status == MFI_STAT_OK &&
3731                            (MR_ValidateMapInfo(instance, (instance->map_id + 1)))) {
3732                                instance->map_id++;
3733                                fusion->fast_path_io = 1;
3734                        } else {
3735                                fusion->fast_path_io = 0;
3736                        }
3737
3738                        if (instance->adapter_type >= INVADER_SERIES)
3739                                megasas_set_ld_removed_by_fw(instance);
3740
3741                        megasas_sync_map_info(instance);
3742                        spin_unlock_irqrestore(instance->host->host_lock,
3743                                               flags);
3744
3745                        break;
3746                }
3747                if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3748                    opcode == MR_DCMD_CTRL_EVENT_GET) {
3749                        spin_lock_irqsave(&poll_aen_lock, flags);
3750                        megasas_poll_wait_aen = 0;
3751                        spin_unlock_irqrestore(&poll_aen_lock, flags);
3752                }
3753
3754                /* FW has an updated PD sequence */
3755                if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3756                        (cmd->frame->dcmd.mbox.b[0] == 1)) {
3757
3758                        spin_lock_irqsave(instance->host->host_lock, flags);
3759                        status = cmd->frame->hdr.cmd_status;
3760                        instance->jbod_seq_cmd = NULL;
3761                        megasas_return_cmd(instance, cmd);
3762
3763                        if (status == MFI_STAT_OK) {
3764                                instance->pd_seq_map_id++;
3765                                /* Re-register a pd sync seq num cmd */
3766                                if (megasas_sync_pd_seq_num(instance, true))
3767                                        instance->use_seqnum_jbod_fp = false;
3768                        } else
3769                                instance->use_seqnum_jbod_fp = false;
3770
3771                        spin_unlock_irqrestore(instance->host->host_lock, flags);
3772                        break;
3773                }
3774
3775                /*
3776                 * See if got an event notification
3777                 */
3778                if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3779                        megasas_service_aen(instance, cmd);
3780                else
3781                        megasas_complete_int_cmd(instance, cmd);
3782
3783                break;
3784
3785        case MFI_CMD_ABORT:
3786                /*
3787                 * Cmd issued to abort another cmd returned
3788                 */
3789                megasas_complete_abort(instance, cmd);
3790                break;
3791
3792        default:
3793                dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3794                       hdr->cmd);
3795                megasas_complete_int_cmd(instance, cmd);
3796                break;
3797        }
3798}
3799
3800/**
3801 * megasas_issue_pending_cmds_again -   issue all pending cmds
3802 *                                      in FW again because of the fw reset
3803 * @instance:                           Adapter soft state
3804 */
3805static inline void
3806megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3807{
3808        struct megasas_cmd *cmd;
3809        struct list_head clist_local;
3810        union megasas_evt_class_locale class_locale;
3811        unsigned long flags;
3812        u32 seq_num;
3813
3814        INIT_LIST_HEAD(&clist_local);
3815        spin_lock_irqsave(&instance->hba_lock, flags);
3816        list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3817        spin_unlock_irqrestore(&instance->hba_lock, flags);
3818
3819        while (!list_empty(&clist_local)) {
3820                cmd = list_entry((&clist_local)->next,
3821                                        struct megasas_cmd, list);
3822                list_del_init(&cmd->list);
3823
3824                if (cmd->sync_cmd || cmd->scmd) {
3825                        dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3826                                "detected to be pending while HBA reset\n",
3827                                        cmd, cmd->scmd, cmd->sync_cmd);
3828
3829                        cmd->retry_for_fw_reset++;
3830
3831                        if (cmd->retry_for_fw_reset == 3) {
3832                                dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3833                                        "was tried multiple times during reset."
3834                                        "Shutting down the HBA\n",
3835                                        cmd, cmd->scmd, cmd->sync_cmd);
3836                                instance->instancet->disable_intr(instance);
3837                                atomic_set(&instance->fw_reset_no_pci_access, 1);
3838                                megaraid_sas_kill_hba(instance);
3839                                return;
3840                        }
3841                }
3842
3843                if (cmd->sync_cmd == 1) {
3844                        if (cmd->scmd) {
3845                                dev_notice(&instance->pdev->dev, "unexpected"
3846                                        "cmd attached to internal command!\n");
3847                        }
3848                        dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3849                                                "on the internal reset queue,"
3850                                                "issue it again.\n", cmd);
3851                        cmd->cmd_status_drv = DCMD_INIT;
3852                        instance->instancet->fire_cmd(instance,
3853                                                        cmd->frame_phys_addr,
3854                                                        0, instance->reg_set);
3855                } else if (cmd->scmd) {
3856                        dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3857                        "detected on the internal queue, issue again.\n",
3858                        cmd, cmd->scmd->cmnd[0]);
3859
3860                        atomic_inc(&instance->fw_outstanding);
3861                        instance->instancet->fire_cmd(instance,
3862                                        cmd->frame_phys_addr,
3863                                        cmd->frame_count-1, instance->reg_set);
3864                } else {
3865                        dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3866                                "internal reset defer list while re-issue!!\n",
3867                                cmd);
3868                }
3869        }
3870
3871        if (instance->aen_cmd) {
3872                dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3873                megasas_return_cmd(instance, instance->aen_cmd);
3874
3875                instance->aen_cmd = NULL;
3876        }
3877
3878        /*
3879         * Initiate AEN (Asynchronous Event Notification)
3880         */
3881        seq_num = instance->last_seq_num;
3882        class_locale.members.reserved = 0;
3883        class_locale.members.locale = MR_EVT_LOCALE_ALL;
3884        class_locale.members.class = MR_EVT_CLASS_DEBUG;
3885
3886        megasas_register_aen(instance, seq_num, class_locale.word);
3887}
3888
3889/*
3890 * Move the internal reset pending commands to a deferred queue.
3891 *
3892 * We move the commands pending at internal reset time to a
3893 * pending queue. This queue would be flushed after successful
3894 * completion of the internal reset sequence. if the internal reset
3895 * did not complete in time, the kernel reset handler would flush
3896 * these commands.
3897 */
3898static void
3899megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3900{
3901        struct megasas_cmd *cmd;
3902        int i;
3903        u16 max_cmd = instance->max_fw_cmds;
3904        u32 defer_index;
3905        unsigned long flags;
3906
3907        defer_index = 0;
3908        spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3909        for (i = 0; i < max_cmd; i++) {
3910                cmd = instance->cmd_list[i];
3911                if (cmd->sync_cmd == 1 || cmd->scmd) {
3912                        dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3913                                        "on the defer queue as internal\n",
3914                                defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3915
3916                        if (!list_empty(&cmd->list)) {
3917                                dev_notice(&instance->pdev->dev, "ERROR while"
3918                                        " moving this cmd:%p, %d %p, it was"
3919                                        "discovered on some list?\n",
3920                                        cmd, cmd->sync_cmd, cmd->scmd);
3921
3922                                list_del_init(&cmd->list);
3923                        }
3924                        defer_index++;
3925                        list_add_tail(&cmd->list,
3926                                &instance->internal_reset_pending_q);
3927                }
3928        }
3929        spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3930}
3931
3932
3933static void
3934process_fw_state_change_wq(struct work_struct *work)
3935{
3936        struct megasas_instance *instance =
3937                container_of(work, struct megasas_instance, work_init);
3938        u32 wait;
3939        unsigned long flags;
3940
3941    if (atomic_read(&instance->adprecovery) != MEGASAS_ADPRESET_SM_INFAULT) {
3942                dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3943                                atomic_read(&instance->adprecovery));
3944                return ;
3945        }
3946
3947        if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
3948                dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3949                                        "state, restarting it...\n");
3950
3951                instance->instancet->disable_intr(instance);
3952                atomic_set(&instance->fw_outstanding, 0);
3953
3954                atomic_set(&instance->fw_reset_no_pci_access, 1);
3955                instance->instancet->adp_reset(instance, instance->reg_set);
3956                atomic_set(&instance->fw_reset_no_pci_access, 0);
3957
3958                dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3959                                        "initiating next stage...\n");
3960
3961                dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3962                                        "state 2 starting...\n");
3963
3964                /* waiting for about 20 second before start the second init */
3965                for (wait = 0; wait < 30; wait++) {
3966                        msleep(1000);
3967                }
3968
3969                if (megasas_transition_to_ready(instance, 1)) {
3970                        dev_notice(&instance->pdev->dev, "adapter not ready\n");
3971
3972                        atomic_set(&instance->fw_reset_no_pci_access, 1);
3973                        megaraid_sas_kill_hba(instance);
3974                        return ;
3975                }
3976
3977                if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3978                        (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3979                        (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3980                        ) {
3981                        *instance->consumer = *instance->producer;
3982                } else {
3983                        *instance->consumer = 0;
3984                        *instance->producer = 0;
3985                }
3986
3987                megasas_issue_init_mfi(instance);
3988
3989                spin_lock_irqsave(&instance->hba_lock, flags);
3990                atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
3991                spin_unlock_irqrestore(&instance->hba_lock, flags);
3992                instance->instancet->enable_intr(instance);
3993
3994                megasas_issue_pending_cmds_again(instance);
3995                instance->issuepend_done = 1;
3996        }
3997}
3998
3999/**
4000 * megasas_deplete_reply_queue -        Processes all completed commands
4001 * @instance:                           Adapter soft state
4002 * @alt_status:                         Alternate status to be returned to
4003 *                                      SCSI mid-layer instead of the status
4004 *                                      returned by the FW
4005 * Note: this must be called with hba lock held
4006 */
4007static int
4008megasas_deplete_reply_queue(struct megasas_instance *instance,
4009                                        u8 alt_status)
4010{
4011        u32 mfiStatus;
4012        u32 fw_state;
4013
4014        if ((mfiStatus = instance->instancet->check_reset(instance,
4015                                        instance->reg_set)) == 1) {
4016                return IRQ_HANDLED;