2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2008 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
9 #include <linux/blkdev.h>
10 #include <linux/delay.h>
12 #include <scsi/scsi_tcq.h>
14 static request_t *qla2x00_req_pkt(struct scsi_qla_host *, struct req_que *,
16 static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
18 static void qla25xx_set_que(srb_t *, struct rsp_que **);
20 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
23 * Returns the proper CF_* direction based on CDB.
25 static inline uint16_t
26 qla2x00_get_cmd_direction(srb_t *sp)
32 /* Set transfer direction */
33 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
35 sp->fcport->vha->hw->qla_stats.output_bytes +=
36 scsi_bufflen(sp->cmd);
37 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
39 sp->fcport->vha->hw->qla_stats.input_bytes +=
40 scsi_bufflen(sp->cmd);
46 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
47 * Continuation Type 0 IOCBs to allocate.
49 * @dsds: number of data segment decriptors needed
51 * Returns the number of IOCB entries needed to store @dsds.
54 qla2x00_calc_iocbs_32(uint16_t dsds)
60 iocbs += (dsds - 3) / 7;
68 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
69 * Continuation Type 1 IOCBs to allocate.
71 * @dsds: number of data segment decriptors needed
73 * Returns the number of IOCB entries needed to store @dsds.
76 qla2x00_calc_iocbs_64(uint16_t dsds)
82 iocbs += (dsds - 2) / 5;
90 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
93 * Returns a pointer to the Continuation Type 0 IOCB packet.
95 static inline cont_entry_t *
96 qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
98 cont_entry_t *cont_pkt;
99 struct req_que *req = vha->req;
100 /* Adjust ring index. */
102 if (req->ring_index == req->length) {
104 req->ring_ptr = req->ring;
109 cont_pkt = (cont_entry_t *)req->ring_ptr;
111 /* Load packet defaults. */
112 *((uint32_t *)(&cont_pkt->entry_type)) =
113 __constant_cpu_to_le32(CONTINUE_TYPE);
119 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
122 * Returns a pointer to the continuation type 1 IOCB packet.
124 static inline cont_a64_entry_t *
125 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
127 cont_a64_entry_t *cont_pkt;
129 struct req_que *req = vha->req;
130 /* Adjust ring index. */
132 if (req->ring_index == req->length) {
134 req->ring_ptr = req->ring;
139 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
141 /* Load packet defaults. */
142 *((uint32_t *)(&cont_pkt->entry_type)) =
143 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
149 qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
151 uint8_t guard = scsi_host_get_guard(sp->cmd->device->host);
153 /* We only support T10 DIF right now */
154 if (guard != SHOST_DIX_GUARD_CRC) {
155 DEBUG2(printk(KERN_ERR "Unsupported guard: %d\n", guard));
159 /* We always use DIFF Bundling for best performance */
162 /* Translate SCSI opcode to a protection opcode */
163 switch (scsi_get_prot_op(sp->cmd)) {
164 case SCSI_PROT_READ_STRIP:
165 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
167 case SCSI_PROT_WRITE_INSERT:
168 *fw_prot_opts |= PO_MODE_DIF_INSERT;
170 case SCSI_PROT_READ_INSERT:
171 *fw_prot_opts |= PO_MODE_DIF_INSERT;
173 case SCSI_PROT_WRITE_STRIP:
174 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
176 case SCSI_PROT_READ_PASS:
177 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 case SCSI_PROT_WRITE_PASS:
180 *fw_prot_opts |= PO_MODE_DIF_PASS;
182 default: /* Normal Request */
183 *fw_prot_opts |= PO_MODE_DIF_PASS;
187 return scsi_prot_sg_count(sp->cmd);
191 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
192 * capable IOCB types.
194 * @sp: SRB command to process
195 * @cmd_pkt: Command type 2 IOCB
196 * @tot_dsds: Total number of segments to transfer
198 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
203 scsi_qla_host_t *vha;
204 struct scsi_cmnd *cmd;
205 struct scatterlist *sg;
210 /* Update entry type to indicate Command Type 2 IOCB */
211 *((uint32_t *)(&cmd_pkt->entry_type)) =
212 __constant_cpu_to_le32(COMMAND_TYPE);
214 /* No data transfer */
215 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
216 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
220 vha = sp->fcport->vha;
221 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
223 /* Three DSDs are available in the Command Type 2 IOCB */
225 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
227 /* Load data segments */
228 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
229 cont_entry_t *cont_pkt;
231 /* Allocate additional continuation packets? */
232 if (avail_dsds == 0) {
234 * Seven DSDs are available in the Continuation
237 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
238 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
242 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
243 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
249 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
250 * capable IOCB types.
252 * @sp: SRB command to process
253 * @cmd_pkt: Command type 3 IOCB
254 * @tot_dsds: Total number of segments to transfer
256 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
261 scsi_qla_host_t *vha;
262 struct scsi_cmnd *cmd;
263 struct scatterlist *sg;
268 /* Update entry type to indicate Command Type 3 IOCB */
269 *((uint32_t *)(&cmd_pkt->entry_type)) =
270 __constant_cpu_to_le32(COMMAND_A64_TYPE);
272 /* No data transfer */
273 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
274 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
278 vha = sp->fcport->vha;
279 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
281 /* Two DSDs are available in the Command Type 3 IOCB */
283 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
285 /* Load data segments */
286 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
288 cont_a64_entry_t *cont_pkt;
290 /* Allocate additional continuation packets? */
291 if (avail_dsds == 0) {
293 * Five DSDs are available in the Continuation
296 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
297 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
301 sle_dma = sg_dma_address(sg);
302 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
303 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
304 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
310 * qla2x00_start_scsi() - Send a SCSI command to the ISP
311 * @sp: command to send to the ISP
313 * Returns non-zero if a failure occurred, else zero.
316 qla2x00_start_scsi(srb_t *sp)
320 scsi_qla_host_t *vha;
321 struct scsi_cmnd *cmd;
325 cmd_entry_t *cmd_pkt;
329 struct device_reg_2xxx __iomem *reg;
330 struct qla_hw_data *ha;
334 /* Setup device pointers. */
336 vha = sp->fcport->vha;
338 reg = &ha->iobase->isp;
340 req = ha->req_q_map[0];
341 rsp = ha->rsp_q_map[0];
342 /* So we know we haven't pci_map'ed anything yet */
345 /* Send marker if required */
346 if (vha->marker_needed != 0) {
347 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
349 return (QLA_FUNCTION_FAILED);
350 vha->marker_needed = 0;
353 /* Acquire ring specific lock */
354 spin_lock_irqsave(&ha->hardware_lock, flags);
356 /* Check for room in outstanding command list. */
357 handle = req->current_outstanding_cmd;
358 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
360 if (handle == MAX_OUTSTANDING_COMMANDS)
362 if (!req->outstanding_cmds[handle])
365 if (index == MAX_OUTSTANDING_COMMANDS)
368 /* Map the sg table so we have an accurate count of sg entries needed */
369 if (scsi_sg_count(cmd)) {
370 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
371 scsi_sg_count(cmd), cmd->sc_data_direction);
379 /* Calculate the number of request entries needed. */
380 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
381 if (req->cnt < (req_cnt + 2)) {
382 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
383 if (req->ring_index < cnt)
384 req->cnt = cnt - req->ring_index;
386 req->cnt = req->length -
387 (req->ring_index - cnt);
389 if (req->cnt < (req_cnt + 2))
392 /* Build command packet */
393 req->current_outstanding_cmd = handle;
394 req->outstanding_cmds[handle] = sp;
396 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
399 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
400 cmd_pkt->handle = handle;
401 /* Zero out remaining portion of packet. */
402 clr_ptr = (uint32_t *)cmd_pkt + 2;
403 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
404 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
406 /* Set target ID and LUN number*/
407 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
408 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
410 /* Update tagged queuing modifier */
411 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
413 /* Load SCSI command packet. */
414 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
415 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
417 /* Build IOCB segments */
418 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
420 /* Set total data segment count. */
421 cmd_pkt->entry_count = (uint8_t)req_cnt;
424 /* Adjust ring index. */
426 if (req->ring_index == req->length) {
428 req->ring_ptr = req->ring;
432 sp->flags |= SRB_DMA_VALID;
434 /* Set chip new ring index. */
435 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
436 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
438 /* Manage unprocessed RIO/ZIO commands in response queue. */
439 if (vha->flags.process_response_queue &&
440 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
441 qla2x00_process_response_queue(rsp);
443 spin_unlock_irqrestore(&ha->hardware_lock, flags);
444 return (QLA_SUCCESS);
450 spin_unlock_irqrestore(&ha->hardware_lock, flags);
452 return (QLA_FUNCTION_FAILED);
456 * qla2x00_marker() - Send a marker IOCB to the firmware.
460 * @type: marker modifier
462 * Can be called from both normal and interrupt context.
464 * Returns non-zero if a failure occurred, else zero.
467 __qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
468 struct rsp_que *rsp, uint16_t loop_id,
469 uint16_t lun, uint8_t type)
472 struct mrk_entry_24xx *mrk24;
473 struct qla_hw_data *ha = vha->hw;
474 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
477 mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
479 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
480 __func__, base_vha->host_no));
482 return (QLA_FUNCTION_FAILED);
485 mrk->entry_type = MARKER_TYPE;
486 mrk->modifier = type;
487 if (type != MK_SYNC_ALL) {
488 if (IS_FWI2_CAPABLE(ha)) {
489 mrk24 = (struct mrk_entry_24xx *) mrk;
490 mrk24->nport_handle = cpu_to_le16(loop_id);
491 mrk24->lun[1] = LSB(lun);
492 mrk24->lun[2] = MSB(lun);
493 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
494 mrk24->vp_index = vha->vp_idx;
495 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
497 SET_TARGET_ID(ha, mrk->target, loop_id);
498 mrk->lun = cpu_to_le16(lun);
503 qla2x00_isp_cmd(vha, req);
505 return (QLA_SUCCESS);
509 qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
510 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
514 unsigned long flags = 0;
516 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
517 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
518 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
524 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
527 * Note: The caller must hold the hardware lock before calling this routine.
529 * Returns NULL if function failed, else, a pointer to the request packet.
532 qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
535 struct qla_hw_data *ha = vha->hw;
536 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
537 request_t *pkt = NULL;
541 uint16_t req_cnt = 1;
543 /* Wait 1 second for slot. */
544 for (timer = HZ; timer; timer--) {
545 if ((req_cnt + 2) >= req->cnt) {
546 /* Calculate number of free request entries. */
549 RD_REG_DWORD(®->isp25mq.req_q_out);
552 cnt = (uint16_t)RD_REG_DWORD(
553 ®->isp82.req_q_out);
554 else if (IS_FWI2_CAPABLE(ha))
555 cnt = (uint16_t)RD_REG_DWORD(
556 ®->isp24.req_q_out);
558 cnt = qla2x00_debounce_register(
559 ISP_REQ_Q_OUT(ha, ®->isp));
561 if (req->ring_index < cnt)
562 req->cnt = cnt - req->ring_index;
564 req->cnt = req->length -
565 (req->ring_index - cnt);
567 /* If room for request in request ring. */
568 if ((req_cnt + 2) < req->cnt) {
572 /* Zero out packet. */
573 dword_ptr = (uint32_t *)pkt;
574 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
577 /* Set entry count. */
578 pkt->entry_count = 1;
583 /* Release ring specific lock */
584 spin_unlock_irq(&ha->hardware_lock);
586 udelay(2); /* 2 us */
588 /* Check for pending interrupts. */
589 /* During init we issue marker directly */
590 if (!vha->marker_needed && !vha->flags.init_done)
592 spin_lock_irq(&ha->hardware_lock);
595 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
602 * qla2x00_isp_cmd() - Modify the request ring pointer.
605 * Note: The caller must hold the hardware lock before calling this routine.
608 qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
610 struct qla_hw_data *ha = vha->hw;
611 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
612 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
614 DEBUG5(printk("%s(): IOCB data:\n", __func__));
615 DEBUG5(qla2x00_dump_buffer(
616 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
618 /* Adjust ring index. */
620 if (req->ring_index == req->length) {
622 req->ring_ptr = req->ring;
626 /* Set chip new ring index. */
627 if (IS_QLA82XX(ha)) {
628 uint32_t dbval = 0x04 | (ha->portnum << 5);
630 /* write, read and verify logic */
631 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
633 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
636 (unsigned long __iomem *)ha->nxdb_wr_ptr,
639 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
640 WRT_REG_DWORD((unsigned long __iomem *)
641 ha->nxdb_wr_ptr, dbval);
645 } else if (ha->mqenable) {
646 /* Set chip new ring index. */
647 WRT_REG_DWORD(®->isp25mq.req_q_in, req->ring_index);
648 RD_REG_DWORD(&ioreg->hccr);
650 if (IS_FWI2_CAPABLE(ha)) {
651 WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index);
652 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
654 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp),
656 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
663 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
664 * Continuation Type 1 IOCBs to allocate.
666 * @dsds: number of data segment decriptors needed
668 * Returns the number of IOCB entries needed to store @dsds.
671 qla24xx_calc_iocbs(uint16_t dsds)
677 iocbs += (dsds - 1) / 5;
681 DEBUG3(printk(KERN_DEBUG "%s(): Required PKT(s) = %d\n",
687 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
690 * @sp: SRB command to process
691 * @cmd_pkt: Command type 3 IOCB
692 * @tot_dsds: Total number of segments to transfer
695 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
700 scsi_qla_host_t *vha;
701 struct scsi_cmnd *cmd;
702 struct scatterlist *sg;
708 /* Update entry type to indicate Command Type 3 IOCB */
709 *((uint32_t *)(&cmd_pkt->entry_type)) =
710 __constant_cpu_to_le32(COMMAND_TYPE_7);
712 /* No data transfer */
713 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
714 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
718 vha = sp->fcport->vha;
721 /* Set transfer direction */
722 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
723 cmd_pkt->task_mgmt_flags =
724 __constant_cpu_to_le16(TMF_WRITE_DATA);
725 sp->fcport->vha->hw->qla_stats.output_bytes +=
726 scsi_bufflen(sp->cmd);
727 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
728 cmd_pkt->task_mgmt_flags =
729 __constant_cpu_to_le16(TMF_READ_DATA);
730 sp->fcport->vha->hw->qla_stats.input_bytes +=
731 scsi_bufflen(sp->cmd);
734 /* One DSD is available in the Command Type 3 IOCB */
736 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
738 /* Load data segments */
740 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
742 cont_a64_entry_t *cont_pkt;
744 /* Allocate additional continuation packets? */
745 if (avail_dsds == 0) {
747 * Five DSDs are available in the Continuation
750 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
751 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
755 sle_dma = sg_dma_address(sg);
756 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
757 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
758 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
763 struct fw_dif_context {
766 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
767 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
771 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
775 qla24xx_set_t10dif_tags(struct scsi_cmnd *cmd, struct fw_dif_context *pkt,
776 unsigned int protcnt)
778 struct sd_dif_tuple *spt;
779 unsigned char op = scsi_get_prot_op(cmd);
781 switch (scsi_get_prot_type(cmd)) {
782 /* For TYPE 0 protection: no checking */
783 case SCSI_PROT_DIF_TYPE0:
784 pkt->ref_tag_mask[0] = 0x00;
785 pkt->ref_tag_mask[1] = 0x00;
786 pkt->ref_tag_mask[2] = 0x00;
787 pkt->ref_tag_mask[3] = 0x00;
791 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
792 * match LBA in CDB + N
794 case SCSI_PROT_DIF_TYPE2:
797 /* For Type 3 protection: 16 bit GUARD only */
798 case SCSI_PROT_DIF_TYPE3:
799 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
800 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
805 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
808 case SCSI_PROT_DIF_TYPE1:
809 if (!ql2xenablehba_err_chk)
812 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
813 op == SCSI_PROT_WRITE_PASS)) {
814 spt = page_address(sg_page(scsi_prot_sglist(cmd))) +
815 scsi_prot_sglist(cmd)[0].offset;
816 DEBUG18(printk(KERN_DEBUG
817 "%s(): LBA from user %p, lba = 0x%x\n",
818 __func__, spt, (int)spt->ref_tag));
819 pkt->ref_tag = swab32(spt->ref_tag);
820 pkt->app_tag_mask[0] = 0x0;
821 pkt->app_tag_mask[1] = 0x0;
823 pkt->ref_tag = cpu_to_le32((uint32_t)
824 (0xffffffff & scsi_get_lba(cmd)));
825 pkt->app_tag = __constant_cpu_to_le16(0);
826 pkt->app_tag_mask[0] = 0x0;
827 pkt->app_tag_mask[1] = 0x0;
829 /* enable ALL bytes of the ref tag */
830 pkt->ref_tag_mask[0] = 0xff;
831 pkt->ref_tag_mask[1] = 0xff;
832 pkt->ref_tag_mask[2] = 0xff;
833 pkt->ref_tag_mask[3] = 0xff;
837 DEBUG18(printk(KERN_DEBUG
838 "%s(): Setting protection Tags: (BIG) ref tag = 0x%x,"
839 " app tag = 0x%x, prot SG count %d , cmd lba 0x%x,"
840 " prot_type=%u\n", __func__, pkt->ref_tag, pkt->app_tag, protcnt,
841 (int)scsi_get_lba(cmd), scsi_get_prot_type(cmd)));
846 qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
850 uint8_t avail_dsds = 0;
851 uint32_t dsd_list_len;
852 struct dsd_dma *dsd_ptr;
853 struct scatterlist *sg;
854 uint32_t *cur_dsd = dsd;
856 uint16_t used_dsds = tot_dsds;
860 scsi_for_each_sg(sp->cmd, sg, tot_dsds, i) {
863 /* Allocate additional continuation packets? */
864 if (avail_dsds == 0) {
865 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
866 QLA_DSDS_PER_IOCB : used_dsds;
867 dsd_list_len = (avail_dsds + 1) * 12;
868 used_dsds -= avail_dsds;
870 /* allocate tracking DS */
871 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
875 /* allocate new list */
876 dsd_ptr->dsd_addr = next_dsd =
877 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
878 &dsd_ptr->dsd_list_dma);
882 * Need to cleanup only this dsd_ptr, rest
883 * will be done by sp_free_dma()
889 list_add_tail(&dsd_ptr->list,
890 &((struct crc_context *)sp->ctx)->dsd_list);
892 sp->flags |= SRB_CRC_CTX_DSD_VALID;
894 /* add new list to cmd iocb or last list */
895 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
896 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
897 *cur_dsd++ = dsd_list_len;
898 cur_dsd = (uint32_t *)next_dsd;
900 sle_dma = sg_dma_address(sg);
901 DEBUG18(printk("%s(): %p, sg entry %d - addr =0x%x 0x%x,"
902 " len =%d\n", __func__ , cur_dsd, i, LSD(sle_dma),
903 MSD(sle_dma), sg_dma_len(sg)));
904 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
905 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
906 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
909 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
910 cp = page_address(sg_page(sg)) + sg->offset;
911 DEBUG18(printk("%s(): User Data buffer= %p:\n",
915 /* Null termination */
923 qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
928 uint8_t avail_dsds = 0;
929 uint32_t dsd_list_len;
930 struct dsd_dma *dsd_ptr;
931 struct scatterlist *sg;
933 struct scsi_cmnd *cmd;
934 uint32_t *cur_dsd = dsd;
935 uint16_t used_dsds = tot_dsds;
941 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
944 /* Allocate additional continuation packets? */
945 if (avail_dsds == 0) {
946 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
947 QLA_DSDS_PER_IOCB : used_dsds;
948 dsd_list_len = (avail_dsds + 1) * 12;
949 used_dsds -= avail_dsds;
951 /* allocate tracking DS */
952 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
956 /* allocate new list */
957 dsd_ptr->dsd_addr = next_dsd =
958 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
959 &dsd_ptr->dsd_list_dma);
963 * Need to cleanup only this dsd_ptr, rest
964 * will be done by sp_free_dma()
970 list_add_tail(&dsd_ptr->list,
971 &((struct crc_context *)sp->ctx)->dsd_list);
973 sp->flags |= SRB_CRC_CTX_DSD_VALID;
975 /* add new list to cmd iocb or last list */
976 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
977 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
978 *cur_dsd++ = dsd_list_len;
979 cur_dsd = (uint32_t *)next_dsd;
981 sle_dma = sg_dma_address(sg);
982 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
983 DEBUG18(printk(KERN_DEBUG
984 "%s(): %p, sg entry %d - addr =0x%x"
985 "0x%x, len =%d\n", __func__ , cur_dsd, i,
986 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg)));
988 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
989 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
990 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
992 if (scsi_get_prot_op(sp->cmd) == SCSI_PROT_WRITE_PASS) {
993 cp = page_address(sg_page(sg)) + sg->offset;
994 DEBUG18(printk("%s(): Protection Data buffer = %p:\n",
999 /* Null termination */
1007 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1008 * Type 6 IOCB types.
1010 * @sp: SRB command to process
1011 * @cmd_pkt: Command type 3 IOCB
1012 * @tot_dsds: Total number of segments to transfer
1015 qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1016 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1018 uint32_t *cur_dsd, *fcp_dl;
1019 scsi_qla_host_t *vha;
1020 struct scsi_cmnd *cmd;
1021 struct scatterlist *cur_seg;
1023 uint32_t total_bytes;
1024 uint32_t data_bytes;
1026 uint8_t bundling = 1;
1029 struct crc_context *crc_ctx_pkt = NULL;
1030 struct qla_hw_data *ha;
1031 uint8_t additional_fcpcdb_len;
1032 uint16_t fcp_cmnd_len;
1033 struct fcp_cmnd *fcp_cmnd;
1034 dma_addr_t crc_ctx_dma;
1039 /* Update entry type to indicate Command Type CRC_2 IOCB */
1040 *((uint32_t *)(&cmd_pkt->entry_type)) =
1041 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1043 /* No data transfer */
1044 data_bytes = scsi_bufflen(cmd);
1045 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1046 DEBUG18(printk(KERN_INFO "%s: Zero data bytes or DMA-NONE %d\n",
1047 __func__, data_bytes));
1048 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1052 vha = sp->fcport->vha;
1055 DEBUG18(printk(KERN_DEBUG
1056 "%s(%ld): Executing cmd sp %p, pid=%ld, prot_op=%u.\n", __func__,
1057 vha->host_no, sp, cmd->serial_number, scsi_get_prot_op(sp->cmd)));
1059 cmd_pkt->vp_index = sp->fcport->vp_idx;
1061 /* Set transfer direction */
1062 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1063 cmd_pkt->control_flags =
1064 __constant_cpu_to_le16(CF_WRITE_DATA);
1065 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1066 cmd_pkt->control_flags =
1067 __constant_cpu_to_le16(CF_READ_DATA);
1070 tot_prot_dsds = scsi_prot_sg_count(cmd);
1074 /* Allocate CRC context from global pool */
1075 crc_ctx_pkt = sp->ctx = dma_pool_alloc(ha->dl_dma_pool,
1076 GFP_ATOMIC, &crc_ctx_dma);
1079 goto crc_queuing_error;
1081 /* Zero out CTX area. */
1082 clr_ptr = (uint8_t *)crc_ctx_pkt;
1083 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1085 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1087 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1090 crc_ctx_pkt->handle = cmd_pkt->handle;
1092 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1094 qla24xx_set_t10dif_tags(cmd, (struct fw_dif_context *)
1095 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1097 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1098 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1099 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1101 /* Determine SCSI command length -- align to 4 byte boundary */
1102 if (cmd->cmd_len > 16) {
1103 DEBUG18(printk(KERN_INFO "%s(): **** SCSI CMD > 16\n",
1105 additional_fcpcdb_len = cmd->cmd_len - 16;
1106 if ((cmd->cmd_len % 4) != 0) {
1107 /* SCSI cmd > 16 bytes must be multiple of 4 */
1108 goto crc_queuing_error;
1110 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1112 additional_fcpcdb_len = 0;
1113 fcp_cmnd_len = 12 + 16 + 4;
1116 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1118 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1119 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1120 fcp_cmnd->additional_cdb_len |= 1;
1121 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1122 fcp_cmnd->additional_cdb_len |= 2;
1124 int_to_scsilun(sp->cmd->device->lun, &fcp_cmnd->lun);
1125 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1126 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1127 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1128 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1129 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1130 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1131 fcp_cmnd->task_attribute = 0;
1132 fcp_cmnd->task_managment = 0;
1134 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1136 DEBUG18(printk(KERN_INFO "%s(%ld): Total SG(s) Entries %d, Data"
1137 "entries %d, data bytes %d, Protection entries %d\n",
1138 __func__, vha->host_no, tot_dsds, (tot_dsds-tot_prot_dsds),
1139 data_bytes, tot_prot_dsds));
1141 /* Compute dif len and adjust data len to incude protection */
1142 total_bytes = data_bytes;
1144 blk_size = cmd->device->sector_size;
1145 if (scsi_get_prot_type(cmd) == SCSI_PROT_DIF_TYPE1) {
1146 dif_bytes = (data_bytes / blk_size) * 8;
1147 total_bytes += dif_bytes;
1150 if (!ql2xenablehba_err_chk)
1151 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1154 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1157 * Configure Bundling if we need to fetch interlaving
1158 * protection PCI accesses
1160 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1161 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1162 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1164 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1167 /* Finish the common fields of CRC pkt */
1168 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1169 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1170 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1171 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1172 /* Fibre channel byte count */
1173 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1174 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1175 additional_fcpcdb_len);
1176 *fcp_dl = htonl(total_bytes);
1178 DEBUG18(printk(KERN_INFO "%s(%ld): dif bytes = 0x%x (%d), total bytes"
1179 " = 0x%x (%d), dat block size =0x%x (%d)\n", __func__,
1180 vha->host_no, dif_bytes, dif_bytes, total_bytes, total_bytes,
1181 crc_ctx_pkt->blk_size, crc_ctx_pkt->blk_size));
1183 /* Walks data segments */
1185 cmd_pkt->control_flags |=
1186 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
1187 if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
1188 (tot_dsds - tot_prot_dsds)))
1189 goto crc_queuing_error;
1191 if (bundling && tot_prot_dsds) {
1192 /* Walks dif segments */
1193 cur_seg = scsi_prot_sglist(cmd);
1194 cmd_pkt->control_flags |=
1195 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1196 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1197 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1199 goto crc_queuing_error;
1204 DEBUG18(qla_printk(KERN_INFO, ha,
1205 "CMD sent FAILED crc_q error:sp = %p\n", sp));
1206 /* Cleanup will be performed by the caller */
1208 return QLA_FUNCTION_FAILED;
1212 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1213 * @sp: command to send to the ISP
1215 * Returns non-zero if a failure occurred, else zero.
1218 qla24xx_start_scsi(srb_t *sp)
1221 unsigned long flags;
1225 struct cmd_type_7 *cmd_pkt;
1229 struct req_que *req = NULL;
1230 struct rsp_que *rsp = NULL;
1231 struct scsi_cmnd *cmd = sp->cmd;
1232 struct scsi_qla_host *vha = sp->fcport->vha;
1233 struct qla_hw_data *ha = vha->hw;
1235 /* Setup device pointers. */
1238 qla25xx_set_que(sp, &rsp);
1241 /* So we know we haven't pci_map'ed anything yet */
1244 /* Send marker if required */
1245 if (vha->marker_needed != 0) {
1246 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
1248 return QLA_FUNCTION_FAILED;
1249 vha->marker_needed = 0;
1252 /* Acquire ring specific lock */
1253 spin_lock_irqsave(&ha->hardware_lock, flags);
1255 /* Check for room in outstanding command list. */
1256 handle = req->current_outstanding_cmd;
1257 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1259 if (handle == MAX_OUTSTANDING_COMMANDS)
1261 if (!req->outstanding_cmds[handle])
1264 if (index == MAX_OUTSTANDING_COMMANDS)
1267 /* Map the sg table so we have an accurate count of sg entries needed */
1268 if (scsi_sg_count(cmd)) {
1269 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1270 scsi_sg_count(cmd), cmd->sc_data_direction);
1271 if (unlikely(!nseg))
1278 req_cnt = qla24xx_calc_iocbs(tot_dsds);
1279 if (req->cnt < (req_cnt + 2)) {
1280 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1282 if (req->ring_index < cnt)
1283 req->cnt = cnt - req->ring_index;
1285 req->cnt = req->length -
1286 (req->ring_index - cnt);
1288 if (req->cnt < (req_cnt + 2))
1291 /* Build command packet. */
1292 req->current_outstanding_cmd = handle;
1293 req->outstanding_cmds[handle] = sp;
1294 sp->handle = handle;
1295 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1296 req->cnt -= req_cnt;
1298 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
1299 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1301 /* Zero out remaining portion of packet. */
1302 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
1303 clr_ptr = (uint32_t *)cmd_pkt + 2;
1304 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1305 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1307 /* Set NPORT-ID and LUN number*/
1308 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1309 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1310 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1311 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1312 cmd_pkt->vp_index = sp->fcport->vp_idx;
1314 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
1315 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1317 /* Load SCSI command packet. */
1318 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1319 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1321 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1323 /* Build IOCB segments */
1324 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1326 /* Set total data segment count. */
1327 cmd_pkt->entry_count = (uint8_t)req_cnt;
1328 /* Specify response queue number where completion should happen */
1329 cmd_pkt->entry_status = (uint8_t) rsp->id;
1332 /* Adjust ring index. */
1334 if (req->ring_index == req->length) {
1335 req->ring_index = 0;
1336 req->ring_ptr = req->ring;
1340 sp->flags |= SRB_DMA_VALID;
1342 /* Set chip new ring index. */
1343 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1344 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1346 /* Manage unprocessed RIO/ZIO commands in response queue. */
1347 if (vha->flags.process_response_queue &&
1348 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1349 qla24xx_process_response_queue(vha, rsp);
1351 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1356 scsi_dma_unmap(cmd);
1358 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1360 return QLA_FUNCTION_FAILED;
1365 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1366 * @sp: command to send to the ISP
1368 * Returns non-zero if a failure occurred, else zero.
1371 qla24xx_dif_start_scsi(srb_t *sp)
1374 unsigned long flags;
1379 uint16_t req_cnt = 0;
1381 uint16_t tot_prot_dsds;
1382 uint16_t fw_prot_opts = 0;
1383 struct req_que *req = NULL;
1384 struct rsp_que *rsp = NULL;
1385 struct scsi_cmnd *cmd = sp->cmd;
1386 struct scsi_qla_host *vha = sp->fcport->vha;
1387 struct qla_hw_data *ha = vha->hw;
1388 struct cmd_type_crc_2 *cmd_pkt;
1389 uint32_t status = 0;
1391 #define QDSS_GOT_Q_SPACE BIT_0
1393 /* Only process protection in this routine */
1394 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL)
1395 return qla24xx_start_scsi(sp);
1397 /* Setup device pointers. */
1399 qla25xx_set_que(sp, &rsp);
1402 /* So we know we haven't pci_map'ed anything yet */
1405 /* Send marker if required */
1406 if (vha->marker_needed != 0) {
1407 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1409 return QLA_FUNCTION_FAILED;
1410 vha->marker_needed = 0;
1413 /* Acquire ring specific lock */
1414 spin_lock_irqsave(&ha->hardware_lock, flags);
1416 /* Check for room in outstanding command list. */
1417 handle = req->current_outstanding_cmd;
1418 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1420 if (handle == MAX_OUTSTANDING_COMMANDS)
1422 if (!req->outstanding_cmds[handle])
1426 if (index == MAX_OUTSTANDING_COMMANDS)
1429 /* Compute number of required data segments */
1430 /* Map the sg table so we have an accurate count of sg entries needed */
1431 if (scsi_sg_count(cmd)) {
1432 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1433 scsi_sg_count(cmd), cmd->sc_data_direction);
1434 if (unlikely(!nseg))
1437 sp->flags |= SRB_DMA_VALID;
1441 /* number of required data segments */
1444 /* Compute number of required protection segments */
1445 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1446 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1447 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1448 if (unlikely(!nseg))
1451 sp->flags |= SRB_CRC_PROT_DMA_VALID;
1457 /* Total Data and protection sg segment(s) */
1458 tot_prot_dsds = nseg;
1460 if (req->cnt < (req_cnt + 2)) {
1461 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1463 if (req->ring_index < cnt)
1464 req->cnt = cnt - req->ring_index;
1466 req->cnt = req->length -
1467 (req->ring_index - cnt);
1470 if (req->cnt < (req_cnt + 2))
1473 status |= QDSS_GOT_Q_SPACE;
1475 /* Build header part of command packet (excluding the OPCODE). */
1476 req->current_outstanding_cmd = handle;
1477 req->outstanding_cmds[handle] = sp;
1478 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
1479 req->cnt -= req_cnt;
1481 /* Fill-in common area */
1482 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1483 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1485 clr_ptr = (uint32_t *)cmd_pkt + 2;
1486 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1488 /* Set NPORT-ID and LUN number*/
1489 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1490 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1491 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1492 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1494 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
1495 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1497 /* Total Data and protection segment(s) */
1498 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1500 /* Build IOCB segments and adjust for data protection segments */
1501 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1502 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1506 cmd_pkt->entry_count = (uint8_t)req_cnt;
1507 /* Specify response queue number where completion should happen */
1508 cmd_pkt->entry_status = (uint8_t) rsp->id;
1509 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1512 /* Adjust ring index. */
1514 if (req->ring_index == req->length) {
1515 req->ring_index = 0;
1516 req->ring_ptr = req->ring;
1520 /* Set chip new ring index. */
1521 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1522 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1524 /* Manage unprocessed RIO/ZIO commands in response queue. */
1525 if (vha->flags.process_response_queue &&
1526 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1527 qla24xx_process_response_queue(vha, rsp);
1529 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1534 if (status & QDSS_GOT_Q_SPACE) {
1535 req->outstanding_cmds[handle] = NULL;
1536 req->cnt += req_cnt;
1538 /* Cleanup will be performed by the caller (queuecommand) */
1540 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1542 DEBUG18(qla_printk(KERN_INFO, ha,
1543 "CMD sent FAILED SCSI prot_op:%02x\n", scsi_get_prot_op(cmd)));
1544 return QLA_FUNCTION_FAILED;
1548 static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
1550 struct scsi_cmnd *cmd = sp->cmd;
1551 struct qla_hw_data *ha = sp->fcport->vha->hw;
1552 int affinity = cmd->request->cpu;
1554 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
1555 affinity < ha->max_rsp_queues - 1)
1556 *rsp = ha->rsp_q_map[affinity + 1];
1558 *rsp = ha->rsp_q_map[0];
1561 /* Generic Control-SRB manipulation functions. */
1564 qla2x00_alloc_iocbs(srb_t *sp)
1566 scsi_qla_host_t *vha = sp->fcport->vha;
1567 struct qla_hw_data *ha = vha->hw;
1568 struct req_que *req = ha->req_q_map[0];
1569 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1570 uint32_t index, handle;
1572 uint16_t cnt, req_cnt;
1577 /* Check for room in outstanding command list. */
1578 handle = req->current_outstanding_cmd;
1579 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1581 if (handle == MAX_OUTSTANDING_COMMANDS)
1583 if (!req->outstanding_cmds[handle])
1586 if (index == MAX_OUTSTANDING_COMMANDS)
1589 /* Check for room on request queue. */
1590 if (req->cnt < req_cnt) {
1592 cnt = RD_REG_DWORD(®->isp25mq.req_q_out);
1593 else if (IS_FWI2_CAPABLE(ha))
1594 cnt = RD_REG_DWORD(®->isp24.req_q_out);
1596 cnt = qla2x00_debounce_register(
1597 ISP_REQ_Q_OUT(ha, ®->isp));
1599 if (req->ring_index < cnt)
1600 req->cnt = cnt - req->ring_index;
1602 req->cnt = req->length -
1603 (req->ring_index - cnt);
1605 if (req->cnt < req_cnt)
1609 req->current_outstanding_cmd = handle;
1610 req->outstanding_cmds[handle] = sp;
1611 req->cnt -= req_cnt;
1613 pkt = req->ring_ptr;
1614 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1615 pkt->entry_count = req_cnt;
1616 pkt->handle = handle;
1617 sp->handle = handle;
1624 qla2x00_start_iocbs(srb_t *sp)
1626 struct qla_hw_data *ha = sp->fcport->vha->hw;
1627 struct req_que *req = ha->req_q_map[0];
1628 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1629 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
1631 if (IS_QLA82XX(ha)) {
1632 qla82xx_start_iocbs(sp);
1634 /* Adjust ring index. */
1636 if (req->ring_index == req->length) {
1637 req->ring_index = 0;
1638 req->ring_ptr = req->ring;
1642 /* Set chip new ring index. */
1644 WRT_REG_DWORD(®->isp25mq.req_q_in, req->ring_index);
1645 RD_REG_DWORD(&ioreg->hccr);
1646 } else if (IS_QLA82XX(ha)) {
1647 qla82xx_start_iocbs(sp);
1648 } else if (IS_FWI2_CAPABLE(ha)) {
1649 WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index);
1650 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
1652 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp),
1654 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
1660 qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1662 struct srb_ctx *ctx = sp->ctx;
1663 struct srb_iocb *lio = ctx->u.iocb_cmd;
1665 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1666 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
1667 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
1668 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
1669 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
1670 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1671 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1672 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1673 logio->port_id[1] = sp->fcport->d_id.b.area;
1674 logio->port_id[2] = sp->fcport->d_id.b.domain;
1675 logio->vp_index = sp->fcport->vp_idx;
1679 qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1681 struct qla_hw_data *ha = sp->fcport->vha->hw;
1682 struct srb_ctx *ctx = sp->ctx;
1683 struct srb_iocb *lio = ctx->u.iocb_cmd;
1686 mbx->entry_type = MBX_IOCB_TYPE;;
1687 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1688 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
1689 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1690 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
1691 if (HAS_EXTENDED_IDS(ha)) {
1692 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1693 mbx->mb10 = cpu_to_le16(opts);
1695 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1697 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1698 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1699 sp->fcport->d_id.b.al_pa);
1700 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1704 qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1706 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1707 logio->control_flags =
1708 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1709 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1710 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1711 logio->port_id[1] = sp->fcport->d_id.b.area;
1712 logio->port_id[2] = sp->fcport->d_id.b.domain;
1713 logio->vp_index = sp->fcport->vp_idx;
1717 qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1719 struct qla_hw_data *ha = sp->fcport->vha->hw;
1721 mbx->entry_type = MBX_IOCB_TYPE;;
1722 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1723 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1724 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1725 cpu_to_le16(sp->fcport->loop_id):
1726 cpu_to_le16(sp->fcport->loop_id << 8);
1727 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1728 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1729 sp->fcport->d_id.b.al_pa);
1730 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1731 /* Implicit: mbx->mbx10 = 0. */
1735 qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1737 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1738 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1739 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1740 logio->vp_index = sp->fcport->vp_idx;
1744 qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1746 struct qla_hw_data *ha = sp->fcport->vha->hw;
1748 mbx->entry_type = MBX_IOCB_TYPE;
1749 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1750 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1751 if (HAS_EXTENDED_IDS(ha)) {
1752 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1753 mbx->mb10 = cpu_to_le16(BIT_0);
1755 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1757 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1758 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1759 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1760 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1761 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1765 qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1769 struct fc_port *fcport = sp->fcport;
1770 scsi_qla_host_t *vha = fcport->vha;
1771 struct qla_hw_data *ha = vha->hw;
1772 struct srb_ctx *ctx = sp->ctx;
1773 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1774 struct req_que *req = vha->req;
1776 flags = iocb->u.tmf.flags;
1777 lun = iocb->u.tmf.lun;
1779 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1780 tsk->entry_count = 1;
1781 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1782 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
1783 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
1784 tsk->control_flags = cpu_to_le32(flags);
1785 tsk->port_id[0] = fcport->d_id.b.al_pa;
1786 tsk->port_id[1] = fcport->d_id.b.area;
1787 tsk->port_id[2] = fcport->d_id.b.domain;
1788 tsk->vp_index = fcport->vp_idx;
1790 if (flags == TCF_LUN_RESET) {
1791 int_to_scsilun(lun, &tsk->lun);
1792 host_to_fcp_swap((uint8_t *)&tsk->lun,
1798 qla24xx_marker_iocb(srb_t *sp, struct mrk_entry_24xx *mrk)
1802 struct fc_port *fcport = sp->fcport;
1803 scsi_qla_host_t *vha = fcport->vha;
1804 struct srb_ctx *ctx = sp->ctx;
1805 struct srb_iocb *iocb = ctx->u.iocb_cmd;
1806 struct req_que *req = vha->req;
1808 lun = iocb->u.marker.lun;
1809 modif = iocb->u.marker.modif;
1810 mrk->entry_type = MARKER_TYPE;
1811 mrk->modifier = modif;
1812 if (modif != MK_SYNC_ALL) {
1813 mrk->nport_handle = cpu_to_le16(fcport->loop_id);
1814 mrk->lun[1] = LSB(lun);
1815 mrk->lun[2] = MSB(lun);
1816 host_to_fcp_swap(mrk->lun, sizeof(mrk->lun));
1817 mrk->vp_index = vha->vp_idx;
1818 mrk->handle = MAKE_HANDLE(req->id, mrk->handle);
1823 qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
1825 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
1827 els_iocb->entry_type = ELS_IOCB_TYPE;
1828 els_iocb->entry_count = 1;
1829 els_iocb->sys_define = 0;
1830 els_iocb->entry_status = 0;
1831 els_iocb->handle = sp->handle;
1832 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1833 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1834 els_iocb->vp_index = sp->fcport->vp_idx;
1835 els_iocb->sof_type = EST_SOFI3;
1836 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1839 (((struct srb_ctx *)sp->ctx)->type == SRB_ELS_CMD_RPT) ?
1840 bsg_job->request->rqst_data.r_els.els_code :
1841 bsg_job->request->rqst_data.h_els.command_code;
1842 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
1843 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
1844 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
1845 els_iocb->control_flags = 0;
1846 els_iocb->rx_byte_count =
1847 cpu_to_le32(bsg_job->reply_payload.payload_len);
1848 els_iocb->tx_byte_count =
1849 cpu_to_le32(bsg_job->request_payload.payload_len);
1851 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
1852 (bsg_job->request_payload.sg_list)));
1853 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
1854 (bsg_job->request_payload.sg_list)));
1855 els_iocb->tx_len = cpu_to_le32(sg_dma_len
1856 (bsg_job->request_payload.sg_list));
1858 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
1859 (bsg_job->reply_payload.sg_list)));
1860 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
1861 (bsg_job->reply_payload.sg_list)));
1862 els_iocb->rx_len = cpu_to_le32(sg_dma_len
1863 (bsg_job->reply_payload.sg_list));
1867 qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
1869 uint16_t avail_dsds;
1871 struct scatterlist *sg;
1874 scsi_qla_host_t *vha = sp->fcport->vha;
1875 struct fc_bsg_job *bsg_job = ((struct srb_ctx *)sp->ctx)->u.bsg_job;
1876 int loop_iterartion = 0;
1877 int cont_iocb_prsnt = 0;
1878 int entry_count = 1;
1880 ct_iocb->entry_type = CT_IOCB_TYPE;
1881 ct_iocb->entry_status = 0;
1882 ct_iocb->sys_define = 0;
1883 ct_iocb->handle = sp->handle;
1885 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1886 ct_iocb->vp_index = sp->fcport->vp_idx;
1887 ct_iocb->comp_status = __constant_cpu_to_le16(0);
1889 ct_iocb->cmd_dsd_count =
1890 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
1891 ct_iocb->timeout = 0;
1892 ct_iocb->rsp_dsd_count =
1893 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
1894 ct_iocb->rsp_byte_count =
1895 cpu_to_le32(bsg_job->reply_payload.payload_len);
1896 ct_iocb->cmd_byte_count =
1897 cpu_to_le32(bsg_job->request_payload.payload_len);
1898 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
1899 (bsg_job->request_payload.sg_list)));
1900 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
1901 (bsg_job->request_payload.sg_list)));
1902 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
1903 (bsg_job->request_payload.sg_list));
1906 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
1908 tot_dsds = bsg_job->reply_payload.sg_cnt;
1910 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
1912 cont_a64_entry_t *cont_pkt;
1914 /* Allocate additional continuation packets? */
1915 if (avail_dsds == 0) {
1917 * Five DSDs are available in the Cont.
1920 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
1921 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
1923 cont_iocb_prsnt = 1;
1927 sle_dma = sg_dma_address(sg);
1928 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1929 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1930 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1934 ct_iocb->entry_count = entry_count;
1938 qla2x00_start_sp(srb_t *sp)
1941 struct qla_hw_data *ha = sp->fcport->vha->hw;
1943 struct srb_ctx *ctx = sp->ctx;
1944 unsigned long flags;
1946 rval = QLA_FUNCTION_FAILED;
1947 spin_lock_irqsave(&ha->hardware_lock, flags);
1948 pkt = qla2x00_alloc_iocbs(sp);
1953 switch (ctx->type) {
1955 IS_FWI2_CAPABLE(ha) ?
1956 qla24xx_login_iocb(sp, pkt) :
1957 qla2x00_login_iocb(sp, pkt);
1959 case SRB_LOGOUT_CMD:
1960 IS_FWI2_CAPABLE(ha) ?
1961 qla24xx_logout_iocb(sp, pkt) :
1962 qla2x00_logout_iocb(sp, pkt);
1964 case SRB_ELS_CMD_RPT:
1965 case SRB_ELS_CMD_HST:
1966 qla24xx_els_iocb(sp, pkt);
1969 qla24xx_ct_iocb(sp, pkt);
1972 IS_FWI2_CAPABLE(ha) ?
1973 qla24xx_adisc_iocb(sp, pkt) :
1974 qla2x00_adisc_iocb(sp, pkt);
1977 qla24xx_tm_iocb(sp, pkt);
1979 case SRB_MARKER_CMD:
1980 qla24xx_marker_iocb(sp, pkt);
1987 qla2x00_start_iocbs(sp);
1989 spin_unlock_irqrestore(&ha->hardware_lock, flags);