Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/C/Linux/drivers/scsi/mpt3sas/   (Open Source Betriebssystem Version 6.17.9©)  Datei vom 24.10.2025 mit Größe 254 kB image not shown  

Quelle  mpt3sas_base.c   Sprache: C

 
/*
 * This is the Fusion MPT base driver providing common API layer interface
 * for access to MPT (Message Passing Technology) firmware.
 *
 * This code is based on drivers/scsi/mpt3sas/mpt3sas_base.c
 * Copyright (C) 2012-2014  LSI Corporation
 * Copyright (C) 2013-2014 Avago Technologies
 *  (mailto: MPT-FusionLinux.pdl@avagotech.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * NO WARRANTY
 * THE PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT
 * LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is
 * solely responsible for determining the appropriateness of using and
 * distributing the Program and assumes all risks associated with its
 * exercise of rights under this Agreement, including but not limited to
 * the risks and costs of program errors, damage to or loss of data,
 * programs or equipment, and unavailability or interruption of operations.

 * DISCLAIMER OF LIABILITY
 * NEITHER RECIPIENT NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 * USE OR DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED
 * HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */


#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/kdev_t.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/dma-mapping.h>
#include <linux/io.h>
#include <linux/time.h>
#include <linux/ktime.h>
#include <linux/kthread.h>
#include <asm/page.h>        /* To get host page size per arch */


#include "mpt3sas_base.h"

static MPT_CALLBACK mpt_callbacks[MPT_MAX_CALLBACKS];


#define FAULT_POLLING_INTERVAL 1000 /* in milliseconds */

 /* maximum controller queue depth */
#define MAX_HBA_QUEUE_DEPTH 30000
#define MAX_CHAIN_DEPTH  100000
static int max_queue_depth = -1;
module_param(max_queue_depth, int, 0444);
MODULE_PARM_DESC(max_queue_depth, " max controller queue depth ");

static int max_sgl_entries = -1;
module_param(max_sgl_entries, int, 0444);
MODULE_PARM_DESC(max_sgl_entries, " max sg entries ");

static int msix_disable = -1;
module_param(msix_disable, int, 0444);
MODULE_PARM_DESC(msix_disable, " disable msix routed interrupts (default=0)");

static int smp_affinity_enable = 1;
module_param(smp_affinity_enable, int, 0444);
MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");

static int max_msix_vectors = -1;
module_param(max_msix_vectors, int, 0444);
MODULE_PARM_DESC(max_msix_vectors,
 " max msix vectors");

static int irqpoll_weight = -1;
module_param(irqpoll_weight, int, 0444);
MODULE_PARM_DESC(irqpoll_weight,
 "irq poll weight (default= one fourth of HBA queue depth)");

static int mpt3sas_fwfault_debug;
MODULE_PARM_DESC(mpt3sas_fwfault_debug,
 " enable detection of firmware fault and halt firmware - (default=0)");

static int perf_mode = -1;
module_param(perf_mode, int, 0444);
MODULE_PARM_DESC(perf_mode,
 "Performance mode (only for Aero/Sea Generation), options:\n\t\t"
 "0 - balanced: high iops mode is enabled &\n\t\t"
 "interrupt coalescing is enabled only on high iops queues,\n\t\t"
 "1 - iops: high iops mode is disabled &\n\t\t"
 "interrupt coalescing is enabled on all queues,\n\t\t"
 "2 - latency: high iops mode is disabled &\n\t\t"
 "interrupt coalescing is enabled on all queues with timeout value 0xA,\n"
 "\t\tdefault - default perf_mode is 'balanced'"
 );

static int poll_queues;
module_param(poll_queues, int, 0444);
MODULE_PARM_DESC(poll_queues, "Number of queues to be use for io_uring poll mode.\n\t\t"
 "This parameter is effective only if host_tagset_enable=1. &\n\t\t"
 "when poll_queues are enabled then &\n\t\t"
 "perf_mode is set to latency mode. &\n\t\t"
 );

enum mpt3sas_perf_mode {
 MPT_PERF_MODE_DEFAULT = -1,
 MPT_PERF_MODE_BALANCED = 0,
 MPT_PERF_MODE_IOPS = 1,
 MPT_PERF_MODE_LATENCY = 2,
};

static int
_base_wait_on_iocstate(struct MPT3SAS_ADAPTER *ioc,
  u32 ioc_state, int timeout);
static int
_base_get_ioc_facts(struct MPT3SAS_ADAPTER *ioc);
static void
_base_clear_outstanding_commands(struct MPT3SAS_ADAPTER *ioc);

static u32
_base_readl_ext_retry(const void __iomem *addr);

/**
 * mpt3sas_base_check_cmd_timeout - Function
 * to check timeout and command termination due
 * to Host reset.
 *
 * @ioc: per adapter object.
 * @status: Status of issued command.
 * @mpi_request:mf request pointer.
 * @sz: size of buffer.
 *
 * Return: 1/0 Reset to be done or Not
 */

u8
mpt3sas_base_check_cmd_timeout(struct MPT3SAS_ADAPTER *ioc,
  u8 status, void *mpi_request, int sz)
{
 u8 issue_reset = 0;

 if (!(status & MPT3_CMD_RESET))
  issue_reset = 1;

 ioc_err(ioc, "Command %s\n",
  issue_reset == 0 ? "terminated due to Host Reset" : "Timeout");
 _debug_dump_mf(mpi_request, sz);

 return issue_reset;
}

/**
 * _scsih_set_fwfault_debug - global setting of ioc->fwfault_debug.
 * @val: ?
 * @kp: ?
 *
 * Return: ?
 */

static int
_scsih_set_fwfault_debug(const char *val, const struct kernel_param *kp)
{
 int ret = param_set_int(val, kp);
 struct MPT3SAS_ADAPTER *ioc;

 if (ret)
  return ret;

 /* global ioc spinlock to protect controller list on list operations */
 pr_info("setting fwfault_debug(%d)\n", mpt3sas_fwfault_debug);
 spin_lock(&gioc_lock);
 list_for_each_entry(ioc, &mpt3sas_ioc_list, list)
  ioc->fwfault_debug = mpt3sas_fwfault_debug;
 spin_unlock(&gioc_lock);
 return 0;
}
module_param_call(mpt3sas_fwfault_debug, _scsih_set_fwfault_debug,
 param_get_int, &mpt3sas_fwfault_debug, 0644);

/**
 * _base_readl_aero - retry readl for max three times.
 * @addr: MPT Fusion system interface register address
 *
 * Retry the readl() for max three times if it gets zero value
 * while reading the system interface register.
 */

static inline u32
_base_readl_aero(const void __iomem *addr)
{
 u32 i = 0, ret_val;

 do {
  ret_val = readl(addr);
  i++;
 } while (ret_val == 0 && i < 3);

 return ret_val;
}

static u32
_base_readl_ext_retry(const void __iomem *addr)
{
 u32 i, ret_val;

 for (i = 0 ; i < 30 ; i++) {
  ret_val = readl(addr);
  if (ret_val != 0)
   break;
 }

 return ret_val;
}

static inline u32
_base_readl(const void __iomem *addr)
{
 return readl(addr);
}

/**
 * _base_clone_reply_to_sys_mem - copies reply to reply free iomem
 *   in BAR0 space.
 *
 * @ioc: per adapter object
 * @reply: reply message frame(lower 32bit addr)
 * @index: System request message index.
 */

static void
_base_clone_reply_to_sys_mem(struct MPT3SAS_ADAPTER *ioc, u32 reply,
  u32 index)
{
 /*
 * 256 is offset within sys register.
 * 256 offset MPI frame starts. Max MPI frame supported is 32.
 * 32 * 128 = 4K. From here, Clone of reply free for mcpu starts
 */

 u16 cmd_credit = ioc->facts.RequestCredit + 1;
 void __iomem *reply_free_iomem = (void __iomem *)ioc->chip +
   MPI_FRAME_START_OFFSET +
   (cmd_credit * ioc->request_sz) + (index * sizeof(u32));

 writel(reply, reply_free_iomem);
}

/**
 * _base_clone_mpi_to_sys_mem - Writes/copies MPI frames
 * to system/BAR0 region.
 *
 * @dst_iomem: Pointer to the destination location in BAR0 space.
 * @src: Pointer to the Source data.
 * @size: Size of data to be copied.
 */

static void
_base_clone_mpi_to_sys_mem(void *dst_iomem, void *src, u32 size)
{
 int i;
 u32 *src_virt_mem = (u32 *)src;

 for (i = 0; i < size/4; i++)
  writel((u32)src_virt_mem[i],
    (void __iomem *)dst_iomem + (i * 4));
}

/**
 * _base_clone_to_sys_mem - Writes/copies data to system/BAR0 region
 *
 * @dst_iomem: Pointer to the destination location in BAR0 space.
 * @src: Pointer to the Source data.
 * @size: Size of data to be copied.
 */

static void
_base_clone_to_sys_mem(void __iomem *dst_iomem, void *src, u32 size)
{
 int i;
 u32 *src_virt_mem = (u32 *)(src);

 for (i = 0; i < size/4; i++)
  writel((u32)src_virt_mem[i],
   (void __iomem *)dst_iomem + (i * 4));
}

/**
 * _base_get_chain - Calculates and Returns virtual chain address
 *  for the provided smid in BAR0 space.
 *
 * @ioc: per adapter object
 * @smid: system request message index
 * @sge_chain_count: Scatter gather chain count.
 *
 * Return: the chain address.
 */

static inline void __iomem*
_base_get_chain(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  u8 sge_chain_count)
{
 void __iomem *base_chain, *chain_virt;
 u16 cmd_credit = ioc->facts.RequestCredit + 1;

 base_chain  = (void __iomem *)ioc->chip + MPI_FRAME_START_OFFSET +
  (cmd_credit * ioc->request_sz) +
  REPLY_FREE_POOL_SIZE;
 chain_virt = base_chain + (smid * ioc->facts.MaxChainDepth *
   ioc->request_sz) + (sge_chain_count * ioc->request_sz);
 return chain_virt;
}

