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);
20 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
23 * Returns the proper CF_* direction based on CDB.
25 static inline uint16_t
26 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
32 /* Set transfer direction */
33 if (cmd->sc_data_direction == DMA_TO_DEVICE)
35 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
41 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
42 * Continuation Type 0 IOCBs to allocate.
44 * @dsds: number of data segment decriptors needed
46 * Returns the number of IOCB entries needed to store @dsds.
49 qla2x00_calc_iocbs_32(uint16_t dsds)
55 iocbs += (dsds - 3) / 7;
63 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
64 * Continuation Type 1 IOCBs to allocate.
66 * @dsds: number of data segment decriptors needed
68 * Returns the number of IOCB entries needed to store @dsds.
71 qla2x00_calc_iocbs_64(uint16_t dsds)
77 iocbs += (dsds - 2) / 5;
85 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
88 * Returns a pointer to the Continuation Type 0 IOCB packet.
90 static inline cont_entry_t *
91 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
93 cont_entry_t *cont_pkt;
95 /* Adjust ring index. */
97 if (ha->req_ring_index == ha->request_q_length) {
98 ha->req_ring_index = 0;
99 ha->request_ring_ptr = ha->request_ring;
101 ha->request_ring_ptr++;
104 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
106 /* Load packet defaults. */
107 *((uint32_t *)(&cont_pkt->entry_type)) =
108 __constant_cpu_to_le32(CONTINUE_TYPE);
114 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
117 * Returns a pointer to the continuation type 1 IOCB packet.
119 static inline cont_a64_entry_t *
120 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
122 cont_a64_entry_t *cont_pkt;
124 /* Adjust ring index. */
125 ha->req_ring_index++;
126 if (ha->req_ring_index == ha->request_q_length) {
127 ha->req_ring_index = 0;
128 ha->request_ring_ptr = ha->request_ring;
130 ha->request_ring_ptr++;
133 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
135 /* Load packet defaults. */
136 *((uint32_t *)(&cont_pkt->entry_type)) =
137 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
143 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
144 * capable IOCB types.
146 * @sp: SRB command to process
147 * @cmd_pkt: Command type 2 IOCB
148 * @tot_dsds: Total number of segments to transfer
150 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
156 struct scsi_cmnd *cmd;
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 (cmd->request_bufflen == 0 || 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 if (cmd->use_sg != 0) {
180 struct scatterlist *cur_seg;
181 struct scatterlist *end_seg;
183 cur_seg = (struct scatterlist *)cmd->request_buffer;
184 end_seg = cur_seg + tot_dsds;
185 while (cur_seg < end_seg) {
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(ha);
195 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
199 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
200 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
206 *cur_dsd++ = cpu_to_le32(sp->dma_handle);
207 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
212 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
213 * capable IOCB types.
215 * @sp: SRB command to process
216 * @cmd_pkt: Command type 3 IOCB
217 * @tot_dsds: Total number of segments to transfer
219 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
225 struct scsi_cmnd *cmd;
229 /* Update entry type to indicate Command Type 3 IOCB */
230 *((uint32_t *)(&cmd_pkt->entry_type)) =
231 __constant_cpu_to_le32(COMMAND_A64_TYPE);
233 /* No data transfer */
234 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
235 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
241 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
243 /* Two DSDs are available in the Command Type 3 IOCB */
245 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
247 /* Load data segments */
248 if (cmd->use_sg != 0) {
249 struct scatterlist *cur_seg;
250 struct scatterlist *end_seg;
252 cur_seg = (struct scatterlist *)cmd->request_buffer;
253 end_seg = cur_seg + tot_dsds;
254 while (cur_seg < end_seg) {
256 cont_a64_entry_t *cont_pkt;
258 /* Allocate additional continuation packets? */
259 if (avail_dsds == 0) {
261 * Five DSDs are available in the Continuation
264 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
265 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
269 sle_dma = sg_dma_address(cur_seg);
270 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
271 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
272 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
278 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
279 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
280 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
285 * qla2x00_start_scsi() - Send a SCSI command to the ISP
286 * @sp: command to send to the ISP
288 * Returns non-zero if a failure occured, else zero.
291 qla2x00_start_scsi(srb_t *sp)
296 struct scsi_cmnd *cmd;
300 cmd_entry_t *cmd_pkt;
301 struct scatterlist *sg;
305 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);
391 if (scsi_populate_tag_msg(cmd, tag)) {
394 cmd_pkt->control_flags =
395 __constant_cpu_to_le16(CF_HEAD_TAG);
397 case MSG_ORDERED_TAG:
398 cmd_pkt->control_flags =
399 __constant_cpu_to_le16(CF_ORDERED_TAG);
404 /* Load SCSI command packet. */
405 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
406 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
408 /* Build IOCB segments */
409 ha->isp_ops.build_iocbs(sp, cmd_pkt, tot_dsds);
411 /* Set total data segment count. */
412 cmd_pkt->entry_count = (uint8_t)req_cnt;
415 /* Adjust ring index. */
416 ha->req_ring_index++;
417 if (ha->req_ring_index == ha->request_q_length) {
418 ha->req_ring_index = 0;
419 ha->request_ring_ptr = ha->request_ring;
421 ha->request_ring_ptr++;
423 sp->flags |= SRB_DMA_VALID;
424 sp->state = SRB_ACTIVE_STATE;
426 /* Set chip new ring index. */
427 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
428 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
430 /* Manage unprocessed RIO/ZIO commands in response queue. */
431 if (ha->flags.process_response_queue &&
432 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
433 qla2x00_process_response_queue(ha);
435 spin_unlock_irqrestore(&ha->hardware_lock, flags);
436 return (QLA_SUCCESS);
439 if (cmd->use_sg && tot_dsds) {
440 sg = (struct scatterlist *) cmd->request_buffer;
441 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
442 cmd->sc_data_direction);
443 } else if (tot_dsds) {
444 pci_unmap_single(ha->pdev, sp->dma_handle,
445 cmd->request_bufflen, cmd->sc_data_direction);
447 spin_unlock_irqrestore(&ha->hardware_lock, flags);
449 return (QLA_FUNCTION_FAILED);
453 * qla2x00_marker() - Send a marker IOCB to the firmware.
457 * @type: marker modifier
459 * Can be called from both normal and interrupt context.
461 * Returns non-zero if a failure occured, else zero.
464 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
468 struct mrk_entry_24xx *mrk24;
471 mrk = (mrk_entry_t *)qla2x00_req_pkt(ha);
473 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
474 __func__, ha->host_no));
476 return (QLA_FUNCTION_FAILED);
479 mrk->entry_type = MARKER_TYPE;
480 mrk->modifier = type;
481 if (type != MK_SYNC_ALL) {
482 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
483 mrk24 = (struct mrk_entry_24xx *) mrk;
484 mrk24->nport_handle = cpu_to_le16(loop_id);
485 mrk24->lun[1] = LSB(lun);
486 mrk24->lun[2] = MSB(lun);
488 SET_TARGET_ID(ha, mrk->target, loop_id);
489 mrk->lun = cpu_to_le16(lun);
496 return (QLA_SUCCESS);
500 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
504 unsigned long flags = 0;
506 spin_lock_irqsave(&ha->hardware_lock, flags);
507 ret = __qla2x00_marker(ha, loop_id, lun, type);
508 spin_unlock_irqrestore(&ha->hardware_lock, flags);
514 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
517 * Note: The caller must hold the hardware lock before calling this routine.
519 * Returns NULL if function failed, else, a pointer to the request packet.
522 qla2x00_req_pkt(scsi_qla_host_t *ha)
524 device_reg_t __iomem *reg = ha->iobase;
525 request_t *pkt = NULL;
529 uint16_t req_cnt = 1;
531 /* Wait 1 second for slot. */
532 for (timer = HZ; timer; timer--) {
533 if ((req_cnt + 2) >= ha->req_q_cnt) {
534 /* Calculate number of free request entries. */
535 if (IS_QLA24XX(ha) || IS_QLA25XX(ha))
536 cnt = (uint16_t)RD_REG_DWORD(
537 ®->isp24.req_q_out);
539 cnt = qla2x00_debounce_register(
540 ISP_REQ_Q_OUT(ha, ®->isp));
541 if (ha->req_ring_index < cnt)
542 ha->req_q_cnt = cnt - ha->req_ring_index;
544 ha->req_q_cnt = ha->request_q_length -
545 (ha->req_ring_index - cnt);
547 /* If room for request in request ring. */
548 if ((req_cnt + 2) < ha->req_q_cnt) {
550 pkt = ha->request_ring_ptr;
552 /* Zero out packet. */
553 dword_ptr = (uint32_t *)pkt;
554 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
557 /* Set system defined field. */
558 pkt->sys_define = (uint8_t)ha->req_ring_index;
560 /* Set entry count. */
561 pkt->entry_count = 1;
566 /* Release ring specific lock */
567 spin_unlock(&ha->hardware_lock);
569 udelay(2); /* 2 us */
571 /* Check for pending interrupts. */
572 /* During init we issue marker directly */
573 if (!ha->marker_needed)
576 spin_lock_irq(&ha->hardware_lock);
579 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
586 * qla2x00_isp_cmd() - Modify the request ring pointer.
589 * Note: The caller must hold the hardware lock before calling this routine.
592 qla2x00_isp_cmd(scsi_qla_host_t *ha)
594 device_reg_t __iomem *reg = ha->iobase;
596 DEBUG5(printk("%s(): IOCB data:\n", __func__));
597 DEBUG5(qla2x00_dump_buffer(
598 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
600 /* Adjust ring index. */
601 ha->req_ring_index++;
602 if (ha->req_ring_index == ha->request_q_length) {
603 ha->req_ring_index = 0;
604 ha->request_ring_ptr = ha->request_ring;
606 ha->request_ring_ptr++;
608 /* Set chip new ring index. */
609 if (IS_QLA24XX(ha) || IS_QLA25XX(ha)) {
610 WRT_REG_DWORD(®->isp24.req_q_in, ha->req_ring_index);
611 RD_REG_DWORD_RELAXED(®->isp24.req_q_in);
613 WRT_REG_WORD(ISP_REQ_Q_IN(ha, ®->isp), ha->req_ring_index);
614 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, ®->isp));
620 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
621 * Continuation Type 1 IOCBs to allocate.
623 * @dsds: number of data segment decriptors needed
625 * Returns the number of IOCB entries needed to store @dsds.
627 static inline uint16_t
628 qla24xx_calc_iocbs(uint16_t dsds)
634 iocbs += (dsds - 1) / 5;
642 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
645 * @sp: SRB command to process
646 * @cmd_pkt: Command type 3 IOCB
647 * @tot_dsds: Total number of segments to transfer
650 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
656 struct scsi_cmnd *cmd;
660 /* Update entry type to indicate Command Type 3 IOCB */
661 *((uint32_t *)(&cmd_pkt->entry_type)) =
662 __constant_cpu_to_le32(COMMAND_TYPE_7);
664 /* No data transfer */
665 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
666 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
672 /* Set transfer direction */
673 if (cmd->sc_data_direction == DMA_TO_DEVICE)
674 cmd_pkt->task_mgmt_flags =
675 __constant_cpu_to_le16(TMF_WRITE_DATA);
676 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
677 cmd_pkt->task_mgmt_flags =
678 __constant_cpu_to_le16(TMF_READ_DATA);
680 /* One DSD is available in the Command Type 3 IOCB */
682 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
684 /* Load data segments */
685 if (cmd->use_sg != 0) {
686 struct scatterlist *cur_seg;
687 struct scatterlist *end_seg;
689 cur_seg = (struct scatterlist *)cmd->request_buffer;
690 end_seg = cur_seg + tot_dsds;
691 while (cur_seg < end_seg) {
693 cont_a64_entry_t *cont_pkt;
695 /* Allocate additional continuation packets? */
696 if (avail_dsds == 0) {
698 * Five DSDs are available in the Continuation
701 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
702 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
706 sle_dma = sg_dma_address(cur_seg);
707 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
708 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
709 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
715 *cur_dsd++ = cpu_to_le32(LSD(sp->dma_handle));
716 *cur_dsd++ = cpu_to_le32(MSD(sp->dma_handle));
717 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
723 * qla24xx_start_scsi() - Send a SCSI command to the ISP
724 * @sp: command to send to the ISP
726 * Returns non-zero if a failure occured, else zero.
729 qla24xx_start_scsi(srb_t *sp)
734 struct scsi_cmnd *cmd;
738 struct cmd_type_7 *cmd_pkt;
739 struct scatterlist *sg;
743 struct device_reg_24xx __iomem *reg;
746 /* Setup device pointers. */
749 reg = &ha->iobase->isp24;
751 /* So we know we haven't pci_map'ed anything yet */
754 /* Send marker if required */
755 if (ha->marker_needed != 0) {
756 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
757 return QLA_FUNCTION_FAILED;
759 ha->marker_needed = 0;
762 /* Acquire ring specific lock */
763 spin_lock_irqsave(&ha->hardware_lock, flags);
765 /* Check for room in outstanding command list. */
766 handle = ha->current_outstanding_cmd;
767 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
769 if (handle == MAX_OUTSTANDING_COMMANDS)
771 if (ha->outstanding_cmds[handle] == 0)
774 if (index == MAX_OUTSTANDING_COMMANDS)
777 /* Map the sg table so we have an accurate count of sg entries needed */
779 sg = (struct scatterlist *) cmd->request_buffer;
780 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
781 cmd->sc_data_direction);
784 } else if (cmd->request_bufflen) {
787 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
788 cmd->request_bufflen, cmd->sc_data_direction);
789 if (dma_mapping_error(req_dma))
792 sp->dma_handle = req_dma;
796 req_cnt = qla24xx_calc_iocbs(tot_dsds);
797 if (ha->req_q_cnt < (req_cnt + 2)) {
798 cnt = (uint16_t)RD_REG_DWORD_RELAXED(®->req_q_out);
799 if (ha->req_ring_index < cnt)
800 ha->req_q_cnt = cnt - ha->req_ring_index;
802 ha->req_q_cnt = ha->request_q_length -
803 (ha->req_ring_index - cnt);
805 if (ha->req_q_cnt < (req_cnt + 2))
808 /* Build command packet. */
809 ha->current_outstanding_cmd = handle;
810 ha->outstanding_cmds[handle] = sp;
812 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
813 ha->req_q_cnt -= req_cnt;
815 cmd_pkt = (struct cmd_type_7 *)ha->request_ring_ptr;
816 cmd_pkt->handle = handle;
818 /* Zero out remaining portion of packet. */
819 clr_ptr = (uint32_t *)cmd_pkt + 2;
820 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
821 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
823 /* Set NPORT-ID and LUN number*/
824 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
825 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
826 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
827 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
829 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
831 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
832 if (scsi_populate_tag_msg(cmd, tag)) {
835 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
837 case MSG_ORDERED_TAG:
838 cmd_pkt->task = TSK_ORDERED;
843 /* Load SCSI command packet. */
844 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
845 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
847 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
849 /* Build IOCB segments */
850 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
852 /* Set total data segment count. */
853 cmd_pkt->entry_count = (uint8_t)req_cnt;
856 /* Adjust ring index. */
857 ha->req_ring_index++;
858 if (ha->req_ring_index == ha->request_q_length) {
859 ha->req_ring_index = 0;
860 ha->request_ring_ptr = ha->request_ring;
862 ha->request_ring_ptr++;
864 sp->flags |= SRB_DMA_VALID;
865 sp->state = SRB_ACTIVE_STATE;
867 /* Set chip new ring index. */
868 WRT_REG_DWORD(®->req_q_in, ha->req_ring_index);
869 RD_REG_DWORD_RELAXED(®->req_q_in); /* PCI Posting. */
871 /* Manage unprocessed RIO/ZIO commands in response queue. */
872 if (ha->flags.process_response_queue &&
873 ha->response_ring_ptr->signature != RESPONSE_PROCESSED)
874 qla24xx_process_response_queue(ha);
876 spin_unlock_irqrestore(&ha->hardware_lock, flags);
880 if (cmd->use_sg && tot_dsds) {
881 sg = (struct scatterlist *) cmd->request_buffer;
882 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
883 cmd->sc_data_direction);
884 } else if (tot_dsds) {
885 pci_unmap_single(ha->pdev, sp->dma_handle,
886 cmd->request_bufflen, cmd->sc_data_direction);
888 spin_unlock_irqrestore(&ha->hardware_lock, flags);
890 return QLA_FUNCTION_FAILED;