1 // SPDX-License-Identifier: GPL-2.0-only
3 * QLogic Fibre Channel HBA Driver
4 * Copyright (c) 2003-2014 QLogic Corporation
7 #include "qla_target.h"
9 #include <linux/blkdev.h>
10 #include <linux/delay.h>
12 #include <scsi/scsi_tcq.h>
15 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
18 * Returns the proper CF_* direction based on CDB.
20 static inline uint16_t
21 qla2x00_get_cmd_direction(srb_t *sp)
24 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
25 struct scsi_qla_host *vha = sp->vha;
29 /* Set transfer direction */
30 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
32 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
33 vha->qla_stats.output_requests++;
34 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
36 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
37 vha->qla_stats.input_requests++;
43 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
44 * Continuation Type 0 IOCBs to allocate.
46 * @dsds: number of data segment descriptors needed
48 * Returns the number of IOCB entries needed to store @dsds.
51 qla2x00_calc_iocbs_32(uint16_t dsds)
57 iocbs += (dsds - 3) / 7;
65 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
66 * Continuation Type 1 IOCBs to allocate.
68 * @dsds: number of data segment descriptors needed
70 * Returns the number of IOCB entries needed to store @dsds.
73 qla2x00_calc_iocbs_64(uint16_t dsds)
79 iocbs += (dsds - 2) / 5;
87 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90 * Returns a pointer to the Continuation Type 0 IOCB packet.
92 static inline cont_entry_t *
93 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
95 cont_entry_t *cont_pkt;
96 struct req_que *req = vha->req;
97 /* Adjust ring index. */
99 if (req->ring_index == req->length) {
101 req->ring_ptr = req->ring;
106 cont_pkt = (cont_entry_t *)req->ring_ptr;
108 /* Load packet defaults. */
109 put_unaligned_le32(CONTINUE_TYPE, &cont_pkt->entry_type);
115 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
117 * @req: request queue
119 * Returns a pointer to the continuation type 1 IOCB packet.
121 static inline cont_a64_entry_t *
122 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
124 cont_a64_entry_t *cont_pkt;
126 /* Adjust ring index. */
128 if (req->ring_index == req->length) {
130 req->ring_ptr = req->ring;
135 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
137 /* Load packet defaults. */
138 put_unaligned_le32(IS_QLAFX00(vha->hw) ? CONTINUE_A64_TYPE_FX00 :
139 CONTINUE_A64_TYPE, &cont_pkt->entry_type);
145 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
147 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
148 uint8_t guard = scsi_host_get_guard(cmd->device->host);
150 /* We always use DIFF Bundling for best performance */
153 /* Translate SCSI opcode to a protection opcode */
154 switch (scsi_get_prot_op(cmd)) {
155 case SCSI_PROT_READ_STRIP:
156 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
158 case SCSI_PROT_WRITE_INSERT:
159 *fw_prot_opts |= PO_MODE_DIF_INSERT;
161 case SCSI_PROT_READ_INSERT:
162 *fw_prot_opts |= PO_MODE_DIF_INSERT;
164 case SCSI_PROT_WRITE_STRIP:
165 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
167 case SCSI_PROT_READ_PASS:
168 case SCSI_PROT_WRITE_PASS:
169 if (guard & SHOST_DIX_GUARD_IP)
170 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
172 *fw_prot_opts |= PO_MODE_DIF_PASS;
174 default: /* Normal Request */
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 return scsi_prot_sg_count(cmd);
183 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
184 * capable IOCB types.
186 * @sp: SRB command to process
187 * @cmd_pkt: Command type 2 IOCB
188 * @tot_dsds: Total number of segments to transfer
190 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
194 struct dsd32 *cur_dsd;
195 scsi_qla_host_t *vha;
196 struct scsi_cmnd *cmd;
197 struct scatterlist *sg;
200 cmd = GET_CMD_SP(sp);
202 /* Update entry type to indicate Command Type 2 IOCB */
203 put_unaligned_le32(COMMAND_TYPE, &cmd_pkt->entry_type);
205 /* No data transfer */
206 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
207 cmd_pkt->byte_count = cpu_to_le32(0);
212 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
214 /* Three DSDs are available in the Command Type 2 IOCB */
215 avail_dsds = ARRAY_SIZE(cmd_pkt->dsd32);
216 cur_dsd = cmd_pkt->dsd32;
218 /* Load data segments */
219 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
220 cont_entry_t *cont_pkt;
222 /* Allocate additional continuation packets? */
223 if (avail_dsds == 0) {
225 * Seven DSDs are available in the Continuation
228 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
229 cur_dsd = cont_pkt->dsd;
230 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
233 append_dsd32(&cur_dsd, sg);
239 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
240 * capable IOCB types.
242 * @sp: SRB command to process
243 * @cmd_pkt: Command type 3 IOCB
244 * @tot_dsds: Total number of segments to transfer
246 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
250 struct dsd64 *cur_dsd;
251 scsi_qla_host_t *vha;
252 struct scsi_cmnd *cmd;
253 struct scatterlist *sg;
256 cmd = GET_CMD_SP(sp);
258 /* Update entry type to indicate Command Type 3 IOCB */
259 put_unaligned_le32(COMMAND_A64_TYPE, &cmd_pkt->entry_type);
261 /* No data transfer */
262 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
263 cmd_pkt->byte_count = cpu_to_le32(0);
268 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
270 /* Two DSDs are available in the Command Type 3 IOCB */
271 avail_dsds = ARRAY_SIZE(cmd_pkt->dsd64);
272 cur_dsd = cmd_pkt->dsd64;
274 /* Load data segments */
275 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
276 cont_a64_entry_t *cont_pkt;
278 /* Allocate additional continuation packets? */
279 if (avail_dsds == 0) {
281 * Five DSDs are available in the Continuation
284 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
285 cur_dsd = cont_pkt->dsd;
286 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
289 append_dsd64(&cur_dsd, sg);
295 * Find the first handle that is not in use, starting from
296 * req->current_outstanding_cmd + 1. The caller must hold the lock that is
297 * associated with @req.
299 uint32_t qla2xxx_get_next_handle(struct req_que *req)
301 uint32_t index, handle = req->current_outstanding_cmd;
303 for (index = 1; index < req->num_outstanding_cmds; index++) {
305 if (handle == req->num_outstanding_cmds)
307 if (!req->outstanding_cmds[handle])
315 * qla2x00_start_scsi() - Send a SCSI command to the ISP
316 * @sp: command to send to the ISP
318 * Returns non-zero if a failure occurred, else zero.
321 qla2x00_start_scsi(srb_t *sp)
325 scsi_qla_host_t *vha;
326 struct scsi_cmnd *cmd;
329 cmd_entry_t *cmd_pkt;
333 struct device_reg_2xxx __iomem *reg;
334 struct qla_hw_data *ha;
338 /* Setup device pointers. */
341 reg = &ha->iobase->isp;
342 cmd = GET_CMD_SP(sp);
343 req = ha->req_q_map[0];
344 rsp = ha->rsp_q_map[0];
345 /* So we know we haven't pci_map'ed anything yet */
348 /* Send marker if required */
349 if (vha->marker_needed != 0) {
350 if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
352 return (QLA_FUNCTION_FAILED);
354 vha->marker_needed = 0;
357 /* Acquire ring specific lock */
358 spin_lock_irqsave(&ha->hardware_lock, flags);
360 handle = qla2xxx_get_next_handle(req);
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 = 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
453 * @req: request queue
456 qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
458 struct qla_hw_data *ha = vha->hw;
459 device_reg_t *reg = ISP_QUE_REG(ha, req->id);
461 if (IS_P3P_TYPE(ha)) {
462 qla82xx_start_iocbs(vha);
464 /* Adjust ring index. */
466 if (req->ring_index == req->length) {
468 req->ring_ptr = req->ring;
472 /* Set chip new ring index. */
473 if (ha->mqenable || IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
474 wrt_reg_dword(req->req_q_in, req->ring_index);
475 } else if (IS_QLA83XX(ha)) {
476 wrt_reg_dword(req->req_q_in, req->ring_index);
477 rd_reg_dword_relaxed(&ha->iobase->isp24.hccr);
478 } else if (IS_QLAFX00(ha)) {
479 wrt_reg_dword(®->ispfx00.req_q_in, req->ring_index);
480 rd_reg_dword_relaxed(®->ispfx00.req_q_in);
481 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
482 } else if (IS_FWI2_CAPABLE(ha)) {
483 wrt_reg_dword(®->isp24.req_q_in, req->ring_index);
484 rd_reg_dword_relaxed(®->isp24.req_q_in);
486 wrt_reg_word(ISP_REQ_Q_IN(ha, ®->isp),
488 rd_reg_word_relaxed(ISP_REQ_Q_IN(ha, ®->isp));
494 * qla2x00_marker() - Send a marker IOCB to the firmware.
496 * @qpair: queue pair pointer
499 * @type: marker modifier
501 * Can be called from both normal and interrupt context.
503 * Returns non-zero if a failure occurred, else zero.
506 __qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
507 uint16_t loop_id, uint64_t lun, uint8_t type)
510 struct mrk_entry_24xx *mrk24 = NULL;
511 struct req_que *req = qpair->req;
512 struct qla_hw_data *ha = vha->hw;
513 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
515 mrk = (mrk_entry_t *)__qla2x00_alloc_iocbs(qpair, NULL);
517 ql_log(ql_log_warn, base_vha, 0x3026,
518 "Failed to allocate Marker IOCB.\n");
520 return (QLA_FUNCTION_FAILED);
523 mrk->entry_type = MARKER_TYPE;
524 mrk->modifier = type;
525 if (type != MK_SYNC_ALL) {
526 if (IS_FWI2_CAPABLE(ha)) {
527 mrk24 = (struct mrk_entry_24xx *) mrk;
528 mrk24->nport_handle = cpu_to_le16(loop_id);
529 int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
530 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
531 mrk24->vp_index = vha->vp_idx;
532 mrk24->handle = make_handle(req->id, mrk24->handle);
534 SET_TARGET_ID(ha, mrk->target, loop_id);
535 mrk->lun = cpu_to_le16((uint16_t)lun);
540 qla2x00_start_iocbs(vha, req);
542 return (QLA_SUCCESS);
546 qla2x00_marker(struct scsi_qla_host *vha, struct qla_qpair *qpair,
547 uint16_t loop_id, uint64_t lun, uint8_t type)
550 unsigned long flags = 0;
552 spin_lock_irqsave(qpair->qp_lock_ptr, flags);
553 ret = __qla2x00_marker(vha, qpair, loop_id, lun, type);
554 spin_unlock_irqrestore(qpair->qp_lock_ptr, flags);
560 * qla2x00_issue_marker
563 * Caller CAN have hardware lock held as specified by ha_locked parameter.
564 * Might release it, then reaquire.
566 int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
569 if (__qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
570 MK_SYNC_ALL) != QLA_SUCCESS)
571 return QLA_FUNCTION_FAILED;
573 if (qla2x00_marker(vha, vha->hw->base_qpair, 0, 0,
574 MK_SYNC_ALL) != QLA_SUCCESS)
575 return QLA_FUNCTION_FAILED;
577 vha->marker_needed = 0;
583 qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
586 struct dsd64 *cur_dsd = NULL, *next_dsd;
587 scsi_qla_host_t *vha;
588 struct qla_hw_data *ha;
589 struct scsi_cmnd *cmd;
590 struct scatterlist *cur_seg;
592 uint8_t first_iocb = 1;
593 uint32_t dsd_list_len;
594 struct dsd_dma *dsd_ptr;
596 struct qla_qpair *qpair = sp->qpair;
598 cmd = GET_CMD_SP(sp);
600 /* Update entry type to indicate Command Type 3 IOCB */
601 put_unaligned_le32(COMMAND_TYPE_6, &cmd_pkt->entry_type);
603 /* No data transfer */
604 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE ||
606 cmd_pkt->byte_count = cpu_to_le32(0);
613 /* Set transfer direction */
614 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
615 cmd_pkt->control_flags = cpu_to_le16(CF_WRITE_DATA);
616 qpair->counters.output_bytes += scsi_bufflen(cmd);
617 qpair->counters.output_requests++;
618 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
619 cmd_pkt->control_flags = cpu_to_le16(CF_READ_DATA);
620 qpair->counters.input_bytes += scsi_bufflen(cmd);
621 qpair->counters.input_requests++;
624 cur_seg = scsi_sglist(cmd);
625 ctx = sp->u.scmd.ct6_ctx;
628 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
629 QLA_DSDS_PER_IOCB : tot_dsds;
630 tot_dsds -= avail_dsds;
631 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
633 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
634 struct dsd_dma, list);
635 next_dsd = dsd_ptr->dsd_addr;
636 list_del(&dsd_ptr->list);
638 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
644 put_unaligned_le64(dsd_ptr->dsd_list_dma,
645 &cmd_pkt->fcp_dsd.address);
646 cmd_pkt->fcp_dsd.length = cpu_to_le32(dsd_list_len);
648 put_unaligned_le64(dsd_ptr->dsd_list_dma,
650 cur_dsd->length = cpu_to_le32(dsd_list_len);
655 append_dsd64(&cur_dsd, cur_seg);
656 cur_seg = sg_next(cur_seg);
661 /* Null termination */
662 cur_dsd->address = 0;
665 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
670 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
671 * for Command Type 6.
673 * @dsds: number of data segment descriptors needed
675 * Returns the number of dsd list needed to store @dsds.
677 static inline uint16_t
678 qla24xx_calc_dsd_lists(uint16_t dsds)
680 uint16_t dsd_lists = 0;
682 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
683 if (dsds % QLA_DSDS_PER_IOCB)
690 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
693 * @sp: SRB command to process
694 * @cmd_pkt: Command type 3 IOCB
695 * @tot_dsds: Total number of segments to transfer
696 * @req: pointer to request queue
699 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
700 uint16_t tot_dsds, struct req_que *req)
703 struct dsd64 *cur_dsd;
704 scsi_qla_host_t *vha;
705 struct scsi_cmnd *cmd;
706 struct scatterlist *sg;
708 struct qla_qpair *qpair = sp->qpair;
710 cmd = GET_CMD_SP(sp);
712 /* Update entry type to indicate Command Type 3 IOCB */
713 put_unaligned_le32(COMMAND_TYPE_7, &cmd_pkt->entry_type);
715 /* No data transfer */
716 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
717 cmd_pkt->byte_count = cpu_to_le32(0);
723 /* Set transfer direction */
724 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
725 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_WRITE_DATA);
726 qpair->counters.output_bytes += scsi_bufflen(cmd);
727 qpair->counters.output_requests++;
728 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
729 cmd_pkt->task_mgmt_flags = cpu_to_le16(TMF_READ_DATA);
730 qpair->counters.input_bytes += scsi_bufflen(cmd);
731 qpair->counters.input_requests++;
734 /* One DSD is available in the Command Type 3 IOCB */
736 cur_dsd = &cmd_pkt->dsd;
738 /* Load data segments */
740 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
741 cont_a64_entry_t *cont_pkt;
743 /* Allocate additional continuation packets? */
744 if (avail_dsds == 0) {
746 * Five DSDs are available in the Continuation
749 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, req);
750 cur_dsd = cont_pkt->dsd;
751 avail_dsds = ARRAY_SIZE(cont_pkt->dsd);
754 append_dsd64(&cur_dsd, sg);
759 struct fw_dif_context {
762 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
763 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
767 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
771 qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
772 unsigned int protcnt)
774 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
776 switch (scsi_get_prot_type(cmd)) {
777 case SCSI_PROT_DIF_TYPE0:
779 * No check for ql2xenablehba_err_chk, as it would be an
780 * I/O error if hba tag generation is not done.
782 pkt->ref_tag = cpu_to_le32((uint32_t)
783 (0xffffffff & scsi_get_lba(cmd)));
785 if (!qla2x00_hba_err_chk_enabled(sp))
788 pkt->ref_tag_mask[0] = 0xff;
789 pkt->ref_tag_mask[1] = 0xff;
790 pkt->ref_tag_mask[2] = 0xff;
791 pkt->ref_tag_mask[3] = 0xff;
795 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
796 * match LBA in CDB + N
798 case SCSI_PROT_DIF_TYPE2:
799 pkt->app_tag = cpu_to_le16(0);
800 pkt->app_tag_mask[0] = 0x0;
801 pkt->app_tag_mask[1] = 0x0;
803 pkt->ref_tag = cpu_to_le32((uint32_t)
804 (0xffffffff & scsi_get_lba(cmd)));
806 if (!qla2x00_hba_err_chk_enabled(sp))
809 /* enable ALL bytes of the ref tag */
810 pkt->ref_tag_mask[0] = 0xff;
811 pkt->ref_tag_mask[1] = 0xff;
812 pkt->ref_tag_mask[2] = 0xff;
813 pkt->ref_tag_mask[3] = 0xff;
816 /* For Type 3 protection: 16 bit GUARD only */
817 case SCSI_PROT_DIF_TYPE3:
818 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
819 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
824 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
827 case SCSI_PROT_DIF_TYPE1:
828 pkt->ref_tag = cpu_to_le32((uint32_t)
829 (0xffffffff & scsi_get_lba(cmd)));
830 pkt->app_tag = cpu_to_le16(0);
831 pkt->app_tag_mask[0] = 0x0;
832 pkt->app_tag_mask[1] = 0x0;
834 if (!qla2x00_hba_err_chk_enabled(sp))
837 /* enable ALL bytes of the ref tag */
838 pkt->ref_tag_mask[0] = 0xff;
839 pkt->ref_tag_mask[1] = 0xff;
840 pkt->ref_tag_mask[2] = 0xff;
841 pkt->ref_tag_mask[3] = 0xff;
847 qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
850 struct scatterlist *sg;
851 uint32_t cumulative_partial, sg_len;
852 dma_addr_t sg_dma_addr;
854 if (sgx->num_bytes == sgx->tot_bytes)
858 cumulative_partial = sgx->tot_partial;
860 sg_dma_addr = sg_dma_address(sg);
861 sg_len = sg_dma_len(sg);
863 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
865 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
866 sgx->dma_len = (blk_sz - cumulative_partial);
867 sgx->tot_partial = 0;
868 sgx->num_bytes += blk_sz;
871 sgx->dma_len = sg_len - sgx->bytes_consumed;
872 sgx->tot_partial += sgx->dma_len;
876 sgx->bytes_consumed += sgx->dma_len;
878 if (sg_len == sgx->bytes_consumed) {
882 sgx->bytes_consumed = 0;
889 qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
890 struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
893 uint8_t avail_dsds = 0;
894 uint32_t dsd_list_len;
895 struct dsd_dma *dsd_ptr;
896 struct scatterlist *sg_prot;
897 struct dsd64 *cur_dsd = dsd;
898 uint16_t used_dsds = tot_dsds;
899 uint32_t prot_int; /* protection interval */
903 uint32_t sle_dma_len, tot_prot_dma_len = 0;
904 struct scsi_cmnd *cmd;
906 memset(&sgx, 0, sizeof(struct qla2_sgx));
908 cmd = GET_CMD_SP(sp);
909 prot_int = cmd->device->sector_size;
911 sgx.tot_bytes = scsi_bufflen(cmd);
912 sgx.cur_sg = scsi_sglist(cmd);
915 sg_prot = scsi_prot_sglist(cmd);
917 prot_int = tc->blk_sz;
918 sgx.tot_bytes = tc->bufflen;
920 sg_prot = tc->prot_sg;
926 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
928 sle_dma = sgx.dma_addr;
929 sle_dma_len = sgx.dma_len;
931 /* Allocate additional continuation packets? */
932 if (avail_dsds == 0) {
933 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
934 QLA_DSDS_PER_IOCB : used_dsds;
935 dsd_list_len = (avail_dsds + 1) * 12;
936 used_dsds -= avail_dsds;
938 /* allocate tracking DS */
939 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
943 /* allocate new list */
944 dsd_ptr->dsd_addr = next_dsd =
945 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
946 &dsd_ptr->dsd_list_dma);
950 * Need to cleanup only this dsd_ptr, rest
951 * will be done by sp_free_dma()
958 list_add_tail(&dsd_ptr->list,
959 &sp->u.scmd.crc_ctx->dsd_list);
961 sp->flags |= SRB_CRC_CTX_DSD_VALID;
963 list_add_tail(&dsd_ptr->list,
964 &(tc->ctx->dsd_list));
965 *tc->ctx_dsd_alloced = 1;
969 /* add new list to cmd iocb or last list */
970 put_unaligned_le64(dsd_ptr->dsd_list_dma,
972 cur_dsd->length = cpu_to_le32(dsd_list_len);
975 put_unaligned_le64(sle_dma, &cur_dsd->address);
976 cur_dsd->length = cpu_to_le32(sle_dma_len);
981 /* Got a full protection interval */
982 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
985 tot_prot_dma_len += sle_dma_len;
986 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
987 tot_prot_dma_len = 0;
988 sg_prot = sg_next(sg_prot);
991 partial = 1; /* So as to not re-enter this block */
995 /* Null termination */
996 cur_dsd->address = 0;
1003 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp,
1004 struct dsd64 *dsd, uint16_t tot_dsds, struct qla_tc_param *tc)
1007 uint8_t avail_dsds = 0;
1008 uint32_t dsd_list_len;
1009 struct dsd_dma *dsd_ptr;
1010 struct scatterlist *sg, *sgl;
1011 struct dsd64 *cur_dsd = dsd;
1013 uint16_t used_dsds = tot_dsds;
1014 struct scsi_cmnd *cmd;
1017 cmd = GET_CMD_SP(sp);
1018 sgl = scsi_sglist(cmd);
1027 for_each_sg(sgl, sg, tot_dsds, i) {
1028 /* Allocate additional continuation packets? */
1029 if (avail_dsds == 0) {
1030 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1031 QLA_DSDS_PER_IOCB : used_dsds;
1032 dsd_list_len = (avail_dsds + 1) * 12;
1033 used_dsds -= avail_dsds;
1035 /* allocate tracking DS */
1036 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1040 /* allocate new list */
1041 dsd_ptr->dsd_addr = next_dsd =
1042 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1043 &dsd_ptr->dsd_list_dma);
1047 * Need to cleanup only this dsd_ptr, rest
1048 * will be done by sp_free_dma()
1055 list_add_tail(&dsd_ptr->list,
1056 &sp->u.scmd.crc_ctx->dsd_list);
1058 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1060 list_add_tail(&dsd_ptr->list,
1061 &(tc->ctx->dsd_list));
1062 *tc->ctx_dsd_alloced = 1;
1065 /* add new list to cmd iocb or last list */
1066 put_unaligned_le64(dsd_ptr->dsd_list_dma,
1068 cur_dsd->length = cpu_to_le32(dsd_list_len);
1071 append_dsd64(&cur_dsd, sg);
1075 /* Null termination */
1076 cur_dsd->address = 0;
1077 cur_dsd->length = 0;
1083 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1084 struct dsd64 *cur_dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
1086 struct dsd_dma *dsd_ptr = NULL, *dif_dsd, *nxt_dsd;
1087 struct scatterlist *sg, *sgl;
1088 struct crc_context *difctx = NULL;
1089 struct scsi_qla_host *vha;
1091 uint avail_dsds = 0;
1092 uint used_dsds = tot_dsds;
1093 bool dif_local_dma_alloc = false;
1094 bool direction_to_device = false;
1098 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1100 sgl = scsi_prot_sglist(cmd);
1102 difctx = sp->u.scmd.crc_ctx;
1103 direction_to_device = cmd->sc_data_direction == DMA_TO_DEVICE;
1104 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
1105 "%s: scsi_cmnd: %p, crc_ctx: %p, sp: %p\n",
1106 __func__, cmd, difctx, sp);
1111 direction_to_device = tc->dma_data_direction == DMA_TO_DEVICE;
1117 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe021,
1118 "%s: enter (write=%u)\n", __func__, direction_to_device);
1120 /* if initiator doing write or target doing read */
1121 if (direction_to_device) {
1122 for_each_sg(sgl, sg, tot_dsds, i) {
1123 u64 sle_phys = sg_phys(sg);
1125 /* If SGE addr + len flips bits in upper 32-bits */
1126 if (MSD(sle_phys + sg->length) ^ MSD(sle_phys)) {
1127 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe022,
1128 "%s: page boundary crossing (phys=%llx len=%x)\n",
1129 __func__, sle_phys, sg->length);
1132 ha->dif_bundle_crossed_pages++;
1133 dif_local_dma_alloc = true;
1135 ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
1137 "%s: difctx pointer is NULL\n",
1143 ha->dif_bundle_writes++;
1145 ha->dif_bundle_reads++;
1148 if (ql2xdifbundlinginternalbuffers)
1149 dif_local_dma_alloc = direction_to_device;
1151 if (dif_local_dma_alloc) {
1152 u32 track_difbundl_buf = 0;
1153 u32 ldma_sg_len = 0;
1156 difctx->no_dif_bundl = 0;
1157 difctx->dif_bundl_len = 0;
1159 /* Track DSD buffers */
1160 INIT_LIST_HEAD(&difctx->ldif_dsd_list);
1161 /* Track local DMA buffers */
1162 INIT_LIST_HEAD(&difctx->ldif_dma_hndl_list);
1164 for_each_sg(sgl, sg, tot_dsds, i) {
1165 u32 sglen = sg_dma_len(sg);
1167 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe023,
1168 "%s: sg[%x] (phys=%llx sglen=%x) ldma_sg_len: %x dif_bundl_len: %x ldma_needed: %x\n",
1169 __func__, i, (u64)sg_phys(sg), sglen, ldma_sg_len,
1170 difctx->dif_bundl_len, ldma_needed);
1177 * Allocate list item to store
1180 dsd_ptr = kzalloc(sizeof(*dsd_ptr),
1183 ql_dbg(ql_dbg_tgt, vha, 0xe024,
1184 "%s: failed alloc dsd_ptr\n",
1188 ha->dif_bundle_kallocs++;
1190 /* allocate dma buffer */
1191 dsd_ptr->dsd_addr = dma_pool_alloc
1192 (ha->dif_bundl_pool, GFP_ATOMIC,
1193 &dsd_ptr->dsd_list_dma);
1194 if (!dsd_ptr->dsd_addr) {
1195 ql_dbg(ql_dbg_tgt, vha, 0xe024,
1196 "%s: failed alloc ->dsd_ptr\n",
1199 * need to cleanup only this
1200 * dsd_ptr rest will be done
1204 ha->dif_bundle_kallocs--;
1207 ha->dif_bundle_dma_allocs++;
1209 difctx->no_dif_bundl++;
1210 list_add_tail(&dsd_ptr->list,
1211 &difctx->ldif_dma_hndl_list);
1214 /* xfrlen is min of dma pool size and sglen */
1216 (DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len)) ?
1217 DIF_BUNDLING_DMA_POOL_SIZE - ldma_sg_len :
1220 /* replace with local allocated dma buffer */
1221 sg_pcopy_to_buffer(sgl, sg_nents(sgl),
1222 dsd_ptr->dsd_addr + ldma_sg_len, xfrlen,
1223 difctx->dif_bundl_len);
1224 difctx->dif_bundl_len += xfrlen;
1226 ldma_sg_len += xfrlen;
1227 if (ldma_sg_len == DIF_BUNDLING_DMA_POOL_SIZE ||
1235 track_difbundl_buf = used_dsds = difctx->no_dif_bundl;
1236 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe025,
1237 "dif_bundl_len=%x, no_dif_bundl=%x track_difbundl_buf: %x\n",
1238 difctx->dif_bundl_len, difctx->no_dif_bundl,
1239 track_difbundl_buf);
1242 sp->flags |= SRB_DIF_BUNDL_DMA_VALID;
1244 tc->prot_flags = DIF_BUNDL_DMA_VALID;
1246 list_for_each_entry_safe(dif_dsd, nxt_dsd,
1247 &difctx->ldif_dma_hndl_list, list) {
1248 u32 sglen = (difctx->dif_bundl_len >
1249 DIF_BUNDLING_DMA_POOL_SIZE) ?
1250 DIF_BUNDLING_DMA_POOL_SIZE : difctx->dif_bundl_len;
1252 BUG_ON(track_difbundl_buf == 0);
1254 /* Allocate additional continuation packets? */
1255 if (avail_dsds == 0) {
1256 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha,
1258 "%s: adding continuation iocb's\n",
1260 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1261 QLA_DSDS_PER_IOCB : used_dsds;
1262 dsd_list_len = (avail_dsds + 1) * 12;
1263 used_dsds -= avail_dsds;
1265 /* allocate tracking DS */
1266 dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
1268 ql_dbg(ql_dbg_tgt, vha, 0xe026,
1269 "%s: failed alloc dsd_ptr\n",
1273 ha->dif_bundle_kallocs++;
1275 difctx->no_ldif_dsd++;
1276 /* allocate new list */
1278 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1279 &dsd_ptr->dsd_list_dma);
1280 if (!dsd_ptr->dsd_addr) {
1281 ql_dbg(ql_dbg_tgt, vha, 0xe026,
1282 "%s: failed alloc ->dsd_addr\n",
1285 * need to cleanup only this dsd_ptr
1286 * rest will be done by sp_free_dma()
1289 ha->dif_bundle_kallocs--;
1292 ha->dif_bundle_dma_allocs++;
1295 list_add_tail(&dsd_ptr->list,
1296 &difctx->ldif_dsd_list);
1297 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1299 list_add_tail(&dsd_ptr->list,
1300 &difctx->ldif_dsd_list);
1301 tc->ctx_dsd_alloced = 1;
1304 /* add new list to cmd iocb or last list */
1305 put_unaligned_le64(dsd_ptr->dsd_list_dma,
1307 cur_dsd->length = cpu_to_le32(dsd_list_len);
1308 cur_dsd = dsd_ptr->dsd_addr;
1310 put_unaligned_le64(dif_dsd->dsd_list_dma,
1312 cur_dsd->length = cpu_to_le32(sglen);
1315 difctx->dif_bundl_len -= sglen;
1316 track_difbundl_buf--;
1319 ql_dbg(ql_dbg_tgt + ql_dbg_verbose, vha, 0xe026,
1320 "%s: no_ldif_dsd:%x, no_dif_bundl:%x\n", __func__,
1321 difctx->no_ldif_dsd, difctx->no_dif_bundl);
1323 for_each_sg(sgl, sg, tot_dsds, i) {
1324 /* Allocate additional continuation packets? */
1325 if (avail_dsds == 0) {
1326 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1327 QLA_DSDS_PER_IOCB : used_dsds;
1328 dsd_list_len = (avail_dsds + 1) * 12;
1329 used_dsds -= avail_dsds;
1331 /* allocate tracking DS */
1332 dsd_ptr = kzalloc(sizeof(*dsd_ptr), GFP_ATOMIC);
1334 ql_dbg(ql_dbg_tgt + ql_dbg_verbose,
1336 "%s: failed alloc dsd_dma...\n",
1341 /* allocate new list */
1343 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1344 &dsd_ptr->dsd_list_dma);
1345 if (!dsd_ptr->dsd_addr) {
1346 /* need to cleanup only this dsd_ptr */
1347 /* rest will be done by sp_free_dma() */
1353 list_add_tail(&dsd_ptr->list,
1355 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1357 list_add_tail(&dsd_ptr->list,
1359 tc->ctx_dsd_alloced = 1;
1362 /* add new list to cmd iocb or last list */
1363 put_unaligned_le64(dsd_ptr->dsd_list_dma,
1365 cur_dsd->length = cpu_to_le32(dsd_list_len);
1366 cur_dsd = dsd_ptr->dsd_addr;
1368 append_dsd64(&cur_dsd, sg);
1372 /* Null termination */
1373 cur_dsd->address = 0;
1374 cur_dsd->length = 0;
1380 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1381 * Type 6 IOCB types.
1383 * @sp: SRB command to process
1384 * @cmd_pkt: Command type 3 IOCB
1385 * @tot_dsds: Total number of segments to transfer
1386 * @tot_prot_dsds: Total number of segments with protection information
1387 * @fw_prot_opts: Protection options to be passed to firmware
1390 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1391 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1393 struct dsd64 *cur_dsd;
1395 scsi_qla_host_t *vha;
1396 struct scsi_cmnd *cmd;
1397 uint32_t total_bytes = 0;
1398 uint32_t data_bytes;
1400 uint8_t bundling = 1;
1402 struct crc_context *crc_ctx_pkt = NULL;
1403 struct qla_hw_data *ha;
1404 uint8_t additional_fcpcdb_len;
1405 uint16_t fcp_cmnd_len;
1406 struct fcp_cmnd *fcp_cmnd;
1407 dma_addr_t crc_ctx_dma;
1409 cmd = GET_CMD_SP(sp);
1411 /* Update entry type to indicate Command Type CRC_2 IOCB */
1412 put_unaligned_le32(COMMAND_TYPE_CRC_2, &cmd_pkt->entry_type);
1417 /* No data transfer */
1418 data_bytes = scsi_bufflen(cmd);
1419 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1420 cmd_pkt->byte_count = cpu_to_le32(0);
1424 cmd_pkt->vp_index = sp->vha->vp_idx;
1426 /* Set transfer direction */
1427 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1428 cmd_pkt->control_flags =
1429 cpu_to_le16(CF_WRITE_DATA);
1430 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1431 cmd_pkt->control_flags =
1432 cpu_to_le16(CF_READ_DATA);
1435 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1436 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1437 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1438 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
1441 /* Allocate CRC context from global pool */
1442 crc_ctx_pkt = sp->u.scmd.crc_ctx =
1443 dma_pool_zalloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
1446 goto crc_queuing_error;
1448 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1450 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1453 crc_ctx_pkt->handle = cmd_pkt->handle;
1455 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1457 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
1458 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1460 put_unaligned_le64(crc_ctx_dma, &cmd_pkt->crc_context_address);
1461 cmd_pkt->crc_context_len = cpu_to_le16(CRC_CONTEXT_LEN_FW);
1463 /* Determine SCSI command length -- align to 4 byte boundary */
1464 if (cmd->cmd_len > 16) {
1465 additional_fcpcdb_len = cmd->cmd_len - 16;
1466 if ((cmd->cmd_len % 4) != 0) {
1467 /* SCSI cmd > 16 bytes must be multiple of 4 */
1468 goto crc_queuing_error;
1470 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1472 additional_fcpcdb_len = 0;
1473 fcp_cmnd_len = 12 + 16 + 4;
1476 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1478 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1479 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1480 fcp_cmnd->additional_cdb_len |= 1;
1481 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1482 fcp_cmnd->additional_cdb_len |= 2;
1484 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
1485 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1486 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1487 put_unaligned_le64(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF,
1488 &cmd_pkt->fcp_cmnd_dseg_address);
1489 fcp_cmnd->task_management = 0;
1490 fcp_cmnd->task_attribute = TSK_SIMPLE;
1492 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1494 /* Compute dif len and adjust data len to incude protection */
1496 blk_size = cmd->device->sector_size;
1497 dif_bytes = (data_bytes / blk_size) * 8;
1499 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
1500 case SCSI_PROT_READ_INSERT:
1501 case SCSI_PROT_WRITE_STRIP:
1502 total_bytes = data_bytes;
1503 data_bytes += dif_bytes;
1506 case SCSI_PROT_READ_STRIP:
1507 case SCSI_PROT_WRITE_INSERT:
1508 case SCSI_PROT_READ_PASS:
1509 case SCSI_PROT_WRITE_PASS:
1510 total_bytes = data_bytes + dif_bytes;
1516 if (!qla2x00_hba_err_chk_enabled(sp))
1517 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1518 /* HBA error checking enabled */
1519 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1520 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1521 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1522 SCSI_PROT_DIF_TYPE2))
1523 fw_prot_opts |= BIT_10;
1524 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1525 SCSI_PROT_DIF_TYPE3)
1526 fw_prot_opts |= BIT_11;
1530 cur_dsd = &crc_ctx_pkt->u.nobundling.data_dsd[0];
1533 * Configure Bundling if we need to fetch interlaving
1534 * protection PCI accesses
1536 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1537 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1538 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1540 cur_dsd = &crc_ctx_pkt->u.bundling.data_dsd[0];
1543 /* Finish the common fields of CRC pkt */
1544 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1545 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1546 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1547 crc_ctx_pkt->guard_seed = cpu_to_le16(0);
1548 /* Fibre channel byte count */
1549 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1550 fcp_dl = (__be32 *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1551 additional_fcpcdb_len);
1552 *fcp_dl = htonl(total_bytes);
1554 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1555 cmd_pkt->byte_count = cpu_to_le32(0);
1558 /* Walks data segments */
1560 cmd_pkt->control_flags |= cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1562 if (!bundling && tot_prot_dsds) {
1563 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1564 cur_dsd, tot_dsds, NULL))
1565 goto crc_queuing_error;
1566 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1567 (tot_dsds - tot_prot_dsds), NULL))
1568 goto crc_queuing_error;
1570 if (bundling && tot_prot_dsds) {
1571 /* Walks dif segments */
1572 cmd_pkt->control_flags |= cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1573 cur_dsd = &crc_ctx_pkt->u.bundling.dif_dsd;
1574 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1575 tot_prot_dsds, NULL))
1576 goto crc_queuing_error;
1581 /* Cleanup will be performed by the caller */
1583 return QLA_FUNCTION_FAILED;
1587 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1588 * @sp: command to send to the ISP
1590 * Returns non-zero if a failure occurred, else zero.
1593 qla24xx_start_scsi(srb_t *sp)
1596 unsigned long flags;
1599 struct cmd_type_7 *cmd_pkt;
1603 struct req_que *req = NULL;
1604 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1605 struct scsi_qla_host *vha = sp->vha;
1606 struct qla_hw_data *ha = vha->hw;
1608 /* Setup device pointers. */
1611 /* So we know we haven't pci_map'ed anything yet */
1614 /* Send marker if required */
1615 if (vha->marker_needed != 0) {
1616 if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
1618 return QLA_FUNCTION_FAILED;
1619 vha->marker_needed = 0;
1622 /* Acquire ring specific lock */
1623 spin_lock_irqsave(&ha->hardware_lock, flags);
1625 handle = qla2xxx_get_next_handle(req);
1629 /* Map the sg table so we have an accurate count of sg entries needed */
1630 if (scsi_sg_count(cmd)) {
1631 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1632 scsi_sg_count(cmd), cmd->sc_data_direction);
1633 if (unlikely(!nseg))
1639 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1641 sp->iores.res_type = RESOURCE_INI;
1642 sp->iores.iocb_cnt = req_cnt;
1643 if (qla_get_iocbs(sp->qpair, &sp->iores))
1646 if (req->cnt < (req_cnt + 2)) {
1647 if (IS_SHADOW_REG_CAPABLE(ha)) {
1648 cnt = *req->out_ptr;
1650 cnt = rd_reg_dword_relaxed(req->req_q_out);
1651 if (qla2x00_check_reg16_for_disconnect(vha, cnt))
1655 if (req->ring_index < cnt)
1656 req->cnt = cnt - req->ring_index;
1658 req->cnt = req->length -
1659 (req->ring_index - cnt);
1660 if (req->cnt < (req_cnt + 2))
1664 /* Build command packet. */
1665 req->current_outstanding_cmd = handle;
1666 req->outstanding_cmds[handle] = sp;
1667 sp->handle = handle;
1668 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1669 req->cnt -= req_cnt;
1671 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1672 cmd_pkt->handle = make_handle(req->id, handle);
1674 /* Zero out remaining portion of packet. */
1675 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
1676 clr_ptr = (uint32_t *)cmd_pkt + 2;
1677 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1678 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1680 /* Set NPORT-ID and LUN number*/
1681 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1682 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1683 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1684 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1685 cmd_pkt->vp_index = sp->vha->vp_idx;
1687 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1688 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1690 cmd_pkt->task = TSK_SIMPLE;
1692 /* Load SCSI command packet. */
1693 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1694 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1696 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1698 /* Build IOCB segments */
1699 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
1701 /* Set total data segment count. */
1702 cmd_pkt->entry_count = (uint8_t)req_cnt;
1704 /* Adjust ring index. */
1706 if (req->ring_index == req->length) {
1707 req->ring_index = 0;
1708 req->ring_ptr = req->ring;
1712 sp->flags |= SRB_DMA_VALID;
1714 /* Set chip new ring index. */
1715 wrt_reg_dword(req->req_q_in, req->ring_index);
1717 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1722 scsi_dma_unmap(cmd);
1724 qla_put_iocbs(sp->qpair, &sp->iores);
1725 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1727 return QLA_FUNCTION_FAILED;
1731 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1732 * @sp: command to send to the ISP
1734 * Returns non-zero if a failure occurred, else zero.
1737 qla24xx_dif_start_scsi(srb_t *sp)
1740 unsigned long flags;
1744 uint16_t req_cnt = 0;
1746 uint16_t tot_prot_dsds;
1747 uint16_t fw_prot_opts = 0;
1748 struct req_que *req = NULL;
1749 struct rsp_que *rsp = NULL;
1750 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1751 struct scsi_qla_host *vha = sp->vha;
1752 struct qla_hw_data *ha = vha->hw;
1753 struct cmd_type_crc_2 *cmd_pkt;
1754 uint32_t status = 0;
1756 #define QDSS_GOT_Q_SPACE BIT_0
1758 /* Only process protection or >16 cdb in this routine */
1759 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1760 if (cmd->cmd_len <= 16)
1761 return qla24xx_start_scsi(sp);
1764 /* Setup device pointers. */
1768 /* So we know we haven't pci_map'ed anything yet */
1771 /* Send marker if required */
1772 if (vha->marker_needed != 0) {
1773 if (qla2x00_marker(vha, ha->base_qpair, 0, 0, MK_SYNC_ALL) !=
1775 return QLA_FUNCTION_FAILED;
1776 vha->marker_needed = 0;
1779 /* Acquire ring specific lock */
1780 spin_lock_irqsave(&ha->hardware_lock, flags);
1782 handle = qla2xxx_get_next_handle(req);
1786 /* Compute number of required data segments */
1787 /* Map the sg table so we have an accurate count of sg entries needed */
1788 if (scsi_sg_count(cmd)) {
1789 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1790 scsi_sg_count(cmd), cmd->sc_data_direction);
1791 if (unlikely(!nseg))
1794 sp->flags |= SRB_DMA_VALID;
1796 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1797 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1798 struct qla2_sgx sgx;
1801 memset(&sgx, 0, sizeof(struct qla2_sgx));
1802 sgx.tot_bytes = scsi_bufflen(cmd);
1803 sgx.cur_sg = scsi_sglist(cmd);
1807 while (qla24xx_get_one_block_sg(
1808 cmd->device->sector_size, &sgx, &partial))
1814 /* number of required data segments */
1817 /* Compute number of required protection segments */
1818 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1819 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1820 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1821 if (unlikely(!nseg))
1824 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1826 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1827 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1828 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1835 /* Total Data and protection sg segment(s) */
1836 tot_prot_dsds = nseg;
1839 sp->iores.res_type = RESOURCE_INI;
1840 sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1841 if (qla_get_iocbs(sp->qpair, &sp->iores))
1844 if (req->cnt < (req_cnt + 2)) {
1845 if (IS_SHADOW_REG_CAPABLE(ha)) {
1846 cnt = *req->out_ptr;
1848 cnt = rd_reg_dword_relaxed(req->req_q_out);
1849 if (qla2x00_check_reg16_for_disconnect(vha, cnt))
1852 if (req->ring_index < cnt)
1853 req->cnt = cnt - req->ring_index;
1855 req->cnt = req->length -
1856 (req->ring_index - cnt);
1857 if (req->cnt < (req_cnt + 2))
1861 status |= QDSS_GOT_Q_SPACE;
1863 /* Build header part of command packet (excluding the OPCODE). */
1864 req->current_outstanding_cmd = handle;
1865 req->outstanding_cmds[handle] = sp;
1866 sp->handle = handle;
1867 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1868 req->cnt -= req_cnt;
1870 /* Fill-in common area */
1871 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1872 cmd_pkt->handle = make_handle(req->id, handle);
1874 clr_ptr = (uint32_t *)cmd_pkt + 2;
1875 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1877 /* Set NPORT-ID and LUN number*/
1878 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1879 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1880 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1881 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1883 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
1884 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1886 /* Total Data and protection segment(s) */
1887 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1889 /* Build IOCB segments and adjust for data protection segments */
1890 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1891 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1895 cmd_pkt->entry_count = (uint8_t)req_cnt;
1896 /* Specify response queue number where completion should happen */
1897 cmd_pkt->entry_status = (uint8_t) rsp->id;
1898 cmd_pkt->timeout = cpu_to_le16(0);
1901 /* Adjust ring index. */
1903 if (req->ring_index == req->length) {
1904 req->ring_index = 0;
1905 req->ring_ptr = req->ring;
1909 /* Set chip new ring index. */
1910 wrt_reg_dword(req->req_q_in, req->ring_index);
1912 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1917 if (status & QDSS_GOT_Q_SPACE) {
1918 req->outstanding_cmds[handle] = NULL;
1919 req->cnt += req_cnt;
1921 /* Cleanup will be performed by the caller (queuecommand) */
1923 qla_put_iocbs(sp->qpair, &sp->iores);
1924 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1926 return QLA_FUNCTION_FAILED;
1930 * qla2xxx_start_scsi_mq() - Send a SCSI command to the ISP
1931 * @sp: command to send to the ISP
1933 * Returns non-zero if a failure occurred, else zero.
1936 qla2xxx_start_scsi_mq(srb_t *sp)
1939 unsigned long flags;
1942 struct cmd_type_7 *cmd_pkt;
1946 struct req_que *req = NULL;
1947 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1948 struct scsi_qla_host *vha = sp->fcport->vha;
1949 struct qla_hw_data *ha = vha->hw;
1950 struct qla_qpair *qpair = sp->qpair;
1952 /* Acquire qpair specific lock */
1953 spin_lock_irqsave(&qpair->qp_lock, flags);
1955 /* Setup qpair pointers */
1958 /* So we know we haven't pci_map'ed anything yet */
1961 /* Send marker if required */
1962 if (vha->marker_needed != 0) {
1963 if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
1965 spin_unlock_irqrestore(&qpair->qp_lock, flags);
1966 return QLA_FUNCTION_FAILED;
1968 vha->marker_needed = 0;
1971 handle = qla2xxx_get_next_handle(req);
1975 /* Map the sg table so we have an accurate count of sg entries needed */
1976 if (scsi_sg_count(cmd)) {
1977 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1978 scsi_sg_count(cmd), cmd->sc_data_direction);
1979 if (unlikely(!nseg))
1985 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
1987 sp->iores.res_type = RESOURCE_INI;
1988 sp->iores.iocb_cnt = req_cnt;
1989 if (qla_get_iocbs(sp->qpair, &sp->iores))
1992 if (req->cnt < (req_cnt + 2)) {
1993 if (IS_SHADOW_REG_CAPABLE(ha)) {
1994 cnt = *req->out_ptr;
1996 cnt = rd_reg_dword_relaxed(req->req_q_out);
1997 if (qla2x00_check_reg16_for_disconnect(vha, cnt))
2001 if (req->ring_index < cnt)
2002 req->cnt = cnt - req->ring_index;
2004 req->cnt = req->length -
2005 (req->ring_index - cnt);
2006 if (req->cnt < (req_cnt + 2))
2010 /* Build command packet. */
2011 req->current_outstanding_cmd = handle;
2012 req->outstanding_cmds[handle] = sp;
2013 sp->handle = handle;
2014 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
2015 req->cnt -= req_cnt;
2017 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2018 cmd_pkt->handle = make_handle(req->id, handle);
2020 /* Zero out remaining portion of packet. */
2021 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2022 clr_ptr = (uint32_t *)cmd_pkt + 2;
2023 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2024 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2026 /* Set NPORT-ID and LUN number*/
2027 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2028 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2029 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2030 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2031 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2033 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2034 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2036 cmd_pkt->task = TSK_SIMPLE;
2038 /* Load SCSI command packet. */
2039 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2040 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2042 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2044 /* Build IOCB segments */
2045 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
2047 /* Set total data segment count. */
2048 cmd_pkt->entry_count = (uint8_t)req_cnt;
2050 /* Adjust ring index. */
2052 if (req->ring_index == req->length) {
2053 req->ring_index = 0;
2054 req->ring_ptr = req->ring;
2058 sp->flags |= SRB_DMA_VALID;
2060 /* Set chip new ring index. */
2061 wrt_reg_dword(req->req_q_in, req->ring_index);
2063 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2068 scsi_dma_unmap(cmd);
2070 qla_put_iocbs(sp->qpair, &sp->iores);
2071 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2073 return QLA_FUNCTION_FAILED;
2078 * qla2xxx_dif_start_scsi_mq() - Send a SCSI command to the ISP
2079 * @sp: command to send to the ISP
2081 * Returns non-zero if a failure occurred, else zero.
2084 qla2xxx_dif_start_scsi_mq(srb_t *sp)
2087 unsigned long flags;
2091 uint16_t req_cnt = 0;
2093 uint16_t tot_prot_dsds;
2094 uint16_t fw_prot_opts = 0;
2095 struct req_que *req = NULL;
2096 struct rsp_que *rsp = NULL;
2097 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2098 struct scsi_qla_host *vha = sp->fcport->vha;
2099 struct qla_hw_data *ha = vha->hw;
2100 struct cmd_type_crc_2 *cmd_pkt;
2101 uint32_t status = 0;
2102 struct qla_qpair *qpair = sp->qpair;
2104 #define QDSS_GOT_Q_SPACE BIT_0
2106 /* Check for host side state */
2107 if (!qpair->online) {
2108 cmd->result = DID_NO_CONNECT << 16;
2109 return QLA_INTERFACE_ERROR;
2112 if (!qpair->difdix_supported &&
2113 scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2114 cmd->result = DID_NO_CONNECT << 16;
2115 return QLA_INTERFACE_ERROR;
2118 /* Only process protection or >16 cdb in this routine */
2119 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
2120 if (cmd->cmd_len <= 16)
2121 return qla2xxx_start_scsi_mq(sp);
2124 spin_lock_irqsave(&qpair->qp_lock, flags);
2126 /* Setup qpair pointers */
2130 /* So we know we haven't pci_map'ed anything yet */
2133 /* Send marker if required */
2134 if (vha->marker_needed != 0) {
2135 if (__qla2x00_marker(vha, qpair, 0, 0, MK_SYNC_ALL) !=
2137 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2138 return QLA_FUNCTION_FAILED;
2140 vha->marker_needed = 0;
2143 handle = qla2xxx_get_next_handle(req);
2147 /* Compute number of required data segments */
2148 /* Map the sg table so we have an accurate count of sg entries needed */
2149 if (scsi_sg_count(cmd)) {
2150 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2151 scsi_sg_count(cmd), cmd->sc_data_direction);
2152 if (unlikely(!nseg))
2155 sp->flags |= SRB_DMA_VALID;
2157 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
2158 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
2159 struct qla2_sgx sgx;
2162 memset(&sgx, 0, sizeof(struct qla2_sgx));
2163 sgx.tot_bytes = scsi_bufflen(cmd);
2164 sgx.cur_sg = scsi_sglist(cmd);
2168 while (qla24xx_get_one_block_sg(
2169 cmd->device->sector_size, &sgx, &partial))
2175 /* number of required data segments */
2178 /* Compute number of required protection segments */
2179 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
2180 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
2181 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
2182 if (unlikely(!nseg))
2185 sp->flags |= SRB_CRC_PROT_DMA_VALID;
2187 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
2188 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
2189 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
2196 /* Total Data and protection sg segment(s) */
2197 tot_prot_dsds = nseg;
2200 sp->iores.res_type = RESOURCE_INI;
2201 sp->iores.iocb_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2202 if (qla_get_iocbs(sp->qpair, &sp->iores))
2205 if (req->cnt < (req_cnt + 2)) {
2206 if (IS_SHADOW_REG_CAPABLE(ha)) {
2207 cnt = *req->out_ptr;
2209 cnt = rd_reg_dword_relaxed(req->req_q_out);
2210 if (qla2x00_check_reg16_for_disconnect(vha, cnt))
2214 if (req->ring_index < cnt)
2215 req->cnt = cnt - req->ring_index;
2217 req->cnt = req->length -
2218 (req->ring_index - cnt);
2219 if (req->cnt < (req_cnt + 2))
2223 status |= QDSS_GOT_Q_SPACE;
2225 /* Build header part of command packet (excluding the OPCODE). */
2226 req->current_outstanding_cmd = handle;
2227 req->outstanding_cmds[handle] = sp;
2228 sp->handle = handle;
2229 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
2230 req->cnt -= req_cnt;
2232 /* Fill-in common area */
2233 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
2234 cmd_pkt->handle = make_handle(req->id, handle);
2236 clr_ptr = (uint32_t *)cmd_pkt + 2;
2237 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2239 /* Set NPORT-ID and LUN number*/
2240 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2241 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2242 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2243 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2245 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
2246 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2248 /* Total Data and protection segment(s) */
2249 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2251 /* Build IOCB segments and adjust for data protection segments */
2252 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
2253 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
2257 cmd_pkt->entry_count = (uint8_t)req_cnt;
2258 cmd_pkt->timeout = cpu_to_le16(0);
2261 /* Adjust ring index. */
2263 if (req->ring_index == req->length) {
2264 req->ring_index = 0;
2265 req->ring_ptr = req->ring;
2269 /* Set chip new ring index. */
2270 wrt_reg_dword(req->req_q_in, req->ring_index);
2272 /* Manage unprocessed RIO/ZIO commands in response queue. */
2273 if (vha->flags.process_response_queue &&
2274 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2275 qla24xx_process_response_queue(vha, rsp);
2277 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2282 if (status & QDSS_GOT_Q_SPACE) {
2283 req->outstanding_cmds[handle] = NULL;
2284 req->cnt += req_cnt;
2286 /* Cleanup will be performed by the caller (queuecommand) */
2288 qla_put_iocbs(sp->qpair, &sp->iores);
2289 spin_unlock_irqrestore(&qpair->qp_lock, flags);
2291 return QLA_FUNCTION_FAILED;
2294 /* Generic Control-SRB manipulation functions. */
2296 /* hardware_lock assumed to be held. */
2299 __qla2x00_alloc_iocbs(struct qla_qpair *qpair, srb_t *sp)
2301 scsi_qla_host_t *vha = qpair->vha;
2302 struct qla_hw_data *ha = vha->hw;
2303 struct req_que *req = qpair->req;
2304 device_reg_t *reg = ISP_QUE_REG(ha, req->id);
2307 uint16_t cnt, req_cnt;
2313 if (sp && (sp->type != SRB_SCSI_CMD)) {
2314 /* Adjust entry-counts as needed. */
2315 req_cnt = sp->iocbs;
2318 /* Check for room on request queue. */
2319 if (req->cnt < req_cnt + 2) {
2320 if (qpair->use_shadow_reg)
2321 cnt = *req->out_ptr;
2322 else if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha) ||
2324 cnt = rd_reg_dword(®->isp25mq.req_q_out);
2325 else if (IS_P3P_TYPE(ha))
2326 cnt = rd_reg_dword(reg->isp82.req_q_out);
2327 else if (IS_FWI2_CAPABLE(ha))
2328 cnt = rd_reg_dword(®->isp24.req_q_out);
2329 else if (IS_QLAFX00(ha))
2330 cnt = rd_reg_dword(®->ispfx00.req_q_out);
2332 cnt = qla2x00_debounce_register(
2333 ISP_REQ_Q_OUT(ha, ®->isp));
2335 if (!qpair->use_shadow_reg && cnt == ISP_REG16_DISCONNECT) {
2336 qla_schedule_eeh_work(vha);
2340 if (req->ring_index < cnt)
2341 req->cnt = cnt - req->ring_index;
2343 req->cnt = req->length -
2344 (req->ring_index - cnt);
2346 if (req->cnt < req_cnt + 2)
2350 handle = qla2xxx_get_next_handle(req);
2352 ql_log(ql_log_warn, vha, 0x700b,
2353 "No room on outstanding cmd array.\n");
2357 /* Prep command array. */
2358 req->current_outstanding_cmd = handle;
2359 req->outstanding_cmds[handle] = sp;
2360 sp->handle = handle;
2364 req->cnt -= req_cnt;
2365 pkt = req->ring_ptr;
2366 memset(pkt, 0, REQUEST_ENTRY_SIZE);
2367 if (IS_QLAFX00(ha)) {
2368 wrt_reg_byte((u8 __force __iomem *)&pkt->entry_count, req_cnt);
2369 wrt_reg_dword((__le32 __force __iomem *)&pkt->handle, handle);
2371 pkt->entry_count = req_cnt;
2372 pkt->handle = handle;
2378 qpair->tgt_counters.num_alloc_iocb_failed++;
2383 qla2x00_alloc_iocbs_ready(struct qla_qpair *qpair, srb_t *sp)
2385 scsi_qla_host_t *vha = qpair->vha;
2387 if (qla2x00_reset_active(vha))
2390 return __qla2x00_alloc_iocbs(qpair, sp);
2394 qla2x00_alloc_iocbs(struct scsi_qla_host *vha, srb_t *sp)
2396 return __qla2x00_alloc_iocbs(vha->hw->base_qpair, sp);
2400 qla24xx_prli_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2402 struct srb_iocb *lio = &sp->u.iocb_cmd;
2404 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2405 logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
2406 if (lio->u.logio.flags & SRB_LOGIN_NVME_PRLI) {
2407 logio->control_flags |= cpu_to_le16(LCF_NVME_PRLI);
2408 if (sp->vha->flags.nvme_first_burst)
2409 logio->io_parameter[0] =
2410 cpu_to_le32(NVME_PRLI_SP_FIRST_BURST);
2411 if (sp->vha->flags.nvme2_enabled) {
2412 /* Set service parameter BIT_8 for SLER support */
2413 logio->io_parameter[0] |=
2414 cpu_to_le32(NVME_PRLI_SP_SLER);
2415 /* Set service parameter BIT_9 for PI control support */
2416 logio->io_parameter[0] |=
2417 cpu_to_le32(NVME_PRLI_SP_PI_CTRL);
2421 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2422 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2423 logio->port_id[1] = sp->fcport->d_id.b.area;
2424 logio->port_id[2] = sp->fcport->d_id.b.domain;
2425 logio->vp_index = sp->vha->vp_idx;
2429 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2431 struct srb_iocb *lio = &sp->u.iocb_cmd;
2433 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2434 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2436 if (lio->u.logio.flags & SRB_LOGIN_PRLI_ONLY) {
2437 logio->control_flags = cpu_to_le16(LCF_COMMAND_PRLI);
2439 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
2440 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
2441 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
2442 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
2443 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
2445 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2446 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2447 logio->port_id[1] = sp->fcport->d_id.b.area;
2448 logio->port_id[2] = sp->fcport->d_id.b.domain;
2449 logio->vp_index = sp->vha->vp_idx;
2453 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
2455 struct qla_hw_data *ha = sp->vha->hw;
2456 struct srb_iocb *lio = &sp->u.iocb_cmd;
2459 mbx->entry_type = MBX_IOCB_TYPE;
2460 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2461 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
2462 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
2463 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
2464 if (HAS_EXTENDED_IDS(ha)) {
2465 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2466 mbx->mb10 = cpu_to_le16(opts);
2468 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
2470 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2471 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2472 sp->fcport->d_id.b.al_pa);
2473 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2477 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2479 u16 control_flags = LCF_COMMAND_LOGO;
2480 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2482 if (sp->fcport->explicit_logout) {
2483 control_flags |= LCF_EXPL_LOGO|LCF_FREE_NPORT;
2485 control_flags |= LCF_IMPL_LOGO;
2487 if (!sp->fcport->keep_nport_handle)
2488 control_flags |= LCF_FREE_NPORT;
2491 logio->control_flags = cpu_to_le16(control_flags);
2492 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2493 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
2494 logio->port_id[1] = sp->fcport->d_id.b.area;
2495 logio->port_id[2] = sp->fcport->d_id.b.domain;
2496 logio->vp_index = sp->vha->vp_idx;
2500 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
2502 struct qla_hw_data *ha = sp->vha->hw;
2504 mbx->entry_type = MBX_IOCB_TYPE;
2505 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2506 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
2507 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
2508 cpu_to_le16(sp->fcport->loop_id) :
2509 cpu_to_le16(sp->fcport->loop_id << 8);
2510 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
2511 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
2512 sp->fcport->d_id.b.al_pa);
2513 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2514 /* Implicit: mbx->mbx10 = 0. */
2518 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
2520 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
2521 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
2522 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2523 logio->vp_index = sp->vha->vp_idx;
2527 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
2529 struct qla_hw_data *ha = sp->vha->hw;
2531 mbx->entry_type = MBX_IOCB_TYPE;
2532 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
2533 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
2534 if (HAS_EXTENDED_IDS(ha)) {
2535 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
2536 mbx->mb10 = cpu_to_le16(BIT_0);
2538 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
2540 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
2541 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
2542 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
2543 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
2544 mbx->mb9 = cpu_to_le16(sp->vha->vp_idx);
2548 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
2552 struct fc_port *fcport = sp->fcport;
2553 scsi_qla_host_t *vha = fcport->vha;
2554 struct qla_hw_data *ha = vha->hw;
2555 struct srb_iocb *iocb = &sp->u.iocb_cmd;
2556 struct req_que *req = vha->req;
2558 flags = iocb->u.tmf.flags;
2559 lun = iocb->u.tmf.lun;
2561 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2562 tsk->entry_count = 1;
2563 tsk->handle = make_handle(req->id, tsk->handle);
2564 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2565 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2566 tsk->control_flags = cpu_to_le32(flags);
2567 tsk->port_id[0] = fcport->d_id.b.al_pa;
2568 tsk->port_id[1] = fcport->d_id.b.area;
2569 tsk->port_id[2] = fcport->d_id.b.domain;
2570 tsk->vp_index = fcport->vha->vp_idx;
2572 if (flags == TCF_LUN_RESET) {
2573 int_to_scsilun(lun, &tsk->lun);
2574 host_to_fcp_swap((uint8_t *)&tsk->lun,
2579 void qla2x00_init_timer(srb_t *sp, unsigned long tmo)
2581 timer_setup(&sp->u.iocb_cmd.timer, qla2x00_sp_timeout, 0);
2582 sp->u.iocb_cmd.timer.expires = jiffies + tmo * HZ;
2583 sp->free = qla2x00_sp_free;
2584 if (IS_QLAFX00(sp->vha->hw) && sp->type == SRB_FXIOCB_DCMD)
2585 init_completion(&sp->u.iocb_cmd.u.fxiocb.fxiocb_comp);
2586 sp->start_timer = 1;
2589 static void qla2x00_els_dcmd_sp_free(srb_t *sp)
2591 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2595 if (elsio->u.els_logo.els_logo_pyld)
2596 dma_free_coherent(&sp->vha->hw->pdev->dev, DMA_POOL_SIZE,
2597 elsio->u.els_logo.els_logo_pyld,
2598 elsio->u.els_logo.els_logo_pyld_dma);
2600 del_timer(&elsio->timer);
2605 qla2x00_els_dcmd_iocb_timeout(void *data)
2608 fc_port_t *fcport = sp->fcport;
2609 struct scsi_qla_host *vha = sp->vha;
2610 struct srb_iocb *lio = &sp->u.iocb_cmd;
2611 unsigned long flags = 0;
2614 ql_dbg(ql_dbg_io, vha, 0x3069,
2615 "%s Timeout, hdl=%x, portid=%02x%02x%02x\n",
2616 sp->name, sp->handle, fcport->d_id.b.domain, fcport->d_id.b.area,
2617 fcport->d_id.b.al_pa);
2619 /* Abort the exchange */
2620 res = qla24xx_async_abort_cmd(sp, false);
2622 ql_dbg(ql_dbg_io, vha, 0x3070,
2623 "mbx abort_command failed.\n");
2624 spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2625 for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
2626 if (sp->qpair->req->outstanding_cmds[h] == sp) {
2627 sp->qpair->req->outstanding_cmds[h] = NULL;
2631 spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2632 complete(&lio->u.els_logo.comp);
2634 ql_dbg(ql_dbg_io, vha, 0x3071,
2635 "mbx abort_command success.\n");
2639 static void qla2x00_els_dcmd_sp_done(srb_t *sp, int res)
2641 fc_port_t *fcport = sp->fcport;
2642 struct srb_iocb *lio = &sp->u.iocb_cmd;
2643 struct scsi_qla_host *vha = sp->vha;
2645 ql_dbg(ql_dbg_io, vha, 0x3072,
2646 "%s hdl=%x, portid=%02x%02x%02x done\n",
2647 sp->name, sp->handle, fcport->d_id.b.domain,
2648 fcport->d_id.b.area, fcport->d_id.b.al_pa);
2650 complete(&lio->u.els_logo.comp);
2654 qla24xx_els_dcmd_iocb(scsi_qla_host_t *vha, int els_opcode,
2655 port_id_t remote_did)
2658 fc_port_t *fcport = NULL;
2659 struct srb_iocb *elsio = NULL;
2660 struct qla_hw_data *ha = vha->hw;
2661 struct els_logo_payload logo_pyld;
2662 int rval = QLA_SUCCESS;
2664 fcport = qla2x00_alloc_fcport(vha, GFP_KERNEL);
2666 ql_log(ql_log_info, vha, 0x70e5, "fcport allocation failed\n");
2670 /* Alloc SRB structure */
2671 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2674 ql_log(ql_log_info, vha, 0x70e6,
2675 "SRB allocation failed\n");
2679 elsio = &sp->u.iocb_cmd;
2680 fcport->loop_id = 0xFFFF;
2681 fcport->d_id.b.domain = remote_did.b.domain;
2682 fcport->d_id.b.area = remote_did.b.area;
2683 fcport->d_id.b.al_pa = remote_did.b.al_pa;
2685 ql_dbg(ql_dbg_io, vha, 0x3073, "portid=%02x%02x%02x done\n",
2686 fcport->d_id.b.domain, fcport->d_id.b.area, fcport->d_id.b.al_pa);
2688 sp->type = SRB_ELS_DCMD;
2689 sp->name = "ELS_DCMD";
2690 sp->fcport = fcport;
2691 elsio->timeout = qla2x00_els_dcmd_iocb_timeout;
2692 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT);
2693 init_completion(&sp->u.iocb_cmd.u.els_logo.comp);
2694 sp->done = qla2x00_els_dcmd_sp_done;
2695 sp->free = qla2x00_els_dcmd_sp_free;
2697 elsio->u.els_logo.els_logo_pyld = dma_alloc_coherent(&ha->pdev->dev,
2698 DMA_POOL_SIZE, &elsio->u.els_logo.els_logo_pyld_dma,
2701 if (!elsio->u.els_logo.els_logo_pyld) {
2703 return QLA_FUNCTION_FAILED;
2706 memset(&logo_pyld, 0, sizeof(struct els_logo_payload));
2708 elsio->u.els_logo.els_cmd = els_opcode;
2709 logo_pyld.opcode = els_opcode;
2710 logo_pyld.s_id[0] = vha->d_id.b.al_pa;
2711 logo_pyld.s_id[1] = vha->d_id.b.area;
2712 logo_pyld.s_id[2] = vha->d_id.b.domain;
2713 host_to_fcp_swap(logo_pyld.s_id, sizeof(uint32_t));
2714 memcpy(&logo_pyld.wwpn, vha->port_name, WWN_SIZE);
2716 memcpy(elsio->u.els_logo.els_logo_pyld, &logo_pyld,
2717 sizeof(struct els_logo_payload));
2718 ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x3075, "LOGO buffer:");
2719 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x010a,
2720 elsio->u.els_logo.els_logo_pyld,
2721 sizeof(*elsio->u.els_logo.els_logo_pyld));
2723 rval = qla2x00_start_sp(sp);
2724 if (rval != QLA_SUCCESS) {
2726 return QLA_FUNCTION_FAILED;
2729 ql_dbg(ql_dbg_io, vha, 0x3074,
2730 "%s LOGO sent, hdl=%x, loopid=%x, portid=%02x%02x%02x.\n",
2731 sp->name, sp->handle, fcport->loop_id, fcport->d_id.b.domain,
2732 fcport->d_id.b.area, fcport->d_id.b.al_pa);
2734 wait_for_completion(&elsio->u.els_logo.comp);
2741 qla24xx_els_logo_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2743 scsi_qla_host_t *vha = sp->vha;
2744 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2746 els_iocb->entry_type = ELS_IOCB_TYPE;
2747 els_iocb->entry_count = 1;
2748 els_iocb->sys_define = 0;
2749 els_iocb->entry_status = 0;
2750 els_iocb->handle = sp->handle;
2751 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2752 els_iocb->tx_dsd_count = cpu_to_le16(1);
2753 els_iocb->vp_index = vha->vp_idx;
2754 els_iocb->sof_type = EST_SOFI3;
2755 els_iocb->rx_dsd_count = 0;
2756 els_iocb->opcode = elsio->u.els_logo.els_cmd;
2758 els_iocb->d_id[0] = sp->fcport->d_id.b.al_pa;
2759 els_iocb->d_id[1] = sp->fcport->d_id.b.area;
2760 els_iocb->d_id[2] = sp->fcport->d_id.b.domain;
2761 /* For SID the byte order is different than DID */
2762 els_iocb->s_id[1] = vha->d_id.b.al_pa;
2763 els_iocb->s_id[2] = vha->d_id.b.area;
2764 els_iocb->s_id[0] = vha->d_id.b.domain;
2766 if (elsio->u.els_logo.els_cmd == ELS_DCMD_PLOGI) {
2767 els_iocb->control_flags = 0;
2768 els_iocb->tx_byte_count = els_iocb->tx_len =
2769 cpu_to_le32(sizeof(struct els_plogi_payload));
2770 put_unaligned_le64(elsio->u.els_plogi.els_plogi_pyld_dma,
2771 &els_iocb->tx_address);
2772 els_iocb->rx_dsd_count = cpu_to_le16(1);
2773 els_iocb->rx_byte_count = els_iocb->rx_len =
2774 cpu_to_le32(sizeof(struct els_plogi_payload));
2775 put_unaligned_le64(elsio->u.els_plogi.els_resp_pyld_dma,
2776 &els_iocb->rx_address);
2778 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3073,
2779 "PLOGI ELS IOCB:\n");
2780 ql_dump_buffer(ql_log_info, vha, 0x0109,
2781 (uint8_t *)els_iocb,
2784 els_iocb->control_flags = cpu_to_le16(1 << 13);
2785 els_iocb->tx_byte_count =
2786 cpu_to_le32(sizeof(struct els_logo_payload));
2787 put_unaligned_le64(elsio->u.els_logo.els_logo_pyld_dma,
2788 &els_iocb->tx_address);
2789 els_iocb->tx_len = cpu_to_le32(sizeof(struct els_logo_payload));
2791 els_iocb->rx_byte_count = 0;
2792 els_iocb->rx_address = 0;
2793 els_iocb->rx_len = 0;
2794 ql_dbg(ql_dbg_io + ql_dbg_buffer, vha, 0x3076,
2796 ql_dump_buffer(ql_log_info, vha, 0x010b,
2801 sp->vha->qla_stats.control_requests++;
2805 qla2x00_els_dcmd2_iocb_timeout(void *data)
2808 fc_port_t *fcport = sp->fcport;
2809 struct scsi_qla_host *vha = sp->vha;
2810 unsigned long flags = 0;
2813 ql_dbg(ql_dbg_io + ql_dbg_disc, vha, 0x3069,
2814 "%s hdl=%x ELS Timeout, %8phC portid=%06x\n",
2815 sp->name, sp->handle, fcport->port_name, fcport->d_id.b24);
2817 /* Abort the exchange */
2818 res = qla24xx_async_abort_cmd(sp, false);
2819 ql_dbg(ql_dbg_io, vha, 0x3070,
2820 "mbx abort_command %s\n",
2821 (res == QLA_SUCCESS) ? "successful" : "failed");
2823 spin_lock_irqsave(sp->qpair->qp_lock_ptr, flags);
2824 for (h = 1; h < sp->qpair->req->num_outstanding_cmds; h++) {
2825 if (sp->qpair->req->outstanding_cmds[h] == sp) {
2826 sp->qpair->req->outstanding_cmds[h] = NULL;
2830 spin_unlock_irqrestore(sp->qpair->qp_lock_ptr, flags);
2831 sp->done(sp, QLA_FUNCTION_TIMEOUT);
2835 void qla2x00_els_dcmd2_free(scsi_qla_host_t *vha, struct els_plogi *els_plogi)
2837 if (els_plogi->els_plogi_pyld)
2838 dma_free_coherent(&vha->hw->pdev->dev,
2840 els_plogi->els_plogi_pyld,
2841 els_plogi->els_plogi_pyld_dma);
2843 if (els_plogi->els_resp_pyld)
2844 dma_free_coherent(&vha->hw->pdev->dev,
2846 els_plogi->els_resp_pyld,
2847 els_plogi->els_resp_pyld_dma);
2850 static void qla2x00_els_dcmd2_sp_done(srb_t *sp, int res)
2852 fc_port_t *fcport = sp->fcport;
2853 struct srb_iocb *lio = &sp->u.iocb_cmd;
2854 struct scsi_qla_host *vha = sp->vha;
2855 struct event_arg ea;
2856 struct qla_work_evt *e;
2857 struct fc_port *conflict_fcport;
2858 port_id_t cid; /* conflict Nport id */
2859 const __le32 *fw_status = sp->u.iocb_cmd.u.els_plogi.fw_status;
2862 ql_dbg(ql_dbg_disc, vha, 0x3072,
2863 "%s ELS done rc %d hdl=%x, portid=%06x %8phC\n",
2864 sp->name, res, sp->handle, fcport->d_id.b24, fcport->port_name);
2866 fcport->flags &= ~(FCF_ASYNC_SENT|FCF_ASYNC_ACTIVE);
2867 del_timer(&sp->u.iocb_cmd.timer);
2869 if (sp->flags & SRB_WAKEUP_ON_COMP)
2870 complete(&lio->u.els_plogi.comp);
2872 switch (le32_to_cpu(fw_status[0])) {
2873 case CS_DATA_UNDERRUN:
2875 memset(&ea, 0, sizeof(ea));
2878 qla_handle_els_plogi_done(vha, &ea);
2882 switch (le32_to_cpu(fw_status[1])) {
2883 case LSC_SCODE_PORTID_USED:
2884 lid = le32_to_cpu(fw_status[2]) & 0xffff;
2885 qlt_find_sess_invalidate_other(vha,
2886 wwn_to_u64(fcport->port_name),
2887 fcport->d_id, lid, &conflict_fcport);
2888 if (conflict_fcport) {
2890 * Another fcport shares the same
2891 * loop_id & nport id; conflict
2892 * fcport needs to finish cleanup
2893 * before this fcport can proceed
2896 conflict_fcport->conflict = fcport;
2897 fcport->login_pause = 1;
2898 ql_dbg(ql_dbg_disc, vha, 0x20ed,
2899 "%s %d %8phC pid %06x inuse with lid %#x post gidpn\n",
2902 fcport->d_id.b24, lid);
2904 ql_dbg(ql_dbg_disc, vha, 0x20ed,
2905 "%s %d %8phC pid %06x inuse with lid %#x sched del\n",
2908 fcport->d_id.b24, lid);
2909 qla2x00_clear_loop_id(fcport);
2910 set_bit(lid, vha->hw->loop_id_map);
2911 fcport->loop_id = lid;
2912 fcport->keep_nport_handle = 0;
2913 qlt_schedule_sess_for_deletion(fcport);
2917 case LSC_SCODE_NPORT_USED:
2918 cid.b.domain = (le32_to_cpu(fw_status[2]) >> 16)
2920 cid.b.area = (le32_to_cpu(fw_status[2]) >> 8)
2922 cid.b.al_pa = le32_to_cpu(fw_status[2]) & 0xff;
2925 ql_dbg(ql_dbg_disc, vha, 0x20ec,
2926 "%s %d %8phC lid %#x in use with pid %06x post gnl\n",
2927 __func__, __LINE__, fcport->port_name,
2928 fcport->loop_id, cid.b24);
2929 set_bit(fcport->loop_id,
2930 vha->hw->loop_id_map);
2931 fcport->loop_id = FC_NO_LOOP_ID;
2932 qla24xx_post_gnl_work(vha, fcport);
2935 case LSC_SCODE_NOXCB:
2936 vha->hw->exch_starvation++;
2937 if (vha->hw->exch_starvation > 5) {
2938 ql_log(ql_log_warn, vha, 0xd046,
2939 "Exchange starvation. Resetting RISC\n");
2940 vha->hw->exch_starvation = 0;
2941 set_bit(ISP_ABORT_NEEDED,
2943 qla2xxx_wake_dpc(vha);
2948 ql_dbg(ql_dbg_disc, vha, 0x20eb,
2949 "%s %8phC cmd error fw_status 0x%x 0x%x 0x%x\n",
2950 __func__, sp->fcport->port_name,
2951 fw_status[0], fw_status[1], fw_status[2]);
2953 fcport->flags &= ~FCF_ASYNC_SENT;
2954 qlt_schedule_sess_for_deletion(fcport);
2960 ql_dbg(ql_dbg_disc, vha, 0x20eb,
2961 "%s %8phC cmd error 2 fw_status 0x%x 0x%x 0x%x\n",
2962 __func__, sp->fcport->port_name,
2963 fw_status[0], fw_status[1], fw_status[2]);
2965 sp->fcport->flags &= ~FCF_ASYNC_SENT;
2966 qlt_schedule_sess_for_deletion(fcport);
2970 e = qla2x00_alloc_work(vha, QLA_EVT_UNMAP);
2972 struct srb_iocb *elsio = &sp->u.iocb_cmd;
2974 qla2x00_els_dcmd2_free(vha, &elsio->u.els_plogi);
2979 qla2x00_post_work(vha, e);
2984 qla24xx_els_dcmd2_iocb(scsi_qla_host_t *vha, int els_opcode,
2985 fc_port_t *fcport, bool wait)
2988 struct srb_iocb *elsio = NULL;
2989 struct qla_hw_data *ha = vha->hw;
2990 int rval = QLA_SUCCESS;
2991 void *ptr, *resp_ptr;
2993 /* Alloc SRB structure */
2994 sp = qla2x00_get_sp(vha, fcport, GFP_KERNEL);
2996 ql_log(ql_log_info, vha, 0x70e6,
2997 "SRB allocation failed\n");
2998 fcport->flags &= ~FCF_ASYNC_ACTIVE;
3002 fcport->flags |= FCF_ASYNC_SENT;
3003 qla2x00_set_fcport_disc_state(fcport, DSC_LOGIN_PEND);
3004 elsio = &sp->u.iocb_cmd;
3005 ql_dbg(ql_dbg_io, vha, 0x3073,
3006 "Enter: PLOGI portid=%06x\n", fcport->d_id.b24);
3008 sp->type = SRB_ELS_DCMD;
3009 sp->name = "ELS_DCMD";
3010 sp->fcport = fcport;
3012 elsio->timeout = qla2x00_els_dcmd2_iocb_timeout;
3014 sp->flags = SRB_WAKEUP_ON_COMP;
3016 qla2x00_init_timer(sp, ELS_DCMD_TIMEOUT + 2);
3018 sp->done = qla2x00_els_dcmd2_sp_done;
3019 elsio->u.els_plogi.tx_size = elsio->u.els_plogi.rx_size = DMA_POOL_SIZE;
3021 ptr = elsio->u.els_plogi.els_plogi_pyld =
3022 dma_alloc_coherent(&ha->pdev->dev, elsio->u.els_plogi.tx_size,
3023 &elsio->u.els_plogi.els_plogi_pyld_dma, GFP_KERNEL);
3025 if (!elsio->u.els_plogi.els_plogi_pyld) {
3026 rval = QLA_FUNCTION_FAILED;
3030 resp_ptr = elsio->u.els_plogi.els_resp_pyld =
3031 dma_alloc_coherent(&ha->pdev->dev, elsio->u.els_plogi.rx_size,
3032 &elsio->u.els_plogi.els_resp_pyld_dma, GFP_KERNEL);
3034 if (!elsio->u.els_plogi.els_resp_pyld) {
3035 rval = QLA_FUNCTION_FAILED;
3039 ql_dbg(ql_dbg_io, vha, 0x3073, "PLOGI %p %p\n", ptr, resp_ptr);
3041 memset(ptr, 0, sizeof(struct els_plogi_payload));
3042 memset(resp_ptr, 0, sizeof(struct els_plogi_payload));
3043 memcpy(elsio->u.els_plogi.els_plogi_pyld->data,
3044 &ha->plogi_els_payld.fl_csp, LOGIN_TEMPLATE_SIZE);
3046 elsio->u.els_plogi.els_cmd = els_opcode;
3047 elsio->u.els_plogi.els_plogi_pyld->opcode = els_opcode;
3049 ql_dbg(ql_dbg_disc + ql_dbg_buffer, vha, 0x3073, "PLOGI buffer:\n");
3050 ql_dump_buffer(ql_dbg_disc + ql_dbg_buffer, vha, 0x0109,
3051 (uint8_t *)elsio->u.els_plogi.els_plogi_pyld,
3052 sizeof(*elsio->u.els_plogi.els_plogi_pyld));
3054 init_completion(&elsio->u.els_plogi.comp);
3055 rval = qla2x00_start_sp(sp);
3056 if (rval != QLA_SUCCESS) {
3057 rval = QLA_FUNCTION_FAILED;
3059 ql_dbg(ql_dbg_disc, vha, 0x3074,
3060 "%s PLOGI sent, hdl=%x, loopid=%x, to port_id %06x from port_id %06x\n",
3061 sp->name, sp->handle, fcport->loop_id,
3062 fcport->d_id.b24, vha->d_id.b24);
3066 wait_for_completion(&elsio->u.els_plogi.comp);
3068 if (elsio->u.els_plogi.comp_status != CS_COMPLETE)
3069 rval = QLA_FUNCTION_FAILED;
3075 fcport->flags &= ~(FCF_ASYNC_SENT | FCF_ASYNC_ACTIVE);
3076 qla2x00_els_dcmd2_free(vha, &elsio->u.els_plogi);
3083 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
3085 struct bsg_job *bsg_job = sp->u.bsg_job;
3086 struct fc_bsg_request *bsg_request = bsg_job->request;
3088 els_iocb->entry_type = ELS_IOCB_TYPE;
3089 els_iocb->entry_count = 1;
3090 els_iocb->sys_define = 0;
3091 els_iocb->entry_status = 0;
3092 els_iocb->handle = sp->handle;
3093 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3094 els_iocb->tx_dsd_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
3095 els_iocb->vp_index = sp->vha->vp_idx;
3096 els_iocb->sof_type = EST_SOFI3;
3097 els_iocb->rx_dsd_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
3100 sp->type == SRB_ELS_CMD_RPT ?
3101 bsg_request->rqst_data.r_els.els_code :
3102 bsg_request->rqst_data.h_els.command_code;
3103 els_iocb->d_id[0] = sp->fcport->d_id.b.al_pa;
3104 els_iocb->d_id[1] = sp->fcport->d_id.b.area;
3105 els_iocb->d_id[2] = sp->fcport->d_id.b.domain;
3106 els_iocb->control_flags = 0;
3107 els_iocb->rx_byte_count =
3108 cpu_to_le32(bsg_job->reply_payload.payload_len);
3109 els_iocb->tx_byte_count =
3110 cpu_to_le32(bsg_job->request_payload.payload_len);
3112 put_unaligned_le64(sg_dma_address(bsg_job->request_payload.sg_list),
3113 &els_iocb->tx_address);
3114 els_iocb->tx_len = cpu_to_le32(sg_dma_len
3115 (bsg_job->request_payload.sg_list));
3117 put_unaligned_le64(sg_dma_address(bsg_job->reply_payload.sg_list),
3118 &els_iocb->rx_address);
3119 els_iocb->rx_len = cpu_to_le32(sg_dma_len
3120 (bsg_job->reply_payload.sg_list));
3122 sp->vha->qla_stats.control_requests++;
3126 qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
3128 uint16_t avail_dsds;
3129 struct dsd64 *cur_dsd;
3130 struct scatterlist *sg;
3133 scsi_qla_host_t *vha = sp->vha;
3134 struct qla_hw_data *ha = vha->hw;
3135 struct bsg_job *bsg_job = sp->u.bsg_job;
3136 int entry_count = 1;
3138 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
3139 ct_iocb->entry_type = CT_IOCB_TYPE;
3140 ct_iocb->entry_status = 0;
3141 ct_iocb->handle1 = sp->handle;
3142 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
3143 ct_iocb->status = cpu_to_le16(0);
3144 ct_iocb->control_flags = cpu_to_le16(0);
3145 ct_iocb->timeout = 0;
3146 ct_iocb->cmd_dsd_count =
3147 cpu_to_le16(bsg_job->request_payload.sg_cnt);
3148 ct_iocb->total_dsd_count =
3149 cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
3150 ct_iocb->req_bytecount =
3151 cpu_to_le32(bsg_job->request_payload.payload_len);
3152 ct_iocb->rsp_bytecount =
3153 cpu_to_le32(bsg_job->reply_payload.payload_len);
3155 put_unaligned_le64(sg_dma_address(bsg_job->request_payload.sg_list),
3156 &ct_iocb->req_dsd.address);
3157 ct_iocb->req_dsd.length = ct_iocb->req_bytecount;
3159 put_unaligned_le64(sg_dma_address(bsg_job->reply_payload.sg_list),
3160 &ct_iocb->rsp_dsd.address);
3161 ct_iocb->rsp_dsd.length = ct_iocb->rsp_bytecount;
3164 cur_dsd = &ct_iocb->rsp_dsd;
3166 tot_dsds = bsg_job->reply_payload.sg_cnt;
3168 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
3169 cont_a64_entry_t *cont_pkt;
3171 /* Allocate additional continuation packets? */
3172 if (avail_dsds == 0) {
3174 * Five DSDs are available in the Cont.
3177 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
3178 vha->hw->req_q_map[0]);
3179 cur_dsd = cont_pkt->dsd;
3184 append_dsd64(&cur_dsd, sg);
3187 ct_iocb->entry_count = entry_count;
3189 sp->vha->qla_stats.control_requests++;
3193 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
3195 uint16_t avail_dsds;
3196 struct dsd64 *cur_dsd;
3197 struct scatterlist *sg;
3199 uint16_t cmd_dsds, rsp_dsds;
3200 scsi_qla_host_t *vha = sp->vha;
3201 struct qla_hw_data *ha = vha->hw;
3202 struct bsg_job *bsg_job = sp->u.bsg_job;
3203 int entry_count = 1;
3204 cont_a64_entry_t *cont_pkt = NULL;
3206 ct_iocb->entry_type = CT_IOCB_TYPE;
3207 ct_iocb->entry_status = 0;
3208 ct_iocb->sys_define = 0;
3209 ct_iocb->handle = sp->handle;
3211 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3212 ct_iocb->vp_index = sp->vha->vp_idx;
3213 ct_iocb->comp_status = cpu_to_le16(0);
3215 cmd_dsds = bsg_job->request_payload.sg_cnt;
3216 rsp_dsds = bsg_job->reply_payload.sg_cnt;
3218 ct_iocb->cmd_dsd_count = cpu_to_le16(cmd_dsds);
3219 ct_iocb->timeout = 0;
3220 ct_iocb->rsp_dsd_count = cpu_to_le16(rsp_dsds);
3221 ct_iocb->cmd_byte_count =
3222 cpu_to_le32(bsg_job->request_payload.payload_len);
3225 cur_dsd = ct_iocb->dsd;
3228 for_each_sg(bsg_job->request_payload.sg_list, sg, cmd_dsds, index) {
3229 /* Allocate additional continuation packets? */
3230 if (avail_dsds == 0) {
3232 * Five DSDs are available in the Cont.
3235 cont_pkt = qla2x00_prep_cont_type1_iocb(
3236 vha, ha->req_q_map[0]);
3237 cur_dsd = cont_pkt->dsd;
3242 append_dsd64(&cur_dsd, sg);
3248 for_each_sg(bsg_job->reply_payload.sg_list, sg, rsp_dsds, index) {
3249 /* Allocate additional continuation packets? */
3250 if (avail_dsds == 0) {
3252 * Five DSDs are available in the Cont.
3255 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
3257 cur_dsd = cont_pkt->dsd;
3262 append_dsd64(&cur_dsd, sg);
3265 ct_iocb->entry_count = entry_count;
3269 * qla82xx_start_scsi() - Send a SCSI command to the ISP
3270 * @sp: command to send to the ISP
3272 * Returns non-zero if a failure occurred, else zero.
3275 qla82xx_start_scsi(srb_t *sp)
3278 unsigned long flags;
3279 struct scsi_cmnd *cmd;
3285 struct device_reg_82xx __iomem *reg;
3288 uint8_t additional_cdb_len;
3289 struct ct6_dsd *ctx;
3290 struct scsi_qla_host *vha = sp->vha;
3291 struct qla_hw_data *ha = vha->hw;
3292 struct req_que *req = NULL;
3293 struct rsp_que *rsp = NULL;
3295 /* Setup device pointers. */
3296 reg = &ha->iobase->isp82;
3297 cmd = GET_CMD_SP(sp);
3299 rsp = ha->rsp_q_map[0];
3301 /* So we know we haven't pci_map'ed anything yet */
3304 dbval = 0x04 | (ha->portnum << 5);
3306 /* Send marker if required */
3307 if (vha->marker_needed != 0) {
3308 if (qla2x00_marker(vha, ha->base_qpair,
3309 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
3310 ql_log(ql_log_warn, vha, 0x300c,
3311 "qla2x00_marker failed for cmd=%p.\n", cmd);
3312 return QLA_FUNCTION_FAILED;
3314 vha->marker_needed = 0;
3317 /* Acquire ring specific lock */
3318 spin_lock_irqsave(&ha->hardware_lock, flags);
3320 handle = qla2xxx_get_next_handle(req);
3324 /* Map the sg table so we have an accurate count of sg entries needed */
3325 if (scsi_sg_count(cmd)) {
3326 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
3327 scsi_sg_count(cmd), cmd->sc_data_direction);
3328 if (unlikely(!nseg))
3335 if (tot_dsds > ql2xshiftctondsd) {
3336 struct cmd_type_6 *cmd_pkt;
3337 uint16_t more_dsd_lists = 0;
3338 struct dsd_dma *dsd_ptr;
3341 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
3342 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
3343 ql_dbg(ql_dbg_io, vha, 0x300d,
3344 "Num of DSD list %d is than %d for cmd=%p.\n",
3345 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
3350 if (more_dsd_lists <= ha->gbl_dsd_avail)
3351 goto sufficient_dsds;
3353 more_dsd_lists -= ha->gbl_dsd_avail;
3355 for (i = 0; i < more_dsd_lists; i++) {
3356 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
3358 ql_log(ql_log_fatal, vha, 0x300e,
3359 "Failed to allocate memory for dsd_dma "
3360 "for cmd=%p.\n", cmd);
3364 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
3365 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
3366 if (!dsd_ptr->dsd_addr) {
3368 ql_log(ql_log_fatal, vha, 0x300f,
3369 "Failed to allocate memory for dsd_addr "
3370 "for cmd=%p.\n", cmd);
3373 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
3374 ha->gbl_dsd_avail++;
3380 if (req->cnt < (req_cnt + 2)) {
3381 cnt = (uint16_t)rd_reg_dword_relaxed(
3382 ®->req_q_out[0]);
3383 if (req->ring_index < cnt)
3384 req->cnt = cnt - req->ring_index;
3386 req->cnt = req->length -
3387 (req->ring_index - cnt);
3388 if (req->cnt < (req_cnt + 2))
3392 ctx = sp->u.scmd.ct6_ctx =
3393 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
3395 ql_log(ql_log_fatal, vha, 0x3010,
3396 "Failed to allocate ctx for cmd=%p.\n", cmd);
3400 memset(ctx, 0, sizeof(struct ct6_dsd));
3401 ctx->fcp_cmnd = dma_pool_zalloc(ha->fcp_cmnd_dma_pool,
3402 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
3403 if (!ctx->fcp_cmnd) {
3404 ql_log(ql_log_fatal, vha, 0x3011,
3405 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
3409 /* Initialize the DSD list and dma handle */
3410 INIT_LIST_HEAD(&ctx->dsd_list);
3411 ctx->dsd_use_cnt = 0;
3413 if (cmd->cmd_len > 16) {
3414 additional_cdb_len = cmd->cmd_len - 16;
3415 if ((cmd->cmd_len % 4) != 0) {
3416 /* SCSI command bigger than 16 bytes must be
3419 ql_log(ql_log_warn, vha, 0x3012,
3420 "scsi cmd len %d not multiple of 4 "
3421 "for cmd=%p.\n", cmd->cmd_len, cmd);
3422 goto queuing_error_fcp_cmnd;
3424 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
3426 additional_cdb_len = 0;
3427 ctx->fcp_cmnd_len = 12 + 16 + 4;
3430 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
3431 cmd_pkt->handle = make_handle(req->id, handle);
3433 /* Zero out remaining portion of packet. */
3434 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
3435 clr_ptr = (uint32_t *)cmd_pkt + 2;
3436 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3437 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3439 /* Set NPORT-ID and LUN number*/
3440 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3441 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3442 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3443 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3444 cmd_pkt->vp_index = sp->vha->vp_idx;
3446 /* Build IOCB segments */
3447 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
3448 goto queuing_error_fcp_cmnd;
3450 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3451 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
3453 /* build FCP_CMND IU */
3454 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
3455 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
3457 if (cmd->sc_data_direction == DMA_TO_DEVICE)
3458 ctx->fcp_cmnd->additional_cdb_len |= 1;
3459 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
3460 ctx->fcp_cmnd->additional_cdb_len |= 2;
3462 /* Populate the FCP_PRIO. */
3463 if (ha->flags.fcp_prio_enabled)
3464 ctx->fcp_cmnd->task_attribute |=
3465 sp->fcport->fcp_prio << 3;
3467 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
3469 fcp_dl = (__be32 *)(ctx->fcp_cmnd->cdb + 16 +
3470 additional_cdb_len);
3471 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
3473 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
3474 put_unaligned_le64(ctx->fcp_cmnd_dma,
3475 &cmd_pkt->fcp_cmnd_dseg_address);
3477 sp->flags |= SRB_FCP_CMND_DMA_VALID;
3478 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3479 /* Set total data segment count. */
3480 cmd_pkt->entry_count = (uint8_t)req_cnt;
3481 /* Specify response queue number where
3482 * completion should happen
3484 cmd_pkt->entry_status = (uint8_t) rsp->id;
3486 struct cmd_type_7 *cmd_pkt;
3488 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
3489 if (req->cnt < (req_cnt + 2)) {
3490 cnt = (uint16_t)rd_reg_dword_relaxed(
3491 ®->req_q_out[0]);
3492 if (req->ring_index < cnt)
3493 req->cnt = cnt - req->ring_index;
3495 req->cnt = req->length -
3496 (req->ring_index - cnt);
3498 if (req->cnt < (req_cnt + 2))
3501 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
3502 cmd_pkt->handle = make_handle(req->id, handle);
3504 /* Zero out remaining portion of packet. */
3505 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
3506 clr_ptr = (uint32_t *)cmd_pkt + 2;
3507 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3508 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
3510 /* Set NPORT-ID and LUN number*/
3511 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3512 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
3513 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
3514 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
3515 cmd_pkt->vp_index = sp->vha->vp_idx;
3517 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
3518 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
3519 sizeof(cmd_pkt->lun));
3521 /* Populate the FCP_PRIO. */
3522 if (ha->flags.fcp_prio_enabled)
3523 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
3525 /* Load SCSI command packet. */
3526 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
3527 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
3529 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
3531 /* Build IOCB segments */
3532 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds, req);
3534 /* Set total data segment count. */
3535 cmd_pkt->entry_count = (uint8_t)req_cnt;
3536 /* Specify response queue number where
3537 * completion should happen.
3539 cmd_pkt->entry_status = (uint8_t) rsp->id;
3542 /* Build command packet. */
3543 req->current_outstanding_cmd = handle;
3544 req->outstanding_cmds[handle] = sp;
3545 sp->handle = handle;
3546 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
3547 req->cnt -= req_cnt;
3550 /* Adjust ring index. */
3552 if (req->ring_index == req->length) {
3553 req->ring_index = 0;
3554 req->ring_ptr = req->ring;
3558 sp->flags |= SRB_DMA_VALID;
3560 /* Set chip new ring index. */
3561 /* write, read and verify logic */
3562 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
3564 qla82xx_wr_32(ha, (uintptr_t __force)ha->nxdb_wr_ptr, dbval);
3566 wrt_reg_dword(ha->nxdb_wr_ptr, dbval);
3568 while (rd_reg_dword(ha->nxdb_rd_ptr) != dbval) {
3569 wrt_reg_dword(ha->nxdb_wr_ptr, dbval);
3574 /* Manage unprocessed RIO/ZIO commands in response queue. */
3575 if (vha->flags.process_response_queue &&
3576 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
3577 qla24xx_process_response_queue(vha, rsp);
3579 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3582 queuing_error_fcp_cmnd:
3583 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
3586 scsi_dma_unmap(cmd);
3588 if (sp->u.scmd.crc_ctx) {
3589 mempool_free(sp->u.scmd.crc_ctx, ha->ctx_mempool);
3590 sp->u.scmd.crc_ctx = NULL;
3592 spin_unlock_irqrestore(&ha->hardware_lock, flags);
3594 return QLA_FUNCTION_FAILED;
3598 qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
3600 struct srb_iocb *aio = &sp->u.iocb_cmd;
3601 scsi_qla_host_t *vha = sp->vha;
3602 struct req_que *req = sp->qpair->req;
3604 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
3605 abt_iocb->entry_type = ABORT_IOCB_TYPE;
3606 abt_iocb->entry_count = 1;
3607 abt_iocb->handle = make_handle(req->id, sp->handle);
3609 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3610 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
3611 abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
3612 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
3614 abt_iocb->handle_to_abort =
3615 make_handle(le16_to_cpu(aio->u.abt.req_que_no),
3616 aio->u.abt.cmd_hndl);
3617 abt_iocb->vp_index = vha->vp_idx;
3618 abt_iocb->req_que_no = aio->u.abt.req_que_no;
3619 /* Send the command to the firmware */
3624 qla2x00_mb_iocb(srb_t *sp, struct mbx_24xx_entry *mbx)
3628 mbx->entry_type = MBX_IOCB_TYPE;
3629 mbx->handle = sp->handle;
3630 sz = min(ARRAY_SIZE(mbx->mb), ARRAY_SIZE(sp->u.iocb_cmd.u.mbx.out_mb));
3632 for (i = 0; i < sz; i++)
3633 mbx->mb[i] = sp->u.iocb_cmd.u.mbx.out_mb[i];
3637 qla2x00_ctpthru_cmd_iocb(srb_t *sp, struct ct_entry_24xx *ct_pkt)
3639 sp->u.iocb_cmd.u.ctarg.iocb = ct_pkt;
3640 qla24xx_prep_ms_iocb(sp->vha, &sp->u.iocb_cmd.u.ctarg);
3641 ct_pkt->handle = sp->handle;
3644 static void qla2x00_send_notify_ack_iocb(srb_t *sp,
3645 struct nack_to_isp *nack)
3647 struct imm_ntfy_from_isp *ntfy = sp->u.iocb_cmd.u.nack.ntfy;
3649 nack->entry_type = NOTIFY_ACK_TYPE;
3650 nack->entry_count = 1;
3651 nack->ox_id = ntfy->ox_id;
3653 nack->u.isp24.handle = sp->handle;
3654 nack->u.isp24.nport_handle = ntfy->u.isp24.nport_handle;
3655 if (le16_to_cpu(ntfy->u.isp24.status) == IMM_NTFY_ELS) {
3656 nack->u.isp24.flags = ntfy->u.isp24.flags &
3657 cpu_to_le16(NOTIFY24XX_FLAGS_PUREX_IOCB);
3659 nack->u.isp24.srr_rx_id = ntfy->u.isp24.srr_rx_id;
3660 nack->u.isp24.status = ntfy->u.isp24.status;
3661 nack->u.isp24.status_subcode = ntfy->u.isp24.status_subcode;
3662 nack->u.isp24.fw_handle = ntfy->u.isp24.fw_handle;
3663 nack->u.isp24.exchange_address = ntfy->u.isp24.exchange_address;
3664 nack->u.isp24.srr_rel_offs = ntfy->u.isp24.srr_rel_offs;
3665 nack->u.isp24.srr_ui = ntfy->u.isp24.srr_ui;
3666 nack->u.isp24.srr_flags = 0;
3667 nack->u.isp24.srr_reject_code = 0;
3668 nack->u.isp24.srr_reject_code_expl = 0;
3669 nack->u.isp24.vp_index = ntfy->u.isp24.vp_index;
3673 * Build NVME LS request
3676 qla_nvme_ls(srb_t *sp, struct pt_ls4_request *cmd_pkt)
3678 struct srb_iocb *nvme;
3680 nvme = &sp->u.iocb_cmd;
3681 cmd_pkt->entry_type = PT_LS4_REQUEST;
3682 cmd_pkt->entry_count = 1;
3683 cmd_pkt->control_flags = cpu_to_le16(CF_LS4_ORIGINATOR << CF_LS4_SHIFT);
3685 cmd_pkt->timeout = cpu_to_le16(nvme->u.nvme.timeout_sec);
3686 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3687 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
3689 cmd_pkt->tx_dseg_count = cpu_to_le16(1);
3690 cmd_pkt->tx_byte_count = cpu_to_le32(nvme->u.nvme.cmd_len);
3691 cmd_pkt->dsd[0].length = cpu_to_le32(nvme->u.nvme.cmd_len);
3692 put_unaligned_le64(nvme->u.nvme.cmd_dma, &cmd_pkt->dsd[0].address);
3694 cmd_pkt->rx_dseg_count = cpu_to_le16(1);
3695 cmd_pkt->rx_byte_count = cpu_to_le32(nvme->u.nvme.rsp_len);
3696 cmd_pkt->dsd[1].length = cpu_to_le32(nvme->u.nvme.rsp_len);
3697 put_unaligned_le64(nvme->u.nvme.rsp_dma, &cmd_pkt->dsd[1].address);
3701 qla25xx_ctrlvp_iocb(srb_t *sp, struct vp_ctrl_entry_24xx *vce)
3705 vce->entry_type = VP_CTRL_IOCB_TYPE;
3706 vce->handle = sp->handle;
3707 vce->entry_count = 1;
3708 vce->command = cpu_to_le16(sp->u.iocb_cmd.u.ctrlvp.cmd);
3709 vce->vp_count = cpu_to_le16(1);
3712 * index map in firmware starts with 1; decrement index
3713 * this is ok as we never use index 0
3715 map = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) / 8;
3716 pos = (sp->u.iocb_cmd.u.ctrlvp.vp_index - 1) & 7;
3717 vce->vp_idx_map[map] |= 1 << pos;
3721 qla24xx_prlo_iocb(srb_t *sp, struct logio_entry_24xx *logio)
3723 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
3724 logio->control_flags =
3725 cpu_to_le16(LCF_COMMAND_PRLO|LCF_IMPL_PRLO);
3727 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
3728 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
3729 logio->port_id[1] = sp->fcport->d_id.b.area;
3730 logio->port_id[2] = sp->fcport->d_id.b.domain;
3731 logio->vp_index = sp->fcport->vha->vp_idx;
3735 qla2x00_start_sp(srb_t *sp)
3737 int rval = QLA_SUCCESS;
3738 scsi_qla_host_t *vha = sp->vha;
3739 struct qla_hw_data *ha = vha->hw;
3740 struct qla_qpair *qp = sp->qpair;
3742 unsigned long flags;
3744 if (vha->hw->flags.eeh_busy)
3747 spin_lock_irqsave(qp->qp_lock_ptr, flags);
3748 pkt = __qla2x00_alloc_iocbs(sp->qpair, sp);
3751 ql_log(ql_log_warn, vha, 0x700c,
3752 "qla2x00_alloc_iocbs failed.\n");
3758 IS_FWI2_CAPABLE(ha) ?
3759 qla24xx_login_iocb(sp, pkt) :
3760 qla2x00_login_iocb(sp, pkt);
3763 qla24xx_prli_iocb(sp, pkt);
3765 case SRB_LOGOUT_CMD:
3766 IS_FWI2_CAPABLE(ha) ?
3767 qla24xx_logout_iocb(sp, pkt) :
3768 qla2x00_logout_iocb(sp, pkt);
3770 case SRB_ELS_CMD_RPT:
3771 case SRB_ELS_CMD_HST:
3772 qla24xx_els_iocb(sp, pkt);
3775 IS_FWI2_CAPABLE(ha) ?
3776 qla24xx_ct_iocb(sp, pkt) :
3777 qla2x00_ct_iocb(sp, pkt);
3780 IS_FWI2_CAPABLE(ha) ?
3781 qla24xx_adisc_iocb(sp, pkt) :
3782 qla2x00_adisc_iocb(sp, pkt);
3786 qlafx00_tm_iocb(sp, pkt) :
3787 qla24xx_tm_iocb(sp, pkt);
3789 case SRB_FXIOCB_DCMD:
3790 case SRB_FXIOCB_BCMD:
3791 qlafx00_fxdisc_iocb(sp, pkt);
3794 qla_nvme_ls(sp, pkt);
3798 qlafx00_abort_iocb(sp, pkt) :
3799 qla24xx_abort_iocb(sp, pkt);
3802 qla24xx_els_logo_iocb(sp, pkt);
3804 case SRB_CT_PTHRU_CMD:
3805 qla2x00_ctpthru_cmd_iocb(sp, pkt);
3808 qla2x00_mb_iocb(sp, pkt);
3810 case SRB_NACK_PLOGI:
3813 qla2x00_send_notify_ack_iocb(sp, pkt);
3816 qla25xx_ctrlvp_iocb(sp, pkt);
3819 qla24xx_prlo_iocb(sp, pkt);
3825 if (sp->start_timer)
3826 add_timer(&sp->u.iocb_cmd.timer);
3829 qla2x00_start_iocbs(vha, qp->req);
3831 spin_unlock_irqrestore(qp->qp_lock_ptr, flags);
3836 qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
3837 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
3839 uint16_t avail_dsds;
3840 struct dsd64 *cur_dsd;
3841 uint32_t req_data_len = 0;
3842 uint32_t rsp_data_len = 0;
3843 struct scatterlist *sg;
3845 int entry_count = 1;
3846 struct bsg_job *bsg_job = sp->u.bsg_job;
3848 /*Update entry type to indicate bidir command */
3849 put_unaligned_le32(COMMAND_BIDIRECTIONAL, &cmd_pkt->entry_type);
3851 /* Set the transfer direction, in this set both flags
3852 * Also set the BD_WRAP_BACK flag, firmware will take care
3853 * assigning DID=SID for outgoing pkts.
3855 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
3856 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
3857 cmd_pkt->control_flags = cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
3860 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
3861 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
3862 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
3863 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
3865 vha->bidi_stats.transfer_bytes += req_data_len;
3866 vha->bidi_stats.io_count++;
3868 vha->qla_stats.output_bytes += req_data_len;
3869 vha->qla_stats.output_requests++;
3871 /* Only one dsd is available for bidirectional IOCB, remaining dsds
3872 * are bundled in continuation iocb
3875 cur_dsd = &cmd_pkt->fcp_dsd;
3879 for_each_sg(bsg_job->request_payload.sg_list, sg,
3880 bsg_job->request_payload.sg_cnt, index) {
3881 cont_a64_entry_t *cont_pkt;
3883 /* Allocate additional continuation packets */
3884 if (avail_dsds == 0) {
3885 /* Continuation type 1 IOCB can accomodate
3888 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
3889 cur_dsd = cont_pkt->dsd;
3893 append_dsd64(&cur_dsd, sg);
3896 /* For read request DSD will always goes to continuation IOCB
3897 * and follow the write DSD. If there is room on the current IOCB
3898 * then it is added to that IOCB else new continuation IOCB is
3901 for_each_sg(bsg_job->reply_payload.sg_list, sg,
3902 bsg_job->reply_payload.sg_cnt, index) {
3903 cont_a64_entry_t *cont_pkt;
3905 /* Allocate additional continuation packets */
3906 if (avail_dsds == 0) {
3907 /* Continuation type 1 IOCB can accomodate
3910 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
3911 cur_dsd = cont_pkt->dsd;
3915 append_dsd64(&cur_dsd, sg);
3918 /* This value should be same as number of IOCB required for this cmd */
3919 cmd_pkt->entry_count = entry_count;
3923 qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
3926 struct qla_hw_data *ha = vha->hw;
3927 unsigned long flags;
3932 struct cmd_bidir *cmd_pkt = NULL;
3933 struct rsp_que *rsp;
3934 struct req_que *req;
3935 int rval = EXT_STATUS_OK;
3939 rsp = ha->rsp_q_map[0];
3942 /* Send marker if required */
3943 if (vha->marker_needed != 0) {
3944 if (qla2x00_marker(vha, ha->base_qpair,
3945 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
3946 return EXT_STATUS_MAILBOX;
3947 vha->marker_needed = 0;
3950 /* Acquire ring specific lock */
3951 spin_lock_irqsave(&ha->hardware_lock, flags);
3953 handle = qla2xxx_get_next_handle(req);
3955 rval = EXT_STATUS_BUSY;
3959 /* Calculate number of IOCB required */
3960 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
3962 /* Check for room on request queue. */
3963 if (req->cnt < req_cnt + 2) {
3964 if (IS_SHADOW_REG_CAPABLE(ha)) {
3965 cnt = *req->out_ptr;
3967 cnt = rd_reg_dword_relaxed(req->req_q_out);
3968 if (qla2x00_check_reg16_for_disconnect(vha, cnt))
3972 if (req->ring_index < cnt)
3973 req->cnt = cnt - req->ring_index;
3975 req->cnt = req->length -
3976 (req->ring_index - cnt);
3978 if (req->cnt < req_cnt + 2) {
3979 rval = EXT_STATUS_BUSY;
3983 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
3984 cmd_pkt->handle = make_handle(req->id, handle);
3986 /* Zero out remaining portion of packet. */
3987 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
3988 clr_ptr = (uint32_t *)cmd_pkt + 2;
3989 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
3991 /* Set NPORT-ID (of vha)*/
3992 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
3993 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
3994 cmd_pkt->port_id[1] = vha->d_id.b.area;
3995 cmd_pkt->port_id[2] = vha->d_id.b.domain;
3997 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
3998 cmd_pkt->entry_status = (uint8_t) rsp->id;
3999 /* Build command packet. */
4000 req->current_outstanding_cmd = handle;
4001 req->outstanding_cmds[handle] = sp;
4002 sp->handle = handle;
4003 req->cnt -= req_cnt;
4005 /* Send the command to the firmware */
4007 qla2x00_start_iocbs(vha, req);
4009 spin_unlock_irqrestore(&ha->hardware_lock, flags);