blob: 6e3c8c81def3e5fc935b1829bb1a30e5b915eedc [file] [log] [blame]
David Somayajuluafaf5a22006-09-19 10:28:00 -07001/*
2 * QLogic iSCSI HBA Driver
3 * Copyright (c) 2003-2006 QLogic Corporation
4 *
5 * See LICENSE.qla4xxx for copyright and licensing details.
6 */
7
8#include "ql4_def.h"
David C Somayajulue08c1822007-05-23 18:14:34 -07009#include "ql4_glbl.h"
10#include "ql4_dbg.h"
11#include "ql4_inline.h"
12
David Somayajuluafaf5a22006-09-19 10:28:00 -070013
14#include <scsi/scsi_tcq.h>
15
16/**
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
20 *
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
25 **/
Adrian Bunk47975472007-04-26 00:35:16 -070026static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha,
27 struct queue_entry **queue_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -070028{
29 uint16_t request_in;
30 uint8_t status = QLA_SUCCESS;
31
32 *queue_entry = ha->request_ptr;
33
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);
37
38 /* Advance request queue pointer and check for queue full */
39 if (request_in == (REQUEST_QUEUE_DEPTH - 1)) {
40 request_in = 0;
41 ha->request_ptr = ha->request_ring;
42 } else {
43 request_in++;
44 ha->request_ptr++;
45 }
46
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;
51 status = QLA_ERROR;
52 } else {
53 ha->request_in = request_in;
54 memset(*queue_entry, 0, sizeof(**queue_entry));
55 }
56
57 return status;
58}
59
60/**
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
64 * @lun: SCSI LUN
65 * @marker_type: marker identifier
66 *
67 * This routine issues a marker IOCB.
68 **/
Adrian Bunk47975472007-04-26 00:35:16 -070069static int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha,
70 struct ddb_entry *ddb_entry, int lun)
David Somayajuluafaf5a22006-09-19 10:28:00 -070071{
72 struct marker_entry *marker_entry;
73 unsigned long flags = 0;
74 uint8_t status = QLA_SUCCESS;
75
76 /* Acquire hardware specific lock */
77 spin_lock_irqsave(&ha->hardware_lock, flags);
78
79 /* Get pointer to the queue entry for the marker */
80 if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) !=
81 QLA_SUCCESS) {
82 status = QLA_ERROR;
83 goto exit_send_marker;
84 }
85
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);
92 wmb();
93
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);
97
98exit_send_marker:
99 spin_unlock_irqrestore(&ha->hardware_lock, flags);
100 return status;
101}
102
Adrian Bunk47975472007-04-26 00:35:16 -0700103static struct continuation_t1_entry* qla4xxx_alloc_cont_entry(
David Somayajuluafaf5a22006-09-19 10:28:00 -0700104 struct scsi_qla_host *ha)
105{
106 struct continuation_t1_entry *cont_entry;
107
108 cont_entry = (struct continuation_t1_entry *)ha->request_ptr;
109
110 /* Advance request queue pointer */
111 if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) {
112 ha->request_in = 0;
113 ha->request_ptr = ha->request_ring;
114 } else {
115 ha->request_in++;
116 ha->request_ptr++;
117 }
118
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);
123
124 return cont_entry;
125}
126
Adrian Bunk47975472007-04-26 00:35:16 -0700127static uint16_t qla4xxx_calc_request_entries(uint16_t dsds)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700128{
129 uint16_t iocbs;
130
131 iocbs = 1;
132 if (dsds > COMMAND_SEG) {
133 iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG;
134 if ((dsds - COMMAND_SEG) % CONTINUE_SEG)
135 iocbs++;
136 }
137 return iocbs;
138}
139
Adrian Bunk47975472007-04-26 00:35:16 -0700140static void qla4xxx_build_scsi_iocbs(struct srb *srb,
141 struct command_t3_entry *cmd_entry,
142 uint16_t tot_dsds)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700143{
144 struct scsi_qla_host *ha;
145 uint16_t avail_dsds;
146 struct data_seg_a64 *cur_dsd;
147 struct scsi_cmnd *cmd;
148
149 cmd = srb->cmd;
150 ha = srb->ha;
151
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);
155 return;
156 }
157
158 avail_dsds = COMMAND_SEG;
159 cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
160
161 /* Load data segments */
162 if (cmd->use_sg) {
163 struct scatterlist *cur_seg;
164 struct scatterlist *end_seg;
165
166 cur_seg = (struct scatterlist *)cmd->request_buffer;
167 end_seg = cur_seg + tot_dsds;
168 while (cur_seg < end_seg) {
169 dma_addr_t sle_dma;
170
171 /* Allocate additional continuation packets? */
172 if (avail_dsds == 0) {
173 struct continuation_t1_entry *cont_entry;
174
175 cont_entry = qla4xxx_alloc_cont_entry(ha);
176 cur_dsd =
177 (struct data_seg_a64 *)
178 &cont_entry->dataseg[0];
179 avail_dsds = CONTINUE_SEG;
180 }
181
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));
186 avail_dsds--;
187
188 cur_dsd++;
189 cur_seg++;
190 }
191 } else {
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);
195 }
196}
197
198/**
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
202 *
203 * This routine is called by qla4xxx_queuecommand to build an ISP
204 * command and pass it to the ISP for execution.
205 **/
206int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
207{
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;
212
213 uint16_t tot_dsds;
214 uint16_t req_cnt;
215
216 unsigned long flags;
217 uint16_t cnt;
218 uint32_t index;
219 char tag[2];
220
221 /* Get real lun and adapter */
222 ddb_entry = srb->ddb;
223
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)
228 return QLA_ERROR;
229
230 ha->marker_needed = 0;
231 }
232 tot_dsds = 0;
233
234 /* Acquire hardware specific lock */
235 spin_lock_irqsave(&ha->hardware_lock, flags);
236
237 index = (uint32_t)cmd->request->tag;
238
239 /* Calculate the number of request entries needed. */
240 if (cmd->use_sg) {
241 sg = (struct scatterlist *)cmd->request_buffer;
242 tot_dsds = pci_map_sg(ha->pdev, sg, cmd->use_sg,
243 cmd->sc_data_direction);
244 if (tot_dsds == 0)
245 goto queuing_error;
246 } else if (cmd->request_bufflen) {
247 dma_addr_t req_dma;
248
249 req_dma = pci_map_single(ha->pdev, cmd->request_buffer,
David C Somayajulue08c1822007-05-23 18:14:34 -0700250 cmd->request_bufflen,
251 cmd->sc_data_direction);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700252 if (dma_mapping_error(req_dma))
253 goto queuing_error;
254
255 srb->dma_handle = req_dma;
256 tot_dsds = 1;
257 }
258 req_cnt = qla4xxx_calc_request_entries(tot_dsds);
259
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;
264 else
265 ha->req_q_count = REQUEST_QUEUE_DEPTH -
266 (ha->request_in - cnt);
267 }
268
269 if (ha->req_q_count < (req_cnt + 2))
270 goto queuing_error;
271
272 /* total iocbs active */
273 if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH)
274 goto queuing_error;
275
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);
283
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;
290
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;
David C Somayajulud9150582006-11-15 17:38:40 -0800301
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;
306 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700307 }
308
309 /* Set tagged queueing control flags */
310 cmd_entry->control_flags |= CF_SIMPLE_TAG;
311 if (scsi_populate_tag_msg(cmd, tag))
312 switch (tag[0]) {
313 case MSG_HEAD_TAG:
314 cmd_entry->control_flags |= CF_HEAD_TAG;
315 break;
316 case MSG_ORDERED_TAG:
317 cmd_entry->control_flags |= CF_ORDERED_TAG;
318 break;
319 }
320
321
322 /* Advance request queue pointer */
323 ha->request_in++;
324 if (ha->request_in == REQUEST_QUEUE_DEPTH) {
325 ha->request_in = 0;
326 ha->request_ptr = ha->request_ring;
327 } else
328 ha->request_ptr++;
329
330
331 qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
332 wmb();
333
334 /*
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.
339 */
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__));
344 goto queuing_error;
345 }
346
347 srb->cmd->host_scribble = (unsigned char *)srb;
348
349 /* update counters */
350 srb->state = SRB_ACTIVE_STATE;
351 srb->flags |= SRB_DMA_VALID;
352
353 /* Track IOCB used */
354 ha->iocb_cnt += req_cnt;
355 srb->iocb_cnt = req_cnt;
356 ha->req_q_count -= req_cnt;
357
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);
362
363 return QLA_SUCCESS;
364
365queuing_error:
366
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);
371 } else if (tot_dsds)
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);
375
376 return QLA_ERROR;
377}
378