/**
 * _base_get_chain_phys - Calculates and Returns physical address
 * in BAR0 for scatter gather chains, for
 * the provided smid.
 *
 * @ioc: per adapter object
 * @smid: system request message index
 * @sge_chain_count: Scatter gather chain count.
 *
 * Return: Physical chain address.
 */

static inline phys_addr_t
_base_get_chain_phys(struct MPT3SAS_ADAPTER *ioc, u16 smid,
  u8 sge_chain_count)
{
 phys_addr_t base_chain_phys, chain_phys;
 u16 cmd_credit = ioc->facts.RequestCredit + 1;

 base_chain_phys  = ioc->chip_phys + MPI_FRAME_START_OFFSET +
  (cmd_credit * ioc->request_sz) +
  REPLY_FREE_POOL_SIZE;
 chain_phys = base_chain_phys + (smid * ioc->facts.MaxChainDepth *
   ioc->request_sz) + (sge_chain_count * ioc->request_sz);
 return chain_phys;
}

/**
 * _base_get_buffer_bar0 - Calculates and Returns BAR0 mapped Host
 * buffer address for the provided smid.
 * (Each smid can have 64K starts from 17024)
 *
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return: Pointer to buffer location in BAR0.
 */


static void __iomem *
_base_get_buffer_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
 u16 cmd_credit = ioc->facts.RequestCredit + 1;
 // Added extra 1 to reach end of chain.
 void __iomem *chain_end = _base_get_chain(ioc,
   cmd_credit + 1,
   ioc->facts.MaxChainDepth);
 return chain_end + (smid * 64 * 1024);
}

/**
 * _base_get_buffer_phys_bar0 - Calculates and Returns BAR0 mapped
 * Host buffer Physical address for the provided smid.
 * (Each smid can have 64K starts from 17024)
 *
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return: Pointer to buffer location in BAR0.
 */

static phys_addr_t
_base_get_buffer_phys_bar0(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
 u16 cmd_credit = ioc->facts.RequestCredit + 1;
 phys_addr_t chain_end_phys = _base_get_chain_phys(ioc,
   cmd_credit + 1,
   ioc->facts.MaxChainDepth);
 return chain_end_phys + (smid * 64 * 1024);
}

/**
 * _base_get_chain_buffer_dma_to_chain_buffer - Iterates chain
 * lookup list and Provides chain_buffer
 * address for the matching dma address.
 * (Each smid can have 64K starts from 17024)
 *
 * @ioc: per adapter object
 * @chain_buffer_dma: Chain buffer dma address.
 *
 * Return: Pointer to chain buffer. Or Null on Failure.
 */

static void *
_base_get_chain_buffer_dma_to_chain_buffer(struct MPT3SAS_ADAPTER *ioc,
  dma_addr_t chain_buffer_dma)
{
 u16 index, j;
 struct chain_tracker *ct;

 for (index = 0; index < ioc->scsiio_depth; index++) {
  for (j = 0; j < ioc->chains_needed_per_io; j++) {
   ct = &ioc->chain_lookup[index].chains_per_smid[j];
   if (ct && ct->chain_buffer_dma == chain_buffer_dma)
    return ct->chain_buffer;
  }
 }
 ioc_info(ioc, "Provided chain_buffer_dma address is not in the lookup list\n");
 return NULL;
}

/**
 * _clone_sg_entries - MPI EP's scsiio and config requests
 * are handled here. Base function for
 * double buffering, before submitting
 * the requests.
 *
 * @ioc: per adapter object.
 * @mpi_request: mf request pointer.
 * @smid: system request message index.
 */

static void _clone_sg_entries(struct MPT3SAS_ADAPTER *ioc,
  void *mpi_request, u16 smid)
{
 Mpi2SGESimple32_t *sgel, *sgel_next;
 u32  sgl_flags, sge_chain_count = 0;
 bool is_write = false;
 u16 i = 0;
 void __iomem *buffer_iomem;
 phys_addr_t buffer_iomem_phys;
 void __iomem *buff_ptr;
 phys_addr_t buff_ptr_phys;
 void __iomem *dst_chain_addr[MCPU_MAX_CHAINS_PER_IO];
 void *src_chain_addr[MCPU_MAX_CHAINS_PER_IO];
 phys_addr_t dst_addr_phys;
 MPI2RequestHeader_t *request_hdr;
 struct scsi_cmnd *scmd;
 struct scatterlist *sg_scmd = NULL;
 int is_scsiio_req = 0;

 request_hdr = (MPI2RequestHeader_t *) mpi_request;

 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST) {
  Mpi25SCSIIORequest_t *scsiio_request =
   (Mpi25SCSIIORequest_t *)mpi_request;
  sgel = (Mpi2SGESimple32_t *) &scsiio_request->SGL;
  is_scsiio_req = 1;
 } else if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
  Mpi2ConfigRequest_t  *config_req =
   (Mpi2ConfigRequest_t *)mpi_request;
  sgel = (Mpi2SGESimple32_t *) &config_req->PageBufferSGE;
 } else
  return;

 /* From smid we can get scsi_cmd, once we have sg_scmd,
 * we just need to get sg_virt and sg_next to get virtual
 * address associated with sgel->Address.
 */


 if (is_scsiio_req) {
  /* Get scsi_cmd using smid */
  scmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
  if (scmd == NULL) {
   ioc_err(ioc, "scmd is NULL\n");
   return;
  }

  /* Get sg_scmd from scmd provided */
  sg_scmd = scsi_sglist(scmd);
 }

 /*
 * 0 - 255 System register
 * 256 - 4352 MPI Frame. (This is based on maxCredit 32)
 * 4352 - 4864 Reply_free pool (512 byte is reserved
 * considering maxCredit 32. Reply need extra
 * room, for mCPU case kept four times of
 * maxCredit).
 * 4864 - 17152 SGE chain element. (32cmd * 3 chain of
 * 128 byte size = 12288)
 * 17152 - x Host buffer mapped with smid.
 * (Each smid can have 64K Max IO.)
 * BAR0+Last 1K MSIX Addr and Data
 * Total size in use 2113664 bytes of 4MB BAR0
 */


 buffer_iomem = _base_get_buffer_bar0(ioc, smid);
 buffer_iomem_phys = _base_get_buffer_phys_bar0(ioc, smid);

 buff_ptr = buffer_iomem;
 buff_ptr_phys = buffer_iomem_phys;
 WARN_ON(buff_ptr_phys > U32_MAX);

 if (le32_to_cpu(sgel->FlagsLength) &
   (MPI2_SGE_FLAGS_HOST_TO_IOC << MPI2_SGE_FLAGS_SHIFT))
  is_write = true;

 for (i = 0; i < MPT_MIN_PHYS_SEGMENTS + ioc->facts.MaxChainDepth; i++) {

  sgl_flags =
      (le32_to_cpu(sgel->FlagsLength) >> MPI2_SGE_FLAGS_SHIFT);

  switch (sgl_flags & MPI2_SGE_FLAGS_ELEMENT_MASK) {
  case MPI2_SGE_FLAGS_CHAIN_ELEMENT:
   /*
 * Helper function which on passing
 * chain_buffer_dma returns chain_buffer. Get
 * the virtual address for sgel->Address
 */

   sgel_next =
    _base_get_chain_buffer_dma_to_chain_buffer(ioc,
      le32_to_cpu(sgel->Address));
   if (sgel_next == NULL)
    return;
   /*
 * This is coping 128 byte chain
 * frame (not a host buffer)
 */

   dst_chain_addr[sge_chain_count] =
    _base_get_chain(ioc,
     smid, sge_chain_count);
   src_chain_addr[sge_chain_count] =
      (void *) sgel_next;
   dst_addr_phys = _base_get_chain_phys(ioc,
      smid, sge_chain_count);
   WARN_ON(dst_addr_phys > U32_MAX);
   sgel->Address =
    cpu_to_le32(lower_32_bits(dst_addr_phys));
   sgel = sgel_next;
   sge_chain_count++;
   break;
  case MPI2_SGE_FLAGS_SIMPLE_ELEMENT:
   if (is_write) {
    if (is_scsiio_req) {
     _base_clone_to_sys_mem(buff_ptr,
         sg_virt(sg_scmd),
         (le32_to_cpu(sgel->FlagsLength) &
         0x00ffffff));
     /*
 * FIXME: this relies on a a zero
 * PCI mem_offset.
 */

     sgel->Address =
         cpu_to_le32((u32)buff_ptr_phys);
    } else {
     _base_clone_to_sys_mem(buff_ptr,
         ioc->config_vaddr,
         (le32_to_cpu(sgel->FlagsLength) &
         0x00ffffff));
     sgel->Address =
         cpu_to_le32((u32)buff_ptr_phys);
    }
   }
   buff_ptr += (le32_to_cpu(sgel->FlagsLength) &
       0x00ffffff);
   buff_ptr_phys += (le32_to_cpu(sgel->FlagsLength) &
       0x00ffffff);
   if ((le32_to_cpu(sgel->FlagsLength) &
       (MPI2_SGE_FLAGS_END_OF_BUFFER
     << MPI2_SGE_FLAGS_SHIFT)))
    goto eob_clone_chain;
   else {
    /*
 * Every single element in MPT will have
 * associated sg_next. Better to sanity that
 * sg_next is not NULL, but it will be a bug
 * if it is null.
 */

    if (is_scsiio_req) {
     sg_scmd = sg_next(sg_scmd);
     if (sg_scmd)
      sgel++;
     else
      goto eob_clone_chain;
    }
   }
   break;
  }
 }

eob_clone_chain:
 for (i = 0; i < sge_chain_count; i++) {
  if (is_scsiio_req)
   _base_clone_to_sys_mem(dst_chain_addr[i],
    src_chain_addr[i], ioc->request_sz);
 }
}

