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

Quelle  qla_iocb.c   Sprache: C

 
// SPDX-License-Identifier: GPL-2.0-only
/*
 * QLogic Fibre Channel HBA Driver
 * Copyright (c)  2003-2014 QLogic Corporation
 */

#include "qla_def.h"
#include "qla_target.h"

#include <linux/blkdev.h>
#include <linux/delay.h>

#include <scsi/scsi_tcq.h>

static int qla_start_scsi_type6(srb_t *sp);
/**
 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
 * @sp: SCSI command
 *
 * Returns the proper CF_* direction based on CDB.
 */

static inline uint16_t
qla2x00_get_cmd_direction(srb_t *sp)
{
 uint16_t cflags;
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 struct scsi_qla_host *vha = sp->vha;

 cflags = 0;

 /* Set transfer direction */
 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  cflags = CF_WRITE;
  vha->qla_stats.output_bytes += scsi_bufflen(cmd);
  vha->qla_stats.output_requests++;
 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  cflags = CF_READ;
  vha->qla_stats.input_bytes += scsi_bufflen(cmd);
  vha->qla_stats.input_requests++;
 }
 return (cflags);
}

/**
 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
 * Continuation Type 0 IOCBs to allocate.
 *
 * @dsds: number of data segment descriptors needed
 *
 * Returns the number of IOCB entries needed to store @dsds.
 */

uint16_t
qla2x00_calc_iocbs_32(uint16_t dsds)
{
 uint16_t iocbs;

 iocbs = 1;
 if (dsds > 3) {
  iocbs += (dsds - 3) / 7;
  if ((dsds - 3) % 7)
   iocbs++;
 }
 return (iocbs);
}

/**
 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
 * Continuation Type 1 IOCBs to allocate.
 *
 * @dsds: number of data segment descriptors needed
 *
 * Returns the number of IOCB entries needed to store @dsds.
 */

uint16_t
qla2x00_calc_iocbs_64(uint16_t dsds)
{
 uint16_t iocbs;

 iocbs = 1;
 if (dsds > 2) {
  iocbs += (dsds - 2) / 5;
  if ((dsds - 2) % 5)
   iocbs++;
 }
 return (iocbs);
}

/**
 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
 * @vha: HA context
 *
 * Returns a pointer to the Continuation Type 0 IOCB packet.
 */

static inline cont_entry_t *
qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
{
 cont_entry_t *cont_pkt;
 struct req_que *req = vha->req;
 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else {
  req->ring_ptr++;
 }

 cont_pkt = (cont_entry_t *)req->ring_ptr;

 /* Load packet defaults. */
 put_unaligned_le32(CONTINUE_TYPE, &cont_pkt->entry_type);

 return (cont_pkt);
}

/**
 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
 * @vha: HA context
 * @req: request queue
 *
 * Returns a pointer to the continuation type 1 IOCB packet.
 */

cont_a64_entry_t *
qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
{
 cont_a64_entry_t *cont_pkt;

 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else {
  req->ring_ptr++;
 }

 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;

 /* Load packet defaults. */
 put_unaligned_le32(IS_QLAFX00(vha->hw) ? CONTINUE_A64_TYPE_FX00 :
      CONTINUE_A64_TYPE, &cont_pkt->entry_type);

 return (cont_pkt);
}

inline int
qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
{
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);

 /* We always use DIFF Bundling for best performance */
 *fw_prot_opts = 0;

 /* Translate SCSI opcode to a protection opcode */
 switch (scsi_get_prot_op(cmd)) {
 case SCSI_PROT_READ_STRIP:
  *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  break;
 case SCSI_PROT_WRITE_INSERT:
  *fw_prot_opts |= PO_MODE_DIF_INSERT;
  break;
 case SCSI_PROT_READ_INSERT:
  *fw_prot_opts |= PO_MODE_DIF_INSERT;
  break;
 case SCSI_PROT_WRITE_STRIP:
  *fw_prot_opts |= PO_MODE_DIF_REMOVE;
  break;
 case SCSI_PROT_READ_PASS:
 case SCSI_PROT_WRITE_PASS:
  if (cmd->prot_flags & SCSI_PROT_IP_CHECKSUM)
   *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
  else
   *fw_prot_opts |= PO_MODE_DIF_PASS;
  break;
 default/* Normal Request */
  *fw_prot_opts |= PO_MODE_DIF_PASS;
  break;
 }

 if (!(cmd->prot_flags & SCSI_PROT_GUARD_CHECK))
  *fw_prot_opts |= PO_DISABLE_GUARD_CHECK;

 return scsi_prot_sg_count(cmd);
}

/*
 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
 * capable IOCB types.
 *
 * @sp: SRB command to process
 * @cmd_pkt: Command type 2 IOCB
 * @tot_dsds: Total number of segments to transfer
 */

void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
    uint16_t tot_dsds)
{
 uint16_t avail_dsds;
 struct dsd32 *cur_dsd;
 scsi_qla_host_t *vha;
 struct scsi_cmnd *cmd;
 struct scatterlist *sg;
 int i;

 cmd = GET_CMD_SP(sp);

 /* Update entry type to indicate Command Type 2 IOCB */
 put_unaligned_le32(COMMAND_TYPE, &cmd_pkt->entry_type);

 /* No data transfer */
 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return;
 }

 vha = sp->vha;
 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));

 /* Three DSDs are available in the Command Type 2 IOCB */
 avail_dsds = ARRAY_SIZE(cmd_pkt->dsd32);
 cur_dsd = cmd_pkt->dsd32;

 /* Load data segments */
 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  cont_entry_t *cont_pkt;

  /* Allocate additional continuation packets? */
  if (avail_dsds == 0) {
   /*
 * Seven DSDs are available in the Continuation
 * Type 0 IOCB.
 */

   cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
   cur_dsd = cont_pkt->dsd;
   avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
  }

  append_dsd32(&cur_dsd, sg);
  avail_dsds--;
 }
}

/**
 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
 * capable IOCB types.
 *
 * @sp: SRB command to process
 * @cmd_pkt: Command type 3 IOCB
 * @tot_dsds: Total number of segments to transfer
 */

void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
    uint16_t tot_dsds)
{
 uint16_t avail_dsds;
 struct dsd64 *cur_dsd;
 scsi_qla_host_t *vha;
 struct scsi_cmnd *cmd;
 struct scatterlist *sg;
 int i;

 cmd = GET_CMD_SP(sp);

 /* Update entry type to indicate Command Type 3 IOCB */
 put_unaligned_le32(COMMAND_A64_TYPE, &cmd_pkt->entry_type);

 /* No data transfer */
 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return;
 }

 vha = sp->vha;
 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));

 /* Two DSDs are available in the Command Type 3 IOCB */
 avail_dsds = ARRAY_SIZE(cmd_pkt->dsd64);
 cur_dsd = cmd_pkt->dsd64;

 /* Load data segments */
 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  cont_a64_entry_t *cont_pkt;

  /* Allocate additional continuation packets? */
  if (avail_dsds == 0) {
   /*
 * Five DSDs are available in the Continuation
 * Type 1 IOCB.
 */

   cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
   cur_dsd = cont_pkt->dsd;
   avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
  }

  append_dsd64(&cur_dsd, sg);
  avail_dsds--;
 }
}

/*
 * Find the first handle that is not in use, starting from
 * req->current_outstanding_cmd + 1. The caller must hold the lock that is
 * associated with @req.
 */

uint32_t qla2xxx_get_next_handle(struct req_que *req)
{
 uint32_t index, handle = req->current_outstanding_cmd;

 for (index = 1; index < req->num_outstanding_cmds; index++) {
  handle++;
  if (handle == req->num_outstanding_cmds)
   handle = 1;
  if (!req->outstanding_cmds[handle])
   return handle;
 }

 return 0;
}

/**
 * qla2x00_start_scsi() - Send a SCSI command to the ISP
 * @sp: command to send to the ISP
 *
 * Returns non-zero if a failure occurred, else zero.
 */

