2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 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 inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
15 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
16 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
17 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
18 static void qla2x00_isp_cmd(scsi_qla_host_t *ha);
21 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
24 * Returns the proper CF_* direction based on CDB.
26 static inline uint16_t
27 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
33 /* Set transfer direction */
34 if (cmd->sc_data_direction == DMA_TO_DEVICE)
36 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
42 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
43 * Continuation Type 0 IOCBs to allocate.
45 * @dsds: number of data segment decriptors needed
47 * Returns the number of IOCB entries needed to store @dsds.
50 qla2x00_calc_iocbs_32(uint16_t dsds)
56 iocbs += (dsds - 3) / 7;
64 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
65 * Continuation Type 1 IOCBs to allocate.
67 * @dsds: number of data segment decriptors needed
69 * Returns the number of IOCB entries needed to store @dsds.
72 qla2x00_calc_iocbs_64(uint16_t dsds)
78 iocbs += (dsds - 2) / 5;
86 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
89 * Returns a pointer to the Continuation Type 0 IOCB packet.
91 static inline cont_entry_t *
92 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
94 cont_entry_t *cont_pkt;
96 /* Adjust ring index. */
98 if (ha->req_ring_index == ha->request_q_length) {
99 ha->req_ring_index = 0;
100 ha->request_ring_ptr = ha->request_ring;
102 ha->request_ring_ptr++;
105 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
107 /* Load packet defaults. */
108 *((uint32_t *)(&cont_pkt->entry_type)) =
109 __constant_cpu_to_le32(CONTINUE_TYPE);
115 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
118 * Returns a pointer to the continuation type 1 IOCB packet.
120 static inline cont_a64_entry_t *
121 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
123 cont_a64_entry_t *cont_pkt;
125 /* Adjust ring index. */
126 ha->req_ring_index++;
127 if (ha->req_ring_index == ha->request_q_length) {
128 ha->req_ring_index = 0;
129 ha->request_ring_ptr = ha->request_ring;
131 ha->request_ring_ptr++;
134 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
136 /* Load packet defaults. */
137 *((uint32_t *)(&cont_pkt->entry_type)) =
138 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
144 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
145 * capable IOCB types.
147 * @sp: SRB command to process
148 * @cmd_pkt: Command type 2 IOCB
149 * @tot_dsds: Total number of segments to transfer
151 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
157 struct scsi_cmnd *cmd;
158 struct scatterlist *sg;
163 /* Update entry type to indicate Command Type 2 IOCB */
164 *((uint32_t *)(&cmd_pkt->entry_type)) =
165 __constant_cpu_to_le32(COMMAND_TYPE);
167 /* No data transfer */
168 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
169 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
175 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
177 /* Three DSDs are available in the Command Type 2 IOCB */
179 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
181 /* Load data segments */
182 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
183 cont_entry_t *cont_pkt;
185 /* Allocate additional continuation packets? */
186 if (avail_dsds == 0) {
188 * Seven DSDs are available in the Continuation
191 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
192 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
196 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
197 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
203 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
204 * capable IOCB types.
206 * @sp: SRB command to process
207 * @cmd_pkt: Command type 3 IOCB
208 * @tot_dsds: Total number of segments to transfer
210 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
216 struct scsi_cmnd *cmd;
217 struct scatterlist *sg;
222 /* Update entry type to indicate Command Type 3 IOCB */
223 *((uint32_t *)(&cmd_pkt->entry_type)) =
224 __constant_cpu_to_le32(COMMAND_A64_TYPE);
226 /* No data transfer */
227 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
228 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
234 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
236 /* Two DSDs are available in the Command Type 3 IOCB */
238 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
240 /* Load data segments */
241 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
243 cont_a64_entry_t *cont_pkt;
245 /* Allocate additional continuation packets? */
246 if (avail_dsds == 0) {
248 * Five DSDs are available in the Continuation
251 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
252 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
256 sle_dma = sg_dma_address(sg);
257 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
258 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
259 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
265 * qla2x00_start_scsi() - Send a SCSI command to the ISP
266 * @sp: command to send to the ISP
268 * Returns non-zero if a failure occured, else zero.
271 qla2x00_start_scsi(srb_t *sp)
276 struct scsi_cmnd *cmd;
280 cmd_entry_t *cmd_pkt;
284 struct device_reg_2xxx __iomem *reg;
286 /* Setup device pointers. */
289 reg = &ha->iobase->isp;
291 /* So we know we haven't pci_map'ed anything yet */
294 /* Send marker if required */
295 if (ha->marker_needed != 0) {
296 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
297 return (QLA_FUNCTION_FAILED);
299 ha->marker_needed = 0;
302 /* Acquire ring specific lock */
303 spin_lock_irqsave(&ha->hardware_lock, flags);
305 /* Check for room in outstanding command list. */
306 handle = ha->current_outstanding_cmd;
307 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
309 if (handle == MAX_OUTSTANDING_COMMANDS)
311 if (ha->outstanding_cmds[handle] == 0)
314 if (index == MAX_OUTSTANDING_COMMANDS)
317 /* Map the sg table so we have an accurate count of sg entries needed */
318 if (scsi_sg_count(cmd)) {
319 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
320 scsi_sg_count(cmd), cmd->sc_data_direction);
328 /* Calculate the number of request entries needed. */
329 req_cnt = ha->isp_ops.calc_req_entries(tot_dsds);
330 if (ha->req_q_cnt < (req_cnt + 2)) {
331 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
332 if (ha->req_ring_index < cnt)
333 ha->req_q_cnt = cnt - ha->req_ring_index;
335 ha->req_q_cnt = ha->request_q_length -
336 (ha->req_ring_index - cnt);
338 if (ha->req_q_cnt < (req_cnt + 2))
341 /* Build command packet */
342 ha->current_outstanding_cmd = handle;
343 ha->outstanding_cmds[handle] = sp;
345 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
346 ha->req_q_cnt -= req_cnt;
348 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
349 cmd_pkt->handle = handle;
350 /* Zero out remaining portion of packet. */
351 clr_ptr = (uint32_t *)cmd_pkt + 2;
352 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
353 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
355 /* Set target ID and LUN number*/
356 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
357 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
359 /* Update tagged queuing modifier */
360 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
362 /* Load SCSI command packet. */
363 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
364 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
366 /* Build IOCB segments */
367 ha->isp_ops.build_iocbs(sp, cmd_pkt, tot_dsds);
369 /* Set total data segment count. */
370 cmd_pkt->entry_count = (uint8_t)req_cnt;
373 /* Adjust ring index. */
374 ha->req_ring_index++;
375 if (ha->req_ring_index == ha->request_q_length) {
376 ha->req_ring_index = 0;
377 ha->request_ring_ptr = ha->request_ring;
379 ha->request_ring_ptr++;
381 sp->flags |= SRB_DMA_VALID;
383 /* Set chip new ring index. */
384 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
385 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
387 /* Manage unprocessed RIO/ZIO commands in response queue. */
388 if (ha->flags.process_response_queue &&
389 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
390 qla2x00_process_response_queue(ha);
392 spin_unlock_irqrestore(&ha->hardware_lock, flags);
393 return (QLA_SUCCESS);
399 spin_unlock_irqrestore(&ha->hardware_lock, flags);
401 return (QLA_FUNCTION_FAILED);
405 * qla2x00_marker() - Send a marker IOCB to the firmware.
409 * @type: marker modifier
411 * Can be called from both normal and interrupt context.
413 * Returns non-zero if a failure occured, else zero.
416 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
420 struct mrk_entry_24xx *mrk24;
421 scsi_qla_host_t *pha = to_qla_parent(ha);
424 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
426 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
427 __func__, ha->host_no));
429 return (QLA_FUNCTION_FAILED);
432 mrk->entry_type = MARKER_TYPE;
433 mrk->modifier = type;
434 if (type != MK_SYNC_ALL) {
435 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
436 mrk24 = (struct mrk_entry_24xx *) mrk;
437 mrk24->nport_handle = cpu_to_le16(loop_id);
438 mrk24->lun[1] = LSB(lun);
439 mrk24->lun[2] = MSB(lun);
440 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
441 mrk24->vp_index = ha->vp_idx;
443 SET_TARGET_ID(ha, mrk->target, loop_id);
444 mrk->lun = cpu_to_le16(lun);
449 qla2x00_isp_cmd(pha);
451 return (QLA_SUCCESS);
455 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
459 unsigned long flags = 0;
461 spin_lock_irqsave(&ha->hardware_lock, flags);
462 ret = __qla2x00_marker(ha, loop_id, lun, type);
463 spin_unlock_irqrestore(&ha->hardware_lock, flags);
469 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
472 * Note: The caller must hold the hardware lock before calling this routine.
474 * Returns NULL if function failed, else, a pointer to the request packet.
477 qla2x00_req_pkt(scsi_qla_host_t *ha)
479 device_reg_t __iomem *reg = ha->iobase;
480 request_t *pkt = NULL;
484 uint16_t req_cnt = 1;
486 /* Wait 1 second for slot. */
487 for (timer = HZ; timer; timer--) {
488 if ((req_cnt + 2) >= ha->req_q_cnt) {
489 /* Calculate number of free request entries. */
490 if (IS_QLA24XX(ha) || IS_QLA54XX(ha))
491 cnt = (uint16_t)RD_REG_DWORD(
492 ®->isp24.req_q_out);
494 cnt = qla2x00_debounce_register(
495 ISP_REQ_Q_OUT(ha, ®->isp));
496 if (ha->req_ring_index < cnt)
497 ha->req_q_cnt = cnt - ha->req_ring_index;
499 ha->req_q_cnt = ha->request_q_length -
500 (ha->req_ring_index - cnt);
502 /* If room for request in request ring. */
503 if ((req_cnt + 2) < ha->req_q_cnt) {
505 pkt = ha->request_ring_ptr;
507 /* Zero out packet. */
508 dword_ptr = (uint32_t *)pkt;
509 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
512 /* Set system defined field. */
513 pkt->sys_define = (uint8_t)ha->req_ring_index;
515 /* Set entry count. */
516 pkt->entry_count = 1;
521 /* Release ring specific lock */
522 spin_unlock(&ha->hardware_lock);
524 udelay(2); /* 2 us */
526 /* Check for pending interrupts. */
527 /* During init we issue marker directly */
528 if (!ha->marker_needed)
531 spin_lock_irq(&ha->hardware_lock);
534 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
541 * qla2x00_isp_cmd() - Modify the request ring pointer.
544 * Note: The caller must hold the hardware lock before calling this routine.
547 qla2x00_isp_cmd(scsi_qla_host_t *ha)
549 device_reg_t __iomem *reg = ha->iobase;
551 DEBUG5(printk("%s(): IOCB data:\n", __func__));
552 DEBUG5(qla2x00_dump_buffer(
553 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
555 /* Adjust ring index. */
556 ha->req_ring_index++;
557 if (ha->req_ring_index == ha->request_q_length) {
558 ha->req_ring_index = 0;
559 ha->request_ring_ptr = ha->request_ring;
561 ha->request_ring_ptr++;
563 /* Set chip new ring index. */
564 if (IS_QLA24XX(ha) || IS_QLA54XX(ha)) {
565 WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index);
566 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
568 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index);
569 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
575 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
576 * Continuation Type 1 IOCBs to allocate.
578 * @dsds: number of data segment decriptors needed
580 * Returns the number of IOCB entries needed to store @dsds.
582 static inline uint16_t
583 qla24xx_calc_iocbs(uint16_t dsds)
589 iocbs += (dsds - 1) / 5;
597 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
600 * @sp: SRB command to process
601 * @cmd_pkt: Command type 3 IOCB
602 * @tot_dsds: Total number of segments to transfer
605 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
611 struct scsi_cmnd *cmd;
612 struct scatterlist *sg;
617 /* Update entry type to indicate Command Type 3 IOCB */
618 *((uint32_t *)(&cmd_pkt->entry_type)) =
619 __constant_cpu_to_le32(COMMAND_TYPE_7);
621 /* No data transfer */
622 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
623 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
629 /* Set transfer direction */
630 if (cmd->sc_data_direction == DMA_TO_DEVICE)
631 cmd_pkt->task_mgmt_flags =
632 __constant_cpu_to_le16(TMF_WRITE_DATA);
633 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
634 cmd_pkt->task_mgmt_flags =
635 __constant_cpu_to_le16(TMF_READ_DATA);
637 /* One DSD is available in the Command Type 3 IOCB */
639 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
641 /* Load data segments */
643 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
645 cont_a64_entry_t *cont_pkt;
647 /* Allocate additional continuation packets? */
648 if (avail_dsds == 0) {
650 * Five DSDs are available in the Continuation
653 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
654 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
658 sle_dma = sg_dma_address(sg);
659 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
660 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
661 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
668 * qla24xx_start_scsi() - Send a SCSI command to the ISP
669 * @sp: command to send to the ISP
671 * Returns non-zero if a failure occured, else zero.
674 qla24xx_start_scsi(srb_t *sp)
679 struct scsi_cmnd *cmd;
683 struct cmd_type_7 *cmd_pkt;
687 struct device_reg_24xx __iomem *reg;
689 /* Setup device pointers. */
692 reg = &ha->iobase->isp24;
694 /* So we know we haven't pci_map'ed anything yet */
697 /* Send marker if required */
698 if (ha->marker_needed != 0) {
699 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
700 return QLA_FUNCTION_FAILED;
702 ha->marker_needed = 0;
705 /* Acquire ring specific lock */
706 spin_lock_irqsave(&ha->hardware_lock, flags);
708 /* Check for room in outstanding command list. */
709 handle = ha->current_outstanding_cmd;
710 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
712 if (handle == MAX_OUTSTANDING_COMMANDS)
714 if (ha->outstanding_cmds[handle] == 0)
717 if (index == MAX_OUTSTANDING_COMMANDS)
720 /* Map the sg table so we have an accurate count of sg entries needed */
721 if (scsi_sg_count(cmd)) {
722 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
723 scsi_sg_count(cmd), cmd->sc_data_direction);
731 req_cnt = qla24xx_calc_iocbs(tot_dsds);
732 if (ha->req_q_cnt < (req_cnt + 2)) {
733 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out);
734 if (ha->req_ring_index < cnt)
735 ha->req_q_cnt = cnt - ha->req_ring_index;
737 ha->req_q_cnt = ha->request_q_length -
738 (ha->req_ring_index - cnt);
740 if (ha->req_q_cnt < (req_cnt + 2))
743 /* Build command packet. */
744 ha->current_outstanding_cmd = handle;
745 ha->outstanding_cmds[handle] = sp;
747 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
748 ha->req_q_cnt -= req_cnt;
750 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
751 cmd_pkt->handle = handle;
753 /* Zero out remaining portion of packet. */
754 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
755 clr_ptr = (uint32_t *)cmd_pkt + 2;
756 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
757 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
759 /* Set NPORT-ID and LUN number*/
760 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
761 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
762 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
763 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
764 cmd_pkt->vp_index = sp->fcport->vp_idx;
766 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
767 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
769 /* Load SCSI command packet. */
770 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
771 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
773 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
775 /* Build IOCB segments */
776 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
778 /* Set total data segment count. */
779 cmd_pkt->entry_count = (uint8_t)req_cnt;
782 /* Adjust ring index. */
783 ha->req_ring_index++;
784 if (ha->req_ring_index == ha->request_q_length) {
785 ha->req_ring_index = 0;
786 ha->request_ring_ptr = ha->request_ring;
788 ha->request_ring_ptr++;
790 sp->flags |= SRB_DMA_VALID;
792 /* Set chip new ring index. */
793 WRT_REG_DWORD(®->req_q_in, ha->req_ring_index);
794 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */
796 /* Manage unprocessed RIO/ZIO commands in response queue. */
797 if (ha->flags.process_response_queue &&
798 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
799 qla24xx_process_response_queue(ha);
801 spin_unlock_irqrestore(&ha->hardware_lock, flags);
808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
810 return QLA_FUNCTION_FAILED;