/**
 *  mpt3sas_remove_dead_ioc_func - kthread context to remove dead ioc
 * @arg: input argument, used to derive ioc
 *
 * Return:
 * 0 if controller is removed from pci subsystem.
 * -1 for other case.
 */

static int mpt3sas_remove_dead_ioc_func(void *arg)
{
 struct MPT3SAS_ADAPTER *ioc = (struct MPT3SAS_ADAPTER *)arg;
 struct pci_dev *pdev;

 if (!ioc)
  return -1;

 pdev = ioc->pdev;
 if (!pdev)
  return -1;
 pci_stop_and_remove_bus_device_locked(pdev);
 return 0;
}

/**
 * _base_sync_drv_fw_timestamp - Sync Drive-Fw TimeStamp.
 * @ioc: Per Adapter Object
 *
 * Return: nothing.
 */

static void _base_sync_drv_fw_timestamp(struct MPT3SAS_ADAPTER *ioc)
{
 Mpi26IoUnitControlRequest_t *mpi_request;
 Mpi26IoUnitControlReply_t *mpi_reply;
 u16 smid;
 ktime_t current_time;
 u64 TimeStamp = 0;
 u8 issue_reset = 0;

 mutex_lock(&ioc->scsih_cmds.mutex);
 if (ioc->scsih_cmds.status != MPT3_CMD_NOT_USED) {
  ioc_err(ioc, "scsih_cmd in use %s\n", __func__);
  goto out;
 }
 ioc->scsih_cmds.status = MPT3_CMD_PENDING;
 smid = mpt3sas_base_get_smid(ioc, ioc->scsih_cb_idx);
 if (!smid) {
  ioc_err(ioc, "Failed obtaining a smid %s\n", __func__);
  ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
  goto out;
 }
 mpi_request = mpt3sas_base_get_msg_frame(ioc, smid);
 ioc->scsih_cmds.smid = smid;
 memset(mpi_request, 0, sizeof(Mpi26IoUnitControlRequest_t));
 mpi_request->Function = MPI2_FUNCTION_IO_UNIT_CONTROL;
 mpi_request->Operation = MPI26_CTRL_OP_SET_IOC_PARAMETER;
 mpi_request->IOCParameter = MPI26_SET_IOC_PARAMETER_SYNC_TIMESTAMP;
 current_time = ktime_get_real();
 TimeStamp = ktime_to_ms(current_time);
 mpi_request->Reserved7 = cpu_to_le32(TimeStamp >> 32);
 mpi_request->IOCParameterValue = cpu_to_le32(TimeStamp & 0xFFFFFFFF);
 init_completion(&ioc->scsih_cmds.done);
 ioc->put_smid_default(ioc, smid);
 dinitprintk(ioc, ioc_info(ioc,
     "Io Unit Control Sync TimeStamp (sending), @time %lld ms\n",
     TimeStamp));
 wait_for_completion_timeout(&ioc->scsih_cmds.done,
  MPT3SAS_TIMESYNC_TIMEOUT_SECONDS*HZ);
 if (!(ioc->scsih_cmds.status & MPT3_CMD_COMPLETE)) {
  mpt3sas_check_cmd_timeout(ioc,
      ioc->scsih_cmds.status, mpi_request,
      sizeof(Mpi2SasIoUnitControlRequest_t)/4, issue_reset);
  goto issue_host_reset;
 }
 if (ioc->scsih_cmds.status & MPT3_CMD_REPLY_VALID) {
  mpi_reply = ioc->scsih_cmds.reply;
  dinitprintk(ioc, ioc_info(ioc,
      "Io Unit Control sync timestamp (complete): ioc_status(0x%04x), loginfo(0x%08x)\n",
      le16_to_cpu(mpi_reply->IOCStatus),
      le32_to_cpu(mpi_reply->IOCLogInfo)));
 }
issue_host_reset:
 if (issue_reset)
  mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
 ioc->scsih_cmds.status = MPT3_CMD_NOT_USED;
out:
 mutex_unlock(&ioc->scsih_cmds.mutex);
}

/**
 * _base_fault_reset_work - workq handling ioc fault conditions
 * @work: input argument, used to derive ioc
 *
 * Context: sleep.
 */

static void
_base_fault_reset_work(struct work_struct *work)
{
 struct MPT3SAS_ADAPTER *ioc =
     container_of(work, struct MPT3SAS_ADAPTER, fault_reset_work.work);
 unsigned long  flags;
 u32 doorbell;
 int rc;
 struct task_struct *p;


 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
 if ((ioc->shost_recovery && (ioc->ioc_coredump_loop == 0)) ||
   ioc->pci_error_recovery)
  goto rearm_timer;
 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);

 doorbell = mpt3sas_base_get_iocstate(ioc, 0);
 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_MASK) {
  ioc_err(ioc, "SAS host is non-operational !!!!\n");

  /* It may be possible that EEH recovery can resolve some of
 * pci bus failure issues rather removing the dead ioc function
 * by considering controller is in a non-operational state. So
 * here priority is given to the EEH recovery. If it doesn't
 * not resolve this issue, mpt3sas driver will consider this
 * controller to non-operational state and remove the dead ioc
 * function.
 */

  if (ioc->non_operational_loop++ < 5) {
   spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock,
        flags);
   goto rearm_timer;
  }

  /*
 * Call _scsih_flush_pending_cmds callback so that we flush all
 * pending commands back to OS. This call is required to avoid
 * deadlock at block layer. Dead IOC will fail to do diag reset,
 * and this call is safe since dead ioc will never return any
 * command back from HW.
 */

  mpt3sas_base_pause_mq_polling(ioc);
  ioc->schedule_dead_ioc_flush_running_cmds(ioc);
  /*
 * Set remove_host flag early since kernel thread will
 * take some time to execute.
 */

  ioc->remove_host = 1;
  /*Remove the Dead Host */
  p = kthread_run(mpt3sas_remove_dead_ioc_func, ioc,
      "%s_dead_ioc_%d", ioc->driver_name, ioc->id);
  if (IS_ERR(p))
   ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread failed !!!!\n",
    __func__);
  else
   ioc_err(ioc, "%s: Running mpt3sas_dead_ioc thread success !!!!\n",
    __func__);
  return/* don't rearm timer */
 }

 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_COREDUMP) {
  u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
      ioc->manu_pg11.CoreDumpTOSec :
      MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;

  timeout /= (FAULT_POLLING_INTERVAL/1000);

  if (ioc->ioc_coredump_loop == 0) {
   mpt3sas_print_coredump_info(ioc,
       doorbell & MPI2_DOORBELL_DATA_MASK);
   /* do not accept any IOs and disable the interrupts */
   spin_lock_irqsave(
       &ioc->ioc_reset_in_progress_lock, flags);
   ioc->shost_recovery = 1;
   spin_unlock_irqrestore(
       &ioc->ioc_reset_in_progress_lock, flags);
   mpt3sas_base_mask_interrupts(ioc);
   mpt3sas_base_pause_mq_polling(ioc);
   _base_clear_outstanding_commands(ioc);
  }

  ioc_info(ioc, "%s: CoreDump loop %d.",
      __func__, ioc->ioc_coredump_loop);

  /* Wait until CoreDump completes or times out */
  if (ioc->ioc_coredump_loop++ < timeout) {
   spin_lock_irqsave(
       &ioc->ioc_reset_in_progress_lock, flags);
   goto rearm_timer;
  }
 }

 if (ioc->ioc_coredump_loop) {
  if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_COREDUMP)
   ioc_err(ioc, "%s: CoreDump completed. LoopCount: %d",
       __func__, ioc->ioc_coredump_loop);
  else
   ioc_err(ioc, "%s: CoreDump Timed out. LoopCount: %d",
       __func__, ioc->ioc_coredump_loop);
  ioc->ioc_coredump_loop = MPT3SAS_COREDUMP_LOOP_DONE;
 }
 ioc->non_operational_loop = 0;
 if ((doorbell & MPI2_IOC_STATE_MASK) != MPI2_IOC_STATE_OPERATIONAL) {
  rc = mpt3sas_base_hard_reset_handler(ioc, FORCE_BIG_HAMMER);
  ioc_warn(ioc, "%s: hard reset: %s\n",
    __func__, rc == 0 ? "success" : "failed");
  doorbell = mpt3sas_base_get_iocstate(ioc, 0);
  if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
   mpt3sas_print_fault_code(ioc, doorbell &
       MPI2_DOORBELL_DATA_MASK);
  } else if ((doorbell & MPI2_IOC_STATE_MASK) ==
      MPI2_IOC_STATE_COREDUMP)
   mpt3sas_print_coredump_info(ioc, doorbell &
       MPI2_DOORBELL_DATA_MASK);
  if (rc && (doorbell & MPI2_IOC_STATE_MASK) !=
      MPI2_IOC_STATE_OPERATIONAL)
   return/* don't rearm timer */
 }
 ioc->ioc_coredump_loop = 0;
 if (ioc->time_sync_interval &&
     ++ioc->timestamp_update_count >= ioc->time_sync_interval) {
  ioc->timestamp_update_count = 0;
  _base_sync_drv_fw_timestamp(ioc);
 }
 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
 rearm_timer:
 if (ioc->fault_reset_work_q)
  queue_delayed_work(ioc->fault_reset_work_q,
      &ioc->fault_reset_work,
      msecs_to_jiffies(FAULT_POLLING_INTERVAL));
 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
}

/**
 * mpt3sas_base_start_watchdog - start the fault_reset_work_q
 * @ioc: per adapter object
 *
 * Context: sleep.
 */

