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 ******************************************************************************/
23 static inline uint16_t qla2x00_get_cmd_direction(struct scsi_cmnd *cmd);
24 static inline cont_entry_t *qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *);
25 static inline cont_a64_entry_t *qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *);
28 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
31 * Returns the proper CF_* direction based on CDB.
33 static inline uint16_t
34 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
40 /* Set transfer direction */
41 if (cmd->sc_data_direction == DMA_TO_DEVICE)
43 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
46 switch (cmd->data_cmnd[0]) {
71 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
72 * Continuation Type 0 IOCBs to allocate.
74 * @dsds: number of data segment decriptors needed
76 * Returns the number of IOCB entries needed to store @dsds.
79 qla2x00_calc_iocbs_32(uint16_t dsds)
85 iocbs += (dsds - 3) / 7;
93 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
94 * Continuation Type 1 IOCBs to allocate.
96 * @dsds: number of data segment decriptors needed
98 * Returns the number of IOCB entries needed to store @dsds.
101 qla2x00_calc_iocbs_64(uint16_t dsds)
107 iocbs += (dsds - 2) / 5;
115 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
118 * Returns a pointer to the Continuation Type 0 IOCB packet.
120 static inline cont_entry_t *
121 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
123 cont_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_entry_t *)ha->request_ring_ptr;
136 /* Load packet defaults. */
137 *((uint32_t *)(&cont_pkt->entry_type)) =
138 __constant_cpu_to_le32(CONTINUE_TYPE);
144 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
147 * Returns a pointer to the continuation type 1 IOCB packet.
149 static inline cont_a64_entry_t *
150 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
152 cont_a64_entry_t *cont_pkt;
154 /* Adjust ring index. */
155 ha->req_ring_index++;
156 if (ha->req_ring_index == ha->request_q_length) {
157 ha->req_ring_index = 0;
158 ha->request_ring_ptr = ha->request_ring;
160 ha->request_ring_ptr++;
163 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
165 /* Load packet defaults. */
166 *((uint32_t *)(&cont_pkt->entry_type)) =
167 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
173 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
174 * capable IOCB types.
176 * @sp: SRB command to process
177 * @cmd_pkt: Command type 2 IOCB
178 * @tot_dsds: Total number of segments to transfer
180 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
186 struct scsi_cmnd *cmd;
190 /* Update entry type to indicate Command Type 2 IOCB */
191 *((uint32_t *)(&cmd_pkt->entry_type)) =
192 __constant_cpu_to_le32(COMMAND_TYPE);
194 /* No data transfer */
195 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
196 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
202 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
204 /* Three DSDs are available in the Command Type 2 IOCB */
206 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
208 /* Load data segments */
209 if (cmd->use_sg != 0) {
210 struct scatterlist *cur_seg;
211 struct scatterlist *end_seg;
213 cur_seg = (struct scatterlist *)cmd->request_buffer;
214 end_seg = cur_seg + tot_dsds;
215 while (cur_seg < end_seg) {
216 cont_entry_t *cont_pkt;
218 /* Allocate additional continuation packets? */
219 if (avail_dsds == 0) {
221 * Seven DSDs are available in the Continuation
224 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
225 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
229 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
230 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
238 unsigned long offset;
240 page = virt_to_page(cmd->request_buffer);
241 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
242 req_dma = pci_map_page(ha->pdev, page, offset,
243 cmd->request_bufflen, cmd->sc_data_direction);
245 sp->dma_handle = req_dma;
247 *cur_dsd++ = cpu_to_le32(req_dma);
248 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
253 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
254 * capable IOCB types.
256 * @sp: SRB command to process
257 * @cmd_pkt: Command type 3 IOCB
258 * @tot_dsds: Total number of segments to transfer
260 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
266 struct scsi_cmnd *cmd;
270 /* Update entry type to indicate Command Type 3 IOCB */
271 *((uint32_t *)(&cmd_pkt->entry_type)) =
272 __constant_cpu_to_le32(COMMAND_A64_TYPE);
274 /* No data transfer */
275 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
276 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
282 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
284 /* Two DSDs are available in the Command Type 3 IOCB */
286 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
288 /* Load data segments */
289 if (cmd->use_sg != 0) {
290 struct scatterlist *cur_seg;
291 struct scatterlist *end_seg;
293 cur_seg = (struct scatterlist *)cmd->request_buffer;
294 end_seg = cur_seg + tot_dsds;
295 while (cur_seg < end_seg) {
297 cont_a64_entry_t *cont_pkt;
299 /* Allocate additional continuation packets? */
300 if (avail_dsds == 0) {
302 * Five DSDs are available in the Continuation
305 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
306 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
310 sle_dma = sg_dma_address(cur_seg);
311 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
312 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
313 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
321 unsigned long offset;
323 page = virt_to_page(cmd->request_buffer);
324 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
325 req_dma = pci_map_page(ha->pdev, page, offset,
326 cmd->request_bufflen, cmd->sc_data_direction);
328 sp->dma_handle = req_dma;
330 *cur_dsd++ = cpu_to_le32(LSD(req_dma));
331 *cur_dsd++ = cpu_to_le32(MSD(req_dma));
332 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
337 * qla2x00_start_scsi() - Send a SCSI command to the ISP
338 * @sp: command to send to the ISP
340 * Returns non-zero if a failure occured, else zero.
343 qla2x00_start_scsi(srb_t *sp)
349 struct scsi_cmnd *cmd;
354 cmd_entry_t *cmd_pkt;
356 struct scatterlist *sg;
360 /* Setup device pointers. */
362 fclun = sp->lun_queue->fclun;
363 ha = fclun->fcport->ha;
367 /* Send marker if required */
368 if (ha->marker_needed != 0) {
369 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
370 return (QLA_FUNCTION_FAILED);
372 ha->marker_needed = 0;
375 /* Calculate number of segments and entries required. */
376 if (sp->req_cnt == 0) {
379 sg = (struct scatterlist *) cmd->request_buffer;
380 sp->tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
381 cmd->sc_data_direction);
382 } else if (cmd->request_bufflen) {
385 sp->req_cnt = (ha->calc_request_entries)(sp->tot_dsds);
388 /* Acquire ring specific lock */
389 spin_lock_irqsave(&ha->hardware_lock, flags);
391 if (ha->req_q_cnt < (sp->req_cnt + 2)) {
392 /* Calculate number of free request entries */
393 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
394 if (ha->req_ring_index < cnt)
395 ha->req_q_cnt = cnt - ha->req_ring_index;
397 ha->req_q_cnt = ha->request_q_length -
398 (ha->req_ring_index - cnt);
401 /* If no room for request in request ring */
402 if (ha->req_q_cnt < (sp->req_cnt + 2)) {
403 DEBUG5(printk("scsi(%ld): in-ptr=%x req_q_cnt=%x "
405 ha->host_no, ha->req_ring_index, ha->req_q_cnt,
411 /* Check for room in outstanding command list. */
412 handle = ha->current_outstanding_cmd;
413 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
415 if (handle == MAX_OUTSTANDING_COMMANDS)
417 if (ha->outstanding_cmds[handle] == 0) {
418 ha->current_outstanding_cmd = handle;
422 if (index == MAX_OUTSTANDING_COMMANDS) {
423 DEBUG5(printk("scsi(%ld): Unable to queue command -- NO ROOM "
424 "IN OUTSTANDING ARRAY (req_q_cnt=%x).\n",
425 ha->host_no, ha->req_q_cnt));
429 /* Build command packet */
430 ha->outstanding_cmds[handle] = sp;
432 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
433 ha->req_q_cnt -= sp->req_cnt;
435 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
436 cmd_pkt->handle = handle;
437 /* Zero out remaining portion of packet. */
438 clr_ptr = (uint32_t *)cmd_pkt + 2;
439 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
440 cmd_pkt->dseg_count = cpu_to_le16(sp->tot_dsds);
443 SET_TARGET_ID(ha, cmd_pkt->target, fclun->fcport->loop_id);
446 cmd_pkt->lun = cpu_to_le16(fclun->lun);
448 /* Update tagged queuing modifier */
449 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
450 if (cmd->device->tagged_supported) {
452 case HEAD_OF_QUEUE_TAG:
453 cmd_pkt->control_flags =
454 __constant_cpu_to_le16(CF_HEAD_TAG);
456 case ORDERED_QUEUE_TAG:
457 cmd_pkt->control_flags =
458 __constant_cpu_to_le16(CF_ORDERED_TAG);
464 * Allocate at least 5 (+ QLA_CMD_TIMER_DELTA) seconds for RISC timeout.
466 timeout = (uint32_t)(cmd->timeout_per_command / HZ);
468 cmd_pkt->timeout = __constant_cpu_to_le16(0);
469 else if (timeout > 25)
470 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout -
471 (5 + QLA_CMD_TIMER_DELTA));
473 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout);
475 /* Load SCSI command packet. */
476 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
477 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
479 /* Build IOCB segments */
480 (ha->build_scsi_iocbs)(sp, cmd_pkt, sp->tot_dsds);
482 /* Set total data segment count. */
483 cmd_pkt->entry_count = (uint8_t)sp->req_cnt;
485 /* Adjust ring index. */
486 ha->req_ring_index++;
487 if (ha->req_ring_index == ha->request_q_length) {
488 ha->req_ring_index = 0;
489 ha->request_ring_ptr = ha->request_ring;
491 ha->request_ring_ptr++;
495 sp->lun_queue->out_cnt++;
496 sp->flags |= SRB_DMA_VALID;
497 sp->state = SRB_ACTIVE_STATE;
498 sp->u_start = jiffies;
500 /* Set chip new ring index. */
501 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
502 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
504 spin_unlock_irqrestore(&ha->hardware_lock, flags);
505 return (QLA_SUCCESS);
508 spin_unlock_irqrestore(&ha->hardware_lock, flags);
510 return (QLA_FUNCTION_FAILED);
514 * qla2x00_marker() - Send a marker IOCB to the firmware.
518 * @type: marker modifier
520 * Can be called from both normal and interrupt context.
522 * Returns non-zero if a failure occured, else zero.
525 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
530 pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
532 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
534 return (QLA_FUNCTION_FAILED);
537 pkt->entry_type = MARKER_TYPE;
538 pkt->modifier = type;
540 if (type != MK_SYNC_ALL) {
541 pkt->lun = cpu_to_le16(lun);
542 SET_TARGET_ID(ha, pkt->target, loop_id);
545 /* Issue command to ISP */
548 return (QLA_SUCCESS);
552 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
556 unsigned long flags = 0;
558 spin_lock_irqsave(&ha->hardware_lock, flags);
559 ret = __qla2x00_marker(ha, loop_id, lun, type);
560 spin_unlock_irqrestore(&ha->hardware_lock, flags);
566 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
569 * Note: The caller must hold the hardware lock before calling this routine.
571 * Returns NULL if function failed, else, a pointer to the request packet.
574 qla2x00_req_pkt(scsi_qla_host_t *ha)
576 device_reg_t *reg = ha->iobase;
577 request_t *pkt = NULL;
581 uint16_t req_cnt = 1;
583 /* Wait 1 second for slot. */
584 for (timer = HZ; timer; timer--) {
585 if ((req_cnt + 2) >= ha->req_q_cnt) {
586 /* Calculate number of free request entries. */
587 cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
588 if (ha->req_ring_index < cnt)
589 ha->req_q_cnt = cnt - ha->req_ring_index;
591 ha->req_q_cnt = ha->request_q_length -
592 (ha->req_ring_index - cnt);
594 /* If room for request in request ring. */
595 if ((req_cnt + 2) < ha->req_q_cnt) {
597 pkt = ha->request_ring_ptr;
599 /* Zero out packet. */
600 dword_ptr = (uint32_t *)pkt;
601 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
604 /* Set system defined field. */
605 pkt->sys_define = (uint8_t)ha->req_ring_index;
607 /* Set entry count. */
608 pkt->entry_count = 1;
613 /* Release ring specific lock */
614 spin_unlock(&ha->hardware_lock);
616 udelay(2); /* 2 us */
618 /* Check for pending interrupts. */
619 /* During init we issue marker directly */
620 if (!ha->marker_needed)
623 spin_lock_irq(&ha->hardware_lock);
626 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
633 * qla2x00_ms_req_pkt() - Retrieve a Management Server request packet from
636 * @sp: pointer to handle post function call
638 * Note: The caller must hold the hardware lock before calling this routine.
640 * Returns NULL if function failed, else, a pointer to the request packet.
643 qla2x00_ms_req_pkt(scsi_qla_host_t *ha, srb_t *sp)
645 device_reg_t *reg = ha->iobase;
646 request_t *pkt = NULL;
647 uint16_t cnt, i, index;
651 uint16_t req_cnt = 1;
653 /* Wait 1 second for slot. */
654 for (timer = HZ; timer; timer--) {
655 if ((req_cnt + 2) >= ha->req_q_cnt) {
656 /* Calculate number of free request entries. */
657 cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
658 if (ha->req_ring_index < cnt) {
659 ha->req_q_cnt = cnt - ha->req_ring_index;
661 ha->req_q_cnt = ha->request_q_length -
662 (ha->req_ring_index - cnt);
666 /* Check for room in outstanding command list. */
667 cnt = ha->current_outstanding_cmd;
668 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
670 if (cnt == MAX_OUTSTANDING_COMMANDS)
673 if (ha->outstanding_cmds[cnt] == 0) {
675 ha->current_outstanding_cmd = cnt;
680 /* If room for request in request ring. */
681 if (found && (req_cnt + 2) < ha->req_q_cnt) {
682 pkt = ha->request_ring_ptr;
684 /* Zero out packet. */
685 dword_ptr = (uint32_t *)pkt;
686 for (i = 0; i < REQUEST_ENTRY_SIZE / 4; i++ )
689 DEBUG5(printk("%s(): putting sp=%p in "
690 "outstanding_cmds[%x]\n",
694 ha->outstanding_cmds[cnt] = sp;
696 /* save the handle */
697 sp->cmd->host_scribble = (unsigned char *) (u_long) cnt;
698 CMD_SP(sp->cmd) = (void *)sp;
701 pkt->handle = (uint32_t)cnt;
703 /* Set system defined field. */
704 pkt->sys_define = (uint8_t)ha->req_ring_index;
705 pkt->entry_status = 0;
710 /* Release ring specific lock */
711 spin_unlock(&ha->hardware_lock);
714 /* Check for pending interrupts. */
717 spin_lock_irq(&ha->hardware_lock);
720 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
727 * qla2x00_isp_cmd() - Modify the request ring pointer.
730 * Note: The caller must hold the hardware lock before calling this routine.
733 qla2x00_isp_cmd(scsi_qla_host_t *ha)
735 device_reg_t *reg = ha->iobase;
737 DEBUG5(printk("%s(): IOCB data:\n", __func__));
738 DEBUG5(qla2x00_dump_buffer(
739 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
741 /* Adjust ring index. */
742 ha->req_ring_index++;
743 if (ha->req_ring_index == ha->request_q_length) {
744 ha->req_ring_index = 0;
745 ha->request_ring_ptr = ha->request_ring;
747 ha->request_ring_ptr++;
749 /* Set chip new ring index. */
750 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
751 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */