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 *ha);
15 static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
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->ha->qla_stats.output_bytes +=
34 scsi_bufflen(sp->cmd);
35 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
37 sp->fcport->ha->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 *ha)
96 cont_entry_t *cont_pkt;
98 /* Adjust ring index. */
100 if (ha->req_ring_index == ha->request_q_length) {
101 ha->req_ring_index = 0;
102 ha->request_ring_ptr = ha->request_ring;
104 ha->request_ring_ptr++;
107 cont_pkt = (cont_entry_t *)ha->request_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 *ha)
125 cont_a64_entry_t *cont_pkt;
127 /* Adjust ring index. */
128 ha->req_ring_index++;
129 if (ha->req_ring_index == ha->request_q_length) {
130 ha->req_ring_index = 0;
131 ha->request_ring_ptr = ha->request_ring;
133 ha->request_ring_ptr++;
136 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
138 /* Load packet defaults. */
139 *((uint32_t *)(&cont_pkt->entry_type)) =
140 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
146 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
147 * capable IOCB types.
149 * @sp: SRB command to process
150 * @cmd_pkt: Command type 2 IOCB
151 * @tot_dsds: Total number of segments to transfer
153 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
159 struct scsi_cmnd *cmd;
160 struct scatterlist *sg;
165 /* Update entry type to indicate Command Type 2 IOCB */
166 *((uint32_t *)(&cmd_pkt->entry_type)) =
167 __constant_cpu_to_le32(COMMAND_TYPE);
169 /* No data transfer */
170 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
171 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
177 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
179 /* Three DSDs are available in the Command Type 2 IOCB */
181 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
183 /* Load data segments */
184 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
185 cont_entry_t *cont_pkt;
187 /* Allocate additional continuation packets? */
188 if (avail_dsds == 0) {
190 * Seven DSDs are available in the Continuation
193 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
194 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
198 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
199 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
205 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
206 * capable IOCB types.
208 * @sp: SRB command to process
209 * @cmd_pkt: Command type 3 IOCB
210 * @tot_dsds: Total number of segments to transfer
212 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
218 struct scsi_cmnd *cmd;
219 struct scatterlist *sg;
224 /* Update entry type to indicate Command Type 3 IOCB */
225 *((uint32_t *)(&cmd_pkt->entry_type)) =
226 __constant_cpu_to_le32(COMMAND_A64_TYPE);
228 /* No data transfer */
229 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
230 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
236 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
238 /* Two DSDs are available in the Command Type 3 IOCB */
240 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
242 /* Load data segments */
243 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
245 cont_a64_entry_t *cont_pkt;
247 /* Allocate additional continuation packets? */
248 if (avail_dsds == 0) {
250 * Five DSDs are available in the Continuation
253 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
254 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
258 sle_dma = sg_dma_address(sg);
259 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
260 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
261 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
267 * qla2x00_start_scsi() - Send a SCSI command to the ISP
268 * @sp: command to send to the ISP
270 * Returns non-zero if a failure occurred, else zero.
273 qla2x00_start_scsi(srb_t *sp)
278 struct scsi_cmnd *cmd;
282 cmd_entry_t *cmd_pkt;
286 struct device_reg_2xxx __iomem *reg;
288 /* Setup device pointers. */
291 reg = &ha->iobase->isp;
293 /* So we know we haven't pci_map'ed anything yet */
296 /* Send marker if required */
297 if (ha->marker_needed != 0) {
298 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
299 return (QLA_FUNCTION_FAILED);
301 ha->marker_needed = 0;
304 /* Acquire ring specific lock */
305 spin_lock_irqsave(&ha->hardware_lock, flags);
307 /* Check for room in outstanding command list. */
308 handle = ha->current_outstanding_cmd;
309 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
311 if (handle == MAX_OUTSTANDING_COMMANDS)
313 if (!ha->outstanding_cmds[handle])
316 if (index == MAX_OUTSTANDING_COMMANDS)
319 /* Map the sg table so we have an accurate count of sg entries needed */
320 if (scsi_sg_count(cmd)) {
321 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
322 scsi_sg_count(cmd), cmd->sc_data_direction);
330 /* Calculate the number of request entries needed. */
331 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
332 if (ha->req_q_cnt < (req_cnt + 2)) {
333 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
334 if (ha->req_ring_index < cnt)
335 ha->req_q_cnt = cnt - ha->req_ring_index;
337 ha->req_q_cnt = ha->request_q_length -
338 (ha->req_ring_index - cnt);
340 if (ha->req_q_cnt < (req_cnt + 2))
343 /* Build command packet */
344 ha->current_outstanding_cmd = handle;
345 ha->outstanding_cmds[handle] = sp;
347 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
348 ha->req_q_cnt -= req_cnt;
350 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
351 cmd_pkt->handle = handle;
352 /* Zero out remaining portion of packet. */
353 clr_ptr = (uint32_t *)cmd_pkt + 2;
354 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
355 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
357 /* Set target ID and LUN number*/
358 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
359 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
361 /* Update tagged queuing modifier */
362 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
364 /* Load SCSI command packet. */
365 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
366 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
368 /* Build IOCB segments */
369 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
371 /* Set total data segment count. */
372 cmd_pkt->entry_count = (uint8_t)req_cnt;
375 /* Adjust ring index. */
376 ha->req_ring_index++;
377 if (ha->req_ring_index == ha->request_q_length) {
378 ha->req_ring_index = 0;
379 ha->request_ring_ptr = ha->request_ring;
381 ha->request_ring_ptr++;
383 sp->flags |= SRB_DMA_VALID;
385 /* Set chip new ring index. */
386 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
387 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
389 /* Manage unprocessed RIO/ZIO commands in response queue. */
390 if (ha->flags.process_response_queue &&
391 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
392 qla2x00_process_response_queue(ha);
394 spin_unlock_irqrestore(&ha->hardware_lock, flags);
395 return (QLA_SUCCESS);
401 spin_unlock_irqrestore(&ha->hardware_lock, flags);
403 return (QLA_FUNCTION_FAILED);
407 * qla2x00_marker() - Send a marker IOCB to the firmware.
411 * @type: marker modifier
413 * Can be called from both normal and interrupt context.
415 * Returns non-zero if a failure occurred, else zero.
418 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
422 struct mrk_entry_24xx *mrk24;
423 scsi_qla_host_t *pha = to_qla_parent(ha);
426 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
428 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
429 __func__, ha->host_no));
431 return (QLA_FUNCTION_FAILED);
434 mrk->entry_type = MARKER_TYPE;
435 mrk->modifier = type;
436 if (type != MK_SYNC_ALL) {
437 if (IS_FWI2_CAPABLE(ha)) {
438 mrk24 = (struct mrk_entry_24xx *) mrk;
439 mrk24->nport_handle = cpu_to_le16(loop_id);
440 mrk24->lun[1] = LSB(lun);
441 mrk24->lun[2] = MSB(lun);
442 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
443 mrk24->vp_index = ha->vp_idx;
445 SET_TARGET_ID(ha, mrk->target, loop_id);
446 mrk->lun = cpu_to_le16(lun);
451 qla2x00_isp_cmd(pha);
453 return (QLA_SUCCESS);
457 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
461 unsigned long flags = 0;
462 scsi_qla_host_t *pha = to_qla_parent(ha);
464 spin_lock_irqsave(&pha->hardware_lock, flags);
465 ret = __qla2x00_marker(ha, loop_id, lun, type);
466 spin_unlock_irqrestore(&pha->hardware_lock, flags);
472 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
475 * Note: The caller must hold the hardware lock before calling this routine.
477 * Returns NULL if function failed, else, a pointer to the request packet.
480 qla2x00_req_pkt(scsi_qla_host_t *ha)
482 device_reg_t __iomem *reg = ha->iobase;
483 request_t *pkt = NULL;
487 uint16_t req_cnt = 1;
489 /* Wait 1 second for slot. */
490 for (timer = HZ; timer; timer--) {
491 if ((req_cnt + 2) >= ha->req_q_cnt) {
492 /* Calculate number of free request entries. */
493 if (IS_FWI2_CAPABLE(ha))
494 cnt = (uint16_t)RD_REG_DWORD(
495 ®->isp24.req_q_out);
497 cnt = qla2x00_debounce_register(
498 ISP_REQ_Q_OUT(ha, ®->isp));
499 if (ha->req_ring_index < cnt)
500 ha->req_q_cnt = cnt - ha->req_ring_index;
502 ha->req_q_cnt = ha->request_q_length -
503 (ha->req_ring_index - cnt);
505 /* If room for request in request ring. */
506 if ((req_cnt + 2) < ha->req_q_cnt) {
508 pkt = ha->request_ring_ptr;
510 /* Zero out packet. */
511 dword_ptr = (uint32_t *)pkt;
512 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
515 /* Set system defined field. */
516 pkt->sys_define = (uint8_t)ha->req_ring_index;
518 /* Set entry count. */
519 pkt->entry_count = 1;
524 /* Release ring specific lock */
525 spin_unlock(&ha->hardware_lock);
527 udelay(2); /* 2 us */
529 /* Check for pending interrupts. */
530 /* During init we issue marker directly */
531 if (!ha->marker_needed && !ha->flags.init_done)
534 spin_lock_irq(&ha->hardware_lock);
537 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
544 * qla2x00_isp_cmd() - Modify the request ring pointer.
547 * Note: The caller must hold the hardware lock before calling this routine.
550 qla2x00_isp_cmd(scsi_qla_host_t *ha)
552 device_reg_t __iomem *reg = ha->iobase;
554 DEBUG5(printk("%s(): IOCB data:\n", __func__));
555 DEBUG5(qla2x00_dump_buffer(
556 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
558 /* Adjust ring index. */
559 ha->req_ring_index++;
560 if (ha->req_ring_index == ha->request_q_length) {
561 ha->req_ring_index = 0;
562 ha->request_ring_ptr = ha->request_ring;
564 ha->request_ring_ptr++;
566 /* Set chip new ring index. */
567 if (IS_FWI2_CAPABLE(ha)) {
568 WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index);
569 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
571 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index);
572 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
578 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
579 * Continuation Type 1 IOCBs to allocate.
581 * @dsds: number of data segment decriptors needed
583 * Returns the number of IOCB entries needed to store @dsds.
585 static inline uint16_t
586 qla24xx_calc_iocbs(uint16_t dsds)
592 iocbs += (dsds - 1) / 5;
600 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
603 * @sp: SRB command to process
604 * @cmd_pkt: Command type 3 IOCB
605 * @tot_dsds: Total number of segments to transfer
608 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
614 struct scsi_cmnd *cmd;
615 struct scatterlist *sg;
620 /* Update entry type to indicate Command Type 3 IOCB */
621 *((uint32_t *)(&cmd_pkt->entry_type)) =
622 __constant_cpu_to_le32(COMMAND_TYPE_7);
624 /* No data transfer */
625 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
626 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
632 /* Set transfer direction */
633 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
634 cmd_pkt->task_mgmt_flags =
635 __constant_cpu_to_le16(TMF_WRITE_DATA);
636 sp->fcport->ha->qla_stats.output_bytes +=
637 scsi_bufflen(sp->cmd);
638 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
639 cmd_pkt->task_mgmt_flags =
640 __constant_cpu_to_le16(TMF_READ_DATA);
641 sp->fcport->ha->qla_stats.input_bytes +=
642 scsi_bufflen(sp->cmd);
645 /* One DSD is available in the Command Type 3 IOCB */
647 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
649 /* Load data segments */
651 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
653 cont_a64_entry_t *cont_pkt;
655 /* Allocate additional continuation packets? */
656 if (avail_dsds == 0) {
658 * Five DSDs are available in the Continuation
661 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
662 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
666 sle_dma = sg_dma_address(sg);
667 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
668 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
669 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
676 * qla24xx_start_scsi() - Send a SCSI command to the ISP
677 * @sp: command to send to the ISP
679 * Returns non-zero if a failure occurred, else zero.
682 qla24xx_start_scsi(srb_t *sp)
686 scsi_qla_host_t *ha, *pha;
687 struct scsi_cmnd *cmd;
691 struct cmd_type_7 *cmd_pkt;
695 struct device_reg_24xx __iomem *reg;
697 /* Setup device pointers. */
700 pha = to_qla_parent(ha);
701 reg = &ha->iobase->isp24;
703 /* So we know we haven't pci_map'ed anything yet */
706 /* Send marker if required */
707 if (ha->marker_needed != 0) {
708 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
709 return QLA_FUNCTION_FAILED;
711 ha->marker_needed = 0;
714 /* Acquire ring specific lock */
715 spin_lock_irqsave(&pha->hardware_lock, flags);
717 /* Check for room in outstanding command list. */
718 handle = ha->current_outstanding_cmd;
719 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
721 if (handle == MAX_OUTSTANDING_COMMANDS)
723 if (!ha->outstanding_cmds[handle])
726 if (index == MAX_OUTSTANDING_COMMANDS)
729 /* Map the sg table so we have an accurate count of sg entries needed */
730 if (scsi_sg_count(cmd)) {
731 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
732 scsi_sg_count(cmd), cmd->sc_data_direction);
740 req_cnt = qla24xx_calc_iocbs(tot_dsds);
741 if (ha->req_q_cnt < (req_cnt + 2)) {
742 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out);
743 if (ha->req_ring_index < cnt)
744 ha->req_q_cnt = cnt - ha->req_ring_index;
746 ha->req_q_cnt = ha->request_q_length -
747 (ha->req_ring_index - cnt);
749 if (ha->req_q_cnt < (req_cnt + 2))
752 /* Build command packet. */
753 ha->current_outstanding_cmd = handle;
754 ha->outstanding_cmds[handle] = sp;
756 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
757 ha->req_q_cnt -= req_cnt;
759 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
760 cmd_pkt->handle = handle;
762 /* Zero out remaining portion of packet. */
763 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
764 clr_ptr = (uint32_t *)cmd_pkt + 2;
765 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
766 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
768 /* Set NPORT-ID and LUN number*/
769 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
770 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
771 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
772 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
773 cmd_pkt->vp_index = sp->fcport->vp_idx;
775 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
776 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
778 /* Load SCSI command packet. */
779 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
780 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
782 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
784 /* Build IOCB segments */
785 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
787 /* Set total data segment count. */
788 cmd_pkt->entry_count = (uint8_t)req_cnt;
791 /* Adjust ring index. */
792 ha->req_ring_index++;
793 if (ha->req_ring_index == ha->request_q_length) {
794 ha->req_ring_index = 0;
795 ha->request_ring_ptr = ha->request_ring;
797 ha->request_ring_ptr++;
799 sp->flags |= SRB_DMA_VALID;
801 /* Set chip new ring index. */
802 WRT_REG_DWORD(®->req_q_in, ha->req_ring_index);
803 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */
805 /* Manage unprocessed RIO/ZIO commands in response queue. */
806 if (ha->flags.process_response_queue &&
807 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
808 qla24xx_process_response_queue(ha);
810 spin_unlock_irqrestore(&pha->hardware_lock, flags);
817 spin_unlock_irqrestore(&pha->hardware_lock, flags);
819 return QLA_FUNCTION_FAILED;