void
mpt3sas_base_start_watchdog(struct MPT3SAS_ADAPTER *ioc)
{
 unsigned long  flags;

 if (ioc->fault_reset_work_q)
  return;

 ioc->timestamp_update_count = 0;
 /* initialize fault polling */

 INIT_DELAYED_WORK(&ioc->fault_reset_work, _base_fault_reset_work);
 snprintf(ioc->fault_reset_work_q_name,
     sizeof(ioc->fault_reset_work_q_name), "poll_%s%d_status",
     ioc->driver_name, ioc->id);
 ioc->fault_reset_work_q = alloc_ordered_workqueue(
  "%s", WQ_MEM_RECLAIM, ioc->fault_reset_work_q_name);
 if (!ioc->fault_reset_work_q) {
  ioc_err(ioc, "%s: failed (line=%d)\n", __func__, __LINE__);
  return;
 }
 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
 if (ioc->fault_reset_work_q)
  queue_delayed_work(ioc->fault_reset_work_q,
      &ioc->fault_reset_work,
      msecs_to_jiffies(FAULT_POLLING_INTERVAL));
 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
}

/**
 * mpt3sas_base_stop_watchdog - stop the fault_reset_work_q
 * @ioc: per adapter object
 *
 * Context: sleep.
 */

void
mpt3sas_base_stop_watchdog(struct MPT3SAS_ADAPTER *ioc)
{
 unsigned long flags;
 struct workqueue_struct *wq;

 spin_lock_irqsave(&ioc->ioc_reset_in_progress_lock, flags);
 wq = ioc->fault_reset_work_q;
 ioc->fault_reset_work_q = NULL;
 spin_unlock_irqrestore(&ioc->ioc_reset_in_progress_lock, flags);
 if (wq) {
  if (!cancel_delayed_work_sync(&ioc->fault_reset_work))
   flush_workqueue(wq);
  destroy_workqueue(wq);
 }
}

/**
 * mpt3sas_base_fault_info - verbose translation of firmware FAULT code
 * @ioc: per adapter object
 * @fault_code: fault code
 */

void
mpt3sas_base_fault_info(struct MPT3SAS_ADAPTER *ioc, u16 fault_code)
{
 ioc_err(ioc, "fault_state(0x%04x)!\n", fault_code);
}

/**
 * mpt3sas_base_coredump_info - verbose translation of firmware CoreDump state
 * @ioc: per adapter object
 * @fault_code: fault code
 *
 * Return: nothing.
 */

void
mpt3sas_base_coredump_info(struct MPT3SAS_ADAPTER *ioc, u16 fault_code)
{
 ioc_err(ioc, "coredump_state(0x%04x)!\n", fault_code);
}

/**
 * mpt3sas_base_wait_for_coredump_completion - Wait until coredump
 * completes or times out
 * @ioc: per adapter object
 * @caller: caller function name
 *
 * Return: 0 for success, non-zero for failure.
 */

int
mpt3sas_base_wait_for_coredump_completion(struct MPT3SAS_ADAPTER *ioc,
  const char *caller)
{
 u8 timeout = (ioc->manu_pg11.CoreDumpTOSec) ?
   ioc->manu_pg11.CoreDumpTOSec :
   MPT3SAS_DEFAULT_COREDUMP_TIMEOUT_SECONDS;

 int ioc_state = _base_wait_on_iocstate(ioc, MPI2_IOC_STATE_FAULT,
     timeout);

 if (ioc_state)
  ioc_err(ioc,
      "%s: CoreDump timed out. (ioc_state=0x%x)\n",
      caller, ioc_state);
 else
  ioc_info(ioc,
      "%s: CoreDump completed. (ioc_state=0x%x)\n",
      caller, ioc_state);

 return ioc_state;
}

/**
 * mpt3sas_halt_firmware - halt's mpt controller firmware
 * @ioc: per adapter object
 *
 * For debugging timeout related issues.  Writing 0xCOFFEE00
 * to the doorbell register will halt controller firmware. With
 * the purpose to stop both driver and firmware, the enduser can
 * obtain a ring buffer from controller UART.
 */

void
mpt3sas_halt_firmware(struct MPT3SAS_ADAPTER *ioc)
{
 u32 doorbell;

 if (!ioc->fwfault_debug)
  return;

 dump_stack();

 doorbell = ioc->base_readl_ext_retry(&ioc->chip->Doorbell);
 if ((doorbell & MPI2_IOC_STATE_MASK) == MPI2_IOC_STATE_FAULT) {
  mpt3sas_print_fault_code(ioc, doorbell &
      MPI2_DOORBELL_DATA_MASK);
 } else if ((doorbell & MPI2_IOC_STATE_MASK) ==
     MPI2_IOC_STATE_COREDUMP) {
  mpt3sas_print_coredump_info(ioc, doorbell &
      MPI2_DOORBELL_DATA_MASK);
 } else {
  writel(0xC0FFEE00, &ioc->chip->Doorbell);
  ioc_err(ioc, "Firmware is halted due to command timeout\n");
 }

 if (ioc->fwfault_debug == 2)
  for (;;)
   ;
 else
  panic("panic in %s\n", __func__);
}

/**
 * _base_sas_ioc_info - verbose translation of the ioc status
 * @ioc: per adapter object
 * @mpi_reply: reply mf payload returned from firmware
 * @request_hdr: request mf
 */

static void
_base_sas_ioc_info(struct MPT3SAS_ADAPTER *ioc, MPI2DefaultReply_t *mpi_reply,
 MPI2RequestHeader_t *request_hdr)
{
 u16 ioc_status = le16_to_cpu(mpi_reply->IOCStatus) &
     MPI2_IOCSTATUS_MASK;
 char *desc = NULL;
 u16 frame_sz;
 char *func_str = NULL;

 /* SCSI_IO, RAID_PASS are handled from _scsih_scsi_ioc_info */
 if (request_hdr->Function == MPI2_FUNCTION_SCSI_IO_REQUEST ||
     request_hdr->Function == MPI2_FUNCTION_RAID_SCSI_IO_PASSTHROUGH ||
     request_hdr->Function == MPI2_FUNCTION_EVENT_NOTIFICATION)
  return;

 if (ioc_status == MPI2_IOCSTATUS_CONFIG_INVALID_PAGE)
  return;
 /*
 * Older Firmware version doesn't support driver trigger pages.
 * So, skip displaying 'config invalid type' type
 * of error message.
 */

 if (request_hdr->Function == MPI2_FUNCTION_CONFIG) {
  Mpi2ConfigRequest_t *rqst = (Mpi2ConfigRequest_t *)request_hdr;

  if ((rqst->ExtPageType ==
      MPI2_CONFIG_EXTPAGETYPE_DRIVER_PERSISTENT_TRIGGER) &&
      !(ioc->logging_level & MPT_DEBUG_CONFIG)) {
   return;
  }
 }

 switch (ioc_status) {

/****************************************************************************
*  Common IOCStatus values for all replies
****************************************************************************/


 case MPI2_IOCSTATUS_INVALID_FUNCTION:
  desc = "invalid function";
  break;
 case MPI2_IOCSTATUS_BUSY:
  desc = "busy";
  break;
 case MPI2_IOCSTATUS_INVALID_SGL:
  desc = "invalid sgl";
  break;
 case MPI2_IOCSTATUS_INTERNAL_ERROR:
  desc = "internal error";
  break;
 case MPI2_IOCSTATUS_INVALID_VPID:
  desc = "invalid vpid";
  break;
 case MPI2_IOCSTATUS_INSUFFICIENT_RESOURCES:
  desc = "insufficient resources";
  break;
 case MPI2_IOCSTATUS_INSUFFICIENT_POWER:
  desc = "insufficient power";
  break;
 case MPI2_IOCSTATUS_INVALID_FIELD:
  desc = "invalid field";
  break;
 case MPI2_IOCSTATUS_INVALID_STATE:
  desc = "invalid state";
  break;
 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
  desc = "op state not supported";
  break;

/****************************************************************************
*  Config IOCStatus values
****************************************************************************/


 case MPI2_IOCSTATUS_CONFIG_INVALID_ACTION:
  desc = "config invalid action";
  break;
 case MPI2_IOCSTATUS_CONFIG_INVALID_TYPE:
  desc = "config invalid type";
  break;
 case MPI2_IOCSTATUS_CONFIG_INVALID_PAGE:
  desc = "config invalid page";
  break;
 case MPI2_IOCSTATUS_CONFIG_INVALID_DATA:
  desc = "config invalid data";
  break;
 case MPI2_IOCSTATUS_CONFIG_NO_DEFAULTS:
  desc = "config no defaults";
  break;
 case MPI2_IOCSTATUS_CONFIG_CANT_COMMIT:
  desc = "config can't commit";
  break;

/****************************************************************************
*  SCSI IO Reply
****************************************************************************/


 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
  break;

/****************************************************************************
*  For use by SCSI Initiator and SCSI Target end-to-end data protection
****************************************************************************/


 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
  desc = "eedp guard error";
  break;
 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
  desc = "eedp ref tag error";
  break;
 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
  desc = "eedp app tag error";
  break;

/****************************************************************************
*  SCSI Target values
****************************************************************************/


 case MPI2_IOCSTATUS_TARGET_INVALID_IO_INDEX:
  desc = "target invalid io index";
  break;
 case MPI2_IOCSTATUS_TARGET_ABORTED:
  desc = "target aborted";
  break;
 case MPI2_IOCSTATUS_TARGET_NO_CONN_RETRYABLE:
  desc = "target no conn retryable";
  break;
 case MPI2_IOCSTATUS_TARGET_NO_CONNECTION:
  desc = "target no connection";
  break;
 case MPI2_IOCSTATUS_TARGET_XFER_COUNT_MISMATCH:
  desc = "target xfer count mismatch";
  break;
 case MPI2_IOCSTATUS_TARGET_DATA_OFFSET_ERROR:
  desc = "target data offset error";
  break;
 case MPI2_IOCSTATUS_TARGET_TOO_MUCH_WRITE_DATA:
  desc = "target too much write data";
  break;
 case MPI2_IOCSTATUS_TARGET_IU_TOO_SHORT:
  desc = "target iu too short";
  break;
 case MPI2_IOCSTATUS_TARGET_ACK_NAK_TIMEOUT:
  desc = "target ack nak timeout";
  break;
 case MPI2_IOCSTATUS_TARGET_NAK_RECEIVED:
  desc = "target nak received";
  break;

/****************************************************************************
*  Serial Attached SCSI values
****************************************************************************/


 case MPI2_IOCSTATUS_SAS_SMP_REQUEST_FAILED:
  desc = "smp request failed";
  break;
 case MPI2_IOCSTATUS_SAS_SMP_DATA_OVERRUN:
  desc = "smp data overrun";
  break;

/****************************************************************************
*  Diagnostic Buffer Post / Diagnostic Release values
****************************************************************************/


 case MPI2_IOCSTATUS_DIAGNOSTIC_RELEASED:
  desc = "diagnostic released";
  break;
 default:
  break;
 }

 if (!desc)
  return;

 switch (request_hdr->Function) {
 case MPI2_FUNCTION_CONFIG:
  frame_sz = sizeof(Mpi2ConfigRequest_t) + ioc->sge_size;
  func_str = "config_page";
  break;
 case MPI2_FUNCTION_SCSI_TASK_MGMT:
  frame_sz = sizeof(Mpi2SCSITaskManagementRequest_t);
  func_str = "task_mgmt";
  break;
 case MPI2_FUNCTION_SAS_IO_UNIT_CONTROL:
  frame_sz = sizeof(Mpi2SasIoUnitControlRequest_t);
  func_str = "sas_iounit_ctl";
  break;
 case MPI2_FUNCTION_SCSI_ENCLOSURE_PROCESSOR:
  frame_sz = sizeof(Mpi2SepRequest_t);
  func_str = "enclosure";
  break;
 case MPI2_FUNCTION_IOC_INIT:
  frame_sz = sizeof(Mpi2IOCInitRequest_t);
  func_str = "ioc_init";
  break;
 case MPI2_FUNCTION_PORT_ENABLE:
  frame_sz = sizeof(Mpi2PortEnableRequest_t);
  func_str = "port_enable";
  break;
 case MPI2_FUNCTION_SMP_PASSTHROUGH:
  frame_sz = sizeof(Mpi2SmpPassthroughRequest_t) + ioc->sge_size;
  func_str = "smp_passthru";
  break;
 case MPI2_FUNCTION_NVME_ENCAPSULATED:
  frame_sz = sizeof(Mpi26NVMeEncapsulatedRequest_t) +
      ioc->sge_size;
  func_str = "nvme_encapsulated";
  break;
 case MPI2_FUNCTION_MCTP_PASSTHROUGH:
  frame_sz = sizeof(Mpi26MctpPassthroughRequest_t) +
      ioc->sge_size;
  func_str = "mctp_passthru";
  break;
 default:
  frame_sz = 32;
  func_str = "unknown";
  break;
 }

 ioc_warn(ioc, "ioc_status: %s(0x%04x), request(0x%p),(%s)\n",
   desc, ioc_status, request_hdr, func_str);

 _debug_dump_mf(request_hdr, frame_sz/4);
}