int
qla2x00_start_scsi(srb_t *sp)
{
 int  nseg;
 unsigned long   flags;
 scsi_qla_host_t *vha;
 struct scsi_cmnd *cmd;
 uint32_t *clr_ptr;
 uint32_t handle;
 cmd_entry_t *cmd_pkt;
 uint16_t cnt;
 uint16_t req_cnt;
 uint16_t tot_dsds;
 struct device_reg_2xxx __iomem *reg;
 struct qla_hw_data *ha;
 struct req_que *req;
 struct rsp_que *rsp;

 /* Setup device pointers. */
 vha = sp->vha;
 ha = vha->hw;
 reg = &ha->iobase->isp;
 cmd = GET_CMD_SP(sp);
 req = ha->req_q_map[0];
 rsp = ha->rsp_q_map[0];
 /* So we know we haven't pci_map'ed anything yet */
 tot_dsds = 0;

 /* Send marker if required */
 if (vha->marker_needed != 0) {
  if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
      QLA_SUCCESS) {
   return (QLA_FUNCTION_FAILED);
  }
  vha->marker_needed = 0;
 }

 /* Acquire ring specific lock */
 spin_lock_irqsave(&ha->hardware_lock, flags);

 handle = qla2xxx_get_next_handle(req);
 if (handle == 0)
  goto queuing_error;

 /* Map the sg table so we have an accurate count of sg entries needed */
 if (scsi_sg_count(cmd)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
      scsi_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
 } else
  nseg = 0;

 tot_dsds = nseg;

 /* Calculate the number of request entries needed. */
 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
 if (req->cnt < (req_cnt + 2)) {
  cnt = rd_reg_word_relaxed(ISP_REQ_Q_OUT(ha, reg));
  if (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
       (req->ring_index - cnt);
  /* If still no head room then bail out */
  if (req->cnt < (req_cnt + 2))
   goto queuing_error;
 }

 /* Build command packet */
 req->current_outstanding_cmd = handle;
 req->outstanding_cmds[handle] = sp;
 sp->handle = handle;
 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
 req->cnt -= req_cnt;

 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
 cmd_pkt->handle = handle;
 /* Zero out remaining portion of packet. */
 clr_ptr = (uint32_t *)cmd_pkt + 2;
 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);

 /* Set target ID and LUN number*/
 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
 cmd_pkt->control_flags = cpu_to_le16(CF_SIMPLE_TAG);

 /* Load SCSI command packet. */
 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));

 /* Build IOCB segments */
 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);

 /* Set total data segment count. */
 cmd_pkt->entry_count = (uint8_t)req_cnt;
 wmb();

 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else
  req->ring_ptr++;

 sp->flags |= SRB_DMA_VALID;

 /* Set chip new ring index. */
 wrt_reg_word(ISP_REQ_Q_IN(ha, reg), req->ring_index);
 rd_reg_word_relaxed(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */

 /* Manage unprocessed RIO/ZIO commands in response queue. */
 if (vha->flags.process_response_queue &&
     rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  qla2x00_process_response_queue(rsp);

 spin_unlock_irqrestore(&ha->hardware_lock, flags);
 return (QLA_SUCCESS);

queuing_error:
 if (tot_dsds)
  scsi_dma_unmap(cmd);

 spin_unlock_irqrestore(&ha->hardware_lock, flags);

 return (QLA_FUNCTION_FAILED);
}

/**
 * qla2x00_start_iocbs() - Execute the IOCB command
 * @vha: HA context
 * @req: request queue
 */

void
qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
{
 struct qla_hw_data *ha = vha->hw;
 device_reg_t *reg = ISP_QUE_REG(ha, req->id);

 if (IS_P3P_TYPE(ha)) {
  qla82xx_start_iocbs(vha);
 } else {
  /* Adjust ring index. */
  req->ring_index++;
  if (req->ring_index == req->length) {
   req->ring_index = 0;
   req->ring_ptr = req->ring;
  } else
   req->ring_ptr++;

  /* Set chip new ring index. */
  if (ha->mqenable || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
   wrt_reg_dword(req->req_q_in, req->ring_index);
  } else if (IS_QLA83XX(ha)) {
   wrt_reg_dword(req->req_q_in, req->ring_index);
   rd_reg_dword_relaxed(&ha->iobase->isp24.hccr);
  } else if (IS_QLAFX00(ha)) {
   wrt_reg_dword(®->ispfx00.req_q_in, req->ring_index);
   rd_reg_dword_relaxed(®->ispfx00.req_q_in);
   QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
  } else if (IS_FWI2_CAPABLE(ha)) {
   wrt_reg_dword(®->isp24.req_q_in, req->ring_index);
   rd_reg_dword_relaxed(®->isp24.req_q_in);
  } else {
   wrt_reg_word(ISP_REQ_Q_IN(ha, ®->isp),
    req->ring_index);
   rd_reg_word_relaxed(ISP_REQ_Q_IN(ha, ®->isp));
  }
 }
}

/**
 * __qla2x00_marker() - Send a marker IOCB to the firmware.
 * @vha: HA context
 * @qpair: queue pair pointer
 * @loop_id: loop ID
 * @lun: LUN
 * @type: marker modifier
 *
 * Can be called from both normal and interrupt context.
 *
 * Returns non-zero if a failure occurred, else zero.
 */

static int
__qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
    uint16_t loop_id, uint64_t lun, uint8_t type)
{
 mrk_entry_t *mrk;
 struct mrk_entry_24xx *mrk24 = NULL;
 struct req_que *req = qpair->req;
 struct qla_hw_data *ha = vha->hw;
 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);

 mrk = (mrk_entry_t *)__qla2x00_alloc_iocbs(qpair, NULL);
 if (mrk == NULL) {
  ql_log(ql_log_warn, base_vha, 0x3026,
      "Failed to allocate Marker IOCB.\n");

  return (QLA_FUNCTION_FAILED);
 }

 mrk24 = (struct mrk_entry_24xx *)mrk;

 mrk->entry_type = MARKER_TYPE;
 mrk->modifier = type;
 if (type != MK_SYNC_ALL) {
  if (IS_FWI2_CAPABLE(ha)) {
   mrk24->nport_handle = cpu_to_le16(loop_id);
   int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
   host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
   mrk24->vp_index = vha->vp_idx;
  } else {
   SET_TARGET_ID(ha, mrk->target, loop_id);
   mrk->lun = cpu_to_le16((uint16_t)lun);
  }
 }

 if (IS_FWI2_CAPABLE(ha))
  mrk24->handle = QLA_SKIP_HANDLE;

 wmb();

 qla2x00_start_iocbs(vha, req);

 return (QLA_SUCCESS);
}

int
qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
    uint16_t loop_id, uint64_t lun, uint8_t type)
{
 int ret;
 unsigned long flags = 0;

 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
 ret = __qla2x00_marker(vha, qpair, loop_id, lun, type);
 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);

 return (ret);
}

/*
 * qla2x00_issue_marker
 *
 * Issue marker
 * Caller CAN have hardware lock held as specified by ha_locked parameter.
 * Might release it, then reaquire.
 */

int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
{
 if (ha_locked) {
  if (__qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
     MK_SYNC_ALL) != QLA_SUCCESS)
   return QLA_FUNCTION_FAILED;
 } else {
  if (qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
     MK_SYNC_ALL) != QLA_SUCCESS)
   return QLA_FUNCTION_FAILED;
 }
 vha->marker_needed = 0;

 return QLA_SUCCESS;
}

static inline int
qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
 uint16_t tot_dsds)
{
 struct dsd64 *cur_dsd = NULL, *next_dsd;
 struct scsi_cmnd *cmd;
 struct scatterlist *cur_seg;
 uint8_t avail_dsds;
 uint8_t first_iocb = 1;
 uint32_t dsd_list_len;
 struct dsd_dma *dsd_ptr;
 struct ct6_dsd *ctx;
 struct qla_qpair *qpair = sp->qpair;

 cmd = GET_CMD_SP(sp);

 /* Update entry type to indicate Command Type 3 IOCB */
 put_unaligned_le32(COMMAND_TYPE_6, &cmd_pkt->entry_type);

 /* No data transfer */
 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE ||
     tot_dsds == 0) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return 0;
 }

 /* Set transfer direction */
 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
  qpair->counters.output_bytes += scsi_bufflen(cmd);
  qpair->counters.output_requests++;
 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
  qpair->counters.input_bytes += scsi_bufflen(cmd);
  qpair->counters.input_requests++;
 }

 cur_seg = scsi_sglist(cmd);
 ctx = &sp->u.scmd.ct6_ctx;

 while (tot_dsds) {
  avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
      QLA_DSDS_PER_IOCB : tot_dsds;
  tot_dsds -= avail_dsds;
  dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;

  dsd_ptr = list_first_entry(&qpair->dsd_list, struct dsd_dma, list);
  next_dsd = dsd_ptr->dsd_addr;
  list_del(&dsd_ptr->list);
  qpair->dsd_avail--;
  list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
  ctx->dsd_use_cnt++;
  qpair->dsd_inuse++;

  if (first_iocb) {
   first_iocb = 0;
   put_unaligned_le64(dsd_ptr->dsd_list_dma,
        &cmd_pkt->fcp_dsd.address);
   cmd_pkt->fcp_dsd.length = cpu_to_le32(dsd_list_len);
  } else {
   put_unaligned_le64(dsd_ptr->dsd_list_dma,
        &cur_dsd->address);
   cur_dsd->length = cpu_to_le32(dsd_list_len);
   cur_dsd++;
  }
  cur_dsd = next_dsd;
  while (avail_dsds) {
   append_dsd64(&cur_dsd, cur_seg);
   cur_seg = sg_next(cur_seg);
   avail_dsds--;
  }
 }

 /* Null termination */
 cur_dsd->address = 0;
 cur_dsd->length = 0;
 cur_dsd++;
 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
 return 0;
}

/*
 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
 * for Command Type 6.
 *
 * @dsds: number of data segment descriptors needed
 *
 * Returns the number of dsd list needed to store @dsds.
 */

static inline uint16_t
qla24xx_calc_dsd_lists(uint16_t dsds)
{
 uint16_t dsd_lists = 0;

 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
 if (dsds % QLA_DSDS_PER_IOCB)
  dsd_lists++;
 return dsd_lists;
}


/**
 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
 * IOCB types.
 *
 * @sp: SRB command to process
 * @cmd_pkt: Command type 3 IOCB
 * @tot_dsds: Total number of segments to transfer
 * @req: pointer to request queue
 */

inline void
qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
 uint16_t tot_dsds, struct req_que *req)
{
 uint16_t avail_dsds;
 struct dsd64 *cur_dsd;
 scsi_qla_host_t *vha;
 struct scsi_cmnd *cmd;
 struct scatterlist *sg;
 int i;
 struct qla_qpair *qpair = sp->qpair;

 cmd = GET_CMD_SP(sp);

 /* Update entry type to indicate Command Type 3 IOCB */
 put_unaligned_le32(COMMAND_TYPE_7, &cmd_pkt->entry_type);

 /* No data transfer */
 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return;
 }

 vha = sp->vha;

 /* Set transfer direction */
 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_WRITE_DATA);
  qpair->counters.output_bytes += scsi_bufflen(cmd);
  qpair->counters.output_requests++;
 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_READ_DATA);
  qpair->counters.input_bytes += scsi_bufflen(cmd);
  qpair->counters.input_requests++;
 }

 /* One DSD is available in the Command Type 3 IOCB */
 avail_dsds = 1;
 cur_dsd = &cmd_pkt->dsd;

 /* Load data segments */

 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
  cont_a64_entry_t *cont_pkt;

  /* Allocate additional continuation packets? */
  if (avail_dsds == 0) {
   /*
 * Five DSDs are available in the Continuation
 * Type 1 IOCB.
 */

   cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req);
   cur_dsd = cont_pkt->dsd;
   avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
  }

  append_dsd64(&cur_dsd, sg);
  avail_dsds--;
 }
}

struct fw_dif_context {
 __le32 ref_tag;
 __le16 app_tag;
 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
};

/*
 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
 *
 */

static inline void
qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
    unsigned int protcnt)
{
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);

 pkt->ref_tag = cpu_to_le32(scsi_prot_ref_tag(cmd));

 if (cmd->prot_flags & SCSI_PROT_REF_CHECK &&
     qla2x00_hba_err_chk_enabled(sp)) {
  pkt->ref_tag_mask[0] = 0xff;
  pkt->ref_tag_mask[1] = 0xff;
  pkt->ref_tag_mask[2] = 0xff;
  pkt->ref_tag_mask[3] = 0xff;
 }

 pkt->app_tag = cpu_to_le16(0);
 pkt->app_tag_mask[0] = 0x0;
 pkt->app_tag_mask[1] = 0x0;
}

int
qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
 uint32_t *partial)
{
 struct scatterlist *sg;
 uint32_t cumulative_partial, sg_len;
 dma_addr_t sg_dma_addr;

 if (sgx->num_bytes == sgx->tot_bytes)
  return 0;

 sg = sgx->cur_sg;
 cumulative_partial = sgx->tot_partial;

 sg_dma_addr = sg_dma_address(sg);
 sg_len = sg_dma_len(sg);

 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;

 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
  sgx->dma_len = (blk_sz - cumulative_partial);
  sgx->tot_partial = 0;
  sgx->num_bytes += blk_sz;
  *partial = 0;
 } else {
  sgx->dma_len = sg_len - sgx->bytes_consumed;
  sgx->tot_partial += sgx->dma_len;
  *partial = 1;
 }

 sgx->bytes_consumed += sgx->dma_len;

 if (sg_len == sgx->bytes_consumed) {
  sg = sg_next(sg);
  sgx->num_sg++;
  sgx->cur_sg = sg;
  sgx->bytes_consumed = 0;
 }

 return 1;
}

int
qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
 struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
{
 void *next_dsd;
 uint8_t avail_dsds = 0;
 uint32_t dsd_list_len;
 struct dsd_dma *dsd_ptr;
 struct scatterlist *sg_prot;
 struct dsd64 *cur_dsd = dsd;
 uint16_t used_dsds = tot_dsds;
 uint32_t prot_int; /* protection interval */
 uint32_t partial;
 struct qla2_sgx sgx;
 dma_addr_t sle_dma;
 uint32_t sle_dma_len, tot_prot_dma_len = 0;
 struct scsi_cmnd *cmd;

 memset(&sgx, 0, sizeof(struct qla2_sgx));
 if (sp) {
  cmd = GET_CMD_SP(sp);
  prot_int = scsi_prot_interval(cmd);

  sgx.tot_bytes = scsi_bufflen(cmd);
  sgx.cur_sg = scsi_sglist(cmd);
  sgx.sp = sp;

  sg_prot = scsi_prot_sglist(cmd);
 } else if (tc) {
  prot_int      = tc->blk_sz;
  sgx.tot_bytes = tc->bufflen;
  sgx.cur_sg    = tc->sg;
  sg_prot       = tc->prot_sg;
 } else {
  BUG();
  return 1;
 }

 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {

  sle_dma = sgx.dma_addr;
  sle_dma_len = sgx.dma_len;
alloc_and_fill:
  /* Allocate additional continuation packets? */
  if (avail_dsds == 0) {
   avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
     QLA_DSDS_PER_IOCB : used_dsds;
   dsd_list_len = (avail_dsds + 1) * 12;
   used_dsds -= avail_dsds;

   /* allocate tracking DS */
   dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
   if (!dsd_ptr)
    return 1;

   /* allocate new list */
   dsd_ptr->dsd_addr = next_dsd =
       dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
    &dsd_ptr->dsd_list_dma);

   if (!next_dsd) {
    /*
 * Need to cleanup only this dsd_ptr, rest
 * will be done by sp_free_dma()
 */

    kfree(dsd_ptr);
    return 1;
   }

   if (sp) {
    list_add_tail(&dsd_ptr->list,
           &sp->u.scmd.crc_ctx->dsd_list);

    sp->flags |= SRB_CRC_CTX_DSD_VALID;
   } else {
    list_add_tail(&dsd_ptr->list,
        &(tc->ctx->dsd_list));
    *tc->ctx_dsd_alloced = 1;
   }


   /* add new list to cmd iocb or last list */
   put_unaligned_le64(dsd_ptr->dsd_list_dma,
        &cur_dsd->address);
   cur_dsd->length = cpu_to_le32(dsd_list_len);
   cur_dsd = next_dsd;
  }
  put_unaligned_le64(sle_dma, &cur_dsd->address);
  cur_dsd->length = cpu_to_le32(sle_dma_len);
  cur_dsd++;
  avail_dsds--;

  if (partial == 0) {
   /* Got a full protection interval */
   sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
   sle_dma_len = 8;

   tot_prot_dma_len += sle_dma_len;
   if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
    tot_prot_dma_len = 0;
    sg_prot = sg_next(sg_prot);
   }

   partial = 1; /* So as to not re-enter this block */
   goto alloc_and_fill;
  }
 }
 /* Null termination */
 cur_dsd->address = 0;
 cur_dsd->length = 0;
 cur_dsd++;
 return 0;
}

int
qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp,
 struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
{
 void *next_dsd;
 uint8_t avail_dsds = 0;
 uint32_t dsd_list_len;
 struct dsd_dma *dsd_ptr;
 struct scatterlist *sg, *sgl;
 struct dsd64 *cur_dsd = dsd;
 int i;
 uint16_t used_dsds = tot_dsds;
 struct scsi_cmnd *cmd;

 if (sp) {
  cmd = GET_CMD_SP(sp);
  sgl = scsi_sglist(cmd);
 } else if (tc) {
  sgl = tc->sg;
 } else {
  BUG();
  return 1;
 }


 for_each_sg(sgl, sg, tot_dsds, i) {
  /* Allocate additional continuation packets? */
  if (avail_dsds == 0) {
   avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
     QLA_DSDS_PER_IOCB : used_dsds;
   dsd_list_len = (avail_dsds + 1) * 12;
   used_dsds -= avail_dsds;

   /* allocate tracking DS */
   dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
   if (!dsd_ptr)
    return 1;

   /* allocate new list */
   dsd_ptr->dsd_addr = next_dsd =
       dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
    &dsd_ptr->dsd_list_dma);

   if (!next_dsd) {
    /*
 * Need to cleanup only this dsd_ptr, rest
 * will be done by sp_free_dma()
 */

    kfree(dsd_ptr);
    return 1;
   }

   if (sp) {
    list_add_tail(&dsd_ptr->list,
           &sp->u.scmd.crc_ctx->dsd_list);

    sp->flags |= SRB_CRC_CTX_DSD_VALID;
   } else {
    list_add_tail(&dsd_ptr->list,
        &(tc->ctx->dsd_list));
    *tc->ctx_dsd_alloced = 1;
   }

   /* add new list to cmd iocb or last list */
   put_unaligned_le64(dsd_ptr->dsd_list_dma,
        &cur_dsd->address);
   cur_dsd->length = cpu_to_le32(dsd_list_len);
   cur_dsd = next_dsd;
  }
  append_dsd64(&cur_dsd, sg);
  avail_dsds--;

 }
 /* Null termination */
 cur_dsd->address = 0;
 cur_dsd->length = 0;
 cur_dsd++;
 return 0;
}

int
qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
 struct dsd64 *cur_dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
{
 struct dsd_dma *dsd_ptr = NULL, *dif_dsd, *nxt_dsd;
 struct scatterlist *sg, *sgl;
 struct crc_context *difctx = NULL;
 struct scsi_qla_host *vha;
 uint dsd_list_len;
 uint avail_dsds = 0;
 uint used_dsds = tot_dsds;
 bool dif_local_dma_alloc = false;
 bool direction_to_device = false;
 int i;

 if (sp) {
  struct scsi_cmnd *cmd = GET_CMD_SP(sp);

  sgl = scsi_prot_sglist(cmd);
  vha = sp->vha;
  difctx = sp->u.scmd.crc_ctx;
  direction_to_device = cmd->sc_data_direction == DMA_TO_DEVICE;
  ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
    "%s: scsi_cmnd: %p, crc_ctx: %p, sp: %p\n",
   __func__, cmd, difctx, sp);
 } else if (tc) {
  vha = tc->vha;
  sgl = tc->prot_sg;
  difctx = tc->ctx;
  direction_to_device = tc->dma_data_direction == DMA_TO_DEVICE;
 } else {
  BUG();
  return 1;
 }

 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
     "%s: enter (write=%u)\n", __func__, direction_to_device);

 /* if initiator doing write or target doing read */
 if (direction_to_device) {
  for_each_sg(sgl, sg, tot_dsds, i) {
   u64 sle_phys = sg_phys(sg);

   /* If SGE addr + len flips bits in upper 32-bits */
   if (MSD(sle_phys + sg->length) ^ MSD(sle_phys)) {
    ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe022,
        "%s: page boundary crossing (phys=%llx len=%x)\n",
        __func__, sle_phys, sg->length);

    if (difctx) {
     ha->dif_bundle_crossed_pages++;
     dif_local_dma_alloc = true;
    } else {
     ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
         vha, 0xe022,
         "%s: difctx pointer is NULL\n",
         __func__);
    }
    break;
   }
  }
  ha->dif_bundle_writes++;
 } else {
  ha->dif_bundle_reads++;
 }

 if (ql2xdifbundlinginternalbuffers)
  dif_local_dma_alloc = direction_to_device;

 if (dif_local_dma_alloc) {
  u32 track_difbundl_buf = 0;
  u32 ldma_sg_len = 0;
  u8 ldma_needed = 1;

  difctx->no_dif_bundl = 0;
  difctx->dif_bundl_len = 0;

  /* Track DSD buffers */
  INIT_LIST_HEAD(&difctx->ldif_dsd_list);
  /* Track local DMA buffers */
  INIT_LIST_HEAD(&difctx->ldif_dma_hndl_list);

  for_each_sg(sgl, sg, tot_dsds, i) {
   u32 sglen = sg_dma_len(sg);

   ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe023,
       "%s: sg[%x] (phys=%llx sglen=%x) ldma_sg_len: %x dif_bundl_len: %x ldma_needed: %x\n",
       __func__, i, (u64)sg_phys(sg), sglen, ldma_sg_len,
       difctx->dif_bundl_len, ldma_needed);

   while (sglen) {
    u32 xfrlen = 0;

    if (ldma_needed) {
     /*
 * Allocate list item to store
 * the DMA buffers
 */

     dsd_ptr = kzalloc(sizeof(*dsd_ptr),
         GFP_ATOMIC);
     if (!dsd_ptr) {
      ql_dbg(ql_dbg_tgt, vha, 0xe024,
          "%s: failed alloc dsd_ptr\n",
          __func__);
      return 1;
     }
     ha->dif_bundle_kallocs++;

     /* allocate dma buffer */
     dsd_ptr->dsd_addr = dma_pool_alloc
      (ha->dif_bundl_pool, GFP_ATOMIC,
       &dsd_ptr->dsd_list_dma);
     if (!dsd_ptr->dsd_addr) {
      ql_dbg(ql_dbg_tgt, vha, 0xe024,
          "%s: failed alloc ->dsd_ptr\n",
          __func__);
      /*
 * need to cleanup only this
 * dsd_ptr rest will be done
 * by sp_free_dma()
 */

      kfree(dsd_ptr);
      ha->dif_bundle_kallocs--;
      return 1;
     }
     ha->dif_bundle_dma_allocs++;
     ldma_needed = 0;
     difctx->no_dif_bundl++;
     list_add_tail(&dsd_ptr->list,
         &difctx->ldif_dma_hndl_list);
    }

    /* xfrlen is min of dma pool size and sglen */
    xfrlen = (sglen >
       (DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len)) ?
        DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len :
        sglen;

    /* replace with local allocated dma buffer */
    sg_pcopy_to_buffer(sgl, sg_nents(sgl),
        dsd_ptr->dsd_addr + ldma_sg_len, xfrlen,
        difctx->dif_bundl_len);
    difctx->dif_bundl_len += xfrlen;
    sglen -= xfrlen;
    ldma_sg_len += xfrlen;
    if (ldma_sg_len == DIF_BUNDLING_DMA_POOL_SIZE ||
        sg_is_last(sg)) {
     ldma_needed = 1;
     ldma_sg_len = 0;
    }
   }
  }

  track_difbundl_buf = used_dsds = difctx->no_dif_bundl;
  ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe025,
      "dif_bundl_len=%x, no_dif_bundl=%x track_difbundl_buf: %x\n",
      difctx->dif_bundl_len, difctx->no_dif_bundl,
      track_difbundl_buf);

  if (sp)
   sp->flags |= SRB_DIF_BUNDL_DMA_VALID;
  else
   tc->prot_flags = DIF_BUNDL_DMA_VALID;

  list_for_each_entry_safe(dif_dsd, nxt_dsd,
      &difctx->ldif_dma_hndl_list, list) {
   u32 sglen = (difctx->dif_bundl_len >
       DIF_BUNDLING_DMA_POOL_SIZE) ?
       DIF_BUNDLING_DMA_POOL_SIZE : difctx->dif_bundl_len;

   BUG_ON(track_difbundl_buf == 0);

   /* Allocate additional continuation packets? */
   if (avail_dsds == 0) {
    ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha,
        0xe024,
        "%s: adding continuation iocb's\n",
        __func__);
    avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
        QLA_DSDS_PER_IOCB : used_dsds;
    dsd_list_len = (avail_dsds + 1) * 12;
    used_dsds -= avail_dsds;

    /* allocate tracking DS */
    dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
    if (!dsd_ptr) {
     ql_dbg(ql_dbg_tgt, vha, 0xe026,
         "%s: failed alloc dsd_ptr\n",
         __func__);
     return 1;
    }
    ha->dif_bundle_kallocs++;

    difctx->no_ldif_dsd++;
    /* allocate new list */
    dsd_ptr->dsd_addr =
        dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
     &dsd_ptr->dsd_list_dma);
    if (!dsd_ptr->dsd_addr) {
     ql_dbg(ql_dbg_tgt, vha, 0xe026,
         "%s: failed alloc ->dsd_addr\n",
         __func__);
     /*
 * need to cleanup only this dsd_ptr
 *  rest will be done by sp_free_dma()
 */

     kfree(dsd_ptr);
     ha->dif_bundle_kallocs--;
     return 1;
    }
    ha->dif_bundle_dma_allocs++;

    if (sp) {
     list_add_tail(&dsd_ptr->list,
         &difctx->ldif_dsd_list);
     sp->flags |= SRB_CRC_CTX_DSD_VALID;
    } else {
     list_add_tail(&dsd_ptr->list,
         &difctx->ldif_dsd_list);
     tc->ctx_dsd_alloced = 1;
    }

    /* add new list to cmd iocb or last list */
    put_unaligned_le64(dsd_ptr->dsd_list_dma,
         &cur_dsd->address);
    cur_dsd->length = cpu_to_le32(dsd_list_len);
    cur_dsd = dsd_ptr->dsd_addr;
   }
   put_unaligned_le64(dif_dsd->dsd_list_dma,
        &cur_dsd->address);
   cur_dsd->length = cpu_to_le32(sglen);
   cur_dsd++;
   avail_dsds--;
   difctx->dif_bundl_len -= sglen;
   track_difbundl_buf--;
  }

  ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe026,
      "%s: no_ldif_dsd:%x, no_dif_bundl:%x\n", __func__,
   difctx->no_ldif_dsd, difctx->no_dif_bundl);
 } else {
  for_each_sg(sgl, sg, tot_dsds, i) {
   /* Allocate additional continuation packets? */
   if (avail_dsds == 0) {
    avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
        QLA_DSDS_PER_IOCB : used_dsds;
    dsd_list_len = (avail_dsds + 1) * 12;
    used_dsds -= avail_dsds;

    /* allocate tracking DS */
    dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
    if (!dsd_ptr) {
     ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
         vha, 0xe027,
         "%s: failed alloc dsd_dma...\n",
         __func__);
     return 1;
    }

    /* allocate new list */
    dsd_ptr->dsd_addr =
        dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
     &dsd_ptr->dsd_list_dma);
    if (!dsd_ptr->dsd_addr) {
     /* need to cleanup only this dsd_ptr */
     /* rest will be done by sp_free_dma() */
     kfree(dsd_ptr);
     return 1;
    }

    if (sp) {
     list_add_tail(&dsd_ptr->list,
         &difctx->dsd_list);
     sp->flags |= SRB_CRC_CTX_DSD_VALID;
    } else {
     list_add_tail(&dsd_ptr->list,
         &difctx->dsd_list);
     tc->ctx_dsd_alloced = 1;
    }

    /* add new list to cmd iocb or last list */
    put_unaligned_le64(dsd_ptr->dsd_list_dma,
         &cur_dsd->address);
    cur_dsd->length = cpu_to_le32(dsd_list_len);
    cur_dsd = dsd_ptr->dsd_addr;
   }
   append_dsd64(&cur_dsd, sg);
   avail_dsds--;
  }
 }
 /* Null termination */
 cur_dsd->address = 0;
 cur_dsd->length = 0;
 cur_dsd++;
 return 0;
}

