2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2014 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
8 #include "qla_target.h"
10 #include <linux/blkdev.h>
11 #include <linux/delay.h>
13 #include <scsi/scsi_tcq.h>
15 static void qla25xx_set_que(srb_t *, struct rsp_que **);
17 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
20 * Returns the proper CF_* direction based on CDB.
22 static inline uint16_t
23 qla2x00_get_cmd_direction(srb_t *sp)
26 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
27 struct scsi_qla_host *vha = sp->fcport->vha;
31 /* Set transfer direction */
32 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
34 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
35 vha->qla_stats.output_requests++;
36 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
38 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
39 vha->qla_stats.input_requests++;
45 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46 * Continuation Type 0 IOCBs to allocate.
48 * @dsds: number of data segment decriptors needed
50 * Returns the number of IOCB entries needed to store @dsds.
53 qla2x00_calc_iocbs_32(uint16_t dsds)
59 iocbs += (dsds - 3) / 7;
67 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68 * Continuation Type 1 IOCBs to allocate.
70 * @dsds: number of data segment decriptors needed
72 * Returns the number of IOCB entries needed to store @dsds.
75 qla2x00_calc_iocbs_64(uint16_t dsds)
81 iocbs += (dsds - 2) / 5;
89 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
92 * Returns a pointer to the Continuation Type 0 IOCB packet.
94 static inline cont_entry_t *
95 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
97 cont_entry_t *cont_pkt;
98 struct req_que *req = vha->req;
99 /* Adjust ring index. */
101 if (req->ring_index == req->length) {
103 req->ring_ptr = req->ring;
108 cont_pkt = (cont_entry_t *)req->ring_ptr;
110 /* Load packet defaults. */
111 *((uint32_t *)(&cont_pkt->entry_type)) =
112 __constant_cpu_to_le32(CONTINUE_TYPE);
118 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
121 * Returns a pointer to the continuation type 1 IOCB packet.
123 static inline cont_a64_entry_t *
124 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
126 cont_a64_entry_t *cont_pkt;
128 /* Adjust ring index. */
130 if (req->ring_index == req->length) {
132 req->ring_ptr = req->ring;
137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
139 /* Load packet defaults. */
140 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
142 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
148 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
150 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
151 uint8_t guard = scsi_host_get_guard(cmd->device->host);
153 /* We always use DIFF Bundling for best performance */
156 /* Translate SCSI opcode to a protection opcode */
157 switch (scsi_get_prot_op(cmd)) {
158 case SCSI_PROT_READ_STRIP:
159 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
161 case SCSI_PROT_WRITE_INSERT:
162 *fw_prot_opts |= PO_MODE_DIF_INSERT;
164 case SCSI_PROT_READ_INSERT:
165 *fw_prot_opts |= PO_MODE_DIF_INSERT;
167 case SCSI_PROT_WRITE_STRIP:
168 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
170 case SCSI_PROT_READ_PASS:
171 case SCSI_PROT_WRITE_PASS:
172 if (guard & SHOST_DIX_GUARD_IP)
173 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
177 default: /* Normal Request */
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
182 return scsi_prot_sg_count(cmd);
186 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
187 * capable IOCB types.
189 * @sp: SRB command to process
190 * @cmd_pkt: Command type 2 IOCB
191 * @tot_dsds: Total number of segments to transfer
193 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
198 scsi_qla_host_t *vha;
199 struct scsi_cmnd *cmd;
200 struct scatterlist *sg;
203 cmd = GET_CMD_SP(sp);
205 /* Update entry type to indicate Command Type 2 IOCB */
206 *((uint32_t *)(&cmd_pkt->entry_type)) =
207 __constant_cpu_to_le32(COMMAND_TYPE);
209 /* No data transfer */
210 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
211 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
215 vha = sp->fcport->vha;
216 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
218 /* Three DSDs are available in the Command Type 2 IOCB */
220 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
222 /* Load data segments */
223 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
224 cont_entry_t *cont_pkt;
226 /* Allocate additional continuation packets? */
227 if (avail_dsds == 0) {
229 * Seven DSDs are available in the Continuation
232 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
233 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
237 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
238 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
244 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
245 * capable IOCB types.
247 * @sp: SRB command to process
248 * @cmd_pkt: Command type 3 IOCB
249 * @tot_dsds: Total number of segments to transfer
251 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
256 scsi_qla_host_t *vha;
257 struct scsi_cmnd *cmd;
258 struct scatterlist *sg;
261 cmd = GET_CMD_SP(sp);
263 /* Update entry type to indicate Command Type 3 IOCB */
264 *((uint32_t *)(&cmd_pkt->entry_type)) =
265 __constant_cpu_to_le32(COMMAND_A64_TYPE);
267 /* No data transfer */
268 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
269 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
273 vha = sp->fcport->vha;
274 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
276 /* Two DSDs are available in the Command Type 3 IOCB */
278 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
280 /* Load data segments */
281 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
283 cont_a64_entry_t *cont_pkt;
285 /* Allocate additional continuation packets? */
286 if (avail_dsds == 0) {
288 * Five DSDs are available in the Continuation
291 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
292 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
296 sle_dma = sg_dma_address(sg);
297 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
298 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
299 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
305 * qla2x00_start_scsi() - Send a SCSI command to the ISP
306 * @sp: command to send to the ISP
308 * Returns non-zero if a failure occurred, else zero.
311 qla2x00_start_scsi(srb_t *sp)
315 scsi_qla_host_t *vha;
316 struct scsi_cmnd *cmd;
320 cmd_entry_t *cmd_pkt;
324 struct device_reg_2xxx __iomem *reg;
325 struct qla_hw_data *ha;
329 /* Setup device pointers. */
331 vha = sp->fcport->vha;
333 reg = &ha->iobase->isp;
334 cmd = GET_CMD_SP(sp);
335 req = ha->req_q_map[0];
336 rsp = ha->rsp_q_map[0];
337 /* So we know we haven't pci_map'ed anything yet */
340 /* Send marker if required */
341 if (vha->marker_needed != 0) {
342 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
344 return (QLA_FUNCTION_FAILED);
346 vha->marker_needed = 0;
349 /* Acquire ring specific lock */
350 spin_lock_irqsave(&ha->hardware_lock, flags);
352 /* Check for room in outstanding command list. */
353 handle = req->current_outstanding_cmd;
354 for (index = 1; index < req->num_outstanding_cmds; index++) {
356 if (handle == req->num_outstanding_cmds)
358 if (!req->outstanding_cmds[handle])
361 if (index == req->num_outstanding_cmds)
364 /* Map the sg table so we have an accurate count of sg entries needed */
365 if (scsi_sg_count(cmd)) {
366 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
367 scsi_sg_count(cmd), cmd->sc_data_direction);
375 /* Calculate the number of request entries needed. */
376 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
377 if (req->cnt < (req_cnt + 2)) {
378 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
379 if (req->ring_index < cnt)
380 req->cnt = cnt - req->ring_index;
382 req->cnt = req->length -
383 (req->ring_index - cnt);
384 /* If still no head room then bail out */
385 if (req->cnt < (req_cnt + 2))
389 /* Build command packet */
390 req->current_outstanding_cmd = handle;
391 req->outstanding_cmds[handle] = sp;
393 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
396 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
397 cmd_pkt->handle = handle;
398 /* Zero out remaining portion of packet. */
399 clr_ptr = (uint32_t *)cmd_pkt + 2;
400 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
401 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
403 /* Set target ID and LUN number*/
404 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
405 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
406 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
408 /* Load SCSI command packet. */
409 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
410 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
412 /* Build IOCB segments */
413 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
415 /* Set total data segment count. */
416 cmd_pkt->entry_count = (uint8_t)req_cnt;
419 /* Adjust ring index. */
421 if (req->ring_index == req->length) {
423 req->ring_ptr = req->ring;
427 sp->flags |= SRB_DMA_VALID;
429 /* Set chip new ring index. */
430 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
431 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
433 /* Manage unprocessed RIO/ZIO commands in response queue. */
434 if (vha->flags.process_response_queue &&
435 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
436 qla2x00_process_response_queue(rsp);
438 spin_unlock_irqrestore(&ha->hardware_lock, flags);
439 return (QLA_SUCCESS);
445 spin_unlock_irqrestore(&ha->hardware_lock, flags);
447 return (QLA_FUNCTION_FAILED);
451 * qla2x00_start_iocbs() - Execute the IOCB command
454 qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
456 struct qla_hw_data *ha = vha->hw;
457 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
459 if (IS_P3P_TYPE(ha)) {
460 qla82xx_start_iocbs(vha);
462 /* Adjust ring index. */
464 if (req->ring_index == req->length) {
466 req->ring_ptr = req->ring;
470 /* Set chip new ring index. */
471 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
472 WRT_REG_DWORD(req->req_q_in, req->ring_index);
473 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
474 } else if (IS_QLAFX00(ha)) {
475 WRT_REG_DWORD(®->ispfx00.req_q_in, req->ring_index);
476 RD_REG_DWORD_RELAXED(®->ispfx00.req_q_in);
477 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
478 } else if (IS_FWI2_CAPABLE(ha)) {
479 WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index);
480 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
482 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp),
484 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
490 * qla2x00_marker() - Send a marker IOCB to the firmware.
494 * @type: marker modifier
496 * Can be called from both normal and interrupt context.
498 * Returns non-zero if a failure occurred, else zero.
501 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
502 struct rsp_que *rsp, uint16_t loop_id,
503 uint64_t lun, uint8_t type)
506 struct mrk_entry_24xx *mrk24 = NULL;
508 struct qla_hw_data *ha = vha->hw;
509 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
511 req = ha->req_q_map[0];
512 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
514 ql_log(ql_log_warn, base_vha, 0x3026,
515 "Failed to allocate Marker IOCB.\n");
517 return (QLA_FUNCTION_FAILED);
520 mrk->entry_type = MARKER_TYPE;
521 mrk->modifier = type;
522 if (type != MK_SYNC_ALL) {
523 if (IS_FWI2_CAPABLE(ha)) {
524 mrk24 = (struct mrk_entry_24xx *) mrk;
525 mrk24->nport_handle = cpu_to_le16(loop_id);
526 int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
527 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
528 mrk24->vp_index = vha->vp_idx;
529 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
531 SET_TARGET_ID(ha, mrk->target, loop_id);
532 mrk->lun = cpu_to_le16((uint16_t)lun);
537 qla2x00_start_iocbs(vha, req);
539 return (QLA_SUCCESS);
543 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
544 struct rsp_que *rsp, uint16_t loop_id, uint64_t lun,
548 unsigned long flags = 0;
550 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
551 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
552 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
558 * qla2x00_issue_marker
561 * Caller CAN have hardware lock held as specified by ha_locked parameter.
562 * Might release it, then reaquire.
564 int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
567 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
568 MK_SYNC_ALL) != QLA_SUCCESS)
569 return QLA_FUNCTION_FAILED;
571 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
572 MK_SYNC_ALL) != QLA_SUCCESS)
573 return QLA_FUNCTION_FAILED;
575 vha->marker_needed = 0;
581 qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
584 uint32_t *cur_dsd = NULL;
585 scsi_qla_host_t *vha;
586 struct qla_hw_data *ha;
587 struct scsi_cmnd *cmd;
588 struct scatterlist *cur_seg;
592 uint8_t first_iocb = 1;
593 uint32_t dsd_list_len;
594 struct dsd_dma *dsd_ptr;
597 cmd = GET_CMD_SP(sp);
599 /* Update entry type to indicate Command Type 3 IOCB */
600 *((uint32_t *)(&cmd_pkt->entry_type)) =
601 __constant_cpu_to_le32(COMMAND_TYPE_6);
603 /* No data transfer */
604 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
605 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
609 vha = sp->fcport->vha;
612 /* Set transfer direction */
613 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
614 cmd_pkt->control_flags =
615 __constant_cpu_to_le16(CF_WRITE_DATA);
616 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
617 vha->qla_stats.output_requests++;
618 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
619 cmd_pkt->control_flags =
620 __constant_cpu_to_le16(CF_READ_DATA);
621 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
622 vha->qla_stats.input_requests++;
625 cur_seg = scsi_sglist(cmd);
626 ctx = GET_CMD_CTX_SP(sp);
629 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
630 QLA_DSDS_PER_IOCB : tot_dsds;
631 tot_dsds -= avail_dsds;
632 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
634 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
635 struct dsd_dma, list);
636 next_dsd = dsd_ptr->dsd_addr;
637 list_del(&dsd_ptr->list);
639 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
645 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
646 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
647 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
648 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
650 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
651 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
652 *cur_dsd++ = cpu_to_le32(dsd_list_len);
654 cur_dsd = (uint32_t *)next_dsd;
658 sle_dma = sg_dma_address(cur_seg);
659 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
660 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
661 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
662 cur_seg = sg_next(cur_seg);
667 /* Null termination */
671 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
676 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
677 * for Command Type 6.
679 * @dsds: number of data segment decriptors needed
681 * Returns the number of dsd list needed to store @dsds.
684 qla24xx_calc_dsd_lists(uint16_t dsds)
686 uint16_t dsd_lists = 0;
688 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
689 if (dsds % QLA_DSDS_PER_IOCB)
696 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
699 * @sp: SRB command to process
700 * @cmd_pkt: Command type 3 IOCB
701 * @tot_dsds: Total number of segments to transfer
704 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
709 scsi_qla_host_t *vha;
710 struct scsi_cmnd *cmd;
711 struct scatterlist *sg;
715 cmd = GET_CMD_SP(sp);
717 /* Update entry type to indicate Command Type 3 IOCB */
718 *((uint32_t *)(&cmd_pkt->entry_type)) =
719 __constant_cpu_to_le32(COMMAND_TYPE_7);
721 /* No data transfer */
722 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
723 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
727 vha = sp->fcport->vha;
730 /* Set transfer direction */
731 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
732 cmd_pkt->task_mgmt_flags =
733 __constant_cpu_to_le16(TMF_WRITE_DATA);
734 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
735 vha->qla_stats.output_requests++;
736 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
737 cmd_pkt->task_mgmt_flags =
738 __constant_cpu_to_le16(TMF_READ_DATA);
739 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
740 vha->qla_stats.input_requests++;
743 /* One DSD is available in the Command Type 3 IOCB */
745 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
747 /* Load data segments */
749 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
751 cont_a64_entry_t *cont_pkt;
753 /* Allocate additional continuation packets? */
754 if (avail_dsds == 0) {
756 * Five DSDs are available in the Continuation
759 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
760 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
764 sle_dma = sg_dma_address(sg);
765 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
766 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
767 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
772 struct fw_dif_context {
775 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
776 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
780 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
784 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
785 unsigned int protcnt)
787 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
789 switch (scsi_get_prot_type(cmd)) {
790 case SCSI_PROT_DIF_TYPE0:
792 * No check for ql2xenablehba_err_chk, as it would be an
793 * I/O error if hba tag generation is not done.
795 pkt->ref_tag = cpu_to_le32((uint32_t)
796 (0xffffffff & scsi_get_lba(cmd)));
798 if (!qla2x00_hba_err_chk_enabled(sp))
801 pkt->ref_tag_mask[0] = 0xff;
802 pkt->ref_tag_mask[1] = 0xff;
803 pkt->ref_tag_mask[2] = 0xff;
804 pkt->ref_tag_mask[3] = 0xff;
808 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
809 * match LBA in CDB + N
811 case SCSI_PROT_DIF_TYPE2:
812 pkt->app_tag = __constant_cpu_to_le16(0);
813 pkt->app_tag_mask[0] = 0x0;
814 pkt->app_tag_mask[1] = 0x0;
816 pkt->ref_tag = cpu_to_le32((uint32_t)
817 (0xffffffff & scsi_get_lba(cmd)));
819 if (!qla2x00_hba_err_chk_enabled(sp))
822 /* enable ALL bytes of the ref tag */
823 pkt->ref_tag_mask[0] = 0xff;
824 pkt->ref_tag_mask[1] = 0xff;
825 pkt->ref_tag_mask[2] = 0xff;
826 pkt->ref_tag_mask[3] = 0xff;
829 /* For Type 3 protection: 16 bit GUARD only */
830 case SCSI_PROT_DIF_TYPE3:
831 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
832 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
837 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
840 case SCSI_PROT_DIF_TYPE1:
841 pkt->ref_tag = cpu_to_le32((uint32_t)
842 (0xffffffff & scsi_get_lba(cmd)));
843 pkt->app_tag = __constant_cpu_to_le16(0);
844 pkt->app_tag_mask[0] = 0x0;
845 pkt->app_tag_mask[1] = 0x0;
847 if (!qla2x00_hba_err_chk_enabled(sp))
850 /* enable ALL bytes of the ref tag */
851 pkt->ref_tag_mask[0] = 0xff;
852 pkt->ref_tag_mask[1] = 0xff;
853 pkt->ref_tag_mask[2] = 0xff;
854 pkt->ref_tag_mask[3] = 0xff;
860 dma_addr_t dma_addr; /* OUT */
861 uint32_t dma_len; /* OUT */
863 uint32_t tot_bytes; /* IN */
864 struct scatterlist *cur_sg; /* IN */
866 /* for book keeping, bzero on initial invocation */
867 uint32_t bytes_consumed;
869 uint32_t tot_partial;
877 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
880 struct scatterlist *sg;
881 uint32_t cumulative_partial, sg_len;
882 dma_addr_t sg_dma_addr;
884 if (sgx->num_bytes == sgx->tot_bytes)
888 cumulative_partial = sgx->tot_partial;
890 sg_dma_addr = sg_dma_address(sg);
891 sg_len = sg_dma_len(sg);
893 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
895 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
896 sgx->dma_len = (blk_sz - cumulative_partial);
897 sgx->tot_partial = 0;
898 sgx->num_bytes += blk_sz;
901 sgx->dma_len = sg_len - sgx->bytes_consumed;
902 sgx->tot_partial += sgx->dma_len;
906 sgx->bytes_consumed += sgx->dma_len;
908 if (sg_len == sgx->bytes_consumed) {
912 sgx->bytes_consumed = 0;
919 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
920 uint32_t *dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
923 uint8_t avail_dsds = 0;
924 uint32_t dsd_list_len;
925 struct dsd_dma *dsd_ptr;
926 struct scatterlist *sg_prot;
927 uint32_t *cur_dsd = dsd;
928 uint16_t used_dsds = tot_dsds;
930 uint32_t prot_int; /* protection interval */
934 uint32_t sle_dma_len, tot_prot_dma_len = 0;
935 struct scsi_cmnd *cmd;
936 struct scsi_qla_host *vha;
938 memset(&sgx, 0, sizeof(struct qla2_sgx));
940 vha = sp->fcport->vha;
941 cmd = GET_CMD_SP(sp);
942 prot_int = cmd->device->sector_size;
944 sgx.tot_bytes = scsi_bufflen(cmd);
945 sgx.cur_sg = scsi_sglist(cmd);
948 sg_prot = scsi_prot_sglist(cmd);
951 prot_int = tc->blk_sz;
952 sgx.tot_bytes = tc->bufflen;
954 sg_prot = tc->prot_sg;
960 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
962 sle_dma = sgx.dma_addr;
963 sle_dma_len = sgx.dma_len;
965 /* Allocate additional continuation packets? */
966 if (avail_dsds == 0) {
967 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
968 QLA_DSDS_PER_IOCB : used_dsds;
969 dsd_list_len = (avail_dsds + 1) * 12;
970 used_dsds -= avail_dsds;
972 /* allocate tracking DS */
973 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
977 /* allocate new list */
978 dsd_ptr->dsd_addr = next_dsd =
979 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
980 &dsd_ptr->dsd_list_dma);
984 * Need to cleanup only this dsd_ptr, rest
985 * will be done by sp_free_dma()
992 list_add_tail(&dsd_ptr->list,
993 &((struct crc_context *)
994 sp->u.scmd.ctx)->dsd_list);
996 sp->flags |= SRB_CRC_CTX_DSD_VALID;
998 list_add_tail(&dsd_ptr->list,
999 &(tc->ctx->dsd_list));
1000 tc->ctx_dsd_alloced = 1;
1004 /* add new list to cmd iocb or last list */
1005 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1006 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1007 *cur_dsd++ = dsd_list_len;
1008 cur_dsd = (uint32_t *)next_dsd;
1010 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1011 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1012 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1016 /* Got a full protection interval */
1017 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1020 tot_prot_dma_len += sle_dma_len;
1021 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1022 tot_prot_dma_len = 0;
1023 sg_prot = sg_next(sg_prot);
1026 partial = 1; /* So as to not re-enter this block */
1027 goto alloc_and_fill;
1030 /* Null termination */
1038 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1039 uint16_t tot_dsds, struct qla_tgt_cmd *tc)
1042 uint8_t avail_dsds = 0;
1043 uint32_t dsd_list_len;
1044 struct dsd_dma *dsd_ptr;
1045 struct scatterlist *sg, *sgl;
1046 uint32_t *cur_dsd = dsd;
1048 uint16_t used_dsds = tot_dsds;
1049 struct scsi_cmnd *cmd;
1050 struct scsi_qla_host *vha;
1053 cmd = GET_CMD_SP(sp);
1054 sgl = scsi_sglist(cmd);
1055 vha = sp->fcport->vha;
1065 for_each_sg(sgl, sg, tot_dsds, i) {
1068 /* Allocate additional continuation packets? */
1069 if (avail_dsds == 0) {
1070 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1071 QLA_DSDS_PER_IOCB : used_dsds;
1072 dsd_list_len = (avail_dsds + 1) * 12;
1073 used_dsds -= avail_dsds;
1075 /* allocate tracking DS */
1076 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1080 /* allocate new list */
1081 dsd_ptr->dsd_addr = next_dsd =
1082 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1083 &dsd_ptr->dsd_list_dma);
1087 * Need to cleanup only this dsd_ptr, rest
1088 * will be done by sp_free_dma()
1095 list_add_tail(&dsd_ptr->list,
1096 &((struct crc_context *)
1097 sp->u.scmd.ctx)->dsd_list);
1099 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1101 list_add_tail(&dsd_ptr->list,
1102 &(tc->ctx->dsd_list));
1103 tc->ctx_dsd_alloced = 1;
1106 /* add new list to cmd iocb or last list */
1107 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1108 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1109 *cur_dsd++ = dsd_list_len;
1110 cur_dsd = (uint32_t *)next_dsd;
1112 sle_dma = sg_dma_address(sg);
1114 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1115 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1116 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1120 /* Null termination */
1128 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1129 uint32_t *dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
1132 uint8_t avail_dsds = 0;
1133 uint32_t dsd_list_len;
1134 struct dsd_dma *dsd_ptr;
1135 struct scatterlist *sg, *sgl;
1137 struct scsi_cmnd *cmd;
1138 uint32_t *cur_dsd = dsd;
1139 uint16_t used_dsds = tot_dsds;
1140 struct scsi_qla_host *vha;
1143 cmd = GET_CMD_SP(sp);
1144 sgl = scsi_prot_sglist(cmd);
1145 vha = sp->fcport->vha;
1154 ql_dbg(ql_dbg_tgt, vha, 0xe021,
1155 "%s: enter\n", __func__);
1157 for_each_sg(sgl, sg, tot_dsds, i) {
1160 /* Allocate additional continuation packets? */
1161 if (avail_dsds == 0) {
1162 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1163 QLA_DSDS_PER_IOCB : used_dsds;
1164 dsd_list_len = (avail_dsds + 1) * 12;
1165 used_dsds -= avail_dsds;
1167 /* allocate tracking DS */
1168 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1172 /* allocate new list */
1173 dsd_ptr->dsd_addr = next_dsd =
1174 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1175 &dsd_ptr->dsd_list_dma);
1179 * Need to cleanup only this dsd_ptr, rest
1180 * will be done by sp_free_dma()
1187 list_add_tail(&dsd_ptr->list,
1188 &((struct crc_context *)
1189 sp->u.scmd.ctx)->dsd_list);
1191 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1193 list_add_tail(&dsd_ptr->list,
1194 &(tc->ctx->dsd_list));
1195 tc->ctx_dsd_alloced = 1;
1198 /* add new list to cmd iocb or last list */
1199 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1200 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1201 *cur_dsd++ = dsd_list_len;
1202 cur_dsd = (uint32_t *)next_dsd;
1204 sle_dma = sg_dma_address(sg);
1206 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1207 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1208 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1212 /* Null termination */
1220 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1221 * Type 6 IOCB types.
1223 * @sp: SRB command to process
1224 * @cmd_pkt: Command type 3 IOCB
1225 * @tot_dsds: Total number of segments to transfer
1228 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1229 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1231 uint32_t *cur_dsd, *fcp_dl;
1232 scsi_qla_host_t *vha;
1233 struct scsi_cmnd *cmd;
1235 uint32_t total_bytes = 0;
1236 uint32_t data_bytes;
1238 uint8_t bundling = 1;
1241 struct crc_context *crc_ctx_pkt = NULL;
1242 struct qla_hw_data *ha;
1243 uint8_t additional_fcpcdb_len;
1244 uint16_t fcp_cmnd_len;
1245 struct fcp_cmnd *fcp_cmnd;
1246 dma_addr_t crc_ctx_dma;
1248 cmd = GET_CMD_SP(sp);
1251 /* Update entry type to indicate Command Type CRC_2 IOCB */
1252 *((uint32_t *)(&cmd_pkt->entry_type)) =
1253 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1255 vha = sp->fcport->vha;
1258 /* No data transfer */
1259 data_bytes = scsi_bufflen(cmd);
1260 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1261 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1265 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
1267 /* Set transfer direction */
1268 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1269 cmd_pkt->control_flags =
1270 __constant_cpu_to_le16(CF_WRITE_DATA);
1271 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1272 cmd_pkt->control_flags =
1273 __constant_cpu_to_le16(CF_READ_DATA);
1276 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1277 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1278 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1279 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
1282 /* Allocate CRC context from global pool */
1283 crc_ctx_pkt = sp->u.scmd.ctx =
1284 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
1287 goto crc_queuing_error;
1289 /* Zero out CTX area. */
1290 clr_ptr = (uint8_t *)crc_ctx_pkt;
1291 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1293 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1295 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1298 crc_ctx_pkt->handle = cmd_pkt->handle;
1300 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1302 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
1303 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1305 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1306 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1307 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1309 /* Determine SCSI command length -- align to 4 byte boundary */
1310 if (cmd->cmd_len > 16) {
1311 additional_fcpcdb_len = cmd->cmd_len - 16;
1312 if ((cmd->cmd_len % 4) != 0) {
1313 /* SCSI cmd > 16 bytes must be multiple of 4 */
1314 goto crc_queuing_error;
1316 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1318 additional_fcpcdb_len = 0;
1319 fcp_cmnd_len = 12 + 16 + 4;
1322 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1324 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1325 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1326 fcp_cmnd->additional_cdb_len |= 1;
1327 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1328 fcp_cmnd->additional_cdb_len |= 2;
1330 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
1331 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1332 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1333 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1334 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1335 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1336 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1337 fcp_cmnd->task_management = 0;
1338 fcp_cmnd->task_attribute = TSK_SIMPLE;
1340 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1342 /* Compute dif len and adjust data len to incude protection */
1344 blk_size = cmd->device->sector_size;
1345 dif_bytes = (data_bytes / blk_size) * 8;
1347 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
1348 case SCSI_PROT_READ_INSERT:
1349 case SCSI_PROT_WRITE_STRIP:
1350 total_bytes = data_bytes;
1351 data_bytes += dif_bytes;
1354 case SCSI_PROT_READ_STRIP:
1355 case SCSI_PROT_WRITE_INSERT:
1356 case SCSI_PROT_READ_PASS:
1357 case SCSI_PROT_WRITE_PASS:
1358 total_bytes = data_bytes + dif_bytes;
1364 if (!qla2x00_hba_err_chk_enabled(sp))
1365 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1366 /* HBA error checking enabled */
1367 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1368 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1369 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1370 SCSI_PROT_DIF_TYPE2))
1371 fw_prot_opts |= BIT_10;
1372 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1373 SCSI_PROT_DIF_TYPE3)
1374 fw_prot_opts |= BIT_11;
1378 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1381 * Configure Bundling if we need to fetch interlaving
1382 * protection PCI accesses
1384 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1385 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1386 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1388 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1391 /* Finish the common fields of CRC pkt */
1392 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1393 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1394 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1395 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1396 /* Fibre channel byte count */
1397 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1398 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1399 additional_fcpcdb_len);
1400 *fcp_dl = htonl(total_bytes);
1402 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1403 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1406 /* Walks data segments */
1408 cmd_pkt->control_flags |=
1409 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1411 if (!bundling && tot_prot_dsds) {
1412 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1413 cur_dsd, tot_dsds, NULL))
1414 goto crc_queuing_error;
1415 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1416 (tot_dsds - tot_prot_dsds), NULL))
1417 goto crc_queuing_error;
1419 if (bundling && tot_prot_dsds) {
1420 /* Walks dif segments */
1421 cmd_pkt->control_flags |=
1422 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1423 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1424 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1425 tot_prot_dsds, NULL))
1426 goto crc_queuing_error;
1431 /* Cleanup will be performed by the caller */
1433 return QLA_FUNCTION_FAILED;
1437 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1438 * @sp: command to send to the ISP
1440 * Returns non-zero if a failure occurred, else zero.
1443 qla24xx_start_scsi(srb_t *sp)
1446 unsigned long flags;
1450 struct cmd_type_7 *cmd_pkt;
1454 struct req_que *req = NULL;
1455 struct rsp_que *rsp = NULL;
1456 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1457 struct scsi_qla_host *vha = sp->fcport->vha;
1458 struct qla_hw_data *ha = vha->hw;
1460 /* Setup device pointers. */
1463 qla25xx_set_que(sp, &rsp);
1466 /* So we know we haven't pci_map'ed anything yet */
1469 /* Send marker if required */
1470 if (vha->marker_needed != 0) {
1471 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1473 return QLA_FUNCTION_FAILED;
1474 vha->marker_needed = 0;
1477 /* Acquire ring specific lock */
1478 spin_lock_irqsave(&ha->hardware_lock, flags);
1480 /* Check for room in outstanding command list. */
1481 handle = req->current_outstanding_cmd;
1482 for (index = 1; index < req->num_outstanding_cmds; index++) {
1484 if (handle == req->num_outstanding_cmds)
1486 if (!req->outstanding_cmds[handle])
1489 if (index == req->num_outstanding_cmds)
1492 /* Map the sg table so we have an accurate count of sg entries needed */
1493 if (scsi_sg_count(cmd)) {
1494 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1495 scsi_sg_count(cmd), cmd->sc_data_direction);
1496 if (unlikely(!nseg))
1502 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1503 if (req->cnt < (req_cnt + 2)) {
1504 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1505 RD_REG_DWORD_RELAXED(req->req_q_out);
1506 if (req->ring_index < cnt)
1507 req->cnt = cnt - req->ring_index;
1509 req->cnt = req->length -
1510 (req->ring_index - cnt);
1511 if (req->cnt < (req_cnt + 2))
1515 /* Build command packet. */
1516 req->current_outstanding_cmd = handle;
1517 req->outstanding_cmds[handle] = sp;
1518 sp->handle = handle;
1519 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1520 req->cnt -= req_cnt;
1522 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1523 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1525 /* Zero out remaining portion of packet. */
1526 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
1527 clr_ptr = (uint32_t *)cmd_pkt + 2;
1528 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1529 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1531 /* Set NPORT-ID and LUN number*/
1532 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1533 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1534 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1535 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1536 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
1538 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1539 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1541 cmd_pkt->task = TSK_SIMPLE;
1543 /* Load SCSI command packet. */
1544 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1545 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1547 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1549 /* Build IOCB segments */
1550 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1552 /* Set total data segment count. */
1553 cmd_pkt->entry_count = (uint8_t)req_cnt;
1554 /* Specify response queue number where completion should happen */
1555 cmd_pkt->entry_status = (uint8_t) rsp->id;
1557 /* Adjust ring index. */
1559 if (req->ring_index == req->length) {
1560 req->ring_index = 0;
1561 req->ring_ptr = req->ring;
1565 sp->flags |= SRB_DMA_VALID;
1567 /* Set chip new ring index. */
1568 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1569 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1571 /* Manage unprocessed RIO/ZIO commands in response queue. */
1572 if (vha->flags.process_response_queue &&
1573 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1574 qla24xx_process_response_queue(vha, rsp);
1576 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1581 scsi_dma_unmap(cmd);
1583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1585 return QLA_FUNCTION_FAILED;
1589 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1590 * @sp: command to send to the ISP
1592 * Returns non-zero if a failure occurred, else zero.
1595 qla24xx_dif_start_scsi(srb_t *sp)
1598 unsigned long flags;
1603 uint16_t req_cnt = 0;
1605 uint16_t tot_prot_dsds;
1606 uint16_t fw_prot_opts = 0;
1607 struct req_que *req = NULL;
1608 struct rsp_que *rsp = NULL;
1609 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1610 struct scsi_qla_host *vha = sp->fcport->vha;
1611 struct qla_hw_data *ha = vha->hw;
1612 struct cmd_type_crc_2 *cmd_pkt;
1613 uint32_t status = 0;
1615 #define QDSS_GOT_Q_SPACE BIT_0
1617 /* Only process protection or >16 cdb in this routine */
1618 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1619 if (cmd->cmd_len <= 16)
1620 return qla24xx_start_scsi(sp);
1623 /* Setup device pointers. */
1625 qla25xx_set_que(sp, &rsp);
1628 /* So we know we haven't pci_map'ed anything yet */
1631 /* Send marker if required */
1632 if (vha->marker_needed != 0) {
1633 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1635 return QLA_FUNCTION_FAILED;
1636 vha->marker_needed = 0;
1639 /* Acquire ring specific lock */
1640 spin_lock_irqsave(&ha->hardware_lock, flags);
1642 /* Check for room in outstanding command list. */
1643 handle = req->current_outstanding_cmd;
1644 for (index = 1; index < req->num_outstanding_cmds; index++) {
1646 if (handle == req->num_outstanding_cmds)
1648 if (!req->outstanding_cmds[handle])
1652 if (index == req->num_outstanding_cmds)
1655 /* Compute number of required data segments */
1656 /* Map the sg table so we have an accurate count of sg entries needed */
1657 if (scsi_sg_count(cmd)) {
1658 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1659 scsi_sg_count(cmd), cmd->sc_data_direction);
1660 if (unlikely(!nseg))
1663 sp->flags |= SRB_DMA_VALID;
1665 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1666 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1667 struct qla2_sgx sgx;
1670 memset(&sgx, 0, sizeof(struct qla2_sgx));
1671 sgx.tot_bytes = scsi_bufflen(cmd);
1672 sgx.cur_sg = scsi_sglist(cmd);
1676 while (qla24xx_get_one_block_sg(
1677 cmd->device->sector_size, &sgx, &partial))
1683 /* number of required data segments */
1686 /* Compute number of required protection segments */
1687 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1688 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1689 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1690 if (unlikely(!nseg))
1693 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1695 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1696 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1697 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1704 /* Total Data and protection sg segment(s) */
1705 tot_prot_dsds = nseg;
1707 if (req->cnt < (req_cnt + 2)) {
1708 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1709 RD_REG_DWORD_RELAXED(req->req_q_out);
1710 if (req->ring_index < cnt)
1711 req->cnt = cnt - req->ring_index;
1713 req->cnt = req->length -
1714 (req->ring_index - cnt);
1715 if (req->cnt < (req_cnt + 2))
1719 status |= QDSS_GOT_Q_SPACE;
1721 /* Build header part of command packet (excluding the OPCODE). */
1722 req->current_outstanding_cmd = handle;
1723 req->outstanding_cmds[handle] = sp;
1724 sp->handle = handle;
1725 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1726 req->cnt -= req_cnt;
1728 /* Fill-in common area */
1729 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1730 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1732 clr_ptr = (uint32_t *)cmd_pkt + 2;
1733 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1735 /* Set NPORT-ID and LUN number*/
1736 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1737 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1738 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1739 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1741 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1742 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1744 /* Total Data and protection segment(s) */
1745 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1747 /* Build IOCB segments and adjust for data protection segments */
1748 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1749 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1753 cmd_pkt->entry_count = (uint8_t)req_cnt;
1754 /* Specify response queue number where completion should happen */
1755 cmd_pkt->entry_status = (uint8_t) rsp->id;
1756 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1759 /* Adjust ring index. */
1761 if (req->ring_index == req->length) {
1762 req->ring_index = 0;
1763 req->ring_ptr = req->ring;
1767 /* Set chip new ring index. */
1768 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1769 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1771 /* Manage unprocessed RIO/ZIO commands in response queue. */
1772 if (vha->flags.process_response_queue &&
1773 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1774 qla24xx_process_response_queue(vha, rsp);
1776 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1781 if (status & QDSS_GOT_Q_SPACE) {
1782 req->outstanding_cmds[handle] = NULL;
1783 req->cnt += req_cnt;
1785 /* Cleanup will be performed by the caller (queuecommand) */
1787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1788 return QLA_FUNCTION_FAILED;
1792 static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
1794 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1795 struct qla_hw_data *ha = sp->fcport->vha->hw;
1796 int affinity = cmd->request->cpu;
1798 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
1799 affinity < ha->max_rsp_queues - 1)
1800 *rsp = ha->rsp_q_map[affinity + 1];
1802 *rsp = ha->rsp_q_map[0];
1805 /* Generic Control-SRB manipulation functions. */
1807 /* hardware_lock assumed to be held. */
1809 qla2x00_alloc_iocbs_ready(scsi_qla_host_t *vha, srb_t *sp)
1811 if (qla2x00_reset_active(vha))
1814 return qla2x00_alloc_iocbs(vha, sp);
1818 qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
1820 struct qla_hw_data *ha = vha->hw;
1821 struct req_que *req = ha->req_q_map[0];
1822 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1823 uint32_t index, handle;
1825 uint16_t cnt, req_cnt;
1832 goto skip_cmd_array;
1834 /* Check for room in outstanding command list. */
1835 handle = req->current_outstanding_cmd;
1836 for (index = 1; index < req->num_outstanding_cmds; index++) {
1838 if (handle == req->num_outstanding_cmds)
1840 if (!req->outstanding_cmds[handle])
1843 if (index == req->num_outstanding_cmds) {
1844 ql_log(ql_log_warn, vha, 0x700b,
1845 "No room on outstanding cmd array.\n");
1849 /* Prep command array. */
1850 req->current_outstanding_cmd = handle;
1851 req->outstanding_cmds[handle] = sp;
1852 sp->handle = handle;
1854 /* Adjust entry-counts as needed. */
1855 if (sp->type != SRB_SCSI_CMD)
1856 req_cnt = sp->iocbs;
1859 /* Check for room on request queue. */
1860 if (req->cnt < req_cnt + 2) {
1861 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha))
1862 cnt = RD_REG_DWORD(®->isp25mq.req_q_out);
1863 else if (IS_P3P_TYPE(ha))
1864 cnt = RD_REG_DWORD(®->isp82.req_q_out);
1865 else if (IS_FWI2_CAPABLE(ha))
1866 cnt = RD_REG_DWORD(®->isp24.req_q_out);
1867 else if (IS_QLAFX00(ha))
1868 cnt = RD_REG_DWORD(®->ispfx00.req_q_out);
1870 cnt = qla2x00_debounce_register(
1871 ISP_REQ_Q_OUT(ha, ®->isp));
1873 if (req->ring_index < cnt)
1874 req->cnt = cnt - req->ring_index;
1876 req->cnt = req->length -
1877 (req->ring_index - cnt);
1879 if (req->cnt < req_cnt + 2)
1883 req->cnt -= req_cnt;
1884 pkt = req->ring_ptr;
1885 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1886 if (IS_QLAFX00(ha)) {
1887 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
1888 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
1890 pkt->entry_count = req_cnt;
1891 pkt->handle = handle;
1899 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1901 struct srb_iocb *lio = &sp->u.iocb_cmd;
1903 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1904 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
1905 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
1906 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
1907 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
1908 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1909 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1910 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1911 logio->port_id[1] = sp->fcport->d_id.b.area;
1912 logio->port_id[2] = sp->fcport->d_id.b.domain;
1913 logio->vp_index = sp->fcport->vha->vp_idx;
1917 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1919 struct qla_hw_data *ha = sp->fcport->vha->hw;
1920 struct srb_iocb *lio = &sp->u.iocb_cmd;
1923 mbx->entry_type = MBX_IOCB_TYPE;
1924 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1925 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
1926 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1927 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
1928 if (HAS_EXTENDED_IDS(ha)) {
1929 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1930 mbx->mb10 = cpu_to_le16(opts);
1932 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1934 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1935 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1936 sp->fcport->d_id.b.al_pa);
1937 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
1941 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1943 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1944 logio->control_flags =
1945 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1946 if (!sp->fcport->tgt_session ||
1947 !sp->fcport->tgt_session->keep_nport_handle)
1948 logio->control_flags |= cpu_to_le16(LCF_FREE_NPORT);
1949 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1950 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1951 logio->port_id[1] = sp->fcport->d_id.b.area;
1952 logio->port_id[2] = sp->fcport->d_id.b.domain;
1953 logio->vp_index = sp->fcport->vha->vp_idx;
1957 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1959 struct qla_hw_data *ha = sp->fcport->vha->hw;
1961 mbx->entry_type = MBX_IOCB_TYPE;
1962 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1963 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1964 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1965 cpu_to_le16(sp->fcport->loop_id):
1966 cpu_to_le16(sp->fcport->loop_id << 8);
1967 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1968 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1969 sp->fcport->d_id.b.al_pa);
1970 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
1971 /* Implicit: mbx->mbx10 = 0. */
1975 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1977 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1978 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1979 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1980 logio->vp_index = sp->fcport->vha->vp_idx;
1984 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1986 struct qla_hw_data *ha = sp->fcport->vha->hw;
1988 mbx->entry_type = MBX_IOCB_TYPE;
1989 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1990 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1991 if (HAS_EXTENDED_IDS(ha)) {
1992 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1993 mbx->mb10 = cpu_to_le16(BIT_0);
1995 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1997 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1998 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1999 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
2000 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
2001 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
2005 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
2009 struct fc_port *fcport = sp->fcport;
2010 scsi_qla_host_t *vha = fcport->vha;
2011 struct qla_hw_data *ha = vha->hw;
2012 struct srb_iocb *iocb = &sp->u.iocb_cmd;
2013 struct req_que *req = vha->req;
2015 flags = iocb->u.tmf.flags;
2016 lun = iocb->u.tmf.lun;
2018 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2019 tsk->entry_count = 1;
2020 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2021 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2022 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2023 tsk->control_flags = cpu_to_le32(flags);
2024 tsk->port_id[0] = fcport->d_id.b.al_pa;
2025 tsk->port_id[1] = fcport->d_id.b.area;
2026 tsk->port_id[2] = fcport->d_id.b.domain;
2027 tsk->vp_index = fcport->vha->vp_idx;
2029 if (flags == TCF_LUN_RESET) {
2030 int_to_scsilun(lun, &tsk->lun);
2031 host_to_fcp_swap((uint8_t *)&tsk->lun,
2037 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2039 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2041 els_iocb->entry_type = ELS_IOCB_TYPE;
2042 els_iocb->entry_count = 1;
2043 els_iocb->sys_define = 0;
2044 els_iocb->entry_status = 0;
2045 els_iocb->handle = sp->handle;
2046 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2047 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2048 els_iocb->vp_index = sp->fcport->vha->vp_idx;
2049 els_iocb->sof_type = EST_SOFI3;
2050 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2053 sp->type == SRB_ELS_CMD_RPT ?
2054 bsg_job->request->rqst_data.r_els.els_code :
2055 bsg_job->request->rqst_data.h_els.command_code;
2056 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2057 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2058 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2059 els_iocb->control_flags = 0;
2060 els_iocb->rx_byte_count =
2061 cpu_to_le32(bsg_job->reply_payload.payload_len);
2062 els_iocb->tx_byte_count =
2063 cpu_to_le32(bsg_job->request_payload.payload_len);
2065 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2066 (bsg_job->request_payload.sg_list)));
2067 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2068 (bsg_job->request_payload.sg_list)));
2069 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2070 (bsg_job->request_payload.sg_list));
2072 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2073 (bsg_job->reply_payload.sg_list)));
2074 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2075 (bsg_job->reply_payload.sg_list)));
2076 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2077 (bsg_job->reply_payload.sg_list));
2079 sp->fcport->vha->qla_stats.control_requests++;
2083 qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2085 uint16_t avail_dsds;
2087 struct scatterlist *sg;
2090 scsi_qla_host_t *vha = sp->fcport->vha;
2091 struct qla_hw_data *ha = vha->hw;
2092 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2093 int loop_iterartion = 0;
2094 int cont_iocb_prsnt = 0;
2095 int entry_count = 1;
2097 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2098 ct_iocb->entry_type = CT_IOCB_TYPE;
2099 ct_iocb->entry_status = 0;
2100 ct_iocb->handle1 = sp->handle;
2101 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2102 ct_iocb->status = __constant_cpu_to_le16(0);
2103 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2104 ct_iocb->timeout = 0;
2105 ct_iocb->cmd_dsd_count =
2106 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2107 ct_iocb->total_dsd_count =
2108 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2109 ct_iocb->req_bytecount =
2110 cpu_to_le32(bsg_job->request_payload.payload_len);
2111 ct_iocb->rsp_bytecount =
2112 cpu_to_le32(bsg_job->reply_payload.payload_len);
2114 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2115 (bsg_job->request_payload.sg_list)));
2116 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2117 (bsg_job->request_payload.sg_list)));
2118 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2120 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2121 (bsg_job->reply_payload.sg_list)));
2122 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2123 (bsg_job->reply_payload.sg_list)));
2124 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2127 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2129 tot_dsds = bsg_job->reply_payload.sg_cnt;
2131 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2133 cont_a64_entry_t *cont_pkt;
2135 /* Allocate additional continuation packets? */
2136 if (avail_dsds == 0) {
2138 * Five DSDs are available in the Cont.
2141 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2142 vha->hw->req_q_map[0]);
2143 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2145 cont_iocb_prsnt = 1;
2149 sle_dma = sg_dma_address(sg);
2150 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2151 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2152 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2156 ct_iocb->entry_count = entry_count;
2158 sp->fcport->vha->qla_stats.control_requests++;
2162 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2164 uint16_t avail_dsds;
2166 struct scatterlist *sg;
2169 scsi_qla_host_t *vha = sp->fcport->vha;
2170 struct qla_hw_data *ha = vha->hw;
2171 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2172 int loop_iterartion = 0;
2173 int cont_iocb_prsnt = 0;
2174 int entry_count = 1;
2176 ct_iocb->entry_type = CT_IOCB_TYPE;
2177 ct_iocb->entry_status = 0;
2178 ct_iocb->sys_define = 0;
2179 ct_iocb->handle = sp->handle;
2181 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2182 ct_iocb->vp_index = sp->fcport->vha->vp_idx;
2183 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2185 ct_iocb->cmd_dsd_count =
2186 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2187 ct_iocb->timeout = 0;
2188 ct_iocb->rsp_dsd_count =
2189 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2190 ct_iocb->rsp_byte_count =
2191 cpu_to_le32(bsg_job->reply_payload.payload_len);
2192 ct_iocb->cmd_byte_count =
2193 cpu_to_le32(bsg_job->request_payload.payload_len);
2194 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2195 (bsg_job->request_payload.sg_list)));
2196 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2197 (bsg_job->request_payload.sg_list)));
2198 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2199 (bsg_job->request_payload.sg_list));
2202 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2204 tot_dsds = bsg_job->reply_payload.sg_cnt;
2206 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2208 cont_a64_entry_t *cont_pkt;
2210 /* Allocate additional continuation packets? */
2211 if (avail_dsds == 0) {
2213 * Five DSDs are available in the Cont.
2216 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2218 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2220 cont_iocb_prsnt = 1;
2224 sle_dma = sg_dma_address(sg);
2225 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2226 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2227 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2231 ct_iocb->entry_count = entry_count;
2235 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2236 * @sp: command to send to the ISP
2238 * Returns non-zero if a failure occurred, else zero.
2241 qla82xx_start_scsi(srb_t *sp)
2244 unsigned long flags;
2245 struct scsi_cmnd *cmd;
2252 struct device_reg_82xx __iomem *reg;
2255 uint8_t additional_cdb_len;
2256 struct ct6_dsd *ctx;
2257 struct scsi_qla_host *vha = sp->fcport->vha;
2258 struct qla_hw_data *ha = vha->hw;
2259 struct req_que *req = NULL;
2260 struct rsp_que *rsp = NULL;
2262 /* Setup device pointers. */
2264 reg = &ha->iobase->isp82;
2265 cmd = GET_CMD_SP(sp);
2267 rsp = ha->rsp_q_map[0];
2269 /* So we know we haven't pci_map'ed anything yet */
2272 dbval = 0x04 | (ha->portnum << 5);
2274 /* Send marker if required */
2275 if (vha->marker_needed != 0) {
2276 if (qla2x00_marker(vha, req,
2277 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2278 ql_log(ql_log_warn, vha, 0x300c,
2279 "qla2x00_marker failed for cmd=%p.\n", cmd);
2280 return QLA_FUNCTION_FAILED;
2282 vha->marker_needed = 0;
2285 /* Acquire ring specific lock */
2286 spin_lock_irqsave(&ha->hardware_lock, flags);
2288 /* Check for room in outstanding command list. */
2289 handle = req->current_outstanding_cmd;
2290 for (index = 1; index < req->num_outstanding_cmds; index++) {
2292 if (handle == req->num_outstanding_cmds)
2294 if (!req->outstanding_cmds[handle])
2297 if (index == req->num_outstanding_cmds)
2300 /* Map the sg table so we have an accurate count of sg entries needed */
2301 if (scsi_sg_count(cmd)) {
2302 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2303 scsi_sg_count(cmd), cmd->sc_data_direction);
2304 if (unlikely(!nseg))
2311 if (tot_dsds > ql2xshiftctondsd) {
2312 struct cmd_type_6 *cmd_pkt;
2313 uint16_t more_dsd_lists = 0;
2314 struct dsd_dma *dsd_ptr;
2317 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2318 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2319 ql_dbg(ql_dbg_io, vha, 0x300d,
2320 "Num of DSD list %d is than %d for cmd=%p.\n",
2321 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2326 if (more_dsd_lists <= ha->gbl_dsd_avail)
2327 goto sufficient_dsds;
2329 more_dsd_lists -= ha->gbl_dsd_avail;
2331 for (i = 0; i < more_dsd_lists; i++) {
2332 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2334 ql_log(ql_log_fatal, vha, 0x300e,
2335 "Failed to allocate memory for dsd_dma "
2336 "for cmd=%p.\n", cmd);
2340 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2341 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2342 if (!dsd_ptr->dsd_addr) {
2344 ql_log(ql_log_fatal, vha, 0x300f,
2345 "Failed to allocate memory for dsd_addr "
2346 "for cmd=%p.\n", cmd);
2349 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2350 ha->gbl_dsd_avail++;
2356 if (req->cnt < (req_cnt + 2)) {
2357 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2358 ®->req_q_out[0]);
2359 if (req->ring_index < cnt)
2360 req->cnt = cnt - req->ring_index;
2362 req->cnt = req->length -
2363 (req->ring_index - cnt);
2364 if (req->cnt < (req_cnt + 2))
2368 ctx = sp->u.scmd.ctx =
2369 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2371 ql_log(ql_log_fatal, vha, 0x3010,
2372 "Failed to allocate ctx for cmd=%p.\n", cmd);
2376 memset(ctx, 0, sizeof(struct ct6_dsd));
2377 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2378 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2379 if (!ctx->fcp_cmnd) {
2380 ql_log(ql_log_fatal, vha, 0x3011,
2381 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
2385 /* Initialize the DSD list and dma handle */
2386 INIT_LIST_HEAD(&ctx->dsd_list);
2387 ctx->dsd_use_cnt = 0;
2389 if (cmd->cmd_len > 16) {
2390 additional_cdb_len = cmd->cmd_len - 16;
2391 if ((cmd->cmd_len % 4) != 0) {
2392 /* SCSI command bigger than 16 bytes must be
2395 ql_log(ql_log_warn, vha, 0x3012,
2396 "scsi cmd len %d not multiple of 4 "
2397 "for cmd=%p.\n", cmd->cmd_len, cmd);
2398 goto queuing_error_fcp_cmnd;
2400 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2402 additional_cdb_len = 0;
2403 ctx->fcp_cmnd_len = 12 + 16 + 4;
2406 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2407 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2409 /* Zero out remaining portion of packet. */
2410 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2411 clr_ptr = (uint32_t *)cmd_pkt + 2;
2412 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2413 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2415 /* Set NPORT-ID and LUN number*/
2416 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2417 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2418 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2419 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2420 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2422 /* Build IOCB segments */
2423 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2424 goto queuing_error_fcp_cmnd;
2426 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2427 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2429 /* build FCP_CMND IU */
2430 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2431 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
2432 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2434 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2435 ctx->fcp_cmnd->additional_cdb_len |= 1;
2436 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2437 ctx->fcp_cmnd->additional_cdb_len |= 2;
2439 /* Populate the FCP_PRIO. */
2440 if (ha->flags.fcp_prio_enabled)
2441 ctx->fcp_cmnd->task_attribute |=
2442 sp->fcport->fcp_prio << 3;
2444 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2446 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2447 additional_cdb_len);
2448 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2450 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2451 cmd_pkt->fcp_cmnd_dseg_address[0] =
2452 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2453 cmd_pkt->fcp_cmnd_dseg_address[1] =
2454 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2456 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2457 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2458 /* Set total data segment count. */
2459 cmd_pkt->entry_count = (uint8_t)req_cnt;
2460 /* Specify response queue number where
2461 * completion should happen
2463 cmd_pkt->entry_status = (uint8_t) rsp->id;
2465 struct cmd_type_7 *cmd_pkt;
2466 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2467 if (req->cnt < (req_cnt + 2)) {
2468 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2469 ®->req_q_out[0]);
2470 if (req->ring_index < cnt)
2471 req->cnt = cnt - req->ring_index;
2473 req->cnt = req->length -
2474 (req->ring_index - cnt);
2476 if (req->cnt < (req_cnt + 2))
2479 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2480 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2482 /* Zero out remaining portion of packet. */
2483 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2484 clr_ptr = (uint32_t *)cmd_pkt + 2;
2485 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2486 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2488 /* Set NPORT-ID and LUN number*/
2489 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2490 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2491 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2492 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2493 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2495 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2496 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
2497 sizeof(cmd_pkt->lun));
2499 /* Populate the FCP_PRIO. */
2500 if (ha->flags.fcp_prio_enabled)
2501 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2503 /* Load SCSI command packet. */
2504 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2505 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2507 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2509 /* Build IOCB segments */
2510 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2512 /* Set total data segment count. */
2513 cmd_pkt->entry_count = (uint8_t)req_cnt;
2514 /* Specify response queue number where
2515 * completion should happen.
2517 cmd_pkt->entry_status = (uint8_t) rsp->id;
2520 /* Build command packet. */
2521 req->current_outstanding_cmd = handle;
2522 req->outstanding_cmds[handle] = sp;
2523 sp->handle = handle;
2524 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
2525 req->cnt -= req_cnt;
2528 /* Adjust ring index. */
2530 if (req->ring_index == req->length) {
2531 req->ring_index = 0;
2532 req->ring_ptr = req->ring;
2536 sp->flags |= SRB_DMA_VALID;
2538 /* Set chip new ring index. */
2539 /* write, read and verify logic */
2540 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2542 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2545 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2548 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
2550 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2556 /* Manage unprocessed RIO/ZIO commands in response queue. */
2557 if (vha->flags.process_response_queue &&
2558 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2559 qla24xx_process_response_queue(vha, rsp);
2561 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2564 queuing_error_fcp_cmnd:
2565 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2568 scsi_dma_unmap(cmd);
2570 if (sp->u.scmd.ctx) {
2571 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2572 sp->u.scmd.ctx = NULL;
2574 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2576 return QLA_FUNCTION_FAILED;
2580 qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
2582 struct srb_iocb *aio = &sp->u.iocb_cmd;
2583 scsi_qla_host_t *vha = sp->fcport->vha;
2584 struct req_que *req = vha->req;
2586 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
2587 abt_iocb->entry_type = ABORT_IOCB_TYPE;
2588 abt_iocb->entry_count = 1;
2589 abt_iocb->handle = cpu_to_le32(MAKE_HANDLE(req->id, sp->handle));
2590 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2591 abt_iocb->handle_to_abort =
2592 cpu_to_le32(MAKE_HANDLE(req->id, aio->u.abt.cmd_hndl));
2593 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2594 abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
2595 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2596 abt_iocb->vp_index = vha->vp_idx;
2597 abt_iocb->req_que_no = cpu_to_le16(req->id);
2598 /* Send the command to the firmware */
2603 qla2x00_start_sp(srb_t *sp)
2606 struct qla_hw_data *ha = sp->fcport->vha->hw;
2608 unsigned long flags;
2610 rval = QLA_FUNCTION_FAILED;
2611 spin_lock_irqsave(&ha->hardware_lock, flags);
2612 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
2614 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2615 "qla2x00_alloc_iocbs failed.\n");
2622 IS_FWI2_CAPABLE(ha) ?
2623 qla24xx_login_iocb(sp, pkt) :
2624 qla2x00_login_iocb(sp, pkt);
2626 case SRB_LOGOUT_CMD:
2627 IS_FWI2_CAPABLE(ha) ?
2628 qla24xx_logout_iocb(sp, pkt) :
2629 qla2x00_logout_iocb(sp, pkt);
2631 case SRB_ELS_CMD_RPT:
2632 case SRB_ELS_CMD_HST:
2633 qla24xx_els_iocb(sp, pkt);
2636 IS_FWI2_CAPABLE(ha) ?
2637 qla24xx_ct_iocb(sp, pkt) :
2638 qla2x00_ct_iocb(sp, pkt);
2641 IS_FWI2_CAPABLE(ha) ?
2642 qla24xx_adisc_iocb(sp, pkt) :
2643 qla2x00_adisc_iocb(sp, pkt);
2647 qlafx00_tm_iocb(sp, pkt) :
2648 qla24xx_tm_iocb(sp, pkt);
2650 case SRB_FXIOCB_DCMD:
2651 case SRB_FXIOCB_BCMD:
2652 qlafx00_fxdisc_iocb(sp, pkt);
2656 qlafx00_abort_iocb(sp, pkt) :
2657 qla24xx_abort_iocb(sp, pkt);
2664 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
2666 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2671 qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
2672 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
2674 uint16_t avail_dsds;
2676 uint32_t req_data_len = 0;
2677 uint32_t rsp_data_len = 0;
2678 struct scatterlist *sg;
2680 int entry_count = 1;
2681 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2683 /*Update entry type to indicate bidir command */
2684 *((uint32_t *)(&cmd_pkt->entry_type)) =
2685 __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
2687 /* Set the transfer direction, in this set both flags
2688 * Also set the BD_WRAP_BACK flag, firmware will take care
2689 * assigning DID=SID for outgoing pkts.
2691 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2692 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2693 cmd_pkt->control_flags =
2694 __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
2697 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2698 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
2699 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
2700 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
2702 vha->bidi_stats.transfer_bytes += req_data_len;
2703 vha->bidi_stats.io_count++;
2705 vha->qla_stats.output_bytes += req_data_len;
2706 vha->qla_stats.output_requests++;
2708 /* Only one dsd is available for bidirectional IOCB, remaining dsds
2709 * are bundled in continuation iocb
2712 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
2716 for_each_sg(bsg_job->request_payload.sg_list, sg,
2717 bsg_job->request_payload.sg_cnt, index) {
2719 cont_a64_entry_t *cont_pkt;
2721 /* Allocate additional continuation packets */
2722 if (avail_dsds == 0) {
2723 /* Continuation type 1 IOCB can accomodate
2726 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2727 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2731 sle_dma = sg_dma_address(sg);
2732 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2733 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2734 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2737 /* For read request DSD will always goes to continuation IOCB
2738 * and follow the write DSD. If there is room on the current IOCB
2739 * then it is added to that IOCB else new continuation IOCB is
2742 for_each_sg(bsg_job->reply_payload.sg_list, sg,
2743 bsg_job->reply_payload.sg_cnt, index) {
2745 cont_a64_entry_t *cont_pkt;
2747 /* Allocate additional continuation packets */
2748 if (avail_dsds == 0) {
2749 /* Continuation type 1 IOCB can accomodate
2752 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2753 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2757 sle_dma = sg_dma_address(sg);
2758 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2759 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2760 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2763 /* This value should be same as number of IOCB required for this cmd */
2764 cmd_pkt->entry_count = entry_count;
2768 qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
2771 struct qla_hw_data *ha = vha->hw;
2772 unsigned long flags;
2778 struct cmd_bidir *cmd_pkt = NULL;
2779 struct rsp_que *rsp;
2780 struct req_que *req;
2781 int rval = EXT_STATUS_OK;
2785 rsp = ha->rsp_q_map[0];
2788 /* Send marker if required */
2789 if (vha->marker_needed != 0) {
2790 if (qla2x00_marker(vha, req,
2791 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
2792 return EXT_STATUS_MAILBOX;
2793 vha->marker_needed = 0;
2796 /* Acquire ring specific lock */
2797 spin_lock_irqsave(&ha->hardware_lock, flags);
2799 /* Check for room in outstanding command list. */
2800 handle = req->current_outstanding_cmd;
2801 for (index = 1; index < req->num_outstanding_cmds; index++) {
2803 if (handle == req->num_outstanding_cmds)
2805 if (!req->outstanding_cmds[handle])
2809 if (index == req->num_outstanding_cmds) {
2810 rval = EXT_STATUS_BUSY;
2814 /* Calculate number of IOCB required */
2815 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2817 /* Check for room on request queue. */
2818 if (req->cnt < req_cnt + 2) {
2819 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
2820 RD_REG_DWORD_RELAXED(req->req_q_out);
2821 if (req->ring_index < cnt)
2822 req->cnt = cnt - req->ring_index;
2824 req->cnt = req->length -
2825 (req->ring_index - cnt);
2827 if (req->cnt < req_cnt + 2) {
2828 rval = EXT_STATUS_BUSY;
2832 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
2833 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2835 /* Zero out remaining portion of packet. */
2836 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2837 clr_ptr = (uint32_t *)cmd_pkt + 2;
2838 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2840 /* Set NPORT-ID (of vha)*/
2841 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
2842 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
2843 cmd_pkt->port_id[1] = vha->d_id.b.area;
2844 cmd_pkt->port_id[2] = vha->d_id.b.domain;
2846 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
2847 cmd_pkt->entry_status = (uint8_t) rsp->id;
2848 /* Build command packet. */
2849 req->current_outstanding_cmd = handle;
2850 req->outstanding_cmds[handle] = sp;
2851 sp->handle = handle;
2852 req->cnt -= req_cnt;
2854 /* Send the command to the firmware */
2856 qla2x00_start_iocbs(vha, req);
2858 spin_unlock_irqrestore(&ha->hardware_lock, flags);