/**
 * _base_display_event_data - verbose translation of firmware asyn events
 * @ioc: per adapter object
 * @mpi_reply: reply mf payload returned from firmware
 */

static void
_base_display_event_data(struct MPT3SAS_ADAPTER *ioc,
 Mpi2EventNotificationReply_t *mpi_reply)
{
 char *desc = NULL;
 u16 event;

 if (!(ioc->logging_level & MPT_DEBUG_EVENTS))
  return;

 event = le16_to_cpu(mpi_reply->Event);

 switch (event) {
 case MPI2_EVENT_LOG_DATA:
  desc = "Log Data";
  break;
 case MPI2_EVENT_STATE_CHANGE:
  desc = "Status Change";
  break;
 case MPI2_EVENT_HARD_RESET_RECEIVED:
  desc = "Hard Reset Received";
  break;
 case MPI2_EVENT_EVENT_CHANGE:
  desc = "Event Change";
  break;
 case MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE:
  desc = "Device Status Change";
  break;
 case MPI2_EVENT_IR_OPERATION_STATUS:
  if (!ioc->hide_ir_msg)
   desc = "IR Operation Status";
  break;
 case MPI2_EVENT_SAS_DISCOVERY:
 {
  Mpi2EventDataSasDiscovery_t *event_data =
      (Mpi2EventDataSasDiscovery_t *)mpi_reply->EventData;
  ioc_info(ioc, "Discovery: (%s)",
    event_data->ReasonCode == MPI2_EVENT_SAS_DISC_RC_STARTED ?
    "start" : "stop");
  if (event_data->DiscoveryStatus)
   pr_cont(" discovery_status(0x%08x)",
       le32_to_cpu(event_data->DiscoveryStatus));
  pr_cont("\n");
  return;
 }
 case MPI2_EVENT_SAS_BROADCAST_PRIMITIVE:
  desc = "SAS Broadcast Primitive";
  break;
 case MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE:
  desc = "SAS Init Device Status Change";
  break;
 case MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW:
  desc = "SAS Init Table Overflow";
  break;
 case MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST:
  desc = "SAS Topology Change List";
  break;
 case MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE:
  desc = "SAS Enclosure Device Status Change";
  break;
 case MPI2_EVENT_IR_VOLUME:
  if (!ioc->hide_ir_msg)
   desc = "IR Volume";
  break;
 case MPI2_EVENT_IR_PHYSICAL_DISK:
  if (!ioc->hide_ir_msg)
   desc = "IR Physical Disk";
  break;
 case MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST:
  if (!ioc->hide_ir_msg)
   desc = "IR Configuration Change List";
  break;
 case MPI2_EVENT_LOG_ENTRY_ADDED:
  if (!ioc->hide_ir_msg)
   desc = "Log Entry Added";
  break;
 case MPI2_EVENT_TEMP_THRESHOLD:
  desc = "Temperature Threshold";
  break;
 case MPI2_EVENT_ACTIVE_CABLE_EXCEPTION:
  desc = "Cable Event";
  break;
 case MPI2_EVENT_SAS_DEVICE_DISCOVERY_ERROR:
  desc = "SAS Device Discovery Error";
  break;
 case MPI2_EVENT_PCIE_DEVICE_STATUS_CHANGE:
  desc = "PCIE Device Status Change";
  break;
 case MPI2_EVENT_PCIE_ENUMERATION:
 {
  Mpi26EventDataPCIeEnumeration_t *event_data =
   (Mpi26EventDataPCIeEnumeration_t *)mpi_reply->EventData;
  ioc_info(ioc, "PCIE Enumeration: (%s)",
    event_data->ReasonCode == MPI26_EVENT_PCIE_ENUM_RC_STARTED ?
    "start" : "stop");
  if (event_data->EnumerationStatus)
   pr_cont("enumeration_status(0x%08x)",
    le32_to_cpu(event_data->EnumerationStatus));
  pr_cont("\n");
  return;
 }
 case MPI2_EVENT_PCIE_TOPOLOGY_CHANGE_LIST:
  desc = "PCIE Topology Change List";
  break;
 }

 if (!desc)
  return;

 ioc_info(ioc, "%s\n", desc);
}

/**
 * _base_sas_log_info - verbose translation of firmware log info
 * @ioc: per adapter object
 * @log_info: log info
 */

static void
_base_sas_log_info(struct MPT3SAS_ADAPTER *ioc, u32 log_info)
{
 union loginfo_type {
  u32 loginfo;
  struct {
   u32 subcode:16;
   u32 code:8;
   u32 originator:4;
   u32 bus_type:4;
  } dw;
 };
 union loginfo_type sas_loginfo;
 char *originator_str = NULL;

 sas_loginfo.loginfo = log_info;
 if (sas_loginfo.dw.bus_type != 3 /*SAS*/)
  return;

 /* each nexus loss loginfo */
 if (log_info == 0x31170000)
  return;

 /* eat the loginfos associated with task aborts */
 if (ioc->ignore_loginfos && (log_info == 0x30050000 || log_info ==
     0x31140000 || log_info == 0x31130000))
  return;

 switch (sas_loginfo.dw.originator) {
 case 0:
  originator_str = "IOP";
  break;
 case 1:
  originator_str = "PL";
  break;
 case 2:
  if (!ioc->hide_ir_msg)
   originator_str = "IR";
  else
   originator_str = "WarpDrive";
  break;
 }

 ioc_warn(ioc, "log_info(0x%08x): originator(%s), code(0x%02x), sub_code(0x%04x)\n",
   log_info,
   originator_str, sas_loginfo.dw.code, sas_loginfo.dw.subcode);
}

/**
 * _base_display_reply_info - handle reply descriptors depending on IOC Status
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame (lower 32bit addr)
 */

static void
_base_display_reply_info(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
 u32 reply)
{
 MPI2DefaultReply_t *mpi_reply;
 u16 ioc_status;
 u32 loginfo = 0;

 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
 if (unlikely(!mpi_reply)) {
  ioc_err(ioc, "mpi_reply not valid at %s:%d/%s()!\n",
   __FILE__, __LINE__, __func__);
  return;
 }
 ioc_status = le16_to_cpu(mpi_reply->IOCStatus);

 if ((ioc_status & MPI2_IOCSTATUS_MASK) &&
     (ioc->logging_level & MPT_DEBUG_REPLY)) {
  _base_sas_ioc_info(ioc, mpi_reply,
     mpt3sas_base_get_msg_frame(ioc, smid));
 }

 if (ioc_status & MPI2_IOCSTATUS_FLAG_LOG_INFO_AVAILABLE) {
  loginfo = le32_to_cpu(mpi_reply->IOCLogInfo);
  _base_sas_log_info(ioc, loginfo);
 }

 if (ioc_status || loginfo) {
  ioc_status &= MPI2_IOCSTATUS_MASK;
  mpt3sas_trigger_mpi(ioc, ioc_status, loginfo);
 }
}

