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
;
161 /* Update entry type to indicate Command Type 2 IOCB */
162 *((uint32_t *)(&cmd_pkt
->entry_type
)) =
163 __constant_cpu_to_le32(COMMAND_TYPE
);
165 /* No data transfer */
166 if (cmd
->request_bufflen
== 0 || cmd
->sc_data_direction
== DMA_NONE
) {
167 cmd_pkt
->byte_count
= __constant_cpu_to_le32(0);
173 cmd_pkt
->control_flags
|= cpu_to_le16(qla2x00_get_cmd_direction(cmd
));
175 /* Three DSDs are available in the Command Type 2 IOCB */
177 cur_dsd
= (uint32_t *)&cmd_pkt
->dseg_0_address
;
179 /* Load data segments */
180 if (cmd
->use_sg
!= 0) {
181 struct scatterlist
*cur_seg
;
182 struct scatterlist
*end_seg
;
184 cur_seg
= (struct scatterlist
*)cmd
->request_buffer
;
185 end_seg
= cur_seg
+ tot_dsds
;
186 while (cur_seg
< end_seg
) {
187 cont_entry_t
*cont_pkt
;
189 /* Allocate additional continuation packets? */
190 if (avail_dsds
== 0) {
192 * Seven DSDs are available in the Continuation
195 cont_pkt
= qla2x00_prep_cont_type0_iocb(ha
);
196 cur_dsd
= (uint32_t *)&cont_pkt
->dseg_0_address
;
200 *cur_dsd
++ = cpu_to_le32(sg_dma_address(cur_seg
));
201 *cur_dsd
++ = cpu_to_le32(sg_dma_len(cur_seg
));
207 *cur_dsd
++ = cpu_to_le32(sp
->dma_handle
);
208 *cur_dsd
++ = cpu_to_le32(cmd
->request_bufflen
);
213 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
214 * capable IOCB types.
216 * @sp: SRB command to process
217 * @cmd_pkt: Command type 3 IOCB
218 * @tot_dsds: Total number of segments to transfer
220 void qla2x00_build_scsi_iocbs_64(srb_t
*sp
, cmd_entry_t
*cmd_pkt
,
226 struct scsi_cmnd
*cmd
;
230 /* Update entry type to indicate Command Type 3 IOCB */
231 *((uint32_t *)(&cmd_pkt
->entry_type
)) =
232 __constant_cpu_to_le32(COMMAND_A64_TYPE
);
234 /* No data transfer */
235 if (cmd
->request_bufflen
== 0 || cmd
->sc_data_direction
== DMA_NONE
) {
236 cmd_pkt
->byte_count
= __constant_cpu_to_le32(0);
242 cmd_pkt
->control_flags
|= cpu_to_le16(qla2x00_get_cmd_direction(cmd
));
244 /* Two DSDs are available in the Command Type 3 IOCB */
246 cur_dsd
= (uint32_t *)&cmd_pkt
->dseg_0_address
;
248 /* Load data segments */
249 if (cmd
->use_sg
!= 0) {
250 struct scatterlist
*cur_seg
;
251 struct scatterlist
*end_seg
;
253 cur_seg
= (struct scatterlist
*)cmd
->request_buffer
;
254 end_seg
= cur_seg
+ tot_dsds
;
255 while (cur_seg
< end_seg
) {
257 cont_a64_entry_t
*cont_pkt
;
259 /* Allocate additional continuation packets? */
260 if (avail_dsds
== 0) {
262 * Five DSDs are available in the Continuation
265 cont_pkt
= qla2x00_prep_cont_type1_iocb(ha
);
266 cur_dsd
= (uint32_t *)cont_pkt
->dseg_0_address
;
270 sle_dma
= sg_dma_address(cur_seg
);
271 *cur_dsd
++ = cpu_to_le32(LSD(sle_dma
));
272 *cur_dsd
++ = cpu_to_le32(MSD(sle_dma
));
273 *cur_dsd
++ = cpu_to_le32(sg_dma_len(cur_seg
));
279 *cur_dsd
++ = cpu_to_le32(LSD(sp
->dma_handle
));
280 *cur_dsd
++ = cpu_to_le32(MSD(sp
->dma_handle
));
281 *cur_dsd
++ = cpu_to_le32(cmd
->request_bufflen
);
286 * qla2x00_start_scsi() - Send a SCSI command to the ISP
287 * @sp: command to send to the ISP
289 * Returns non-zero if a failure occured, else zero.
292 qla2x00_start_scsi(srb_t
*sp
)
297 struct scsi_cmnd
*cmd
;
301 cmd_entry_t
*cmd_pkt
;
302 struct scatterlist
*sg
;
306 struct device_reg_2xxx __iomem
*reg
;
308 /* Setup device pointers. */
311 reg
= &ha
->iobase
->isp
;
313 /* So we know we haven't pci_map'ed anything yet */
316 /* Send marker if required */
317 if (ha
->marker_needed
!= 0) {
318 if (qla2x00_marker(ha
, 0, 0, MK_SYNC_ALL
) != QLA_SUCCESS
) {
319 return (QLA_FUNCTION_FAILED
);
321 ha
->marker_needed
= 0;
324 /* Acquire ring specific lock */
325 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
327 /* Check for room in outstanding command list. */
328 handle
= ha
->current_outstanding_cmd
;
329 for (index
= 1; index
< MAX_OUTSTANDING_COMMANDS
; index
++) {
331 if (handle
== MAX_OUTSTANDING_COMMANDS
)
333 if (ha
->outstanding_cmds
[handle
] == 0)
336 if (index
== MAX_OUTSTANDING_COMMANDS
)
339 /* Map the sg table so we have an accurate count of sg entries needed */
341 sg
= (struct scatterlist
*) cmd
->request_buffer
;
342 tot_dsds
= pci_map_sg(ha
->pdev
, sg
, cmd
->use_sg
,
343 cmd
->sc_data_direction
);
346 } else if (cmd
->request_bufflen
) {
349 req_dma
= pci_map_single(ha
->pdev
, cmd
->request_buffer
,
350 cmd
->request_bufflen
, cmd
->sc_data_direction
);
351 if (dma_mapping_error(req_dma
))
354 sp
->dma_handle
= req_dma
;
358 /* Calculate the number of request entries needed. */
359 req_cnt
= ha
->isp_ops
.calc_req_entries(tot_dsds
);
360 if (ha
->req_q_cnt
< (req_cnt
+ 2)) {
361 cnt
= RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha
, reg
));
362 if (ha
->req_ring_index
< cnt
)
363 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
365 ha
->req_q_cnt
= ha
->request_q_length
-
366 (ha
->req_ring_index
- cnt
);
368 if (ha
->req_q_cnt
< (req_cnt
+ 2))
371 /* Build command packet */
372 ha
->current_outstanding_cmd
= handle
;
373 ha
->outstanding_cmds
[handle
] = sp
;
375 sp
->cmd
->host_scribble
= (unsigned char *)(unsigned long)handle
;
376 ha
->req_q_cnt
-= req_cnt
;
378 cmd_pkt
= (cmd_entry_t
*)ha
->request_ring_ptr
;
379 cmd_pkt
->handle
= handle
;
380 /* Zero out remaining portion of packet. */
381 clr_ptr
= (uint32_t *)cmd_pkt
+ 2;
382 memset(clr_ptr
, 0, REQUEST_ENTRY_SIZE
- 8);
383 cmd_pkt
->dseg_count
= cpu_to_le16(tot_dsds
);
385 /* Set target ID and LUN number*/
386 SET_TARGET_ID(ha
, cmd_pkt
->target
, sp
->fcport
->loop_id
);
387 cmd_pkt
->lun
= cpu_to_le16(sp
->cmd
->device
->lun
);
389 /* Update tagged queuing modifier */
390 cmd_pkt
->control_flags
= __constant_cpu_to_le16(CF_SIMPLE_TAG
);
392 /* Load SCSI command packet. */
393 memcpy(cmd_pkt
->scsi_cdb
, cmd
->cmnd
, cmd
->cmd_len
);
394 cmd_pkt
->byte_count
= cpu_to_le32((uint32_t)cmd
->request_bufflen
);
396 /* Build IOCB segments */
397 ha
->isp_ops
.build_iocbs(sp
, cmd_pkt
, tot_dsds
);
399 /* Set total data segment count. */
400 cmd_pkt
->entry_count
= (uint8_t)req_cnt
;
403 /* Adjust ring index. */
404 ha
->req_ring_index
++;
405 if (ha
->req_ring_index
== ha
->request_q_length
) {
406 ha
->req_ring_index
= 0;
407 ha
->request_ring_ptr
= ha
->request_ring
;
409 ha
->request_ring_ptr
++;
411 sp
->flags
|= SRB_DMA_VALID
;
413 /* Set chip new ring index. */
414 WRT_REG_WORD(ISP_REQ_Q_IN(ha
, reg
), ha
->req_ring_index
);
415 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha
, reg
)); /* PCI Posting. */
417 /* Manage unprocessed RIO/ZIO commands in response queue. */
418 if (ha
->flags
.process_response_queue
&&
419 ha
->response_ring_ptr
->signature
!= RESPONSE_PROCESSED
)
420 qla2x00_process_response_queue(ha
);
422 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
423 return (QLA_SUCCESS
);
426 if (cmd
->use_sg
&& tot_dsds
) {
427 sg
= (struct scatterlist
*) cmd
->request_buffer
;
428 pci_unmap_sg(ha
->pdev
, sg
, cmd
->use_sg
,
429 cmd
->sc_data_direction
);
430 } else if (tot_dsds
) {
431 pci_unmap_single(ha
->pdev
, sp
->dma_handle
,
432 cmd
->request_bufflen
, cmd
->sc_data_direction
);
434 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
436 return (QLA_FUNCTION_FAILED
);
440 * qla2x00_marker() - Send a marker IOCB to the firmware.
444 * @type: marker modifier
446 * Can be called from both normal and interrupt context.
448 * Returns non-zero if a failure occured, else zero.
451 __qla2x00_marker(scsi_qla_host_t
*ha
, uint16_t loop_id
, uint16_t lun
,
455 struct mrk_entry_24xx
*mrk24
;
458 mrk
= (mrk_entry_t
*)qla2x00_req_pkt(ha
);
460 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
461 __func__
, ha
->host_no
));
463 return (QLA_FUNCTION_FAILED
);
466 mrk
->entry_type
= MARKER_TYPE
;
467 mrk
->modifier
= type
;
468 if (type
!= MK_SYNC_ALL
) {
469 if (IS_QLA24XX(ha
) || IS_QLA54XX(ha
)) {
470 mrk24
= (struct mrk_entry_24xx
*) mrk
;
471 mrk24
->nport_handle
= cpu_to_le16(loop_id
);
472 mrk24
->lun
[1] = LSB(lun
);
473 mrk24
->lun
[2] = MSB(lun
);
475 SET_TARGET_ID(ha
, mrk
->target
, loop_id
);
476 mrk
->lun
= cpu_to_le16(lun
);
483 return (QLA_SUCCESS
);
487 qla2x00_marker(scsi_qla_host_t
*ha
, uint16_t loop_id
, uint16_t lun
,
491 unsigned long flags
= 0;
493 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
494 ret
= __qla2x00_marker(ha
, loop_id
, lun
, type
);
495 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
501 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
504 * Note: The caller must hold the hardware lock before calling this routine.
506 * Returns NULL if function failed, else, a pointer to the request packet.
509 qla2x00_req_pkt(scsi_qla_host_t
*ha
)
511 device_reg_t __iomem
*reg
= ha
->iobase
;
512 request_t
*pkt
= NULL
;
516 uint16_t req_cnt
= 1;
518 /* Wait 1 second for slot. */
519 for (timer
= HZ
; timer
; timer
--) {
520 if ((req_cnt
+ 2) >= ha
->req_q_cnt
) {
521 /* Calculate number of free request entries. */
522 if (IS_QLA24XX(ha
) || IS_QLA54XX(ha
))
523 cnt
= (uint16_t)RD_REG_DWORD(
524 ®
->isp24
.req_q_out
);
526 cnt
= qla2x00_debounce_register(
527 ISP_REQ_Q_OUT(ha
, ®
->isp
));
528 if (ha
->req_ring_index
< cnt
)
529 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
531 ha
->req_q_cnt
= ha
->request_q_length
-
532 (ha
->req_ring_index
- cnt
);
534 /* If room for request in request ring. */
535 if ((req_cnt
+ 2) < ha
->req_q_cnt
) {
537 pkt
= ha
->request_ring_ptr
;
539 /* Zero out packet. */
540 dword_ptr
= (uint32_t *)pkt
;
541 for (cnt
= 0; cnt
< REQUEST_ENTRY_SIZE
/ 4; cnt
++)
544 /* Set system defined field. */
545 pkt
->sys_define
= (uint8_t)ha
->req_ring_index
;
547 /* Set entry count. */
548 pkt
->entry_count
= 1;
553 /* Release ring specific lock */
554 spin_unlock(&ha
->hardware_lock
);
556 udelay(2); /* 2 us */
558 /* Check for pending interrupts. */
559 /* During init we issue marker directly */
560 if (!ha
->marker_needed
)
563 spin_lock_irq(&ha
->hardware_lock
);
566 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__
));
573 * qla2x00_isp_cmd() - Modify the request ring pointer.
576 * Note: The caller must hold the hardware lock before calling this routine.
579 qla2x00_isp_cmd(scsi_qla_host_t
*ha
)
581 device_reg_t __iomem
*reg
= ha
->iobase
;
583 DEBUG5(printk("%s(): IOCB data:\n", __func__
));
584 DEBUG5(qla2x00_dump_buffer(
585 (uint8_t *)ha
->request_ring_ptr
, REQUEST_ENTRY_SIZE
));
587 /* Adjust ring index. */
588 ha
->req_ring_index
++;
589 if (ha
->req_ring_index
== ha
->request_q_length
) {
590 ha
->req_ring_index
= 0;
591 ha
->request_ring_ptr
= ha
->request_ring
;
593 ha
->request_ring_ptr
++;
595 /* Set chip new ring index. */
596 if (IS_QLA24XX(ha
) || IS_QLA54XX(ha
)) {
597 WRT_REG_DWORD(®
->isp24
.req_q_in
, ha
->req_ring_index
);
598 RD_REG_DWORD_RELAXED(®
->isp24
.req_q_in
);
600 WRT_REG_WORD(ISP_REQ_Q_IN(ha
, ®
->isp
), ha
->req_ring_index
);
601 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha
, ®
->isp
));
607 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
608 * Continuation Type 1 IOCBs to allocate.
610 * @dsds: number of data segment decriptors needed
612 * Returns the number of IOCB entries needed to store @dsds.
614 static inline uint16_t
615 qla24xx_calc_iocbs(uint16_t dsds
)
621 iocbs
+= (dsds
- 1) / 5;
629 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
632 * @sp: SRB command to process
633 * @cmd_pkt: Command type 3 IOCB
634 * @tot_dsds: Total number of segments to transfer
637 qla24xx_build_scsi_iocbs(srb_t
*sp
, struct cmd_type_7
*cmd_pkt
,
643 struct scsi_cmnd
*cmd
;
647 /* Update entry type to indicate Command Type 3 IOCB */
648 *((uint32_t *)(&cmd_pkt
->entry_type
)) =
649 __constant_cpu_to_le32(COMMAND_TYPE_7
);
651 /* No data transfer */
652 if (cmd
->request_bufflen
== 0 || cmd
->sc_data_direction
== DMA_NONE
) {
653 cmd_pkt
->byte_count
= __constant_cpu_to_le32(0);
659 /* Set transfer direction */
660 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
661 cmd_pkt
->task_mgmt_flags
=
662 __constant_cpu_to_le16(TMF_WRITE_DATA
);
663 else if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
664 cmd_pkt
->task_mgmt_flags
=
665 __constant_cpu_to_le16(TMF_READ_DATA
);
667 /* One DSD is available in the Command Type 3 IOCB */
669 cur_dsd
= (uint32_t *)&cmd_pkt
->dseg_0_address
;
671 /* Load data segments */
672 if (cmd
->use_sg
!= 0) {
673 struct scatterlist
*cur_seg
;
674 struct scatterlist
*end_seg
;
676 cur_seg
= (struct scatterlist
*)cmd
->request_buffer
;
677 end_seg
= cur_seg
+ tot_dsds
;
678 while (cur_seg
< end_seg
) {
680 cont_a64_entry_t
*cont_pkt
;
682 /* Allocate additional continuation packets? */
683 if (avail_dsds
== 0) {
685 * Five DSDs are available in the Continuation
688 cont_pkt
= qla2x00_prep_cont_type1_iocb(ha
);
689 cur_dsd
= (uint32_t *)cont_pkt
->dseg_0_address
;
693 sle_dma
= sg_dma_address(cur_seg
);
694 *cur_dsd
++ = cpu_to_le32(LSD(sle_dma
));
695 *cur_dsd
++ = cpu_to_le32(MSD(sle_dma
));
696 *cur_dsd
++ = cpu_to_le32(sg_dma_len(cur_seg
));
702 *cur_dsd
++ = cpu_to_le32(LSD(sp
->dma_handle
));
703 *cur_dsd
++ = cpu_to_le32(MSD(sp
->dma_handle
));
704 *cur_dsd
++ = cpu_to_le32(cmd
->request_bufflen
);
710 * qla24xx_start_scsi() - Send a SCSI command to the ISP
711 * @sp: command to send to the ISP
713 * Returns non-zero if a failure occured, else zero.
716 qla24xx_start_scsi(srb_t
*sp
)
721 struct scsi_cmnd
*cmd
;
725 struct cmd_type_7
*cmd_pkt
;
726 struct scatterlist
*sg
;
730 struct device_reg_24xx __iomem
*reg
;
732 /* Setup device pointers. */
735 reg
= &ha
->iobase
->isp24
;
737 /* So we know we haven't pci_map'ed anything yet */
740 /* Send marker if required */
741 if (ha
->marker_needed
!= 0) {
742 if (qla2x00_marker(ha
, 0, 0, MK_SYNC_ALL
) != QLA_SUCCESS
) {
743 return QLA_FUNCTION_FAILED
;
745 ha
->marker_needed
= 0;
748 /* Acquire ring specific lock */
749 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
751 /* Check for room in outstanding command list. */
752 handle
= ha
->current_outstanding_cmd
;
753 for (index
= 1; index
< MAX_OUTSTANDING_COMMANDS
; index
++) {
755 if (handle
== MAX_OUTSTANDING_COMMANDS
)
757 if (ha
->outstanding_cmds
[handle
] == 0)
760 if (index
== MAX_OUTSTANDING_COMMANDS
)
763 /* Map the sg table so we have an accurate count of sg entries needed */
765 sg
= (struct scatterlist
*) cmd
->request_buffer
;
766 tot_dsds
= pci_map_sg(ha
->pdev
, sg
, cmd
->use_sg
,
767 cmd
->sc_data_direction
);
770 } else if (cmd
->request_bufflen
) {
773 req_dma
= pci_map_single(ha
->pdev
, cmd
->request_buffer
,
774 cmd
->request_bufflen
, cmd
->sc_data_direction
);
775 if (dma_mapping_error(req_dma
))
778 sp
->dma_handle
= req_dma
;
782 req_cnt
= qla24xx_calc_iocbs(tot_dsds
);
783 if (ha
->req_q_cnt
< (req_cnt
+ 2)) {
784 cnt
= (uint16_t)RD_REG_DWORD_RELAXED(®
->req_q_out
);
785 if (ha
->req_ring_index
< cnt
)
786 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
788 ha
->req_q_cnt
= ha
->request_q_length
-
789 (ha
->req_ring_index
- cnt
);
791 if (ha
->req_q_cnt
< (req_cnt
+ 2))
794 /* Build command packet. */
795 ha
->current_outstanding_cmd
= handle
;
796 ha
->outstanding_cmds
[handle
] = sp
;
798 sp
->cmd
->host_scribble
= (unsigned char *)(unsigned long)handle
;
799 ha
->req_q_cnt
-= req_cnt
;
801 cmd_pkt
= (struct cmd_type_7
*)ha
->request_ring_ptr
;
802 cmd_pkt
->handle
= handle
;
804 /* Zero out remaining portion of packet. */
805 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
806 clr_ptr
= (uint32_t *)cmd_pkt
+ 2;
807 memset(clr_ptr
, 0, REQUEST_ENTRY_SIZE
- 8);
808 cmd_pkt
->dseg_count
= cpu_to_le16(tot_dsds
);
810 /* Set NPORT-ID and LUN number*/
811 cmd_pkt
->nport_handle
= cpu_to_le16(sp
->fcport
->loop_id
);
812 cmd_pkt
->port_id
[0] = sp
->fcport
->d_id
.b
.al_pa
;
813 cmd_pkt
->port_id
[1] = sp
->fcport
->d_id
.b
.area
;
814 cmd_pkt
->port_id
[2] = sp
->fcport
->d_id
.b
.domain
;
816 int_to_scsilun(sp
->cmd
->device
->lun
, &cmd_pkt
->lun
);
817 host_to_fcp_swap((uint8_t *)&cmd_pkt
->lun
, sizeof(cmd_pkt
->lun
));
819 /* Load SCSI command packet. */
820 memcpy(cmd_pkt
->fcp_cdb
, cmd
->cmnd
, cmd
->cmd_len
);
821 host_to_fcp_swap(cmd_pkt
->fcp_cdb
, sizeof(cmd_pkt
->fcp_cdb
));
823 cmd_pkt
->byte_count
= cpu_to_le32((uint32_t)cmd
->request_bufflen
);
825 /* Build IOCB segments */
826 qla24xx_build_scsi_iocbs(sp
, cmd_pkt
, tot_dsds
);
828 /* Set total data segment count. */
829 cmd_pkt
->entry_count
= (uint8_t)req_cnt
;
832 /* Adjust ring index. */
833 ha
->req_ring_index
++;
834 if (ha
->req_ring_index
== ha
->request_q_length
) {
835 ha
->req_ring_index
= 0;
836 ha
->request_ring_ptr
= ha
->request_ring
;
838 ha
->request_ring_ptr
++;
840 sp
->flags
|= SRB_DMA_VALID
;
842 /* Set chip new ring index. */
843 WRT_REG_DWORD(®
->req_q_in
, ha
->req_ring_index
);
844 RD_REG_DWORD_RELAXED(®
->req_q_in
); /* PCI Posting. */
846 /* Manage unprocessed RIO/ZIO commands in response queue. */
847 if (ha
->flags
.process_response_queue
&&
848 ha
->response_ring_ptr
->signature
!= RESPONSE_PROCESSED
)
849 qla24xx_process_response_queue(ha
);
851 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
855 if (cmd
->use_sg
&& tot_dsds
) {
856 sg
= (struct scatterlist
*) cmd
->request_buffer
;
857 pci_unmap_sg(ha
->pdev
, sg
, cmd
->use_sg
,
858 cmd
->sc_data_direction
);
859 } else if (tot_dsds
) {
860 pci_unmap_single(ha
->pdev
, sp
->dma_handle
,
861 cmd
->request_bufflen
, cmd
->sc_data_direction
);
863 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
865 return QLA_FUNCTION_FAILED
;