blob: 5006ecb3ef5ea935e55a069eef9d8c9218b70a79 [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;
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900148 struct scatterlist *sg;
149 int i;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700150
151 cmd = srb->cmd;
152 ha = srb->ha;
153
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900154 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700155 /* No data being transferred */
156 cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0);
157 return;
158 }
159
160 avail_dsds = COMMAND_SEG;
161 cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
162
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900163 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
164 dma_addr_t sle_dma;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700165
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900166 /* Allocate additional continuation packets? */
167 if (avail_dsds == 0) {
168 struct continuation_t1_entry *cont_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700169
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900170 cont_entry = qla4xxx_alloc_cont_entry(ha);
171 cur_dsd =
172 (struct data_seg_a64 *)
173 &cont_entry->dataseg[0];
174 avail_dsds = CONTINUE_SEG;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700175 }
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900176
177 sle_dma = sg_dma_address(sg);
178 cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma));
179 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma));
180 cur_dsd->count = cpu_to_le32(sg_dma_len(sg));
181 avail_dsds--;
182
183 cur_dsd++;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700184 }
185}
186
187/**
188 * qla4xxx_send_command_to_isp - issues command to HBA
189 * @ha: pointer to host adapter structure.
190 * @srb: pointer to SCSI Request Block to be sent to ISP
191 *
192 * This routine is called by qla4xxx_queuecommand to build an ISP
193 * command and pass it to the ISP for execution.
194 **/
195int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
196{
197 struct scsi_cmnd *cmd = srb->cmd;
198 struct ddb_entry *ddb_entry;
199 struct command_t3_entry *cmd_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700200
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900201 int nseg;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700202 uint16_t tot_dsds;
203 uint16_t req_cnt;
204
205 unsigned long flags;
206 uint16_t cnt;
207 uint32_t index;
208 char tag[2];
209
210 /* Get real lun and adapter */
211 ddb_entry = srb->ddb;
212
213 /* Send marker(s) if needed. */
214 if (ha->marker_needed == 1) {
215 if (qla4xxx_send_marker_iocb(ha, ddb_entry,
216 cmd->device->lun) != QLA_SUCCESS)
217 return QLA_ERROR;
218
219 ha->marker_needed = 0;
220 }
221 tot_dsds = 0;
222
223 /* Acquire hardware specific lock */
224 spin_lock_irqsave(&ha->hardware_lock, flags);
225
226 index = (uint32_t)cmd->request->tag;
227
228 /* Calculate the number of request entries needed. */
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900229 nseg = scsi_dma_map(cmd);
230 if (nseg < 0)
231 goto queuing_error;
232 tot_dsds = nseg;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700233
David Somayajuluafaf5a22006-09-19 10:28:00 -0700234 req_cnt = qla4xxx_calc_request_entries(tot_dsds);
235
236 if (ha->req_q_count < (req_cnt + 2)) {
237 cnt = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
238 if (ha->request_in < cnt)
239 ha->req_q_count = cnt - ha->request_in;
240 else
241 ha->req_q_count = REQUEST_QUEUE_DEPTH -
242 (ha->request_in - cnt);
243 }
244
245 if (ha->req_q_count < (req_cnt + 2))
246 goto queuing_error;
247
248 /* total iocbs active */
249 if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH)
250 goto queuing_error;
251
252 /* Build command packet */
253 cmd_entry = (struct command_t3_entry *) ha->request_ptr;
254 memset(cmd_entry, 0, sizeof(struct command_t3_entry));
255 cmd_entry->hdr.entryType = ET_COMMAND;
256 cmd_entry->handle = cpu_to_le32(index);
257 cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
258 cmd_entry->connection_id = cpu_to_le16(ddb_entry->connection_id);
259
260 int_to_scsilun(cmd->device->lun, &cmd_entry->lun);
261 cmd_entry->cmdSeqNum = cpu_to_le32(ddb_entry->CmdSn);
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900262 cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700263 memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len);
264 cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds);
265 cmd_entry->hdr.entryCount = req_cnt;
266
267 /* Set data transfer direction control flags
268 * NOTE: Look at data_direction bits iff there is data to be
269 * transferred, as the data direction bit is sometimed filled
270 * in when there is no data to be transferred */
271 cmd_entry->control_flags = CF_NO_DATA;
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900272 if (scsi_bufflen(cmd)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700273 if (cmd->sc_data_direction == DMA_TO_DEVICE)
274 cmd_entry->control_flags = CF_WRITE;
275 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
276 cmd_entry->control_flags = CF_READ;
David C Somayajulud9150582006-11-15 17:38:40 -0800277
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900278 ha->bytes_xfered += scsi_bufflen(cmd);
David C Somayajulud9150582006-11-15 17:38:40 -0800279 if (ha->bytes_xfered & ~0xFFFFF){
280 ha->total_mbytes_xferred += ha->bytes_xfered >> 20;
281 ha->bytes_xfered &= 0xFFFFF;
282 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700283 }
284
285 /* Set tagged queueing control flags */
286 cmd_entry->control_flags |= CF_SIMPLE_TAG;
287 if (scsi_populate_tag_msg(cmd, tag))
288 switch (tag[0]) {
289 case MSG_HEAD_TAG:
290 cmd_entry->control_flags |= CF_HEAD_TAG;
291 break;
292 case MSG_ORDERED_TAG:
293 cmd_entry->control_flags |= CF_ORDERED_TAG;
294 break;
295 }
296
297
298 /* Advance request queue pointer */
299 ha->request_in++;
300 if (ha->request_in == REQUEST_QUEUE_DEPTH) {
301 ha->request_in = 0;
302 ha->request_ptr = ha->request_ring;
303 } else
304 ha->request_ptr++;
305
306
307 qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
308 wmb();
309
310 /*
311 * Check to see if adapter is online before placing request on
312 * request queue. If a reset occurs and a request is in the queue,
313 * the firmware will still attempt to process the request, retrieving
314 * garbage for pointers.
315 */
316 if (!test_bit(AF_ONLINE, &ha->flags)) {
317 DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! "
318 "Do not issue command.\n",
319 ha->host_no, __func__));
320 goto queuing_error;
321 }
322
323 srb->cmd->host_scribble = (unsigned char *)srb;
324
325 /* update counters */
326 srb->state = SRB_ACTIVE_STATE;
327 srb->flags |= SRB_DMA_VALID;
328
329 /* Track IOCB used */
330 ha->iocb_cnt += req_cnt;
331 srb->iocb_cnt = req_cnt;
332 ha->req_q_count -= req_cnt;
333
334 /* Debug print statements */
335 writel(ha->request_in, &ha->reg->req_q_in);
336 readl(&ha->reg->req_q_in);
337 spin_unlock_irqrestore(&ha->hardware_lock, flags);
338
339 return QLA_SUCCESS;
340
341queuing_error:
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900342 if (tot_dsds)
343 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700344
David Somayajuluafaf5a22006-09-19 10:28:00 -0700345 spin_unlock_irqrestore(&ha->hardware_lock, flags);
346
347 return QLA_ERROR;
348}
349