/**
 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
 * Type 6 IOCB types.
 *
 * @sp: SRB command to process
 * @cmd_pkt: Command type 3 IOCB
 * @tot_dsds: Total number of segments to transfer
 * @tot_prot_dsds: Total number of segments with protection information
 * @fw_prot_opts: Protection options to be passed to firmware
 */

static inline int
qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
    uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
{
 struct dsd64  *cur_dsd;
 __be32   *fcp_dl;
 scsi_qla_host_t  *vha;
 struct scsi_cmnd *cmd;
 uint32_t  total_bytes = 0;
 uint32_t  data_bytes;
 uint32_t  dif_bytes;
 uint8_t   bundling = 1;
 uint16_t  blk_size;
 struct crc_context *crc_ctx_pkt = NULL;
 struct qla_hw_data *ha;
 uint8_t   additional_fcpcdb_len;
 uint16_t  fcp_cmnd_len;
 struct fcp_cmnd  *fcp_cmnd;
 dma_addr_t  crc_ctx_dma;

 cmd = GET_CMD_SP(sp);

 /* Update entry type to indicate Command Type CRC_2 IOCB */
 put_unaligned_le32(COMMAND_TYPE_CRC_2, &cmd_pkt->entry_type);

 vha = sp->vha;
 ha = vha->hw;

 /* No data transfer */
 data_bytes = scsi_bufflen(cmd);
 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return QLA_SUCCESS;
 }

 cmd_pkt->vp_index = sp->vha->vp_idx;

 /* Set transfer direction */
 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  cmd_pkt->control_flags =
      cpu_to_le16(CF_WRITE_DATA);
 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
  cmd_pkt->control_flags =
      cpu_to_le16(CF_READ_DATA);
 }

 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
     (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
     (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
     (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
  bundling = 0;

 /* Allocate CRC context from global pool */
 crc_ctx_pkt = sp->u.scmd.crc_ctx =
     dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);

 if (!crc_ctx_pkt)
  goto crc_queuing_error;

 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;

 sp->flags |= SRB_CRC_CTX_DMA_VALID;

 /* Set handle */
 crc_ctx_pkt->handle = cmd_pkt->handle;

 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);

 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
     &crc_ctx_pkt->ref_tag, tot_prot_dsds);

 put_unaligned_le64(crc_ctx_dma, &cmd_pkt->crc_context_address);
 cmd_pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);

 /* Determine SCSI command length -- align to 4 byte boundary */
 if (cmd->cmd_len > 16) {
  additional_fcpcdb_len = cmd->cmd_len - 16;
  if ((cmd->cmd_len % 4) != 0) {
   /* SCSI cmd > 16 bytes must be multiple of 4 */
   goto crc_queuing_error;
  }
  fcp_cmnd_len = 12 + cmd->cmd_len + 4;
 } else {
  additional_fcpcdb_len = 0;
  fcp_cmnd_len = 12 + 16 + 4;
 }

 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;

 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
 if (cmd->sc_data_direction == DMA_TO_DEVICE)
  fcp_cmnd->additional_cdb_len |= 1;
 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
  fcp_cmnd->additional_cdb_len |= 2;

 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
 put_unaligned_le64(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF,
      &cmd_pkt->fcp_cmnd_dseg_address);
 fcp_cmnd->task_management = 0;
 fcp_cmnd->task_attribute = TSK_SIMPLE;

 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */

 /* Compute dif len and adjust data len to incude protection */
 dif_bytes = 0;
 blk_size = cmd->device->sector_size;
 dif_bytes = (data_bytes / blk_size) * 8;

 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
 case SCSI_PROT_READ_INSERT:
 case SCSI_PROT_WRITE_STRIP:
  total_bytes = data_bytes;
  data_bytes += dif_bytes;
  break;

 case SCSI_PROT_READ_STRIP:
 case SCSI_PROT_WRITE_INSERT:
 case SCSI_PROT_READ_PASS:
 case SCSI_PROT_WRITE_PASS:
  total_bytes = data_bytes + dif_bytes;
  break;
 default:
  BUG();
 }

 if (!qla2x00_hba_err_chk_enabled(sp))
  fw_prot_opts |= 0x10; /* Disable Guard tag checking */
 /* HBA error checking enabled */
 else if (IS_PI_UNINIT_CAPABLE(ha)) {
  if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
      || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
   SCSI_PROT_DIF_TYPE2))
   fw_prot_opts |= BIT_10;
  else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
      SCSI_PROT_DIF_TYPE3)
   fw_prot_opts |= BIT_11;
 }

 if (!bundling) {
  cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
 } else {
  /*
 * Configure Bundling if we need to fetch interlaving
 * protection PCI accesses
 */

  fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
  crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
  crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
       tot_prot_dsds);
  cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
 }

 /* Finish the common fields of CRC pkt */
 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
 /* Fibre channel byte count */
 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
 fcp_dl = (__be32 *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
     additional_fcpcdb_len);
 *fcp_dl = htonl(total_bytes);

 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
  cmd_pkt->byte_count = cpu_to_le32(0);
  return QLA_SUCCESS;
 }
 /* Walks data segments */

 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);

 if (!bundling && tot_prot_dsds) {
  if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
   cur_dsd, tot_dsds, NULL))
   goto crc_queuing_error;
 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
   (tot_dsds - tot_prot_dsds), NULL))
  goto crc_queuing_error;

 if (bundling && tot_prot_dsds) {
  /* Walks dif segments */
  cmd_pkt->control_flags |= cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
  cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
  if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
    tot_prot_dsds, NULL))
   goto crc_queuing_error;
 }
 return QLA_SUCCESS;

crc_queuing_error:
 /* Cleanup will be performed by the caller */

 return QLA_FUNCTION_FAILED;
}

/**
 * qla24xx_start_scsi() - Send a SCSI command to the ISP
 * @sp: command to send to the ISP
 *
 * Returns non-zero if a failure occurred, else zero.
 */

