[SCSI] qla2xxx: Re-factor isp_operations to static structures.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / qla2xxx / qla_iocb.c
blob3a5e78cb6b3f62c156ba0133a06fdbf00cb6ad35
1 /*
2 * QLogic Fibre Channel HBA Driver
3 * Copyright (c) 2003-2005 QLogic Corporation
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
7 #include "qla_def.h"
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);
20 /**
21 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
22 * @cmd: SCSI command
24 * Returns the proper CF_* direction based on CDB.
26 static inline uint16_t
27 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
29 uint16_t cflags;
31 cflags = 0;
33 /* Set transfer direction */
34 if (cmd->sc_data_direction == DMA_TO_DEVICE)
35 cflags = CF_WRITE;
36 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
37 cflags = CF_READ;
38 return (cflags);
41 /**
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.
49 uint16_t
50 qla2x00_calc_iocbs_32(uint16_t dsds)
52 uint16_t iocbs;
54 iocbs = 1;
55 if (dsds > 3) {
56 iocbs += (dsds - 3) / 7;
57 if ((dsds - 3) % 7)
58 iocbs++;
60 return (iocbs);
63 /**
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.
71 uint16_t
72 qla2x00_calc_iocbs_64(uint16_t dsds)
74 uint16_t iocbs;
76 iocbs = 1;
77 if (dsds > 2) {
78 iocbs += (dsds - 2) / 5;
79 if ((dsds - 2) % 5)
80 iocbs++;
82 return (iocbs);
85 /**
86 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
87 * @ha: HA context
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. */
97 ha->req_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;
101 } else {
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);
111 return (cont_pkt);
115 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
116 * @ha: HA context
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;
130 } else {
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);
140 return (cont_pkt);
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,
152 uint16_t tot_dsds)
154 uint16_t avail_dsds;
155 uint32_t *cur_dsd;
156 scsi_qla_host_t *ha;
157 struct scsi_cmnd *cmd;
158 struct scatterlist *sg;
159 int i;
161 cmd = sp->cmd;
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);
170 return;
173 ha = sp->ha;
175 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
177 /* Three DSDs are available in the Command Type 2 IOCB */
178 avail_dsds = 3;
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
189 * Type 0 IOCB.
191 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
192 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
193 avail_dsds = 7;
196 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
197 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
198 avail_dsds--;
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,
211 uint16_t tot_dsds)
213 uint16_t avail_dsds;
214 uint32_t *cur_dsd;
215 scsi_qla_host_t *ha;
216 struct scsi_cmnd *cmd;
217 struct scatterlist *sg;
218 int i;
220 cmd = sp->cmd;
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);
229 return;
232 ha = sp->ha;
234 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
236 /* Two DSDs are available in the Command Type 3 IOCB */
237 avail_dsds = 2;
238 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
240 /* Load data segments */
241 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
242 dma_addr_t sle_dma;
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
249 * Type 1 IOCB.
251 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
252 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
253 avail_dsds = 5;
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));
260 avail_dsds--;
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)
273 int ret, nseg;
274 unsigned long flags;
275 scsi_qla_host_t *ha;
276 struct scsi_cmnd *cmd;
277 uint32_t *clr_ptr;
278 uint32_t index;
279 uint32_t handle;
280 cmd_entry_t *cmd_pkt;
281 uint16_t cnt;
282 uint16_t req_cnt;
283 uint16_t tot_dsds;
284 struct device_reg_2xxx __iomem *reg;
286 /* Setup device pointers. */
287 ret = 0;
288 ha = sp->ha;
289 reg = &ha->iobase->isp;
290 cmd = sp->cmd;
291 /* So we know we haven't pci_map'ed anything yet */
292 tot_dsds = 0;
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++) {
308 handle++;
309 if (handle == MAX_OUTSTANDING_COMMANDS)
310 handle = 1;
311 if (ha->outstanding_cmds[handle] == 0)
312 break;
314 if (index == MAX_OUTSTANDING_COMMANDS)
315 goto queuing_error;
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);
321 if (unlikely(!nseg))
322 goto queuing_error;
323 } else
324 nseg = 0;
326 tot_dsds = nseg;
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;
334 else
335 ha->req_q_cnt = ha->request_q_length -
336 (ha->req_ring_index - cnt);
338 if (ha->req_q_cnt < (req_cnt + 2))
339 goto queuing_error;
341 /* Build command packet */
342 ha->current_outstanding_cmd = handle;
343 ha->outstanding_cmds[handle] = sp;
344 sp->ha = ha;
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;
371 wmb();
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;
378 } else
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);
395 queuing_error:
396 if (tot_dsds)
397 scsi_dma_unmap(cmd);
399 spin_unlock_irqrestore(&ha->hardware_lock, flags);
401 return (QLA_FUNCTION_FAILED);
405 * qla2x00_marker() - Send a marker IOCB to the firmware.
406 * @ha: HA context
407 * @loop_id: loop ID
408 * @lun: LUN
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,
417 uint8_t type)
419 mrk_entry_t *mrk;
420 struct mrk_entry_24xx *mrk24;
421 scsi_qla_host_t *pha = to_qla_parent(ha);
423 mrk24 = NULL;
424 mrk = (mrk_entry_t *)qla2x00_req_pkt(pha);
425 if (mrk == NULL) {
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_FWI2_CAPABLE(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;
442 } else {
443 SET_TARGET_ID(ha, mrk->target, loop_id);
444 mrk->lun = cpu_to_le16(lun);
447 wmb();
449 qla2x00_isp_cmd(pha);
451 return (QLA_SUCCESS);
455 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
456 uint8_t type)
458 int ret;
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);
465 return (ret);
469 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
470 * @ha: HA context
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.
476 static request_t *
477 qla2x00_req_pkt(scsi_qla_host_t *ha)
479 device_reg_t __iomem *reg = ha->iobase;
480 request_t *pkt = NULL;
481 uint16_t cnt;
482 uint32_t *dword_ptr;
483 uint32_t timer;
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_FWI2_CAPABLE(ha))
491 cnt = (uint16_t)RD_REG_DWORD(
492 &reg->isp24.req_q_out);
493 else
494 cnt = qla2x00_debounce_register(
495 ISP_REQ_Q_OUT(ha, &reg->isp));
496 if (ha->req_ring_index < cnt)
497 ha->req_q_cnt = cnt - ha->req_ring_index;
498 else
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) {
504 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++)
510 *dword_ptr++ = 0;
512 /* Set system defined field. */
513 pkt->sys_define = (uint8_t)ha->req_ring_index;
515 /* Set entry count. */
516 pkt->entry_count = 1;
518 break;
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)
529 qla2x00_poll(ha);
531 spin_lock_irq(&ha->hardware_lock);
533 if (!pkt) {
534 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
537 return (pkt);
541 * qla2x00_isp_cmd() - Modify the request ring pointer.
542 * @ha: HA context
544 * Note: The caller must hold the hardware lock before calling this routine.
546 static void
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;
560 } else
561 ha->request_ring_ptr++;
563 /* Set chip new ring index. */
564 if (IS_FWI2_CAPABLE(ha)) {
565 WRT_REG_DWORD(&reg->isp24.req_q_in, ha->req_ring_index);
566 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
567 } else {
568 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), ha->req_ring_index);
569 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->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)
585 uint16_t iocbs;
587 iocbs = 1;
588 if (dsds > 1) {
589 iocbs += (dsds - 1) / 5;
590 if ((dsds - 1) % 5)
591 iocbs++;
593 return iocbs;
597 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
598 * IOCB types.
600 * @sp: SRB command to process
601 * @cmd_pkt: Command type 3 IOCB
602 * @tot_dsds: Total number of segments to transfer
604 static inline void
605 qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
606 uint16_t tot_dsds)
608 uint16_t avail_dsds;
609 uint32_t *cur_dsd;
610 scsi_qla_host_t *ha;
611 struct scsi_cmnd *cmd;
612 struct scatterlist *sg;
613 int i;
615 cmd = sp->cmd;
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);
624 return;
627 ha = sp->ha;
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 */
638 avail_dsds = 1;
639 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
641 /* Load data segments */
643 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
644 dma_addr_t sle_dma;
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
651 * Type 1 IOCB.
653 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
654 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
655 avail_dsds = 5;
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));
662 avail_dsds--;
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)
676 int ret, nseg;
677 unsigned long flags;
678 scsi_qla_host_t *ha;
679 struct scsi_cmnd *cmd;
680 uint32_t *clr_ptr;
681 uint32_t index;
682 uint32_t handle;
683 struct cmd_type_7 *cmd_pkt;
684 uint16_t cnt;
685 uint16_t req_cnt;
686 uint16_t tot_dsds;
687 struct device_reg_24xx __iomem *reg;
689 /* Setup device pointers. */
690 ret = 0;
691 ha = sp->ha;
692 reg = &ha->iobase->isp24;
693 cmd = sp->cmd;
694 /* So we know we haven't pci_map'ed anything yet */
695 tot_dsds = 0;
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++) {
711 handle++;
712 if (handle == MAX_OUTSTANDING_COMMANDS)
713 handle = 1;
714 if (ha->outstanding_cmds[handle] == 0)
715 break;
717 if (index == MAX_OUTSTANDING_COMMANDS)
718 goto queuing_error;
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);
724 if (unlikely(!nseg))
725 goto queuing_error;
726 } else
727 nseg = 0;
729 tot_dsds = nseg;
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(&reg->req_q_out);
734 if (ha->req_ring_index < cnt)
735 ha->req_q_cnt = cnt - ha->req_ring_index;
736 else
737 ha->req_q_cnt = ha->request_q_length -
738 (ha->req_ring_index - cnt);
740 if (ha->req_q_cnt < (req_cnt + 2))
741 goto queuing_error;
743 /* Build command packet. */
744 ha->current_outstanding_cmd = handle;
745 ha->outstanding_cmds[handle] = sp;
746 sp->ha = ha;
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;
780 wmb();
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;
787 } else
788 ha->request_ring_ptr++;
790 sp->flags |= SRB_DMA_VALID;
792 /* Set chip new ring index. */
793 WRT_REG_DWORD(&reg->req_q_in, ha->req_ring_index);
794 RD_REG_DWORD_RELAXED(&reg->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);
802 return QLA_SUCCESS;
804 queuing_error:
805 if (tot_dsds)
806 scsi_dma_unmap(cmd);
808 spin_unlock_irqrestore(&ha->hardware_lock, flags);
810 return QLA_FUNCTION_FAILED;