/**
 * mpt3sas_base_done - base internal command completion routine
 * @ioc: per adapter object
 * @smid: system request message index
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 *
 * Return:
 * 1 meaning mf should be freed from _base_interrupt
 * 0 means the mf is freed from this function.
 */

u8
mpt3sas_base_done(struct MPT3SAS_ADAPTER *ioc, u16 smid, u8 msix_index,
 u32 reply)
{
 MPI2DefaultReply_t *mpi_reply;

 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
 if (mpi_reply && mpi_reply->Function == MPI2_FUNCTION_EVENT_ACK)
  return mpt3sas_check_for_pending_internal_cmds(ioc, smid);

 if (ioc->base_cmds.status == MPT3_CMD_NOT_USED)
  return 1;

 ioc->base_cmds.status |= MPT3_CMD_COMPLETE;
 if (mpi_reply) {
  ioc->base_cmds.status |= MPT3_CMD_REPLY_VALID;
  memcpy(ioc->base_cmds.reply, mpi_reply, mpi_reply->MsgLength*4);
 }
 ioc->base_cmds.status &= ~MPT3_CMD_PENDING;

 complete(&ioc->base_cmds.done);
 return 1;
}

/**
 * _base_async_event - main callback handler for firmware asyn events
 * @ioc: per adapter object
 * @msix_index: MSIX table index supplied by the OS
 * @reply: reply message frame(lower 32bit addr)
 *
 * Return:
 * 1 meaning mf should be freed from _base_interrupt
 * 0 means the mf is freed from this function.
 */

static u8
_base_async_event(struct MPT3SAS_ADAPTER *ioc, u8 msix_index, u32 reply)
{
 Mpi2EventNotificationReply_t *mpi_reply;
 Mpi2EventAckRequest_t *ack_request;
 u16 smid;
 struct _event_ack_list *delayed_event_ack;

 mpi_reply = mpt3sas_base_get_reply_virt_addr(ioc, reply);
 if (!mpi_reply)
  return 1;
 if (mpi_reply->Function != MPI2_FUNCTION_EVENT_NOTIFICATION)
  return 1;

 _base_display_event_data(ioc, mpi_reply);

 if (!(mpi_reply->AckRequired & MPI2_EVENT_NOTIFICATION_ACK_REQUIRED))
  goto out;
 smid = mpt3sas_base_get_smid(ioc, ioc->base_cb_idx);
 if (!smid) {
  delayed_event_ack = kzalloc(sizeof(*delayed_event_ack),
     GFP_ATOMIC);
  if (!delayed_event_ack)
   goto out;
  INIT_LIST_HEAD(&delayed_event_ack->list);
  delayed_event_ack->Event = mpi_reply->Event;
  delayed_event_ack->EventContext = mpi_reply->EventContext;
  list_add_tail(&delayed_event_ack->list,
    &ioc->delayed_event_ack_list);
  dewtprintk(ioc,
      ioc_info(ioc, "DELAYED: EVENT ACK: event (0x%04x)\n",
        le16_to_cpu(mpi_reply->Event)));
  goto out;
 }

 ack_request = mpt3sas_base_get_msg_frame(ioc, smid);
 memset(ack_request, 0, sizeof(Mpi2EventAckRequest_t));
 ack_request->Function = MPI2_FUNCTION_EVENT_ACK;
 ack_request->Event = mpi_reply->Event;
 ack_request->EventContext = mpi_reply->EventContext;
 ack_request->VF_ID = 0;  /* TODO */
 ack_request->VP_ID = 0;
 ioc->put_smid_default(ioc, smid);

 out:

 /* scsih callback handler */
 mpt3sas_scsih_event_callback(ioc, msix_index, reply);

 /* ctl callback handler */
 mpt3sas_ctl_event_callback(ioc, msix_index, reply);

 return 1;
}

static struct scsiio_tracker *
_get_st_from_smid(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
 struct scsi_cmnd *cmd;

 if (WARN_ON(!smid) ||
     WARN_ON(smid >= ioc->hi_priority_smid))
  return NULL;

 cmd = mpt3sas_scsih_scsi_lookup_get(ioc, smid);
 if (cmd)
  return scsi_cmd_priv(cmd);

 return NULL;
}

/**
 * _base_get_cb_idx - obtain the callback index
 * @ioc: per adapter object
 * @smid: system request message index
 *
 * Return: callback index.
 */

static u8
_base_get_cb_idx(struct MPT3SAS_ADAPTER *ioc, u16 smid)
{
 int i;
 u16 ctl_smid = ioc->scsiio_depth - INTERNAL_SCSIIO_CMDS_COUNT + 1;
 u8 cb_idx = 0xFF;

 if (smid < ioc->hi_priority_smid) {
  struct scsiio_tracker *st;

  if (smid < ctl_smid) {
   st = _get_st_from_smid(ioc, smid);
   if (st)
    cb_idx = st->cb_idx;
  } else if (smid == ctl_smid)
   cb_idx = ioc->ctl_cb_idx;
 } else if (smid < ioc->internal_smid) {
  i = smid - ioc->hi_priority_smid;
  cb_idx = ioc->hpr_lookup[i].cb_idx;
 } else if (smid <= ioc->hba_queue_depth) {
  i = smid - ioc->internal_smid;
  cb_idx = ioc->internal_lookup[i].cb_idx;
 }
 return cb_idx;
}

/**
 * mpt3sas_base_pause_mq_polling - pause polling on the mq poll queues
 * when driver is flushing out the IOs.
 * @ioc: per adapter object
 *
 * Pause polling on the mq poll (io uring) queues when driver is flushing
 * out the IOs. Otherwise we may see the race condition of completing the same
 * IO from two paths.
 *
 * Returns nothing.
 */

void
mpt3sas_base_pause_mq_polling(struct MPT3SAS_ADAPTER *ioc)
{
 int iopoll_q_count =
     ioc->reply_queue_count - ioc->iopoll_q_start_index;
 int qid;

 for (qid = 0; qid < iopoll_q_count; qid++)
  atomic_set(&ioc->io_uring_poll_queues[qid].pause, 1);

 /*
 * wait for current poll to complete.
 */

 for (qid = 0; qid < iopoll_q_count; qid++) {
  while (atomic_read(&ioc->io_uring_poll_queues[qid].busy)) {
   cpu_relax();
   udelay(500);
  }
 }
}

/**
 * mpt3sas_base_resume_mq_polling - Resume polling on mq poll queues.
 * @ioc: per adapter object
 *
 * Returns nothing.
 */

void
mpt3sas_base_resume_mq_polling(struct MPT3SAS_ADAPTER *ioc)
{
 int iopoll_q_count =
     ioc->reply_queue_count - ioc->iopoll_q_start_index;
 int qid;

 for (qid = 0; qid < iopoll_q_count; qid++)
  atomic_set(&ioc->io_uring_poll_queues[qid].pause, 0);
}

/**
 * mpt3sas_base_mask_interrupts - disable interrupts
 * @ioc: per adapter object
 *
 * Disabling ResetIRQ, Reply and Doorbell Interrupts
 */

void
mpt3sas_base_mask_interrupts(struct MPT3SAS_ADAPTER *ioc)
{
 u32 him_register;

 ioc->mask_interrupts = 1;
 him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
 him_register |= MPI2_HIM_DIM + MPI2_HIM_RIM + MPI2_HIM_RESET_IRQ_MASK;
 writel(him_register, &ioc->chip->HostInterruptMask);
 ioc->base_readl(&ioc->chip->HostInterruptMask);
}

/**
 * mpt3sas_base_unmask_interrupts - enable interrupts
 * @ioc: per adapter object
 *
 * Enabling only Reply Interrupts
 */

void
mpt3sas_base_unmask_interrupts(struct MPT3SAS_ADAPTER *ioc)
{
 u32 him_register;

 him_register = ioc->base_readl(&ioc->chip->HostInterruptMask);
 him_register &= ~MPI2_HIM_RIM;
 writel(him_register, &ioc->chip->HostInterruptMask);
 ioc->mask_interrupts = 0;
}

union reply_descriptor {
 u64 word;
 struct {
  u32 low;
  u32 high;
 } u;
};

static u32 base_mod64(u64 dividend, u32 divisor)
{
 u32 remainder;

 if (!divisor)
  pr_err("mpt3sas: DIVISOR is zero, in div fn\n");
 remainder = do_div(dividend, divisor);
 return remainder;
}

/**
 * _base_process_reply_queue - Process reply descriptors from reply
 * descriptor post queue.
 * @reply_q: per IRQ's reply queue object.
 *
 * Return: number of reply descriptors processed from reply
 * descriptor queue.
 */

static int
_base_process_reply_queue(struct adapter_reply_queue *reply_q)
{
 union reply_descriptor rd;
 u64 completed_cmds;
 u8 request_descript_type;
 u16 smid;
 u8 cb_idx;
 u32 reply;
 u8 msix_index = reply_q->msix_index;
 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;
 Mpi2ReplyDescriptorsUnion_t *rpf;
 u8 rc;

 completed_cmds = 0;
 if (!atomic_add_unless(&reply_q->busy, 1, 1))
  return completed_cmds;

 rpf = &reply_q->reply_post_free[reply_q->reply_post_host_index];
 request_descript_type = rpf->Default.ReplyFlags
      & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
 if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) {
  atomic_dec(&reply_q->busy);
  return completed_cmds;
 }

 cb_idx = 0xFF;
 do {
  rd.word = le64_to_cpu(rpf->Words);
  if (rd.u.low == UINT_MAX || rd.u.high == UINT_MAX)
   goto out;
  reply = 0;
  smid = le16_to_cpu(rpf->Default.DescriptorTypeDependent1);
  if (request_descript_type ==
      MPI25_RPY_DESCRIPT_FLAGS_FAST_PATH_SCSI_IO_SUCCESS ||
      request_descript_type ==
      MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS ||
      request_descript_type ==
      MPI26_RPY_DESCRIPT_FLAGS_PCIE_ENCAPSULATED_SUCCESS) {
   cb_idx = _base_get_cb_idx(ioc, smid);
   if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
       (likely(mpt_callbacks[cb_idx] != NULL))) {
    rc = mpt_callbacks[cb_idx](ioc, smid,
        msix_index, 0);
    if (rc)
     mpt3sas_base_free_smid(ioc, smid);
   }
  } else if (request_descript_type ==
      MPI2_RPY_DESCRIPT_FLAGS_ADDRESS_REPLY) {
   reply = le32_to_cpu(
       rpf->AddressReply.ReplyFrameAddress);
   if (reply > ioc->reply_dma_max_address ||
       reply < ioc->reply_dma_min_address)
    reply = 0;
   if (smid) {
    cb_idx = _base_get_cb_idx(ioc, smid);
    if ((likely(cb_idx < MPT_MAX_CALLBACKS)) &&
        (likely(mpt_callbacks[cb_idx] != NULL))) {
     rc = mpt_callbacks[cb_idx](ioc, smid,
         msix_index, reply);
     if (reply)
      _base_display_reply_info(ioc,
          smid, msix_index, reply);
     if (rc)
      mpt3sas_base_free_smid(ioc,
          smid);
    }
   } else {
    _base_async_event(ioc, msix_index, reply);
   }

   /* reply free queue handling */
   if (reply) {
    ioc->reply_free_host_index =
        (ioc->reply_free_host_index ==
        (ioc->reply_free_queue_depth - 1)) ?
        0 : ioc->reply_free_host_index + 1;
    ioc->reply_free[ioc->reply_free_host_index] =
        cpu_to_le32(reply);
    if (ioc->is_mcpu_endpoint)
     _base_clone_reply_to_sys_mem(ioc,
      reply,
      ioc->reply_free_host_index);
    writel(ioc->reply_free_host_index,
        &ioc->chip->ReplyFreeHostIndex);
   }
  }

  rpf->Words = cpu_to_le64(ULLONG_MAX);
  reply_q->reply_post_host_index =
      (reply_q->reply_post_host_index ==
      (ioc->reply_post_queue_depth - 1)) ? 0 :
      reply_q->reply_post_host_index + 1;
  request_descript_type =
      reply_q->reply_post_free[reply_q->reply_post_host_index].
      Default.ReplyFlags & MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
  completed_cmds++;
  /* Update the reply post host index after continuously
 * processing the threshold number of Reply Descriptors.
 * So that FW can find enough entries to post the Reply
 * Descriptors in the reply descriptor post queue.
 */

  if (completed_cmds >= ioc->thresh_hold) {
   if (ioc->combined_reply_queue) {
    writel(reply_q->reply_post_host_index |
      ((msix_index  & 7) <<
       MPI2_RPHI_MSIX_INDEX_SHIFT),
        ioc->replyPostRegisterIndex[msix_index/8]);
   } else {
    writel(reply_q->reply_post_host_index |
      (msix_index <<
       MPI2_RPHI_MSIX_INDEX_SHIFT),
      &ioc->chip->ReplyPostHostIndex);
   }
   if (!reply_q->is_iouring_poll_q &&
       !reply_q->irq_poll_scheduled) {
    reply_q->irq_poll_scheduled = true;
    irq_poll_sched(&reply_q->irqpoll);
   }
   atomic_dec(&reply_q->busy);
   return completed_cmds;
  }
  if (request_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
   goto out;
  if (!reply_q->reply_post_host_index)
   rpf = reply_q->reply_post_free;
  else
   rpf++;
 } while (1);

 out:

 if (!completed_cmds) {
  atomic_dec(&reply_q->busy);
  return completed_cmds;
 }

 if (ioc->is_warpdrive) {
  writel(reply_q->reply_post_host_index,
  ioc->reply_post_host_index[msix_index]);
  atomic_dec(&reply_q->busy);
  return completed_cmds;
 }

 /* Update Reply Post Host Index.
 * For those HBA's which support combined reply queue feature
 * 1. Get the correct Supplemental Reply Post Host Index Register.
 *    i.e. (msix_index / 8)th entry from Supplemental Reply Post Host
 *    Index Register address bank i.e replyPostRegisterIndex[],
 * 2. Then update this register with new reply host index value
 *    in ReplyPostIndex field and the MSIxIndex field with
 *    msix_index value reduced to a value between 0 and 7,
 *    using a modulo 8 operation. Since each Supplemental Reply Post
 *    Host Index Register supports 8 MSI-X vectors.
 *
 * For other HBA's just update the Reply Post Host Index register with
 * new reply host index value in ReplyPostIndex Field and msix_index
 * value in MSIxIndex field.
 */

 if (ioc->combined_reply_queue)
  writel(reply_q->reply_post_host_index | ((msix_index  & 7) <<
   MPI2_RPHI_MSIX_INDEX_SHIFT),
   ioc->replyPostRegisterIndex[msix_index/8]);
 else
  writel(reply_q->reply_post_host_index | (msix_index <<
   MPI2_RPHI_MSIX_INDEX_SHIFT),
   &ioc->chip->ReplyPostHostIndex);
 atomic_dec(&reply_q->busy);
 return completed_cmds;
}

/**
 * mpt3sas_blk_mq_poll - poll the blk mq poll queue
 * @shost: Scsi_Host object
 * @queue_num: hw ctx queue number
 *
 * Return number of entries that has been processed from poll queue.
 */

int mpt3sas_blk_mq_poll(struct Scsi_Host *shost, unsigned int queue_num)
{
 struct MPT3SAS_ADAPTER *ioc =
     (struct MPT3SAS_ADAPTER *)shost->hostdata;
 struct adapter_reply_queue *reply_q;
 int num_entries = 0;
 int qid = queue_num - ioc->iopoll_q_start_index;

 if (atomic_read(&ioc->io_uring_poll_queues[qid].pause) ||
     !atomic_add_unless(&ioc->io_uring_poll_queues[qid].busy, 1, 1))
  return 0;

 reply_q = ioc->io_uring_poll_queues[qid].reply_q;

 num_entries = _base_process_reply_queue(reply_q);
 atomic_dec(&ioc->io_uring_poll_queues[qid].busy);

 return num_entries;
}

/**
 * _base_interrupt - MPT adapter (IOC) specific interrupt handler.
 * @irq: irq number (not used)
 * @bus_id: bus identifier cookie == pointer to MPT_ADAPTER structure
 *
 * Return: IRQ_HANDLED if processed, else IRQ_NONE.
 */

static irqreturn_t
_base_interrupt(int irq, void *bus_id)
{
 struct adapter_reply_queue *reply_q = bus_id;
 struct MPT3SAS_ADAPTER *ioc = reply_q->ioc;

 if (ioc->mask_interrupts)
  return IRQ_NONE;
 if (reply_q->irq_poll_scheduled)
  return IRQ_HANDLED;
 return ((_base_process_reply_queue(reply_q) > 0) ?
   IRQ_HANDLED : IRQ_NONE);
}

/**
 * _base_irqpoll - IRQ poll callback handler
 * @irqpoll: irq_poll object
 * @budget: irq poll weight
 *
 * Return: number of reply descriptors processed
 */

static int
_base_irqpoll(struct irq_poll *irqpoll, int budget)
{
 struct adapter_reply_queue *reply_q;
 int num_entries = 0;

 reply_q = container_of(irqpoll, struct adapter_reply_queue,
   irqpoll);
 if (reply_q->irq_line_enable) {
  disable_irq_nosync(reply_q->os_irq);
  reply_q->irq_line_enable = false;
 }
 num_entries = _base_process_reply_queue(reply_q);
 if (num_entries < budget) {
  irq_poll_complete(irqpoll);
  reply_q->irq_poll_scheduled = false;
  reply_q->irq_line_enable = true;
  enable_irq(reply_q->os_irq);
  /*
 * Go for one more round of processing the
 * reply descriptor post queue in case the HBA
 * Firmware has posted some reply descriptors
 * while reenabling the IRQ.
 */

  _base_process_reply_queue(reply_q);
 }

 return num_entries;
}

/**
 * _base_init_irqpolls - initliaze IRQ polls
 * @ioc: per adapter object
 *
 * Return: nothing
 */

static void
_base_init_irqpolls(struct MPT3SAS_ADAPTER *ioc)
{
 struct adapter_reply_queue *reply_q, *next;

 if (list_empty(&ioc->reply_queue_list))
  return;

 list_for_each_entry_safe(reply_q, next, &ioc->reply_queue_list, list) {
  if (reply_q->is_iouring_poll_q)
   continue;
  irq_poll_init(&reply_q->irqpoll,
   ioc->hba_queue_depth/4, _base_irqpoll);
  reply_q->irq_poll_scheduled = false;
  reply_q->irq_line_enable = true;
  reply_q->os_irq = pci_irq_vector(ioc->pdev,
      reply_q->msix_index);
 }
}

/**
 * _base_is_controller_msix_enabled - is controller support muli-reply queues
 * @ioc: per adapter object
 *
 * Return: Whether or not MSI/X is enabled.
 */

static inline int
_base_is_controller_msix_enabled(struct MPT3SAS_ADAPTER *ioc)
{
 return (ioc->facts.IOCCapabilities &
     MPI2_IOCFACTS_CAPABILITY_MSI_X_INDEX) && ioc->msix_enable;
}

/**
 * mpt3sas_base_sync_reply_irqs - flush pending MSIX interrupts
 * @ioc: per adapter object
 * @poll: poll over reply descriptor pools incase interrupt for
 * timed-out SCSI command got delayed
 * Context: non-ISR context
 *
 * Called when a Task Management request has completed.
 */

void
mpt3sas_base_sync_reply_irqs(struct MPT3SAS_ADAPTER *ioc, u8 poll)
{
 struct adapter_reply_queue *reply_q;

 /* If MSIX capability is turned off
 * then multi-queues are not enabled
 */

 if (!_base_is_controller_msix_enabled(ioc))
  return;

 list_for_each_entry(reply_q, &ioc->reply_queue_list, list) {
  if (ioc->shost_recovery || ioc->remove_host ||
    ioc->pci_error_recovery)
   return;
  /* TMs are on msix_index == 0 */
  if (reply_q->msix_index == 0)
   continue;

  if (reply_q->is_iouring_poll_q) {
   _base_process_reply_queue(reply_q);
   continue;
  }

  synchronize_irq(pci_irq_vector(ioc->pdev, reply_q->msix_index));
  if (reply_q->irq_poll_scheduled) {
   /* Calling irq_poll_disable will wait for any pending
 * callbacks to have completed.
 */

   irq_poll_disable(&reply_q->irqpoll);
   irq_poll_enable(&reply_q->irqpoll);
   /* check how the scheduled poll has ended,
 * clean up only if necessary
 */

   if (reply_q->irq_poll_scheduled) {
    reply_q->irq_poll_scheduled = false;
    reply_q->irq_line_enable = true;
    enable_irq(reply_q->os_irq);
   }
  }

  if (poll)
   _base_process_reply_queue(reply_q);
 }
}

/**
 * mpt3sas_base_release_callback_handler - clear interrupt callback handler
 * @cb_idx: callback index
 */

void
mpt3sas_base_release_callback_handler(u8 cb_idx)
{
 mpt_callbacks[cb_idx] = NULL;
}

/**
 * mpt3sas_base_register_callback_handler - obtain index for the interrupt callback handler
 * @cb_func: callback function
 *
 * Return: Index of @cb_func.
 */

u8
mpt3sas_base_register_callback_handler(MPT_CALLBACK cb_func)
{
 u8 cb_idx;

 for (cb_idx = MPT_MAX_CALLBACKS-1; cb_idx; cb_idx--)
  if (mpt_callbacks[cb_idx] == NULL)
   break;

 mpt_callbacks[cb_idx] = cb_func;
 return cb_idx;
}

/**
 * mpt3sas_base_initialize_callback_handler - initialize the interrupt callback handler
 */

void
mpt3sas_base_initialize_callback_handler(void)
{
 u8 cb_idx;

 for (cb_idx = 0; cb_idx < MPT_MAX_CALLBACKS; cb_idx++)
  mpt3sas_base_release_callback_handler(cb_idx);
}


/**
 * _base_build_zero_len_sge - build zero length sg entry
 * @ioc: per adapter object
 * @paddr: virtual address for SGE
 *
 * Create a zero length scatter gather entry to insure the IOCs hardware has
 * something to use if the target device goes brain dead and tries
 * to send data even when none is asked for.
 */

static void
_base_build_zero_len_sge(struct MPT3SAS_ADAPTER *ioc, void *paddr)
{
 u32 flags_length = (u32)((MPI2_SGE_FLAGS_LAST_ELEMENT |
     MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_END_OF_LIST |
     MPI2_SGE_FLAGS_SIMPLE_ELEMENT) <<
     MPI2_SGE_FLAGS_SHIFT);
 ioc->base_add_sg_single(paddr, flags_length, -1);
}

/**
 * _base_add_sg_single_32 - Place a simple 32 bit SGE at address pAddr.
 * @paddr: virtual address for SGE
 * @flags_length: SGE flags and data transfer length
 * @dma_addr: Physical address
 */

static void
_base_add_sg_single_32(void *paddr, u32 flags_length, dma_addr_t dma_addr)
{
 Mpi2SGESimple32_t *sgel = paddr;

 flags_length |= (MPI2_SGE_FLAGS_32_BIT_ADDRESSING |
     MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
 sgel->FlagsLength = cpu_to_le32(flags_length);
 sgel->Address = cpu_to_le32(dma_addr);
}


/**
 * _base_add_sg_single_64 - Place a simple 64 bit SGE at address pAddr.
 * @paddr: virtual address for SGE
 * @flags_length: SGE flags and data transfer length
 * @dma_addr: Physical address
 */

static void
_base_add_sg_single_64(void *paddr, u32 flags_length, dma_addr_t dma_addr)
{
 Mpi2SGESimple64_t *sgel = paddr;

 flags_length |= (MPI2_SGE_FLAGS_64_BIT_ADDRESSING |
     MPI2_SGE_FLAGS_SYSTEM_ADDRESS) << MPI2_SGE_FLAGS_SHIFT;
 sgel->FlagsLength = cpu_to_le32(flags_length);
 sgel->Address = cpu_to_le64(dma_addr);
}

/**
 * _base_get_chain_buffer_tracker - obtain chain tracker
 * @ioc: per adapter object
 * @scmd: SCSI commands of the IO request
 *
 * Return: chain tracker from chain_lookup table using key as
 * smid and smid's chain_offset.
 */

static struct chain_tracker *
_base_get_chain_buffer_tracker(struct MPT3SAS_ADAPTER *ioc,
          struct scsi_cmnd *scmd)
{
 struct chain_tracker *chain_req;
 struct scsiio_tracker *st = scsi_cmd_priv(scmd);
 u16 smid = st->smid;
 u8 chain_offset =
    atomic_read(&ioc->chain_lookup[smid - 1].chain_offset);

 if (chain_offset == ioc->chains_needed_per_io)
  return NULL;

 chain_req = &ioc->chain_lookup[smid - 1].chains_per_smid[chain_offset];
 atomic_inc(&ioc->chain_lookup[smid - 1].chain_offset);
 return chain_req;
}


/**
 * _base_build_sg - build generic sg
 * @ioc: per adapter object
 * @psge: virtual address for SGE
 * @data_out_dma: physical address for WRITES
 * @data_out_sz: data xfer size for WRITES
 * @data_in_dma: physical address for READS
 * @data_in_sz: data xfer size for READS
 */

static void
_base_build_sg(struct MPT3SAS_ADAPTER *ioc, void *psge,
 dma_addr_t data_out_dma, size_t data_out_sz, dma_addr_t data_in_dma,
 size_t data_in_sz)
{
 u32 sgl_flags;

 if (!data_out_sz && !data_in_sz) {
  _base_build_zero_len_sge(ioc, psge);
  return;
 }

 if (data_out_sz && data_in_sz) {
  /* WRITE sgel first */
  sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
      MPI2_SGE_FLAGS_END_OF_BUFFER | MPI2_SGE_FLAGS_HOST_TO_IOC);
  sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  ioc->base_add_sg_single(psge, sgl_flags |
      data_out_sz, data_out_dma);

  /* incr sgel */
  psge += ioc->sge_size;

  /* READ sgel last */
  sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
      MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
      MPI2_SGE_FLAGS_END_OF_LIST);
  sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  ioc->base_add_sg_single(psge, sgl_flags |
      data_in_sz, data_in_dma);
 } else if (data_out_sz) /* WRITE */ {
  sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
      MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
      MPI2_SGE_FLAGS_END_OF_LIST | MPI2_SGE_FLAGS_HOST_TO_IOC);
  sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  ioc->base_add_sg_single(psge, sgl_flags |
      data_out_sz, data_out_dma);
 } else if (data_in_sz) /* READ */ {
  sgl_flags = (MPI2_SGE_FLAGS_SIMPLE_ELEMENT |
      MPI2_SGE_FLAGS_LAST_ELEMENT | MPI2_SGE_FLAGS_END_OF_BUFFER |
      MPI2_SGE_FLAGS_END_OF_LIST);
  sgl_flags = sgl_flags << MPI2_SGE_FLAGS_SHIFT;
  ioc->base_add_sg_single(psge, sgl_flags |
      data_in_sz, data_in_dma);
 }
}

/* IEEE format sgls */

/**
 * _base_build_nvme_prp - This function is called for NVMe end devices to build
 *                        a native SGL (NVMe PRP).
 * @ioc: per adapter object
 * @smid: system request message index for getting asscociated SGL
 * @nvme_encap_request: the NVMe request msg frame pointer
 * @data_out_dma: physical address for WRITES
 * @data_out_sz: data xfer size for WRITES
 * @data_in_dma: physical address for READS
 * @data_in_sz: data xfer size for READS
 *
 * The native SGL is built starting in the first PRP
 * entry of the NVMe message (PRP1).  If the data buffer is small enough to be
 * described entirely using PRP1, then PRP2 is not used.  If needed, PRP2 is
 * used to describe a larger data buffer.  If the data buffer is too large to
 * describe using the two PRP entriess inside the NVMe message, then PRP1
 * describes the first data memory segment, and PRP2 contains a pointer to a PRP
 * list located elsewhere in memory to describe the remaining data memory
 * segments.  The PRP list will be contiguous.
 *
 * The native SGL for NVMe devices is a Physical Region Page (PRP).  A PRP
 * consists of a list of PRP entries to describe a number of noncontigous
 * physical memory segments as a single memory buffer, just as a SGL does.  Note
 * however, that this function is only used by the IOCTL call, so the memory
 * given will be guaranteed to be contiguous.  There is no need to translate
 * non-contiguous SGL into a PRP in this case.  All PRPs will describe
 * contiguous space that is one page size each.
 *
 * Each NVMe message contains two PRP entries.  The first (PRP1) either contains
 * a PRP list pointer or a PRP element, depending upon the command.  PRP2
 * contains the second PRP element if the memory being described fits within 2
 * PRP entries, or a PRP list pointer if the PRP spans more than two entries.
 *
 * A PRP list pointer contains the address of a PRP list, structured as a linear
 * array of PRP entries.  Each PRP entry in this list describes a segment of
 * physical memory.
 *
--> --------------------

--> maximum size reached

--> --------------------

Messung V0.5
C=96 H=91 G=93

¤ Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.0.22Bemerkung:  (vorverarbeitet)  ¤

*Bot Zugriff






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.