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 *);
30 static request_t *qla2x00_req_pkt(scsi_qla_host_t *ha);
33 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
36 * Returns the proper CF_* direction based on CDB.
38 static inline uint16_t
39 qla2x00_get_cmd_direction(struct scsi_cmnd *cmd)
45 /* Set transfer direction */
46 if (cmd->sc_data_direction == DMA_TO_DEVICE)
48 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
54 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
55 * Continuation Type 0 IOCBs to allocate.
57 * @dsds: number of data segment decriptors needed
59 * Returns the number of IOCB entries needed to store @dsds.
62 qla2x00_calc_iocbs_32(uint16_t dsds)
68 iocbs += (dsds - 3) / 7;
76 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
77 * Continuation Type 1 IOCBs to allocate.
79 * @dsds: number of data segment decriptors needed
81 * Returns the number of IOCB entries needed to store @dsds.
84 qla2x00_calc_iocbs_64(uint16_t dsds)
90 iocbs += (dsds - 2) / 5;
98 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
101 * Returns a pointer to the Continuation Type 0 IOCB packet.
103 static inline cont_entry_t *
104 qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *ha)
106 cont_entry_t *cont_pkt;
108 /* Adjust ring index. */
109 ha->req_ring_index++;
110 if (ha->req_ring_index == ha->request_q_length) {
111 ha->req_ring_index = 0;
112 ha->request_ring_ptr = ha->request_ring;
114 ha->request_ring_ptr++;
117 cont_pkt = (cont_entry_t *)ha->request_ring_ptr;
119 /* Load packet defaults. */
120 *((uint32_t *)(&cont_pkt->entry_type)) =
121 __constant_cpu_to_le32(CONTINUE_TYPE);
127 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
130 * Returns a pointer to the continuation type 1 IOCB packet.
132 static inline cont_a64_entry_t *
133 qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *ha)
135 cont_a64_entry_t *cont_pkt;
137 /* Adjust ring index. */
138 ha->req_ring_index++;
139 if (ha->req_ring_index == ha->request_q_length) {
140 ha->req_ring_index = 0;
141 ha->request_ring_ptr = ha->request_ring;
143 ha->request_ring_ptr++;
146 cont_pkt = (cont_a64_entry_t *)ha->request_ring_ptr;
148 /* Load packet defaults. */
149 *((uint32_t *)(&cont_pkt->entry_type)) =
150 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
156 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
157 * capable IOCB types.
159 * @sp: SRB command to process
160 * @cmd_pkt: Command type 2 IOCB
161 * @tot_dsds: Total number of segments to transfer
163 void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
169 struct scsi_cmnd *cmd;
173 /* Update entry type to indicate Command Type 2 IOCB */
174 *((uint32_t *)(&cmd_pkt->entry_type)) =
175 __constant_cpu_to_le32(COMMAND_TYPE);
177 /* No data transfer */
178 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
179 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
185 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
187 /* Three DSDs are available in the Command Type 2 IOCB */
189 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
191 /* Load data segments */
192 if (cmd->use_sg != 0) {
193 struct scatterlist *cur_seg;
194 struct scatterlist *end_seg;
196 cur_seg = (struct scatterlist *)cmd->request_buffer;
197 end_seg = cur_seg + tot_dsds;
198 while (cur_seg < end_seg) {
199 cont_entry_t *cont_pkt;
201 /* Allocate additional continuation packets? */
202 if (avail_dsds == 0) {
204 * Seven DSDs are available in the Continuation
207 cont_pkt = qla2x00_prep_cont_type0_iocb(ha);
208 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
212 *cur_dsd++ = cpu_to_le32(sg_dma_address(cur_seg));
213 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
221 unsigned long offset;
223 page = virt_to_page(cmd->request_buffer);
224 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
225 req_dma = pci_map_page(ha->pdev, page, offset,
226 cmd->request_bufflen, cmd->sc_data_direction);
228 sp->dma_handle = req_dma;
230 *cur_dsd++ = cpu_to_le32(req_dma);
231 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
236 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
237 * capable IOCB types.
239 * @sp: SRB command to process
240 * @cmd_pkt: Command type 3 IOCB
241 * @tot_dsds: Total number of segments to transfer
243 void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
249 struct scsi_cmnd *cmd;
253 /* Update entry type to indicate Command Type 3 IOCB */
254 *((uint32_t *)(&cmd_pkt->entry_type)) =
255 __constant_cpu_to_le32(COMMAND_A64_TYPE);
257 /* No data transfer */
258 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
259 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
265 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(cmd));
267 /* Two DSDs are available in the Command Type 3 IOCB */
269 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
271 /* Load data segments */
272 if (cmd->use_sg != 0) {
273 struct scatterlist *cur_seg;
274 struct scatterlist *end_seg;
276 cur_seg = (struct scatterlist *)cmd->request_buffer;
277 end_seg = cur_seg + tot_dsds;
278 while (cur_seg < end_seg) {
280 cont_a64_entry_t *cont_pkt;
282 /* Allocate additional continuation packets? */
283 if (avail_dsds == 0) {
285 * Five DSDs are available in the Continuation
288 cont_pkt = qla2x00_prep_cont_type1_iocb(ha);
289 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
293 sle_dma = sg_dma_address(cur_seg);
294 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
295 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
296 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
304 unsigned long offset;
306 page = virt_to_page(cmd->request_buffer);
307 offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
308 req_dma = pci_map_page(ha->pdev, page, offset,
309 cmd->request_bufflen, cmd->sc_data_direction);
311 sp->dma_handle = req_dma;
313 *cur_dsd++ = cpu_to_le32(LSD(req_dma));
314 *cur_dsd++ = cpu_to_le32(MSD(req_dma));
315 *cur_dsd++ = cpu_to_le32(cmd->request_bufflen);
320 * qla2x00_start_scsi() - Send a SCSI command to the ISP
321 * @sp: command to send to the ISP
323 * Returns non-zero if a failure occured, else zero.
326 qla2x00_start_scsi(srb_t *sp)
332 struct scsi_cmnd *cmd;
336 cmd_entry_t *cmd_pkt;
338 struct scatterlist *sg;
342 device_reg_t __iomem *reg;
345 /* Setup device pointers. */
347 fclun = sp->lun_queue->fclun;
348 ha = fclun->fcport->ha;
352 /* Send marker if required */
353 if (ha->marker_needed != 0) {
354 if (qla2x00_marker(ha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
355 return (QLA_FUNCTION_FAILED);
357 ha->marker_needed = 0;
360 /* Acquire ring specific lock */
361 spin_lock_irqsave(&ha->hardware_lock, flags);
363 /* Check for room in outstanding command list. */
364 handle = ha->current_outstanding_cmd;
365 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
367 if (handle == MAX_OUTSTANDING_COMMANDS)
369 if (ha->outstanding_cmds[handle] == 0)
372 if (index == MAX_OUTSTANDING_COMMANDS)
375 /* Calculate the number of request entries needed. */
376 req_cnt = (ha->calc_request_entries)(cmd->request->nr_hw_segments);
377 if (ha->req_q_cnt < (req_cnt + 2)) {
378 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
379 if (ha->req_ring_index < cnt)
380 ha->req_q_cnt = cnt - ha->req_ring_index;
382 ha->req_q_cnt = ha->request_q_length -
383 (ha->req_ring_index - cnt);
385 if (ha->req_q_cnt < (req_cnt + 2))
388 /* Finally, we have enough space, now perform mappings. */
391 sg = (struct scatterlist *) cmd->request_buffer;
392 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
393 cmd->sc_data_direction);
396 } else if (cmd->request_bufflen) {
399 req_cnt = (ha->calc_request_entries)(tot_dsds);
401 /* Build command packet */
402 ha->current_outstanding_cmd = handle;
403 ha->outstanding_cmds[handle] = sp;
405 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
406 ha->req_q_cnt -= req_cnt;
408 cmd_pkt = (cmd_entry_t *)ha->request_ring_ptr;
409 cmd_pkt->handle = handle;
410 /* Zero out remaining portion of packet. */
411 clr_ptr = (uint32_t *)cmd_pkt + 2;
412 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
413 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
416 SET_TARGET_ID(ha, cmd_pkt->target, fclun->fcport->loop_id);
419 cmd_pkt->lun = cpu_to_le16(fclun->lun);
421 /* Update tagged queuing modifier */
422 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
423 if (scsi_populate_tag_msg(cmd, tag)) {
426 cmd_pkt->control_flags =
427 __constant_cpu_to_le16(CF_HEAD_TAG);
429 case MSG_ORDERED_TAG:
430 cmd_pkt->control_flags =
431 __constant_cpu_to_le16(CF_ORDERED_TAG);
437 * Allocate at least 5 (+ QLA_CMD_TIMER_DELTA) seconds for RISC timeout.
439 timeout = (uint32_t)(cmd->timeout_per_command / HZ);
441 cmd_pkt->timeout = __constant_cpu_to_le16(0);
442 else if (timeout > 25)
443 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout -
444 (5 + QLA_CMD_TIMER_DELTA));
446 cmd_pkt->timeout = cpu_to_le16((uint16_t)timeout);
448 /* Load SCSI command packet. */
449 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
450 cmd_pkt->byte_count = cpu_to_le32((uint32_t)cmd->request_bufflen);
452 /* Build IOCB segments */
453 (ha->build_scsi_iocbs)(sp, cmd_pkt, tot_dsds);
455 /* Set total data segment count. */
456 cmd_pkt->entry_count = (uint8_t)req_cnt;
459 /* Adjust ring index. */
460 ha->req_ring_index++;
461 if (ha->req_ring_index == ha->request_q_length) {
462 ha->req_ring_index = 0;
463 ha->request_ring_ptr = ha->request_ring;
465 ha->request_ring_ptr++;
469 sp->lun_queue->out_cnt++;
470 sp->flags |= SRB_DMA_VALID;
471 sp->state = SRB_ACTIVE_STATE;
472 sp->u_start = jiffies;
474 /* Set chip new ring index. */
475 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
476 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
478 spin_unlock_irqrestore(&ha->hardware_lock, flags);
479 return (QLA_SUCCESS);
482 spin_unlock_irqrestore(&ha->hardware_lock, flags);
484 return (QLA_FUNCTION_FAILED);
488 * qla2x00_marker() - Send a marker IOCB to the firmware.
492 * @type: marker modifier
494 * Can be called from both normal and interrupt context.
496 * Returns non-zero if a failure occured, else zero.
499 __qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
504 pkt = (mrk_entry_t *)qla2x00_req_pkt(ha);
506 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
508 return (QLA_FUNCTION_FAILED);
511 pkt->entry_type = MARKER_TYPE;
512 pkt->modifier = type;
514 if (type != MK_SYNC_ALL) {
515 pkt->lun = cpu_to_le16(lun);
516 SET_TARGET_ID(ha, pkt->target, loop_id);
520 /* Issue command to ISP */
523 return (QLA_SUCCESS);
527 qla2x00_marker(scsi_qla_host_t *ha, uint16_t loop_id, uint16_t lun,
531 unsigned long flags = 0;
533 spin_lock_irqsave(&ha->hardware_lock, flags);
534 ret = __qla2x00_marker(ha, loop_id, lun, type);
535 spin_unlock_irqrestore(&ha->hardware_lock, flags);
541 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
544 * Note: The caller must hold the hardware lock before calling this routine.
546 * Returns NULL if function failed, else, a pointer to the request packet.
549 qla2x00_req_pkt(scsi_qla_host_t *ha)
551 device_reg_t __iomem *reg = ha->iobase;
552 request_t *pkt = NULL;
556 uint16_t req_cnt = 1;
558 /* Wait 1 second for slot. */
559 for (timer = HZ; timer; timer--) {
560 if ((req_cnt + 2) >= ha->req_q_cnt) {
561 /* Calculate number of free request entries. */
562 cnt = qla2x00_debounce_register(ISP_REQ_Q_OUT(ha, reg));
563 if (ha->req_ring_index < cnt)
564 ha->req_q_cnt = cnt - ha->req_ring_index;
566 ha->req_q_cnt = ha->request_q_length -
567 (ha->req_ring_index - cnt);
569 /* If room for request in request ring. */
570 if ((req_cnt + 2) < ha->req_q_cnt) {
572 pkt = ha->request_ring_ptr;
574 /* Zero out packet. */
575 dword_ptr = (uint32_t *)pkt;
576 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
579 /* Set system defined field. */
580 pkt->sys_define = (uint8_t)ha->req_ring_index;
582 /* Set entry count. */
583 pkt->entry_count = 1;
588 /* Release ring specific lock */
589 spin_unlock(&ha->hardware_lock);
591 udelay(2); /* 2 us */
593 /* Check for pending interrupts. */
594 /* During init we issue marker directly */
595 if (!ha->marker_needed)
598 spin_lock_irq(&ha->hardware_lock);
601 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
608 * qla2x00_isp_cmd() - Modify the request ring pointer.
611 * Note: The caller must hold the hardware lock before calling this routine.
614 qla2x00_isp_cmd(scsi_qla_host_t *ha)
616 device_reg_t __iomem *reg = ha->iobase;
618 DEBUG5(printk("%s(): IOCB data:\n", __func__));
619 DEBUG5(qla2x00_dump_buffer(
620 (uint8_t *)ha->request_ring_ptr, REQUEST_ENTRY_SIZE));
622 /* Adjust ring index. */
623 ha->req_ring_index++;
624 if (ha->req_ring_index == ha->request_q_length) {
625 ha->req_ring_index = 0;
626 ha->request_ring_ptr = ha->request_ring;
628 ha->request_ring_ptr++;
630 /* Set chip new ring index. */
631 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), ha->req_ring_index);
632 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */