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(struct scsi_cmnd *cmd)
30 /* Set transfer direction */
31 if (cmd->sc_data_direction == DMA_TO_DEVICE)
33 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
39 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
40 * Continuation Type 0 IOCBs to allocate.
42 * @dsds: number of data segment decriptors needed
44 * Returns the number of IOCB entries needed to store @dsds.
47 qla2x00_calc_iocbs_32(uint16_t dsds)
53 iocbs += (dsds - 3) / 7;
61 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
62 * Continuation Type 1 IOCBs to allocate.
64 * @dsds: number of data segment decriptors needed
66 * Returns the number of IOCB entries needed to store @dsds.
69 qla2x00_calc_iocbs_64(uint16_t dsds)
75 iocbs += (dsds - 2) / 5;
83 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
86 * Returns a pointer to the Continuation Type 0 IOCB packet.
88 static inline cont_entry_t *
89 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
91 cont_entry_t *cont_pkt;
93 /* Adjust ring index. */
95 if (ha->req_ring_index == ha->request_q_length) {
96 ha->req_ring_index = 0;
97 ha->request_ring_ptr = ha->request_ring;
99 ha->request_ring_ptr++;
102 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
104 /* Load packet defaults. */
105 *((uint32_t *)(&cont_pkt->entry_type)) =
106 __constant_cpu_to_le32(CONTINUE_TYPE);
112 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
115 * Returns a pointer to the continuation type 1 IOCB packet.
117 static inline cont_a64_entry_t *
118 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
120 cont_a64_entry_t *cont_pkt;
122 /* Adjust ring index. */
123 ha->req_ring_index++;
124 if (ha->req_ring_index == ha->request_q_length) {
125 ha->req_ring_index = 0;
126 ha->request_ring_ptr = ha->request_ring;
128 ha->request_ring_ptr++;
131 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
133 /* Load packet defaults. */
134 *((uint32_t *)(&cont_pkt->entry_type)) =
135 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
141 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
142 * capable IOCB types.
144 * @sp: SRB command to process
145 * @cmd_pkt: Command type 2 IOCB
146 * @tot_dsds: Total number of segments to transfer
148 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
154 struct scsi_cmnd *cmd;
155 struct scatterlist *sg;
160 /* Update entry type to indicate Command Type 2 IOCB */
161 *((uint32_t *)(&cmd_pkt->entry_type)) =
162 __constant_cpu_to_le32(COMMAND_TYPE);
164 /* No data transfer */
165 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
166 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
172 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
174 /* Three DSDs are available in the Command Type 2 IOCB */
176 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
178 /* Load data segments */
179 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
180 cont_entry_t *cont_pkt;
182 /* Allocate additional continuation packets? */
183 if (avail_dsds == 0) {
185 * Seven DSDs are available in the Continuation
188 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
189 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
193 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
194 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
200 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
201 * capable IOCB types.
203 * @sp: SRB command to process
204 * @cmd_pkt: Command type 3 IOCB
205 * @tot_dsds: Total number of segments to transfer
207 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
213 struct scsi_cmnd *cmd;
214 struct scatterlist *sg;
219 /* Update entry type to indicate Command Type 3 IOCB */
220 *((uint32_t *)(&cmd_pkt->entry_type)) =
221 __constant_cpu_to_le32(COMMAND_A64_TYPE);
223 /* No data transfer */
224 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
225 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
231 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
233 /* Two DSDs are available in the Command Type 3 IOCB */
235 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
237 /* Load data segments */
238 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
240 cont_a64_entry_t *cont_pkt;
242 /* Allocate additional continuation packets? */
243 if (avail_dsds == 0) {
245 * Five DSDs are available in the Continuation
248 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
249 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
253 sle_dma = sg_dma_address(sg);
254 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
255 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
256 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
262 * qla2x00_start_scsi() - Send a SCSI command to the ISP
263 * @sp: command to send to the ISP
265 * Returns non-zero if a failure occured, else zero.
268 qla2x00_start_scsi(srb_t *sp)
273 struct scsi_cmnd *cmd;
277 cmd_entry_t *cmd_pkt;
281 struct device_reg_2xxx __iomem *reg;
283 /* Setup device pointers. */
286 reg = &ha->iobase->isp;
288 /* So we know we haven't pci_map'ed anything yet */
291 /* Send marker if required */
292 if (ha->marker_needed != 0) {
293 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
294 return (QLA_FUNCTION_FAILED);
296 ha->marker_needed = 0;
299 /* Acquire ring specific lock */
300 spin_lock_irqsave(&ha->hardware_lock, flags);
302 /* Check for room in outstanding command list. */
303 handle = ha->current_outstanding_cmd;
304 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
306 if (handle == MAX_OUTSTANDING_COMMANDS)
308 if (!ha->outstanding_cmds[handle])
311 if (index == MAX_OUTSTANDING_COMMANDS)
314 /* Map the sg table so we have an accurate count of sg entries needed */
315 if (scsi_sg_count(cmd)) {
316 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
317 scsi_sg_count(cmd), cmd->sc_data_direction);
325 /* Calculate the number of request entries needed. */
326 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
327 if (ha->req_q_cnt < (req_cnt + 2)) {
328 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
329 if (ha->req_ring_index < cnt)
330 ha->req_q_cnt = cnt - ha->req_ring_index;
332 ha->req_q_cnt = ha->request_q_length -
333 (ha->req_ring_index - cnt);
335 if (ha->req_q_cnt < (req_cnt + 2))
338 /* Build command packet */
339 ha->current_outstanding_cmd = handle;
340 ha->outstanding_cmds[handle] = sp;
342 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
343 ha->req_q_cnt -= req_cnt;
345 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
346 cmd_pkt->handle = handle;
347 /* Zero out remaining portion of packet. */
348 clr_ptr = (uint32_t *)cmd_pkt + 2;
349 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
350 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
352 /* Set target ID and LUN number*/
353 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
354 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
356 /* Update tagged queuing modifier */
357 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
359 /* Load SCSI command packet. */
360 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
361 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
363 /* Build IOCB segments */
364 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
366 /* Set total data segment count. */
367 cmd_pkt->entry_count = (uint8_t)req_cnt;
370 /* Adjust ring index. */
371 ha->req_ring_index++;
372 if (ha->req_ring_index == ha->request_q_length) {
373 ha->req_ring_index = 0;
374 ha->request_ring_ptr = ha->request_ring;
376 ha->request_ring_ptr++;
378 sp->flags |= SRB_DMA_VALID;
380 /* Set chip new ring index. */
381 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
382 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
384 /* Manage unprocessed RIO/ZIO commands in response queue. */
385 if (ha->flags.process_response_queue &&
386 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
387 qla2x00_process_response_queue(ha);
389 spin_unlock_irqrestore(&ha->hardware_lock, flags);
390 return (QLA_SUCCESS);
396 spin_unlock_irqrestore(&ha->hardware_lock, flags);
398 return (QLA_FUNCTION_FAILED);
402 * qla2x00_marker() - Send a marker IOCB to the firmware.
406 * @type: marker modifier
408 * Can be called from both normal and interrupt context.
410 * Returns non-zero if a failure occured, else zero.
413 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
417 struct mrk_entry_24xx *mrk24;
418 scsi_qla_host_t *pha = to_qla_parent(ha);
421 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
423 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
424 __func__, ha->host_no));
426 return (QLA_FUNCTION_FAILED);
429 mrk->entry_type = MARKER_TYPE;
430 mrk->modifier = type;
431 if (type != MK_SYNC_ALL) {
432 if (IS_FWI2_CAPABLE(ha)) {
433 mrk24 = (struct mrk_entry_24xx *) mrk;
434 mrk24->nport_handle = cpu_to_le16(loop_id);
435 mrk24->lun[1] = LSB(lun);
436 mrk24->lun[2] = MSB(lun);
437 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
438 mrk24->vp_index = ha->vp_idx;
440 SET_TARGET_ID(ha, mrk->target, loop_id);
441 mrk->lun = cpu_to_le16(lun);
446 qla2x00_isp_cmd(pha);
448 return (QLA_SUCCESS);
452 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
456 unsigned long flags = 0;
457 scsi_qla_host_t *pha = to_qla_parent(ha);
459 spin_lock_irqsave(&pha->hardware_lock, flags);
460 ret = __qla2x00_marker(ha, loop_id, lun, type);
461 spin_unlock_irqrestore(&pha->hardware_lock, flags);
467 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
470 * Note: The caller must hold the hardware lock before calling this routine.
472 * Returns NULL if function failed, else, a pointer to the request packet.
475 qla2x00_req_pkt(scsi_qla_host_t *ha)
477 device_reg_t __iomem *reg = ha->iobase;
478 request_t *pkt = NULL;
482 uint16_t req_cnt = 1;
484 /* Wait 1 second for slot. */
485 for (timer = HZ; timer; timer--) {
486 if ((req_cnt + 2) >= ha->req_q_cnt) {
487 /* Calculate number of free request entries. */
488 if (IS_FWI2_CAPABLE(ha))
489 cnt = (uint16_t)RD_REG_DWORD(
490 ®->isp24.req_q_out);
492 cnt = qla2x00_debounce_register(
493 ISP_REQ_Q_OUT(ha, ®->isp));
494 if (ha->req_ring_index < cnt)
495 ha->req_q_cnt = cnt - ha->req_ring_index;
497 ha->req_q_cnt = ha->request_q_length -
498 (ha->req_ring_index - cnt);
500 /* If room for request in request ring. */
501 if ((req_cnt + 2) < ha->req_q_cnt) {
503 pkt = ha->request_ring_ptr;
505 /* Zero out packet. */
506 dword_ptr = (uint32_t *)pkt;
507 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
510 /* Set system defined field. */
511 pkt->sys_define = (uint8_t)ha->req_ring_index;
513 /* Set entry count. */
514 pkt->entry_count = 1;
519 /* Release ring specific lock */
520 spin_unlock(&ha->hardware_lock);
522 udelay(2); /* 2 us */
524 /* Check for pending interrupts. */
525 /* During init we issue marker directly */
526 if (!ha->marker_needed && !ha->flags.init_done)
529 spin_lock_irq(&ha->hardware_lock);
532 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
539 * qla2x00_isp_cmd() - Modify the request ring pointer.
542 * Note: The caller must hold the hardware lock before calling this routine.
545 qla2x00_isp_cmd(scsi_qla_host_t *ha)
547 device_reg_t __iomem *reg = ha->iobase;
549 DEBUG5(printk("%s(): IOCB data:\n", __func__));
550 DEBUG5(qla2x00_dump_buffer(
551 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
553 /* Adjust ring index. */
554 ha->req_ring_index++;
555 if (ha->req_ring_index == ha->request_q_length) {
556 ha->req_ring_index = 0;
557 ha->request_ring_ptr = ha->request_ring;
559 ha->request_ring_ptr++;
561 /* Set chip new ring index. */
562 if (IS_FWI2_CAPABLE(ha)) {
563 WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index);
564 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
566 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index);
567 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
573 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
574 * Continuation Type 1 IOCBs to allocate.
576 * @dsds: number of data segment decriptors needed
578 * Returns the number of IOCB entries needed to store @dsds.
580 static inline uint16_t
581 qla24xx_calc_iocbs(uint16_t dsds)
587 iocbs += (dsds - 1) / 5;
595 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
598 * @sp: SRB command to process
599 * @cmd_pkt: Command type 3 IOCB
600 * @tot_dsds: Total number of segments to transfer
603 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
609 struct scsi_cmnd *cmd;
610 struct scatterlist *sg;
615 /* Update entry type to indicate Command Type 3 IOCB */
616 *((uint32_t *)(&cmd_pkt->entry_type)) =
617 __constant_cpu_to_le32(COMMAND_TYPE_7);
619 /* No data transfer */
620 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
621 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
627 /* Set transfer direction */
628 if (cmd->sc_data_direction == DMA_TO_DEVICE)
629 cmd_pkt->task_mgmt_flags =
630 __constant_cpu_to_le16(TMF_WRITE_DATA);
631 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
632 cmd_pkt->task_mgmt_flags =
633 __constant_cpu_to_le16(TMF_READ_DATA);
635 /* One DSD is available in the Command Type 3 IOCB */
637 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
639 /* Load data segments */
641 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
643 cont_a64_entry_t *cont_pkt;
645 /* Allocate additional continuation packets? */
646 if (avail_dsds == 0) {
648 * Five DSDs are available in the Continuation
651 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
652 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
656 sle_dma = sg_dma_address(sg);
657 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
658 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
659 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
666 * qla24xx_start_scsi() - Send a SCSI command to the ISP
667 * @sp: command to send to the ISP
669 * Returns non-zero if a failure occured, else zero.
672 qla24xx_start_scsi(srb_t *sp)
676 scsi_qla_host_t *ha, *pha;
677 struct scsi_cmnd *cmd;
681 struct cmd_type_7 *cmd_pkt;
685 struct device_reg_24xx __iomem *reg;
687 /* Setup device pointers. */
690 pha = to_qla_parent(ha);
691 reg = &ha->iobase->isp24;
693 /* So we know we haven't pci_map'ed anything yet */
696 /* Send marker if required */
697 if (ha->marker_needed != 0) {
698 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
699 return QLA_FUNCTION_FAILED;
701 ha->marker_needed = 0;
704 /* Acquire ring specific lock */
705 spin_lock_irqsave(&pha->hardware_lock, flags);
707 /* Check for room in outstanding command list. */
708 handle = ha->current_outstanding_cmd;
709 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
711 if (handle == MAX_OUTSTANDING_COMMANDS)
713 if (!ha->outstanding_cmds[handle])
716 if (index == MAX_OUTSTANDING_COMMANDS)
719 /* Map the sg table so we have an accurate count of sg entries needed */
720 if (scsi_sg_count(cmd)) {
721 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
722 scsi_sg_count(cmd), cmd->sc_data_direction);
730 req_cnt = qla24xx_calc_iocbs(tot_dsds);
731 if (ha->req_q_cnt < (req_cnt + 2)) {
732 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out);
733 if (ha->req_ring_index < cnt)
734 ha->req_q_cnt = cnt - ha->req_ring_index;
736 ha->req_q_cnt = ha->request_q_length -
737 (ha->req_ring_index - cnt);
739 if (ha->req_q_cnt < (req_cnt + 2))
742 /* Build command packet. */
743 ha->current_outstanding_cmd = handle;
744 ha->outstanding_cmds[handle] = sp;
746 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
747 ha->req_q_cnt -= req_cnt;
749 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
750 cmd_pkt->handle = handle;
752 /* Zero out remaining portion of packet. */
753 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
754 clr_ptr = (uint32_t *)cmd_pkt + 2;
755 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
756 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
758 /* Set NPORT-ID and LUN number*/
759 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
760 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
761 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
762 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
763 cmd_pkt->vp_index = sp->fcport->vp_idx;
765 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
766 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
768 /* Load SCSI command packet. */
769 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
770 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
772 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
774 /* Build IOCB segments */
775 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
777 /* Set total data segment count. */
778 cmd_pkt->entry_count = (uint8_t)req_cnt;
781 /* Adjust ring index. */
782 ha->req_ring_index++;
783 if (ha->req_ring_index == ha->request_q_length) {
784 ha->req_ring_index = 0;
785 ha->request_ring_ptr = ha->request_ring;
787 ha->request_ring_ptr++;
789 sp->flags |= SRB_DMA_VALID;
791 /* Set chip new ring index. */
792 WRT_REG_DWORD(®->req_q_in, ha->req_ring_index);
793 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */
795 /* Manage unprocessed RIO/ZIO commands in response queue. */
796 if (ha->flags.process_response_queue &&
797 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
798 qla24xx_process_response_queue(ha);
800 spin_unlock_irqrestore(&pha->hardware_lock, flags);
807 spin_unlock_irqrestore(&pha->hardware_lock, flags);
809 return QLA_FUNCTION_FAILED;