int
qla24xx_start_scsi(srb_t *sp)
{
 int  nseg;
 unsigned long   flags;
 uint32_t *clr_ptr;
 uint32_t handle;
 struct cmd_type_7 *cmd_pkt;
 uint16_t cnt;
 uint16_t req_cnt;
 uint16_t tot_dsds;
 struct req_que *req = NULL;
 struct rsp_que *rsp;
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 struct scsi_qla_host *vha = sp->vha;
 struct qla_hw_data *ha = vha->hw;

 if (sp->fcport->edif.enable  && (sp->fcport->flags & FCF_FCSP_DEVICE))
  return qla28xx_start_scsi_edif(sp);

 /* Setup device pointers. */
 req = vha->req;
 rsp = req->rsp;

 /* So we know we haven't pci_map'ed anything yet */
 tot_dsds = 0;

 /* Send marker if required */
 if (vha->marker_needed != 0) {
  if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
      QLA_SUCCESS)
   return QLA_FUNCTION_FAILED;
  vha->marker_needed = 0;
 }

 /* Acquire ring specific lock */
 spin_lock_irqsave(&ha->hardware_lock, flags);

 handle = qla2xxx_get_next_handle(req);
 if (handle == 0)
  goto queuing_error;

 /* Map the sg table so we have an accurate count of sg entries needed */
 if (scsi_sg_count(cmd)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
      scsi_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
 } else
  nseg = 0;

 tot_dsds = nseg;
 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);

 sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
 sp->iores.exch_cnt = 1;
 sp->iores.iocb_cnt = req_cnt;
 if (qla_get_fw_resources(sp->qpair, &sp->iores))
  goto queuing_error;

 if (req->cnt < (req_cnt + 2)) {
  if (IS_SHADOW_REG_CAPABLE(ha)) {
   cnt = *req->out_ptr;
  } else {
   cnt = rd_reg_dword_relaxed(req->req_q_out);
   if (qla2x00_check_reg16_for_disconnect(vha, cnt))
    goto queuing_error;
  }

  if (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
    (req->ring_index - cnt);
  if (req->cnt < (req_cnt + 2))
   goto queuing_error;
 }

 /* Build command packet. */
 req->current_outstanding_cmd = handle;
 req->outstanding_cmds[handle] = sp;
 sp->handle = handle;
 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
 req->cnt -= req_cnt;

 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
 cmd_pkt->handle = make_handle(req->id, handle);

 /* Zero out remaining portion of packet. */
 /*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
 clr_ptr = (uint32_t *)cmd_pkt + 2;
 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);

 /* Set NPORT-ID and LUN number*/
 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
 cmd_pkt->vp_index = sp->vha->vp_idx;

 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));

 cmd_pkt->task = TSK_SIMPLE;

 /* Load SCSI command packet. */
 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));

 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));

 /* Build IOCB segments */
 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);

 /* Set total data segment count. */
 cmd_pkt->entry_count = (uint8_t)req_cnt;
 wmb();
 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else
  req->ring_ptr++;

 sp->qpair->cmd_cnt++;
 sp->flags |= SRB_DMA_VALID;

 /* Set chip new ring index. */
 wrt_reg_dword(req->req_q_in, req->ring_index);

 /* Manage unprocessed RIO/ZIO commands in response queue. */
 if (vha->flags.process_response_queue &&
     rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  qla24xx_process_response_queue(vha, rsp);

 spin_unlock_irqrestore(&ha->hardware_lock, flags);
 return QLA_SUCCESS;

queuing_error:
 if (tot_dsds)
  scsi_dma_unmap(cmd);

 qla_put_fw_resources(sp->qpair, &sp->iores);
 spin_unlock_irqrestore(&ha->hardware_lock, flags);

 return QLA_FUNCTION_FAILED;
}

/**
 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
 * @sp: command to send to the ISP
 *
 * Returns non-zero if a failure occurred, else zero.
 */

int
qla24xx_dif_start_scsi(srb_t *sp)
{
 int   nseg;
 unsigned long  flags;
 uint32_t  *clr_ptr;
 uint32_t  handle;
 uint16_t  cnt;
 uint16_t  req_cnt = 0;
 uint16_t  tot_dsds;
 uint16_t  tot_prot_dsds;
 uint16_t  fw_prot_opts = 0;
 struct req_que  *req = NULL;
 struct rsp_que  *rsp = NULL;
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 struct scsi_qla_host *vha = sp->vha;
 struct qla_hw_data *ha = vha->hw;
 struct cmd_type_crc_2 *cmd_pkt;
 uint32_t  status = 0;

#define QDSS_GOT_Q_SPACE BIT_0

 /* Only process protection or >16 cdb in this routine */
 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  if (cmd->cmd_len <= 16)
   return qla24xx_start_scsi(sp);
  else
   return qla_start_scsi_type6(sp);
 }

 /* Setup device pointers. */
 req = vha->req;
 rsp = req->rsp;

 /* So we know we haven't pci_map'ed anything yet */
 tot_dsds = 0;

 /* Send marker if required */
 if (vha->marker_needed != 0) {
  if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
      QLA_SUCCESS)
   return QLA_FUNCTION_FAILED;
  vha->marker_needed = 0;
 }

 /* Acquire ring specific lock */
 spin_lock_irqsave(&ha->hardware_lock, flags);

 handle = qla2xxx_get_next_handle(req);
 if (handle == 0)
  goto queuing_error;

 /* Compute number of required data segments */
 /* Map the sg table so we have an accurate count of sg entries needed */
 if (scsi_sg_count(cmd)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
      scsi_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
  else
   sp->flags |= SRB_DMA_VALID;

  if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
      (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
   struct qla2_sgx sgx;
   uint32_t partial;

   memset(&sgx, 0, sizeof(struct qla2_sgx));
   sgx.tot_bytes = scsi_bufflen(cmd);
   sgx.cur_sg = scsi_sglist(cmd);
   sgx.sp = sp;

   nseg = 0;
   while (qla24xx_get_one_block_sg(
       cmd->device->sector_size, &sgx, &partial))
    nseg++;
  }
 } else
  nseg = 0;

 /* number of required data segments */
 tot_dsds = nseg;

 /* Compute number of required protection segments */
 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
      scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
  else
   sp->flags |= SRB_CRC_PROT_DMA_VALID;

  if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
      (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
   nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
  }
 } else {
  nseg = 0;
 }

 req_cnt = 1;
 /* Total Data and protection sg segment(s) */
 tot_prot_dsds = nseg;
 tot_dsds += nseg;

 sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
 sp->iores.exch_cnt = 1;
 sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
 if (qla_get_fw_resources(sp->qpair, &sp->iores))
  goto queuing_error;

 if (req->cnt < (req_cnt + 2)) {
  if (IS_SHADOW_REG_CAPABLE(ha)) {
   cnt = *req->out_ptr;
  } else {
   cnt = rd_reg_dword_relaxed(req->req_q_out);
   if (qla2x00_check_reg16_for_disconnect(vha, cnt))
    goto queuing_error;
  }
  if (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
    (req->ring_index - cnt);
  if (req->cnt < (req_cnt + 2))
   goto queuing_error;
 }

 status |= QDSS_GOT_Q_SPACE;

 /* Build header part of command packet (excluding the OPCODE). */
 req->current_outstanding_cmd = handle;
 req->outstanding_cmds[handle] = sp;
 sp->handle = handle;
 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
 req->cnt -= req_cnt;

 /* Fill-in common area */
 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
 cmd_pkt->handle = make_handle(req->id, handle);

 clr_ptr = (uint32_t *)cmd_pkt + 2;
 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);

 /* Set NPORT-ID and LUN number*/
 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;

 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));

 /* Total Data and protection segment(s) */
 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);

 /* Build IOCB segments and adjust for data protection segments */
 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
     req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  QLA_SUCCESS)
  goto queuing_error;

 cmd_pkt->entry_count = (uint8_t)req_cnt;
 /* Specify response queue number where completion should happen */
 cmd_pkt->entry_status = (uint8_t) rsp->id;
 cmd_pkt->timeout = cpu_to_le16(0);
 wmb();

 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else
  req->ring_ptr++;

 sp->qpair->cmd_cnt++;
 /* Set chip new ring index. */
 wrt_reg_dword(req->req_q_in, req->ring_index);

 /* Manage unprocessed RIO/ZIO commands in response queue. */
 if (vha->flags.process_response_queue &&
     rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  qla24xx_process_response_queue(vha, rsp);

 spin_unlock_irqrestore(&ha->hardware_lock, flags);

 return QLA_SUCCESS;

queuing_error:
 if (status & QDSS_GOT_Q_SPACE) {
  req->outstanding_cmds[handle] = NULL;
  req->cnt += req_cnt;
 }
 /* Cleanup will be performed by the caller (queuecommand) */

 qla_put_fw_resources(sp->qpair, &sp->iores);
 spin_unlock_irqrestore(&ha->hardware_lock, flags);

 return QLA_FUNCTION_FAILED;
}

