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(scsi_qla_host_t *);
15 static void qla2x00_isp_cmd(scsi_qla_host_t *);
18 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
21 * Returns the proper CF_* direction based on CDB.
23 static inline uint16_t
24 qla2x00_get_cmd_direction(srb_t *sp)
30 /* Set transfer direction */
31 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
33 sp->fcport->vha->hw->qla_stats.output_bytes +=
34 scsi_bufflen(sp->cmd);
35 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
37 sp->fcport->vha->hw->qla_stats.input_bytes +=
38 scsi_bufflen(sp->cmd);
44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
45 * Continuation Type 0 IOCBs to allocate.
47 * @dsds: number of data segment decriptors needed
49 * Returns the number of IOCB entries needed to store @dsds.
52 qla2x00_calc_iocbs_32(uint16_t dsds)
58 iocbs += (dsds - 3) / 7;
66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
67 * Continuation Type 1 IOCBs to allocate.
69 * @dsds: number of data segment decriptors needed
71 * Returns the number of IOCB entries needed to store @dsds.
74 qla2x00_calc_iocbs_64(uint16_t dsds)
80 iocbs += (dsds - 2) / 5;
88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
91 * Returns a pointer to the Continuation Type 0 IOCB packet.
93 static inline cont_entry_t *
94 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *vha)
96 cont_entry_t *cont_pkt;
97 struct req_que *req = vha->hw->req;
98 /* Adjust ring index. */
100 if (req->ring_index == req->length) {
102 req->ring_ptr = req->ring;
107 cont_pkt = (cont_entry_t *)req->ring_ptr;
109 /* Load packet defaults. */
110 *((uint32_t *)(&cont_pkt->entry_type)) =
111 __constant_cpu_to_le32(CONTINUE_TYPE);
117 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
120 * Returns a pointer to the continuation type 1 IOCB packet.
122 static inline cont_a64_entry_t *
123 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
125 cont_a64_entry_t *cont_pkt;
126 struct req_que *req = vha->hw->req;
128 /* Adjust ring index. */
130 if (req->ring_index == req->length) {
132 req->ring_ptr = req->ring;
137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
139 /* Load packet defaults. */
140 *((uint32_t *)(&cont_pkt->entry_type)) =
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
147 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
148 * capable IOCB types.
150 * @sp: SRB command to process
151 * @cmd_pkt: Command type 2 IOCB
152 * @tot_dsds: Total number of segments to transfer
154 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
159 scsi_qla_host_t *vha;
160 struct scsi_cmnd *cmd;
161 struct scatterlist *sg;
166 /* Update entry type to indicate Command Type 2 IOCB */
167 *((uint32_t *)(&cmd_pkt->entry_type)) =
168 __constant_cpu_to_le32(COMMAND_TYPE);
170 /* No data transfer */
171 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
172 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
178 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
180 /* Three DSDs are available in the Command Type 2 IOCB */
182 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
184 /* Load data segments */
185 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
186 cont_entry_t *cont_pkt;
188 /* Allocate additional continuation packets? */
189 if (avail_dsds == 0) {
191 * Seven DSDs are available in the Continuation
194 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
195 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
199 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
200 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
206 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
207 * capable IOCB types.
209 * @sp: SRB command to process
210 * @cmd_pkt: Command type 3 IOCB
211 * @tot_dsds: Total number of segments to transfer
213 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
218 scsi_qla_host_t *vha;
219 struct scsi_cmnd *cmd;
220 struct scatterlist *sg;
225 /* Update entry type to indicate Command Type 3 IOCB */
226 *((uint32_t *)(&cmd_pkt->entry_type)) =
227 __constant_cpu_to_le32(COMMAND_A64_TYPE);
229 /* No data transfer */
230 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
231 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
237 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
239 /* Two DSDs are available in the Command Type 3 IOCB */
241 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
243 /* Load data segments */
244 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
246 cont_a64_entry_t *cont_pkt;
248 /* Allocate additional continuation packets? */
249 if (avail_dsds == 0) {
251 * Five DSDs are available in the Continuation
254 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
255 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
259 sle_dma = sg_dma_address(sg);
260 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
261 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
262 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
268 * qla2x00_start_scsi() - Send a SCSI command to the ISP
269 * @sp: command to send to the ISP
271 * Returns non-zero if a failure occurred, else zero.
274 qla2x00_start_scsi(srb_t *sp)
278 scsi_qla_host_t *vha;
279 struct scsi_cmnd *cmd;
283 cmd_entry_t *cmd_pkt;
287 struct device_reg_2xxx __iomem *reg;
288 struct qla_hw_data *ha;
291 /* Setup device pointers. */
295 reg = &ha->iobase->isp;
298 /* So we know we haven't pci_map'ed anything yet */
301 /* Send marker if required */
302 if (vha->marker_needed != 0) {
303 if (qla2x00_marker(vha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
304 return (QLA_FUNCTION_FAILED);
305 vha->marker_needed = 0;
308 /* Acquire ring specific lock */
309 spin_lock_irqsave(&ha->hardware_lock, flags);
311 /* Check for room in outstanding command list. */
312 handle = req->current_outstanding_cmd;
313 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
315 if (handle == MAX_OUTSTANDING_COMMANDS)
317 if (!req->outstanding_cmds[handle])
320 if (index == MAX_OUTSTANDING_COMMANDS)
323 /* Map the sg table so we have an accurate count of sg entries needed */
324 if (scsi_sg_count(cmd)) {
325 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
326 scsi_sg_count(cmd), cmd->sc_data_direction);
334 /* Calculate the number of request entries needed. */
335 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
336 if (req->cnt < (req_cnt + 2)) {
337 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
338 if (req->ring_index < cnt)
339 req->cnt = cnt - req->ring_index;
341 req->cnt = req->length -
342 (req->ring_index - cnt);
344 if (req->cnt < (req_cnt + 2))
347 /* Build command packet */
348 req->current_outstanding_cmd = handle;
349 req->outstanding_cmds[handle] = sp;
351 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
354 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
355 cmd_pkt->handle = handle;
356 /* Zero out remaining portion of packet. */
357 clr_ptr = (uint32_t *)cmd_pkt + 2;
358 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
359 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
361 /* Set target ID and LUN number*/
362 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
363 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
365 /* Update tagged queuing modifier */
366 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
368 /* Load SCSI command packet. */
369 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
370 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
372 /* Build IOCB segments */
373 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
375 /* Set total data segment count. */
376 cmd_pkt->entry_count = (uint8_t)req_cnt;
379 /* Adjust ring index. */
381 if (req->ring_index == req->length) {
383 req->ring_ptr = req->ring;
387 sp->flags |= SRB_DMA_VALID;
389 /* Set chip new ring index. */
390 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
391 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
393 /* Manage unprocessed RIO/ZIO commands in response queue. */
394 if (vha->flags.process_response_queue &&
395 ha->rsp->ring_ptr->signature != RESPONSE_PROCESSED)
396 qla2x00_process_response_queue(vha);
398 spin_unlock_irqrestore(&ha->hardware_lock, flags);
399 return (QLA_SUCCESS);
405 spin_unlock_irqrestore(&ha->hardware_lock, flags);
407 return (QLA_FUNCTION_FAILED);
411 * qla2x00_marker() - Send a marker IOCB to the firmware.
415 * @type: marker modifier
417 * Can be called from both normal and interrupt context.
419 * Returns non-zero if a failure occurred, else zero.
422 __qla2x00_marker(scsi_qla_host_t *vha, uint16_t loop_id, uint16_t lun,
426 struct mrk_entry_24xx *mrk24;
427 struct qla_hw_data *ha = vha->hw;
428 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
431 mrk = (mrk_entry_t *)qla2x00_req_pkt(base_vha);
433 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
434 __func__, base_vha->host_no));
436 return (QLA_FUNCTION_FAILED);
439 mrk->entry_type = MARKER_TYPE;
440 mrk->modifier = type;
441 if (type != MK_SYNC_ALL) {
442 if (IS_FWI2_CAPABLE(ha)) {
443 mrk24 = (struct mrk_entry_24xx *) mrk;
444 mrk24->nport_handle = cpu_to_le16(loop_id);
445 mrk24->lun[1] = LSB(lun);
446 mrk24->lun[2] = MSB(lun);
447 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
448 mrk24->vp_index = vha->vp_idx;
450 SET_TARGET_ID(ha, mrk->target, loop_id);
451 mrk->lun = cpu_to_le16(lun);
456 qla2x00_isp_cmd(base_vha);
458 return (QLA_SUCCESS);
462 qla2x00_marker(scsi_qla_host_t *vha, uint16_t loop_id, uint16_t lun,
466 unsigned long flags = 0;
467 struct qla_hw_data *ha = vha->hw;
469 spin_lock_irqsave(&ha->hardware_lock, flags);
470 ret = __qla2x00_marker(vha, loop_id, lun, type);
471 spin_unlock_irqrestore(&ha->hardware_lock, flags);
477 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
480 * Note: The caller must hold the hardware lock before calling this routine.
482 * Returns NULL if function failed, else, a pointer to the request packet.
485 qla2x00_req_pkt(scsi_qla_host_t *vha)
487 struct qla_hw_data *ha = vha->hw;
488 device_reg_t __iomem *reg = ha->iobase;
489 request_t *pkt = NULL;
493 uint16_t req_cnt = 1;
494 struct req_que *req = ha->req;
496 /* Wait 1 second for slot. */
497 for (timer = HZ; timer; timer--) {
498 if ((req_cnt + 2) >= req->cnt) {
499 /* Calculate number of free request entries. */
500 if (IS_FWI2_CAPABLE(ha))
501 cnt = (uint16_t)RD_REG_DWORD(
502 ®->isp24.req_q_out);
504 cnt = qla2x00_debounce_register(
505 ISP_REQ_Q_OUT(ha, ®->isp));
506 if (req->ring_index < cnt)
507 req->cnt = cnt - req->ring_index;
509 req->cnt = req->length -
510 (req->ring_index - cnt);
512 /* If room for request in request ring. */
513 if ((req_cnt + 2) < req->cnt) {
517 /* Zero out packet. */
518 dword_ptr = (uint32_t *)pkt;
519 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
522 /* Set system defined field. */
523 pkt->sys_define = (uint8_t)req->ring_index;
525 /* Set entry count. */
526 pkt->entry_count = 1;
531 /* Release ring specific lock */
532 spin_unlock_irq(&ha->hardware_lock);
534 udelay(2); /* 2 us */
536 /* Check for pending interrupts. */
537 /* During init we issue marker directly */
538 if (!vha->marker_needed && !vha->flags.init_done)
539 qla2x00_poll(ha->rsp);
540 spin_lock_irq(&ha->hardware_lock);
543 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
550 * qla2x00_isp_cmd() - Modify the request ring pointer.
553 * Note: The caller must hold the hardware lock before calling this routine.
556 qla2x00_isp_cmd(scsi_qla_host_t *vha)
558 struct qla_hw_data *ha = vha->hw;
559 device_reg_t __iomem *reg = ha->iobase;
560 struct req_que *req = ha->req;
562 DEBUG5(printk("%s(): IOCB data:\n", __func__));
563 DEBUG5(qla2x00_dump_buffer(
564 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
566 /* Adjust ring index. */
568 if (req->ring_index == req->length) {
570 req->ring_ptr = req->ring;
574 /* Set chip new ring index. */
575 if (IS_FWI2_CAPABLE(ha)) {
576 WRT_REG_DWORD(®->isp24.req_q_in, req->ring_index);
577 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
579 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), req->ring_index);
580 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
586 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
587 * Continuation Type 1 IOCBs to allocate.
589 * @dsds: number of data segment decriptors needed
591 * Returns the number of IOCB entries needed to store @dsds.
593 static inline uint16_t
594 qla24xx_calc_iocbs(uint16_t dsds)
600 iocbs += (dsds - 1) / 5;
608 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
611 * @sp: SRB command to process
612 * @cmd_pkt: Command type 3 IOCB
613 * @tot_dsds: Total number of segments to transfer
616 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
621 scsi_qla_host_t *vha;
622 struct scsi_cmnd *cmd;
623 struct scatterlist *sg;
628 /* Update entry type to indicate Command Type 3 IOCB */
629 *((uint32_t *)(&cmd_pkt->entry_type)) =
630 __constant_cpu_to_le32(COMMAND_TYPE_7);
632 /* No data transfer */
633 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
634 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
640 /* Set transfer direction */
641 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
642 cmd_pkt->task_mgmt_flags =
643 __constant_cpu_to_le16(TMF_WRITE_DATA);
644 sp->fcport->vha->hw->qla_stats.output_bytes +=
645 scsi_bufflen(sp->cmd);
646 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
647 cmd_pkt->task_mgmt_flags =
648 __constant_cpu_to_le16(TMF_READ_DATA);
649 sp->fcport->vha->hw->qla_stats.input_bytes +=
650 scsi_bufflen(sp->cmd);
653 /* One DSD is available in the Command Type 3 IOCB */
655 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
657 /* Load data segments */
659 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
661 cont_a64_entry_t *cont_pkt;
663 /* Allocate additional continuation packets? */
664 if (avail_dsds == 0) {
666 * Five DSDs are available in the Continuation
669 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
670 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
674 sle_dma = sg_dma_address(sg);
675 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
676 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
677 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
684 * qla24xx_start_scsi() - Send a SCSI command to the ISP
685 * @sp: command to send to the ISP
687 * Returns non-zero if a failure occurred, else zero.
690 qla24xx_start_scsi(srb_t *sp)
694 scsi_qla_host_t *vha;
695 struct scsi_cmnd *cmd;
699 struct cmd_type_7 *cmd_pkt;
703 struct device_reg_24xx __iomem *reg;
704 struct qla_hw_data *ha;
707 /* Setup device pointers. */
711 reg = &ha->iobase->isp24;
714 /* So we know we haven't pci_map'ed anything yet */
717 /* Send marker if required */
718 if (vha->marker_needed != 0) {
719 if (qla2x00_marker(vha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
720 return QLA_FUNCTION_FAILED;
721 vha->marker_needed = 0;
724 /* Acquire ring specific lock */
725 spin_lock_irqsave(&ha->hardware_lock, flags);
727 /* Check for room in outstanding command list. */
728 handle = req->current_outstanding_cmd;
729 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
731 if (handle == MAX_OUTSTANDING_COMMANDS)
733 if (!req->outstanding_cmds[handle])
736 if (index == MAX_OUTSTANDING_COMMANDS)
739 /* Map the sg table so we have an accurate count of sg entries needed */
740 if (scsi_sg_count(cmd)) {
741 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
742 scsi_sg_count(cmd), cmd->sc_data_direction);
750 req_cnt = qla24xx_calc_iocbs(tot_dsds);
751 if (req->cnt < (req_cnt + 2)) {
752 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out);
753 if (req->ring_index < cnt)
754 req->cnt = cnt - req->ring_index;
756 req->cnt = req->length -
757 (req->ring_index - cnt);
759 if (req->cnt < (req_cnt + 2))
762 /* Build command packet. */
763 req->current_outstanding_cmd = handle;
764 req->outstanding_cmds[handle] = sp;
766 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
769 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
770 cmd_pkt->handle = handle;
772 /* Zero out remaining portion of packet. */
773 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
774 clr_ptr = (uint32_t *)cmd_pkt + 2;
775 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
776 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
778 /* Set NPORT-ID and LUN number*/
779 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
780 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
781 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
782 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
783 cmd_pkt->vp_index = sp->fcport->vp_idx;
785 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
786 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
788 /* Load SCSI command packet. */
789 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
790 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
792 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
794 /* Build IOCB segments */
795 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
797 /* Set total data segment count. */
798 cmd_pkt->entry_count = (uint8_t)req_cnt;
801 /* Adjust ring index. */
803 if (req->ring_index == req->length) {
805 req->ring_ptr = req->ring;
809 sp->flags |= SRB_DMA_VALID;
811 /* Set chip new ring index. */
812 WRT_REG_DWORD(®->req_q_in, req->ring_index);
813 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */
815 /* Manage unprocessed RIO/ZIO commands in response queue. */
816 if (vha->flags.process_response_queue &&
817 ha->rsp->ring_ptr->signature != RESPONSE_PROCESSED)
818 qla24xx_process_response_queue(vha);
820 spin_unlock_irqrestore(&ha->hardware_lock, flags);
827 spin_unlock_irqrestore(&ha->hardware_lock, flags);
829 return QLA_FUNCTION_FAILED;