1 /******************************************************************************
2 * QLOGIC LINUX SOFTWARE
4 * QLogic ISP2x00 device driver for Linux 2.6.x
5 * Copyright (C) 2003-2004 QLogic Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2, or (at your option) any
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 ******************************************************************************/
22 #include <linux/blkdev.h>
23 #include <linux/delay.h>
25 #include <scsi/scsi_tcq.h>
27 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd
*cmd
);
28 static inline cont_entry_t
*qla2x00_prep_cont_type0_iocb(scsi_qla_host_t
*);
29 static inline cont_a64_entry_t
*qla2x00_prep_cont_type1_iocb(scsi_qla_host_t
*);
32 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
35 * Returns the proper CF_* direction based on CDB.
37 static inline uint16_t
38 qla2x00_get_cmd_direction(struct scsi_cmnd
*cmd
)
44 /* Set transfer direction */
45 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
47 else if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
53 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
54 * Continuation Type 0 IOCBs to allocate.
56 * @dsds: number of data segment decriptors needed
58 * Returns the number of IOCB entries needed to store @dsds.
61 qla2x00_calc_iocbs_32(uint16_t dsds
)
67 iocbs
+= (dsds
- 3) / 7;
75 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
76 * Continuation Type 1 IOCBs to allocate.
78 * @dsds: number of data segment decriptors needed
80 * Returns the number of IOCB entries needed to store @dsds.
83 qla2x00_calc_iocbs_64(uint16_t dsds
)
89 iocbs
+= (dsds
- 2) / 5;
97 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
100 * Returns a pointer to the Continuation Type 0 IOCB packet.
102 static inline cont_entry_t
*
103 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t
*ha
)
105 cont_entry_t
*cont_pkt
;
107 /* Adjust ring index. */
108 ha
->req_ring_index
++;
109 if (ha
->req_ring_index
== ha
->request_q_length
) {
110 ha
->req_ring_index
= 0;
111 ha
->request_ring_ptr
= ha
->request_ring
;
113 ha
->request_ring_ptr
++;
116 cont_pkt
= (cont_entry_t
*)ha
->request_ring_ptr
;
118 /* Load packet defaults. */
119 *((uint32_t *)(&cont_pkt
->entry_type
)) =
120 __constant_cpu_to_le32(CONTINUE_TYPE
);
126 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
129 * Returns a pointer to the continuation type 1 IOCB packet.
131 static inline cont_a64_entry_t
*
132 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t
*ha
)
134 cont_a64_entry_t
*cont_pkt
;
136 /* Adjust ring index. */
137 ha
->req_ring_index
++;
138 if (ha
->req_ring_index
== ha
->request_q_length
) {
139 ha
->req_ring_index
= 0;
140 ha
->request_ring_ptr
= ha
->request_ring
;
142 ha
->request_ring_ptr
++;
145 cont_pkt
= (cont_a64_entry_t
*)ha
->request_ring_ptr
;
147 /* Load packet defaults. */
148 *((uint32_t *)(&cont_pkt
->entry_type
)) =
149 __constant_cpu_to_le32(CONTINUE_A64_TYPE
);
155 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
156 * capable IOCB types.
158 * @sp: SRB command to process
159 * @cmd_pkt: Command type 2 IOCB
160 * @tot_dsds: Total number of segments to transfer
162 void qla2x00_build_scsi_iocbs_32(srb_t
*sp
, cmd_entry_t
*cmd_pkt
,
168 struct scsi_cmnd
*cmd
;
172 /* Update entry type to indicate Command Type 2 IOCB */
173 *((uint32_t *)(&cmd_pkt
->entry_type
)) =
174 __constant_cpu_to_le32(COMMAND_TYPE
);
176 /* No data transfer */
177 if (cmd
->request_bufflen
== 0 || cmd
->sc_data_direction
== DMA_NONE
) {
178 cmd_pkt
->byte_count
= __constant_cpu_to_le32(0);
184 cmd_pkt
->control_flags
|= cpu_to_le16(qla2x00_get_cmd_direction(cmd
));
186 /* Three DSDs are available in the Command Type 2 IOCB */
188 cur_dsd
= (uint32_t *)&cmd_pkt
->dseg_0_address
;
190 /* Load data segments */
191 if (cmd
->use_sg
!= 0) {
192 struct scatterlist
*cur_seg
;
193 struct scatterlist
*end_seg
;
195 cur_seg
= (struct scatterlist
*)cmd
->request_buffer
;
196 end_seg
= cur_seg
+ tot_dsds
;
197 while (cur_seg
< end_seg
) {
198 cont_entry_t
*cont_pkt
;
200 /* Allocate additional continuation packets? */
201 if (avail_dsds
== 0) {
203 * Seven DSDs are available in the Continuation
206 cont_pkt
= qla2x00_prep_cont_type0_iocb(ha
);
207 cur_dsd
= (uint32_t *)&cont_pkt
->dseg_0_address
;
211 *cur_dsd
++ = cpu_to_le32(sg_dma_address(cur_seg
));
212 *cur_dsd
++ = cpu_to_le32(sg_dma_len(cur_seg
));
220 unsigned long offset
;
222 page
= virt_to_page(cmd
->request_buffer
);
223 offset
= ((unsigned long)cmd
->request_buffer
& ~PAGE_MASK
);
224 req_dma
= pci_map_page(ha
->pdev
, page
, offset
,
225 cmd
->request_bufflen
, cmd
->sc_data_direction
);
227 sp
->dma_handle
= req_dma
;
229 *cur_dsd
++ = cpu_to_le32(req_dma
);
230 *cur_dsd
++ = cpu_to_le32(cmd
->request_bufflen
);
235 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
236 * capable IOCB types.
238 * @sp: SRB command to process
239 * @cmd_pkt: Command type 3 IOCB
240 * @tot_dsds: Total number of segments to transfer
242 void qla2x00_build_scsi_iocbs_64(srb_t
*sp
, cmd_entry_t
*cmd_pkt
,
248 struct scsi_cmnd
*cmd
;
252 /* Update entry type to indicate Command Type 3 IOCB */
253 *((uint32_t *)(&cmd_pkt
->entry_type
)) =
254 __constant_cpu_to_le32(COMMAND_A64_TYPE
);
256 /* No data transfer */
257 if (cmd
->request_bufflen
== 0 || cmd
->sc_data_direction
== DMA_NONE
) {
258 cmd_pkt
->byte_count
= __constant_cpu_to_le32(0);
264 cmd_pkt
->control_flags
|= cpu_to_le16(qla2x00_get_cmd_direction(cmd
));
266 /* Two DSDs are available in the Command Type 3 IOCB */
268 cur_dsd
= (uint32_t *)&cmd_pkt
->dseg_0_address
;
270 /* Load data segments */
271 if (cmd
->use_sg
!= 0) {
272 struct scatterlist
*cur_seg
;
273 struct scatterlist
*end_seg
;
275 cur_seg
= (struct scatterlist
*)cmd
->request_buffer
;
276 end_seg
= cur_seg
+ tot_dsds
;
277 while (cur_seg
< end_seg
) {
279 cont_a64_entry_t
*cont_pkt
;
281 /* Allocate additional continuation packets? */
282 if (avail_dsds
== 0) {
284 * Five DSDs are available in the Continuation
287 cont_pkt
= qla2x00_prep_cont_type1_iocb(ha
);
288 cur_dsd
= (uint32_t *)cont_pkt
->dseg_0_address
;
292 sle_dma
= sg_dma_address(cur_seg
);
293 *cur_dsd
++ = cpu_to_le32(LSD(sle_dma
));
294 *cur_dsd
++ = cpu_to_le32(MSD(sle_dma
));
295 *cur_dsd
++ = cpu_to_le32(sg_dma_len(cur_seg
));
303 unsigned long offset
;
305 page
= virt_to_page(cmd
->request_buffer
);
306 offset
= ((unsigned long)cmd
->request_buffer
& ~PAGE_MASK
);
307 req_dma
= pci_map_page(ha
->pdev
, page
, offset
,
308 cmd
->request_bufflen
, cmd
->sc_data_direction
);
310 sp
->dma_handle
= req_dma
;
312 *cur_dsd
++ = cpu_to_le32(LSD(req_dma
));
313 *cur_dsd
++ = cpu_to_le32(MSD(req_dma
));
314 *cur_dsd
++ = cpu_to_le32(cmd
->request_bufflen
);
319 * qla2x00_start_scsi() - Send a SCSI command to the ISP
320 * @sp: command to send to the ISP
322 * Returns non-zero if a failure occured, else zero.
325 qla2x00_start_scsi(srb_t
*sp
)
331 struct scsi_cmnd
*cmd
;
335 cmd_entry_t
*cmd_pkt
;
337 struct scatterlist
*sg
;
344 /* Setup device pointers. */
346 fclun
= sp
->lun_queue
->fclun
;
347 ha
= fclun
->fcport
->ha
;
351 /* Send marker if required */
352 if (ha
->marker_needed
!= 0) {
353 if (qla2x00_marker(ha
, 0, 0, MK_SYNC_ALL
) != QLA_SUCCESS
) {
354 return (QLA_FUNCTION_FAILED
);
356 ha
->marker_needed
= 0;
359 /* Acquire ring specific lock */
360 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
362 /* Check for room in outstanding command list. */
363 handle
= ha
->current_outstanding_cmd
;
364 for (index
= 1; index
< MAX_OUTSTANDING_COMMANDS
; index
++) {
366 if (handle
== MAX_OUTSTANDING_COMMANDS
)
368 if (ha
->outstanding_cmds
[handle
] == 0)
371 if (index
== MAX_OUTSTANDING_COMMANDS
)
374 /* Calculate the number of request entries needed. */
375 req_cnt
= (ha
->calc_request_entries
)(cmd
->request
->nr_hw_segments
);
376 if (ha
->req_q_cnt
< (req_cnt
+ 2)) {
377 cnt
= RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha
, reg
));
378 if (ha
->req_ring_index
< cnt
)
379 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
381 ha
->req_q_cnt
= ha
->request_q_length
-
382 (ha
->req_ring_index
- cnt
);
384 if (ha
->req_q_cnt
< (req_cnt
+ 2))
387 /* Finally, we have enough space, now perform mappings. */
390 sg
= (struct scatterlist
*) cmd
->request_buffer
;
391 tot_dsds
= pci_map_sg(ha
->pdev
, sg
, cmd
->use_sg
,
392 cmd
->sc_data_direction
);
395 } else if (cmd
->request_bufflen
) {
398 req_cnt
= (ha
->calc_request_entries
)(tot_dsds
);
400 /* Build command packet */
401 ha
->current_outstanding_cmd
= handle
;
402 ha
->outstanding_cmds
[handle
] = sp
;
404 sp
->cmd
->host_scribble
= (unsigned char *)(unsigned long)handle
;
405 ha
->req_q_cnt
-= req_cnt
;
407 cmd_pkt
= (cmd_entry_t
*)ha
->request_ring_ptr
;
408 cmd_pkt
->handle
= handle
;
409 /* Zero out remaining portion of packet. */
410 clr_ptr
= (uint32_t *)cmd_pkt
+ 2;
411 memset(clr_ptr
, 0, REQUEST_ENTRY_SIZE
- 8);
412 cmd_pkt
->dseg_count
= cpu_to_le16(tot_dsds
);
415 SET_TARGET_ID(ha
, cmd_pkt
->target
, fclun
->fcport
->loop_id
);
418 cmd_pkt
->lun
= cpu_to_le16(fclun
->lun
);
420 /* Update tagged queuing modifier */
421 if (scsi_populate_tag_msg(cmd
, tag
)) {
424 cmd_pkt
->control_flags
=
425 __constant_cpu_to_le16(CF_SIMPLE_TAG
);
428 cmd_pkt
->control_flags
=
429 __constant_cpu_to_le16(CF_HEAD_TAG
);
431 case MSG_ORDERED_TAG
:
432 cmd_pkt
->control_flags
=
433 __constant_cpu_to_le16(CF_ORDERED_TAG
);
439 * Allocate at least 5 (+ QLA_CMD_TIMER_DELTA) seconds for RISC timeout.
441 timeout
= (uint32_t)(cmd
->timeout_per_command
/ HZ
);
443 cmd_pkt
->timeout
= __constant_cpu_to_le16(0);
444 else if (timeout
> 25)
445 cmd_pkt
->timeout
= cpu_to_le16((uint16_t)timeout
-
446 (5 + QLA_CMD_TIMER_DELTA
));
448 cmd_pkt
->timeout
= cpu_to_le16((uint16_t)timeout
);
450 /* Load SCSI command packet. */
451 memcpy(cmd_pkt
->scsi_cdb
, cmd
->cmnd
, cmd
->cmd_len
);
452 cmd_pkt
->byte_count
= cpu_to_le32((uint32_t)cmd
->request_bufflen
);
454 /* Build IOCB segments */
455 (ha
->build_scsi_iocbs
)(sp
, cmd_pkt
, tot_dsds
);
457 /* Set total data segment count. */
458 cmd_pkt
->entry_count
= (uint8_t)req_cnt
;
461 /* Adjust ring index. */
462 ha
->req_ring_index
++;
463 if (ha
->req_ring_index
== ha
->request_q_length
) {
464 ha
->req_ring_index
= 0;
465 ha
->request_ring_ptr
= ha
->request_ring
;
467 ha
->request_ring_ptr
++;
471 sp
->lun_queue
->out_cnt
++;
472 sp
->flags
|= SRB_DMA_VALID
;
473 sp
->state
= SRB_ACTIVE_STATE
;
474 sp
->u_start
= jiffies
;
476 /* Set chip new ring index. */
477 WRT_REG_WORD(ISP_REQ_Q_IN(ha
, reg
), ha
->req_ring_index
);
478 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha
, reg
)); /* PCI Posting. */
480 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
481 return (QLA_SUCCESS
);
484 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
486 return (QLA_FUNCTION_FAILED
);
490 * qla2x00_marker() - Send a marker IOCB to the firmware.
494 * @type: marker modifier
496 * Can be called from both normal and interrupt context.
498 * Returns non-zero if a failure occured, else zero.
501 __qla2x00_marker(scsi_qla_host_t
*ha
, uint16_t loop_id
, uint16_t lun
,
506 pkt
= (mrk_entry_t
*)qla2x00_req_pkt(ha
);
508 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__
));
510 return (QLA_FUNCTION_FAILED
);
513 pkt
->entry_type
= MARKER_TYPE
;
514 pkt
->modifier
= type
;
516 if (type
!= MK_SYNC_ALL
) {
517 pkt
->lun
= cpu_to_le16(lun
);
518 SET_TARGET_ID(ha
, pkt
->target
, loop_id
);
522 /* Issue command to ISP */
525 return (QLA_SUCCESS
);
529 qla2x00_marker(scsi_qla_host_t
*ha
, uint16_t loop_id
, uint16_t lun
,
533 unsigned long flags
= 0;
535 spin_lock_irqsave(&ha
->hardware_lock
, flags
);
536 ret
= __qla2x00_marker(ha
, loop_id
, lun
, type
);
537 spin_unlock_irqrestore(&ha
->hardware_lock
, flags
);
543 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
546 * Note: The caller must hold the hardware lock before calling this routine.
548 * Returns NULL if function failed, else, a pointer to the request packet.
551 qla2x00_req_pkt(scsi_qla_host_t
*ha
)
553 device_reg_t
*reg
= ha
->iobase
;
554 request_t
*pkt
= NULL
;
558 uint16_t req_cnt
= 1;
560 /* Wait 1 second for slot. */
561 for (timer
= HZ
; timer
; timer
--) {
562 if ((req_cnt
+ 2) >= ha
->req_q_cnt
) {
563 /* Calculate number of free request entries. */
564 cnt
= qla2x00_debounce_register(ISP_REQ_Q_OUT(ha
, reg
));
565 if (ha
->req_ring_index
< cnt
)
566 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
568 ha
->req_q_cnt
= ha
->request_q_length
-
569 (ha
->req_ring_index
- cnt
);
571 /* If room for request in request ring. */
572 if ((req_cnt
+ 2) < ha
->req_q_cnt
) {
574 pkt
= ha
->request_ring_ptr
;
576 /* Zero out packet. */
577 dword_ptr
= (uint32_t *)pkt
;
578 for (cnt
= 0; cnt
< REQUEST_ENTRY_SIZE
/ 4; cnt
++)
581 /* Set system defined field. */
582 pkt
->sys_define
= (uint8_t)ha
->req_ring_index
;
584 /* Set entry count. */
585 pkt
->entry_count
= 1;
590 /* Release ring specific lock */
591 spin_unlock(&ha
->hardware_lock
);
593 udelay(2); /* 2 us */
595 /* Check for pending interrupts. */
596 /* During init we issue marker directly */
597 if (!ha
->marker_needed
)
600 spin_lock_irq(&ha
->hardware_lock
);
603 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__
));
610 * qla2x00_ms_req_pkt() - Retrieve a Management Server request packet from
613 * @sp: pointer to handle post function call
615 * Note: The caller must hold the hardware lock before calling this routine.
617 * Returns NULL if function failed, else, a pointer to the request packet.
620 qla2x00_ms_req_pkt(scsi_qla_host_t
*ha
, srb_t
*sp
)
622 device_reg_t
*reg
= ha
->iobase
;
623 request_t
*pkt
= NULL
;
624 uint16_t cnt
, i
, index
;
628 uint16_t req_cnt
= 1;
630 /* Wait 1 second for slot. */
631 for (timer
= HZ
; timer
; timer
--) {
632 if ((req_cnt
+ 2) >= ha
->req_q_cnt
) {
633 /* Calculate number of free request entries. */
634 cnt
= qla2x00_debounce_register(ISP_REQ_Q_OUT(ha
, reg
));
635 if (ha
->req_ring_index
< cnt
) {
636 ha
->req_q_cnt
= cnt
- ha
->req_ring_index
;
638 ha
->req_q_cnt
= ha
->request_q_length
-
639 (ha
->req_ring_index
- cnt
);
643 /* Check for room in outstanding command list. */
644 cnt
= ha
->current_outstanding_cmd
;
645 for (index
= 1; index
< MAX_OUTSTANDING_COMMANDS
; index
++) {
647 if (cnt
== MAX_OUTSTANDING_COMMANDS
)
650 if (ha
->outstanding_cmds
[cnt
] == 0) {
652 ha
->current_outstanding_cmd
= cnt
;
657 /* If room for request in request ring. */
658 if (found
&& (req_cnt
+ 2) < ha
->req_q_cnt
) {
659 pkt
= ha
->request_ring_ptr
;
661 /* Zero out packet. */
662 dword_ptr
= (uint32_t *)pkt
;
663 for (i
= 0; i
< REQUEST_ENTRY_SIZE
/ 4; i
++ )
666 DEBUG5(printk("%s(): putting sp=%p in "
667 "outstanding_cmds[%x]\n",
671 ha
->outstanding_cmds
[cnt
] = sp
;
673 /* save the handle */
674 sp
->cmd
->host_scribble
= (unsigned char *) (u_long
) cnt
;
675 CMD_SP(sp
->cmd
) = (void *)sp
;
678 pkt
->handle
= (uint32_t)cnt
;
680 /* Set system defined field. */
681 pkt
->sys_define
= (uint8_t)ha
->req_ring_index
;
682 pkt
->entry_status
= 0;
687 /* Release ring specific lock */
688 spin_unlock(&ha
->hardware_lock
);
691 /* Check for pending interrupts. */
694 spin_lock_irq(&ha
->hardware_lock
);
697 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__
));
704 * qla2x00_isp_cmd() - Modify the request ring pointer.
707 * Note: The caller must hold the hardware lock before calling this routine.
710 qla2x00_isp_cmd(scsi_qla_host_t
*ha
)
712 device_reg_t
*reg
= ha
->iobase
;
714 DEBUG5(printk("%s(): IOCB data:\n", __func__
));
715 DEBUG5(qla2x00_dump_buffer(
716 (uint8_t *)ha
->request_ring_ptr
, REQUEST_ENTRY_SIZE
));
718 /* Adjust ring index. */
719 ha
->req_ring_index
++;
720 if (ha
->req_ring_index
== ha
->request_q_length
) {
721 ha
->req_ring_index
= 0;
722 ha
->request_ring_ptr
= ha
->request_ring
;
724 ha
->request_ring_ptr
++;
726 /* Set chip new ring index. */
727 WRT_REG_WORD(ISP_REQ_Q_IN(ha
, reg
), ha
->req_ring_index
);
728 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha
, reg
)); /* PCI Posting. */