2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
5 * See LICENSE.qla4xxx for copyright and licensing details.
11 #include "ql4_inline.h"
14 #include <scsi/scsi_tcq.h>
17 * qla4xxx_get_req_pkt - returns a valid entry in request queue.
18 * @ha: Pointer to host adapter structure.
19 * @queue_entry: Pointer to pointer to queue entry structure
21 * This routine performs the following tasks:
22 * - returns the current request_in pointer (if queue not full)
23 * - advances the request_in pointer
24 * - checks for queue full
26 static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha,
27 struct queue_entry **queue_entry)
30 uint8_t status = QLA_SUCCESS;
32 *queue_entry = ha->request_ptr;
34 /* get the latest request_in and request_out index */
35 request_in = ha->request_in;
36 ha->request_out = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
38 /* Advance request queue pointer and check for queue full */
39 if (request_in == (REQUEST_QUEUE_DEPTH - 1)) {
41 ha->request_ptr = ha->request_ring;
47 /* request queue is full, try again later */
48 if ((ha->iocb_cnt + 1) >= ha->iocb_hiwat) {
49 /* restore request pointer */
50 ha->request_ptr = *queue_entry;
53 ha->request_in = request_in;
54 memset(*queue_entry, 0, sizeof(**queue_entry));
61 * qla4xxx_send_marker_iocb - issues marker iocb to HBA
62 * @ha: Pointer to host adapter structure.
63 * @ddb_entry: Pointer to device database entry
65 * @marker_type: marker identifier
67 * This routine issues a marker IOCB.
69 static int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha,
70 struct ddb_entry *ddb_entry, int lun)
72 struct marker_entry *marker_entry;
73 unsigned long flags = 0;
74 uint8_t status = QLA_SUCCESS;
76 /* Acquire hardware specific lock */
77 spin_lock_irqsave(&ha->hardware_lock, flags);
79 /* Get pointer to the queue entry for the marker */
80 if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) !=
83 goto exit_send_marker;
86 /* Put the marker in the request queue */
87 marker_entry->hdr.entryType = ET_MARKER;
88 marker_entry->hdr.entryCount = 1;
89 marker_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
90 marker_entry->modifier = cpu_to_le16(MM_LUN_RESET);
91 int_to_scsilun(lun, &marker_entry->lun);
94 /* Tell ISP it's got a new I/O request */
95 writel(ha->request_in, &ha->reg->req_q_in);
96 readl(&ha->reg->req_q_in);
99 spin_unlock_irqrestore(&ha->hardware_lock, flags);
103 static struct continuation_t1_entry* qla4xxx_alloc_cont_entry(
104 struct scsi_qla_host *ha)
106 struct continuation_t1_entry *cont_entry;
108 cont_entry = (struct continuation_t1_entry *)ha->request_ptr;
110 /* Advance request queue pointer */
111 if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) {
113 ha->request_ptr = ha->request_ring;
119 /* Load packet defaults */
120 cont_entry->hdr.entryType = ET_CONTINUE;
121 cont_entry->hdr.entryCount = 1;
122 cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in);
127 static uint16_t qla4xxx_calc_request_entries(uint16_t dsds)
132 if (dsds > COMMAND_SEG) {
133 iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG;
134 if ((dsds - COMMAND_SEG) % CONTINUE_SEG)
140 static void qla4xxx_build_scsi_iocbs(struct srb *srb,
141 struct command_t3_entry *cmd_entry,
144 struct scsi_qla_host *ha;
146 struct data_seg_a64 *cur_dsd;
147 struct scsi_cmnd *cmd;
152 if (cmd->request_bufflen == 0 || cmd->sc_data_direction == DMA_NONE) {
153 /* No data being transferred */
154 cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0);
158 avail_dsds = COMMAND_SEG;
159 cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
161 /* Load data segments */
163 struct scatterlist *cur_seg;
164 struct scatterlist *end_seg;
166 cur_seg = (struct scatterlist *)cmd->request_buffer;
167 end_seg = cur_seg + tot_dsds;
168 while (cur_seg < end_seg) {
171 /* Allocate additional continuation packets? */
172 if (avail_dsds == 0) {
173 struct continuation_t1_entry *cont_entry;
175 cont_entry = qla4xxx_alloc_cont_entry(ha);
177 (struct data_seg_a64 *)
178 &cont_entry->dataseg[0];
179 avail_dsds = CONTINUE_SEG;
182 sle_dma = sg_dma_address(cur_seg);
183 cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma));
184 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma));
185 cur_dsd->count = cpu_to_le32(sg_dma_len(cur_seg));
192 cur_dsd->base.addrLow = cpu_to_le32(LSDW(srb->dma_handle));
193 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(srb->dma_handle));
194 cur_dsd->count = cpu_to_le32(cmd->request_bufflen);
199 * qla4xxx_send_command_to_isp - issues command to HBA
200 * @ha: pointer to host adapter structure.
201 * @srb: pointer to SCSI Request Block to be sent to ISP
203 * This routine is called by qla4xxx_queuecommand to build an ISP
204 * command and pass it to the ISP for execution.
206 int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
208 struct scsi_cmnd *cmd = srb->cmd;
209 struct ddb_entry *ddb_entry;
210 struct command_t3_entry *cmd_entry;
211 struct scatterlist *sg = NULL;
221 /* Get real lun and adapter */
222 ddb_entry = srb->ddb;
224 /* Send marker(s) if needed. */
225 if (ha->marker_needed == 1) {
226 if (qla4xxx_send_marker_iocb(ha, ddb_entry,
227 cmd->device->lun) != QLA_SUCCESS)
230 ha->marker_needed = 0;
234 /* Acquire hardware specific lock */
235 spin_lock_irqsave(&ha->hardware_lock, flags);
237 index = (uint32_t)cmd->request->tag;
239 /* Calculate the number of request entries needed. */
241 sg = (struct scatterlist *)cmd->request_buffer;
242 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
243 cmd->sc_data_direction);
246 } else if (cmd->request_bufflen) {
249 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
250 cmd->request_bufflen,
251 cmd->sc_data_direction);
252 if (dma_mapping_error(req_dma))
255 srb->dma_handle = req_dma;
258 req_cnt = qla4xxx_calc_request_entries(tot_dsds);
260 if (ha->req_q_count < (req_cnt + 2)) {
261 cnt = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
262 if (ha->request_in < cnt)
263 ha->req_q_count = cnt - ha->request_in;
265 ha->req_q_count = REQUEST_QUEUE_DEPTH -
266 (ha->request_in - cnt);
269 if (ha->req_q_count < (req_cnt + 2))
272 /* total iocbs active */
273 if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH)
276 /* Build command packet */
277 cmd_entry = (struct command_t3_entry *) ha->request_ptr;
278 memset(cmd_entry, 0, sizeof(struct command_t3_entry));
279 cmd_entry->hdr.entryType = ET_COMMAND;
280 cmd_entry->handle = cpu_to_le32(index);
281 cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
282 cmd_entry->connection_id = cpu_to_le16(ddb_entry->connection_id);
284 int_to_scsilun(cmd->device->lun, &cmd_entry->lun);
285 cmd_entry->cmdSeqNum = cpu_to_le32(ddb_entry->CmdSn);
286 cmd_entry->ttlByteCnt = cpu_to_le32(cmd->request_bufflen);
287 memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len);
288 cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds);
289 cmd_entry->hdr.entryCount = req_cnt;
291 /* Set data transfer direction control flags
292 * NOTE: Look at data_direction bits iff there is data to be
293 * transferred, as the data direction bit is sometimed filled
294 * in when there is no data to be transferred */
295 cmd_entry->control_flags = CF_NO_DATA;
296 if (cmd->request_bufflen) {
297 if (cmd->sc_data_direction == DMA_TO_DEVICE)
298 cmd_entry->control_flags = CF_WRITE;
299 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
300 cmd_entry->control_flags = CF_READ;
302 ha->bytes_xfered += cmd->request_bufflen;
303 if (ha->bytes_xfered & ~0xFFFFF){
304 ha->total_mbytes_xferred += ha->bytes_xfered >> 20;
305 ha->bytes_xfered &= 0xFFFFF;
309 /* Set tagged queueing control flags */
310 cmd_entry->control_flags |= CF_SIMPLE_TAG;
311 if (scsi_populate_tag_msg(cmd, tag))
314 cmd_entry->control_flags |= CF_HEAD_TAG;
316 case MSG_ORDERED_TAG:
317 cmd_entry->control_flags |= CF_ORDERED_TAG;
322 /* Advance request queue pointer */
324 if (ha->request_in == REQUEST_QUEUE_DEPTH) {
326 ha->request_ptr = ha->request_ring;
331 qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
335 * Check to see if adapter is online before placing request on
336 * request queue. If a reset occurs and a request is in the queue,
337 * the firmware will still attempt to process the request, retrieving
338 * garbage for pointers.
340 if (!test_bit(AF_ONLINE, &ha->flags)) {
341 DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! "
342 "Do not issue command.\n",
343 ha->host_no, __func__));
347 srb->cmd->host_scribble = (unsigned char *)srb;
349 /* update counters */
350 srb->state = SRB_ACTIVE_STATE;
351 srb->flags |= SRB_DMA_VALID;
353 /* Track IOCB used */
354 ha->iocb_cnt += req_cnt;
355 srb->iocb_cnt = req_cnt;
356 ha->req_q_count -= req_cnt;
358 /* Debug print statements */
359 writel(ha->request_in, &ha->reg->req_q_in);
360 readl(&ha->reg->req_q_in);
361 spin_unlock_irqrestore(&ha->hardware_lock, flags);
367 if (cmd->use_sg && tot_dsds) {
368 sg = (struct scatterlist *) cmd->request_buffer;
369 pci_unmap_sg(ha->pdev, sg, cmd->use_sg,
370 cmd->sc_data_direction);
372 pci_unmap_single(ha->pdev, srb->dma_handle,
373 cmd->request_bufflen, cmd->sc_data_direction);
374 spin_unlock_irqrestore(&ha->hardware_lock, flags);