/**
 * qla2xxx_start_scsi_mq() - Send a SCSI command to the ISP
 * @sp: command to send to the ISP
 *
 * Returns non-zero if a failure occurred, else zero.
 */

static int
qla2xxx_start_scsi_mq(srb_t *sp)
{
 int  nseg;
 unsigned long   flags;
 uint32_t *clr_ptr;
 uint32_t handle;
 struct cmd_type_7 *cmd_pkt;
 uint16_t cnt;
 uint16_t req_cnt;
 uint16_t tot_dsds;
 struct req_que *req = NULL;
 struct rsp_que *rsp;
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 struct scsi_qla_host *vha = sp->fcport->vha;
 struct qla_hw_data *ha = vha->hw;
 struct qla_qpair *qpair = sp->qpair;

 if (sp->fcport->edif.enable && (sp->fcport->flags & FCF_FCSP_DEVICE))
  return qla28xx_start_scsi_edif(sp);

 /* Acquire qpair specific lock */
 spin_lock_irqsave(&qpair->qp_lock, flags);

 /* Setup qpair pointers */
 req = qpair->req;
 rsp = qpair->rsp;

 /* So we know we haven't pci_map'ed anything yet */
 tot_dsds = 0;

 /* Send marker if required */
 if (vha->marker_needed != 0) {
  if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
      QLA_SUCCESS) {
   spin_unlock_irqrestore(&qpair->qp_lock, flags);
   return QLA_FUNCTION_FAILED;
  }
  vha->marker_needed = 0;
 }

 handle = qla2xxx_get_next_handle(req);
 if (handle == 0)
  goto queuing_error;

 /* Map the sg table so we have an accurate count of sg entries needed */
 if (scsi_sg_count(cmd)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
      scsi_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
 } else
  nseg = 0;

 tot_dsds = nseg;
 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);

 sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
 sp->iores.exch_cnt = 1;
 sp->iores.iocb_cnt = req_cnt;
 if (qla_get_fw_resources(sp->qpair, &sp->iores))
  goto queuing_error;

 if (req->cnt < (req_cnt + 2)) {
  if (IS_SHADOW_REG_CAPABLE(ha)) {
   cnt = *req->out_ptr;
  } else {
   cnt = rd_reg_dword_relaxed(req->req_q_out);
   if (qla2x00_check_reg16_for_disconnect(vha, cnt))
    goto queuing_error;
  }

  if (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
    (req->ring_index - cnt);
  if (req->cnt < (req_cnt + 2))
   goto queuing_error;
 }

 /* Build command packet. */
 req->current_outstanding_cmd = handle;
 req->outstanding_cmds[handle] = sp;
 sp->handle = handle;
 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
 req->cnt -= req_cnt;

 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
 cmd_pkt->handle = make_handle(req->id, handle);

 /* Zero out remaining portion of packet. */
 /*    tagged queuing modifier -- default is TSK_SIMPLE (0). */
 clr_ptr = (uint32_t *)cmd_pkt + 2;
 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);

 /* Set NPORT-ID and LUN number*/
 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;

 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));

 cmd_pkt->task = TSK_SIMPLE;

 /* Load SCSI command packet. */
 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));

 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));

 /* Build IOCB segments */
 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);

 /* Set total data segment count. */
 cmd_pkt->entry_count = (uint8_t)req_cnt;
 wmb();
 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else
  req->ring_ptr++;

 sp->qpair->cmd_cnt++;
 sp->flags |= SRB_DMA_VALID;

 /* Set chip new ring index. */
 wrt_reg_dword(req->req_q_in, req->ring_index);

 /* Manage unprocessed RIO/ZIO commands in response queue. */
 if (vha->flags.process_response_queue &&
     rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  qla24xx_process_response_queue(vha, rsp);

 spin_unlock_irqrestore(&qpair->qp_lock, flags);
 return QLA_SUCCESS;

queuing_error:
 if (tot_dsds)
  scsi_dma_unmap(cmd);

 qla_put_fw_resources(sp->qpair, &sp->iores);
 spin_unlock_irqrestore(&qpair->qp_lock, flags);

 return QLA_FUNCTION_FAILED;
}


/**
 * qla2xxx_dif_start_scsi_mq() - Send a SCSI command to the ISP
 * @sp: command to send to the ISP
 *
 * Returns non-zero if a failure occurred, else zero.
 */

int
qla2xxx_dif_start_scsi_mq(srb_t *sp)
{
 int   nseg;
 unsigned long  flags;
 uint32_t  *clr_ptr;
 uint32_t  handle;
 uint16_t  cnt;
 uint16_t  req_cnt = 0;
 uint16_t  tot_dsds;
 uint16_t  tot_prot_dsds;
 uint16_t  fw_prot_opts = 0;
 struct req_que  *req = NULL;
 struct rsp_que  *rsp = NULL;
 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
 struct scsi_qla_host *vha = sp->fcport->vha;
 struct qla_hw_data *ha = vha->hw;
 struct cmd_type_crc_2 *cmd_pkt;
 uint32_t  status = 0;
 struct qla_qpair *qpair = sp->qpair;

#define QDSS_GOT_Q_SPACE BIT_0

 /* Check for host side state */
 if (!qpair->online) {
  cmd->result = DID_NO_CONNECT << 16;
  return QLA_INTERFACE_ERROR;
 }

 if (!qpair->difdix_supported &&
  scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  cmd->result = DID_NO_CONNECT << 16;
  return QLA_INTERFACE_ERROR;
 }

 /* Only process protection or >16 cdb in this routine */
 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
  if (cmd->cmd_len <= 16)
   return qla2xxx_start_scsi_mq(sp);
  else
   return qla_start_scsi_type6(sp);
 }

 spin_lock_irqsave(&qpair->qp_lock, flags);

 /* Setup qpair pointers */
 rsp = qpair->rsp;
 req = qpair->req;

 /* So we know we haven't pci_map'ed anything yet */
 tot_dsds = 0;

 /* Send marker if required */
 if (vha->marker_needed != 0) {
  if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
      QLA_SUCCESS) {
   spin_unlock_irqrestore(&qpair->qp_lock, flags);
   return QLA_FUNCTION_FAILED;
  }
  vha->marker_needed = 0;
 }

 handle = qla2xxx_get_next_handle(req);
 if (handle == 0)
  goto queuing_error;

 /* Compute number of required data segments */
 /* Map the sg table so we have an accurate count of sg entries needed */
 if (scsi_sg_count(cmd)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
      scsi_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
  else
   sp->flags |= SRB_DMA_VALID;

  if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
      (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
   struct qla2_sgx sgx;
   uint32_t partial;

   memset(&sgx, 0, sizeof(struct qla2_sgx));
   sgx.tot_bytes = scsi_bufflen(cmd);
   sgx.cur_sg = scsi_sglist(cmd);
   sgx.sp = sp;

   nseg = 0;
   while (qla24xx_get_one_block_sg(
       cmd->device->sector_size, &sgx, &partial))
    nseg++;
  }
 } else
  nseg = 0;

 /* number of required data segments */
 tot_dsds = nseg;

 /* Compute number of required protection segments */
 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
  nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
      scsi_prot_sg_count(cmd), cmd->sc_data_direction);
  if (unlikely(!nseg))
   goto queuing_error;
  else
   sp->flags |= SRB_CRC_PROT_DMA_VALID;

  if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
      (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
   nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
  }
 } else {
  nseg = 0;
 }

 req_cnt = 1;
 /* Total Data and protection sg segment(s) */
 tot_prot_dsds = nseg;
 tot_dsds += nseg;

 sp->iores.res_type = RESOURCE_IOCB | RESOURCE_EXCH;
 sp->iores.exch_cnt = 1;
 sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
 if (qla_get_fw_resources(sp->qpair, &sp->iores))
  goto queuing_error;

 if (req->cnt < (req_cnt + 2)) {
  if (IS_SHADOW_REG_CAPABLE(ha)) {
   cnt = *req->out_ptr;
  } else {
   cnt = rd_reg_dword_relaxed(req->req_q_out);
   if (qla2x00_check_reg16_for_disconnect(vha, cnt))
    goto queuing_error;
  }

  if (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
    (req->ring_index - cnt);
  if (req->cnt < (req_cnt + 2))
   goto queuing_error;
 }

 status |= QDSS_GOT_Q_SPACE;

 /* Build header part of command packet (excluding the OPCODE). */
 req->current_outstanding_cmd = handle;
 req->outstanding_cmds[handle] = sp;
 sp->handle = handle;
 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
 req->cnt -= req_cnt;

 /* Fill-in common area */
 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
 cmd_pkt->handle = make_handle(req->id, handle);

 clr_ptr = (uint32_t *)cmd_pkt + 2;
 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);

 /* Set NPORT-ID and LUN number*/
 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;

 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));

 /* Total Data and protection segment(s) */
 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);

 /* Build IOCB segments and adjust for data protection segments */
 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
     req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
  QLA_SUCCESS)
  goto queuing_error;

 cmd_pkt->entry_count = (uint8_t)req_cnt;
 cmd_pkt->timeout = cpu_to_le16(0);
 wmb();

 /* Adjust ring index. */
 req->ring_index++;
 if (req->ring_index == req->length) {
  req->ring_index = 0;
  req->ring_ptr = req->ring;
 } else
  req->ring_ptr++;

 sp->qpair->cmd_cnt++;
 /* Set chip new ring index. */
 wrt_reg_dword(req->req_q_in, req->ring_index);

 /* Manage unprocessed RIO/ZIO commands in response queue. */
 if (vha->flags.process_response_queue &&
     rsp->ring_ptr->signature != RESPONSE_PROCESSED)
  qla24xx_process_response_queue(vha, rsp);

 spin_unlock_irqrestore(&qpair->qp_lock, flags);

 return QLA_SUCCESS;

queuing_error:
 if (status & QDSS_GOT_Q_SPACE) {
  req->outstanding_cmds[handle] = NULL;
  req->cnt += req_cnt;
 }
 /* Cleanup will be performed by the caller (queuecommand) */

 qla_put_fw_resources(sp->qpair, &sp->iores);
 spin_unlock_irqrestore(&qpair->qp_lock, flags);

 return QLA_FUNCTION_FAILED;
}

/* Generic Control-SRB manipulation functions. */

/* hardware_lock assumed to be held. */

void *
__qla2x00_alloc_iocbs(struct qla_qpair *qpair, srb_t *sp)
{
 scsi_qla_host_t *vha = qpair->vha;
 struct qla_hw_data *ha = vha->hw;
 struct req_que *req = qpair->req;
 device_reg_t *reg = ISP_QUE_REG(ha, req->id);
 uint32_t handle;
 request_t *pkt;
 uint16_t cnt, req_cnt;

 pkt = NULL;
 req_cnt = 1;
 handle = 0;

 if (sp && (sp->type != SRB_SCSI_CMD)) {
  /* Adjust entry-counts as needed. */
  req_cnt = sp->iocbs;
 }

 /* Check for room on request queue. */
 if (req->cnt < req_cnt + 2) {
  if (qpair->use_shadow_reg)
   cnt = *req->out_ptr;
  else if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
      IS_QLA28XX(ha))
   cnt = rd_reg_dword(®->isp25mq.req_q_out);
  else if (IS_P3P_TYPE(ha))
   cnt = rd_reg_dword(reg->isp82.req_q_out);
  else if (IS_FWI2_CAPABLE(ha))
   cnt = rd_reg_dword(®->isp24.req_q_out);
  else if (IS_QLAFX00(ha))
   cnt = rd_reg_dword(®->ispfx00.req_q_out);
  else
   cnt = qla2x00_debounce_register(
       ISP_REQ_Q_OUT(ha, ®->isp));

  if (!qpair->use_shadow_reg && cnt == ISP_REG16_DISCONNECT) {
   qla_schedule_eeh_work(vha);
   return NULL;
  }

  if  (req->ring_index < cnt)
   req->cnt = cnt - req->ring_index;
  else
   req->cnt = req->length -
       (req->ring_index - cnt);
 }
 if (req->cnt < req_cnt + 2)
  goto queuing_error;

 if (sp) {
  handle = qla2xxx_get_next_handle(req);
  if (handle == 0) {
   ql_log(ql_log_warn, vha, 0x700b,
       "No room on outstanding cmd array.\n");
   goto queuing_error;
  }

  /* Prep command array. */
  req->current_outstanding_cmd = handle;
  req->outstanding_cmds[handle] = sp;
  sp->handle = handle;
 }

 /* Prep packet */
 req->cnt -= req_cnt;
 pkt = req->ring_ptr;
 memset(pkt, 0, REQUEST_ENTRY_SIZE);
 if (IS_QLAFX00(ha)) {
  wrt_reg_byte((u8 __force __iomem *)&pkt->entry_count, req_cnt);
  wrt_reg_dword((__le32 __force __iomem *)&pkt->handle, handle);
 } else {
  pkt->entry_count = req_cnt;
  pkt->handle = handle;
 }

 return pkt;

queuing_error:
 qpair->tgt_counters.num_alloc_iocb_failed++;
 return pkt;
}

void *
qla2x00_alloc_iocbs_ready(struct qla_qpair *qpair, srb_t *sp)
{
 scsi_qla_host_t *vha = qpair->vha;

 if (qla2x00_reset_active(vha))
  return NULL;

 return __qla2x00_alloc_iocbs(qpair, sp);
}

void *
qla2x00_alloc_iocbs(struct scsi_qla_host *vha, srb_t *sp)
{
 return __qla2x00_alloc_iocbs(vha->hw->base_qpair, sp);
}

static void
qla24xx_prli_iocb(srb_t *sp, struct logio_entry_24xx *logio)
{
 struct srb_iocb *lio = &sp->u.iocb_cmd;

 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
 logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
 if (lio->u.logio.flags & SRB_LOGIN_NVME_PRLI) {
  logio->control_flags |= cpu_to_le16(LCF_NVME_PRLI);
  if (sp->vha->flags.nvme_first_burst)
   logio->io_parameter[0] =
    cpu_to_le32(NVME_PRLI_SP_FIRST_BURST);
  if (sp->vha->flags.nvme2_enabled) {
   /* Set service parameter BIT_7 for NVME CONF support */
   logio->io_parameter[0] |=
    cpu_to_le32(NVME_PRLI_SP_CONF);
   /* Set service parameter BIT_8 for SLER support */
   logio->io_parameter[0] |=
    cpu_to_le32(NVME_PRLI_SP_SLER);
   /* Set service parameter BIT_9 for PI control support */
   logio->io_parameter[0] |=
    cpu_to_le32(NVME_PRLI_SP_PI_CTRL);
  }
 }

 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
 logio->port_id[1] = sp->fcport->d_id.b.area;
 logio->port_id[2] = sp->fcport->d_id.b.domain;
 logio->vp_index = sp->vha->vp_idx;
}

static void
qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
{
 struct srb_iocb *lio = &sp->u.iocb_cmd;

 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);

 if (lio->u.logio.flags & SRB_LOGIN_PRLI_ONLY) {
  logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
 } else {
  logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
  if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
   logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
  if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
   logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
  if (lio->u.logio.flags & SRB_LOGIN_FCSP) {
   logio->control_flags |=
       cpu_to_le16(LCF_COMMON_FEAT | LCF_SKIP_PRLI);
   logio->io_parameter[0] =
       cpu_to_le32(LIO_COMM_FEAT_FCSP | LIO_COMM_FEAT_CIO);
  }
 }
 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
 logio->port_id[1] = sp->fcport->d_id.b.area;
 logio->port_id[2] = sp->fcport->d_id.b.domain;
 logio->vp_index = sp->vha->vp_idx;
}

static void
qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
{
 struct qla_hw_data *ha = sp->vha->hw;
 struct srb_iocb *lio = &sp->u.iocb_cmd;
 uint16_t opts;

 mbx->entry_type = MBX_IOCB_TYPE;
 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
 if (HAS_EXTENDED_IDS(ha)) {
  mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
  mbx->mb10 = cpu_to_le16(opts);
 } else {
  mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
 }
 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
     sp->fcport->d_id.b.al_pa);
 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
}

static void
qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
{
 u16 control_flags = LCF_COMMAND_LOGO;
 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;

 if (sp->fcport->explicit_logout) {
  control_flags |= LCF_EXPL_LOGO|LCF_FREE_NPORT;
 } else {
  control_flags |= LCF_IMPL_LOGO;

  if (!sp->fcport->keep_nport_handle)
   control_flags |= LCF_FREE_NPORT;
 }

 logio->control_flags = cpu_to_le16(control_flags);
 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
 logio->port_id[1] = sp->fcport->d_id.b.area;
 logio->port_id[2] = sp->fcport->d_id.b.domain;
 logio->vp_index = sp->vha->vp_idx;
}

static void
qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
{
--> --------------------

--> maximum size reached

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

Messung V0.5
C=90 H=90 G=90

¤ Dauer der Verarbeitung: 0.22 Sekunden  ¤

*© Formatika GbR, Deutschland






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.