blob: f89973deac5ba6735d71a59bc57bc26649689838 [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#include <scsi/scsi_tcq.h>
14
Karen Higgins16ed55f2009-07-15 15:03:02 -050015static int
16qla4xxx_space_in_req_ring(struct scsi_qla_host *ha, uint16_t req_cnt)
17{
18 uint16_t cnt;
19
20 /* Calculate number of free request entries. */
21 if ((req_cnt + 2) >= ha->req_q_count) {
22 cnt = (uint16_t) le32_to_cpu(ha->shadow_regs->req_q_out);
23 if (ha->request_in < cnt)
24 ha->req_q_count = cnt - ha->request_in;
25 else
26 ha->req_q_count = REQUEST_QUEUE_DEPTH -
27 (ha->request_in - cnt);
28 }
29
30 /* Check if room for request in request ring. */
31 if ((req_cnt + 2) < ha->req_q_count)
32 return 1;
33 else
34 return 0;
35}
36
37static void qla4xxx_advance_req_ring_ptr(struct scsi_qla_host *ha)
38{
39 /* Advance request queue pointer */
40 if (ha->request_in == (REQUEST_QUEUE_DEPTH - 1)) {
41 ha->request_in = 0;
42 ha->request_ptr = ha->request_ring;
43 } else {
44 ha->request_in++;
45 ha->request_ptr++;
46 }
47}
48
David Somayajuluafaf5a22006-09-19 10:28:00 -070049/**
50 * qla4xxx_get_req_pkt - returns a valid entry in request queue.
51 * @ha: Pointer to host adapter structure.
52 * @queue_entry: Pointer to pointer to queue entry structure
53 *
54 * This routine performs the following tasks:
55 * - returns the current request_in pointer (if queue not full)
56 * - advances the request_in pointer
57 * - checks for queue full
58 **/
Adrian Bunk47975472007-04-26 00:35:16 -070059static int qla4xxx_get_req_pkt(struct scsi_qla_host *ha,
60 struct queue_entry **queue_entry)
David Somayajuluafaf5a22006-09-19 10:28:00 -070061{
Karen Higgins16ed55f2009-07-15 15:03:02 -050062 uint16_t req_cnt = 1;
David Somayajuluafaf5a22006-09-19 10:28:00 -070063
Karen Higgins16ed55f2009-07-15 15:03:02 -050064 if (qla4xxx_space_in_req_ring(ha, req_cnt)) {
65 *queue_entry = ha->request_ptr;
David Somayajuluafaf5a22006-09-19 10:28:00 -070066 memset(*queue_entry, 0, sizeof(**queue_entry));
Karen Higgins16ed55f2009-07-15 15:03:02 -050067
68 qla4xxx_advance_req_ring_ptr(ha);
69 ha->req_q_count -= req_cnt;
70 return QLA_SUCCESS;
David Somayajuluafaf5a22006-09-19 10:28:00 -070071 }
72
Karen Higgins16ed55f2009-07-15 15:03:02 -050073 return QLA_ERROR;
David Somayajuluafaf5a22006-09-19 10:28:00 -070074}
75
76/**
77 * qla4xxx_send_marker_iocb - issues marker iocb to HBA
78 * @ha: Pointer to host adapter structure.
79 * @ddb_entry: Pointer to device database entry
80 * @lun: SCSI LUN
81 * @marker_type: marker identifier
82 *
83 * This routine issues a marker IOCB.
84 **/
David C Somayajulu9d562912008-03-19 11:23:03 -070085int qla4xxx_send_marker_iocb(struct scsi_qla_host *ha,
86 struct ddb_entry *ddb_entry, int lun, uint16_t mrkr_mod)
David Somayajuluafaf5a22006-09-19 10:28:00 -070087{
Mathieu Desnoyers1c3f0b82007-10-18 23:41:04 -070088 struct qla4_marker_entry *marker_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -070089 unsigned long flags = 0;
90 uint8_t status = QLA_SUCCESS;
91
92 /* Acquire hardware specific lock */
93 spin_lock_irqsave(&ha->hardware_lock, flags);
94
95 /* Get pointer to the queue entry for the marker */
96 if (qla4xxx_get_req_pkt(ha, (struct queue_entry **) &marker_entry) !=
97 QLA_SUCCESS) {
98 status = QLA_ERROR;
99 goto exit_send_marker;
100 }
101
102 /* Put the marker in the request queue */
103 marker_entry->hdr.entryType = ET_MARKER;
104 marker_entry->hdr.entryCount = 1;
105 marker_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
David C Somayajulu9d562912008-03-19 11:23:03 -0700106 marker_entry->modifier = cpu_to_le16(mrkr_mod);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700107 int_to_scsilun(lun, &marker_entry->lun);
108 wmb();
109
110 /* Tell ISP it's got a new I/O request */
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530111 ha->isp_ops->queue_iocb(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700112
113exit_send_marker:
114 spin_unlock_irqrestore(&ha->hardware_lock, flags);
115 return status;
116}
117
Karen Higgins16ed55f2009-07-15 15:03:02 -0500118static struct continuation_t1_entry *
119qla4xxx_alloc_cont_entry(struct scsi_qla_host *ha)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700120{
121 struct continuation_t1_entry *cont_entry;
122
123 cont_entry = (struct continuation_t1_entry *)ha->request_ptr;
124
Karen Higgins16ed55f2009-07-15 15:03:02 -0500125 qla4xxx_advance_req_ring_ptr(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700126
127 /* Load packet defaults */
128 cont_entry->hdr.entryType = ET_CONTINUE;
129 cont_entry->hdr.entryCount = 1;
130 cont_entry->hdr.systemDefined = (uint8_t) cpu_to_le16(ha->request_in);
131
132 return cont_entry;
133}
134
Adrian Bunk47975472007-04-26 00:35:16 -0700135static uint16_t qla4xxx_calc_request_entries(uint16_t dsds)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700136{
137 uint16_t iocbs;
138
139 iocbs = 1;
140 if (dsds > COMMAND_SEG) {
141 iocbs += (dsds - COMMAND_SEG) / CONTINUE_SEG;
142 if ((dsds - COMMAND_SEG) % CONTINUE_SEG)
143 iocbs++;
144 }
145 return iocbs;
146}
147
Adrian Bunk47975472007-04-26 00:35:16 -0700148static void qla4xxx_build_scsi_iocbs(struct srb *srb,
149 struct command_t3_entry *cmd_entry,
150 uint16_t tot_dsds)
David Somayajuluafaf5a22006-09-19 10:28:00 -0700151{
152 struct scsi_qla_host *ha;
153 uint16_t avail_dsds;
154 struct data_seg_a64 *cur_dsd;
155 struct scsi_cmnd *cmd;
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900156 struct scatterlist *sg;
157 int i;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700158
159 cmd = srb->cmd;
160 ha = srb->ha;
161
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900162 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700163 /* No data being transferred */
164 cmd_entry->ttlByteCnt = __constant_cpu_to_le32(0);
165 return;
166 }
167
168 avail_dsds = COMMAND_SEG;
169 cur_dsd = (struct data_seg_a64 *) & (cmd_entry->dataseg[0]);
170
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900171 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
172 dma_addr_t sle_dma;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700173
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900174 /* Allocate additional continuation packets? */
175 if (avail_dsds == 0) {
176 struct continuation_t1_entry *cont_entry;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700177
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900178 cont_entry = qla4xxx_alloc_cont_entry(ha);
179 cur_dsd =
180 (struct data_seg_a64 *)
181 &cont_entry->dataseg[0];
182 avail_dsds = CONTINUE_SEG;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700183 }
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900184
185 sle_dma = sg_dma_address(sg);
186 cur_dsd->base.addrLow = cpu_to_le32(LSDW(sle_dma));
187 cur_dsd->base.addrHigh = cpu_to_le32(MSDW(sle_dma));
188 cur_dsd->count = cpu_to_le32(sg_dma_len(sg));
189 avail_dsds--;
190
191 cur_dsd++;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700192 }
193}
194
195/**
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530196 * qla4_8xxx_queue_iocb - Tell ISP it's got new request(s)
197 * @ha: pointer to host adapter structure.
198 *
199 * This routine notifies the ISP that one or more new request
200 * queue entries have been placed on the request queue.
201 **/
202void qla4_8xxx_queue_iocb(struct scsi_qla_host *ha)
203{
204 uint32_t dbval = 0;
205 unsigned long wtime;
206
207 dbval = 0x14 | (ha->func_num << 5);
208 dbval = dbval | (0 << 8) | (ha->request_in << 16);
209 writel(dbval, (unsigned long __iomem *)ha->nx_db_wr_ptr);
210 wmb();
211
212 wtime = jiffies + (2 * HZ);
213 while (readl((void __iomem *)ha->nx_db_rd_ptr) != dbval &&
214 !time_after_eq(jiffies, wtime)) {
215 writel(dbval, (unsigned long __iomem *)ha->nx_db_wr_ptr);
216 wmb();
217 }
218}
219
220/**
221 * qla4_8xxx_complete_iocb - Tell ISP we're done with response(s)
222 * @ha: pointer to host adapter structure.
223 *
224 * This routine notifies the ISP that one or more response/completion
225 * queue entries have been processed by the driver.
226 * This also clears the interrupt.
227 **/
228void qla4_8xxx_complete_iocb(struct scsi_qla_host *ha)
229{
230 writel(ha->response_out, &ha->qla4_8xxx_reg->rsp_q_out);
231 readl(&ha->qla4_8xxx_reg->rsp_q_out);
232}
233
234/**
235 * qla4xxx_queue_iocb - Tell ISP it's got new request(s)
236 * @ha: pointer to host adapter structure.
237 *
238 * This routine is notifies the ISP that one or more new request
239 * queue entries have been placed on the request queue.
240 **/
241void qla4xxx_queue_iocb(struct scsi_qla_host *ha)
242{
243 writel(ha->request_in, &ha->reg->req_q_in);
244 readl(&ha->reg->req_q_in);
245}
246
247/**
248 * qla4xxx_complete_iocb - Tell ISP we're done with response(s)
249 * @ha: pointer to host adapter structure.
250 *
251 * This routine is notifies the ISP that one or more response/completion
252 * queue entries have been processed by the driver.
253 * This also clears the interrupt.
254 **/
255void qla4xxx_complete_iocb(struct scsi_qla_host *ha)
256{
257 writel(ha->response_out, &ha->reg->rsp_q_out);
258 readl(&ha->reg->rsp_q_out);
259}
260
261/**
David Somayajuluafaf5a22006-09-19 10:28:00 -0700262 * qla4xxx_send_command_to_isp - issues command to HBA
263 * @ha: pointer to host adapter structure.
264 * @srb: pointer to SCSI Request Block to be sent to ISP
265 *
266 * This routine is called by qla4xxx_queuecommand to build an ISP
267 * command and pass it to the ISP for execution.
268 **/
269int qla4xxx_send_command_to_isp(struct scsi_qla_host *ha, struct srb * srb)
270{
271 struct scsi_cmnd *cmd = srb->cmd;
272 struct ddb_entry *ddb_entry;
273 struct command_t3_entry *cmd_entry;
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900274 int nseg;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700275 uint16_t tot_dsds;
276 uint16_t req_cnt;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700277 unsigned long flags;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700278 uint32_t index;
279 char tag[2];
280
281 /* Get real lun and adapter */
282 ddb_entry = srb->ddb;
283
David Somayajuluafaf5a22006-09-19 10:28:00 -0700284 tot_dsds = 0;
285
286 /* Acquire hardware specific lock */
287 spin_lock_irqsave(&ha->hardware_lock, flags);
288
289 index = (uint32_t)cmd->request->tag;
290
Karen Higgins16ed55f2009-07-15 15:03:02 -0500291 /*
292 * Check to see if adapter is online before placing request on
293 * request queue. If a reset occurs and a request is in the queue,
294 * the firmware will still attempt to process the request, retrieving
295 * garbage for pointers.
296 */
297 if (!test_bit(AF_ONLINE, &ha->flags)) {
298 DEBUG2(printk("scsi%ld: %s: Adapter OFFLINE! "
299 "Do not issue command.\n",
300 ha->host_no, __func__));
301 goto queuing_error;
302 }
303
David Somayajuluafaf5a22006-09-19 10:28:00 -0700304 /* Calculate the number of request entries needed. */
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900305 nseg = scsi_dma_map(cmd);
306 if (nseg < 0)
307 goto queuing_error;
308 tot_dsds = nseg;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700309
David Somayajuluafaf5a22006-09-19 10:28:00 -0700310 req_cnt = qla4xxx_calc_request_entries(tot_dsds);
Karen Higgins16ed55f2009-07-15 15:03:02 -0500311 if (!qla4xxx_space_in_req_ring(ha, req_cnt))
David Somayajuluafaf5a22006-09-19 10:28:00 -0700312 goto queuing_error;
313
314 /* total iocbs active */
315 if ((ha->iocb_cnt + req_cnt) >= REQUEST_QUEUE_DEPTH)
316 goto queuing_error;
317
318 /* Build command packet */
319 cmd_entry = (struct command_t3_entry *) ha->request_ptr;
320 memset(cmd_entry, 0, sizeof(struct command_t3_entry));
321 cmd_entry->hdr.entryType = ET_COMMAND;
322 cmd_entry->handle = cpu_to_le32(index);
323 cmd_entry->target = cpu_to_le16(ddb_entry->fw_ddb_index);
324 cmd_entry->connection_id = cpu_to_le16(ddb_entry->connection_id);
325
326 int_to_scsilun(cmd->device->lun, &cmd_entry->lun);
327 cmd_entry->cmdSeqNum = cpu_to_le32(ddb_entry->CmdSn);
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900328 cmd_entry->ttlByteCnt = cpu_to_le32(scsi_bufflen(cmd));
David Somayajuluafaf5a22006-09-19 10:28:00 -0700329 memcpy(cmd_entry->cdb, cmd->cmnd, cmd->cmd_len);
330 cmd_entry->dataSegCnt = cpu_to_le16(tot_dsds);
331 cmd_entry->hdr.entryCount = req_cnt;
332
333 /* Set data transfer direction control flags
334 * NOTE: Look at data_direction bits iff there is data to be
335 * transferred, as the data direction bit is sometimed filled
336 * in when there is no data to be transferred */
337 cmd_entry->control_flags = CF_NO_DATA;
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900338 if (scsi_bufflen(cmd)) {
David Somayajuluafaf5a22006-09-19 10:28:00 -0700339 if (cmd->sc_data_direction == DMA_TO_DEVICE)
340 cmd_entry->control_flags = CF_WRITE;
341 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
342 cmd_entry->control_flags = CF_READ;
David C Somayajulud9150582006-11-15 17:38:40 -0800343
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900344 ha->bytes_xfered += scsi_bufflen(cmd);
David C Somayajulud9150582006-11-15 17:38:40 -0800345 if (ha->bytes_xfered & ~0xFFFFF){
346 ha->total_mbytes_xferred += ha->bytes_xfered >> 20;
347 ha->bytes_xfered &= 0xFFFFF;
348 }
David Somayajuluafaf5a22006-09-19 10:28:00 -0700349 }
350
351 /* Set tagged queueing control flags */
352 cmd_entry->control_flags |= CF_SIMPLE_TAG;
353 if (scsi_populate_tag_msg(cmd, tag))
354 switch (tag[0]) {
355 case MSG_HEAD_TAG:
356 cmd_entry->control_flags |= CF_HEAD_TAG;
357 break;
358 case MSG_ORDERED_TAG:
359 cmd_entry->control_flags |= CF_ORDERED_TAG;
360 break;
361 }
362
Karen Higgins16ed55f2009-07-15 15:03:02 -0500363 qla4xxx_advance_req_ring_ptr(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700364 qla4xxx_build_scsi_iocbs(srb, cmd_entry, tot_dsds);
365 wmb();
366
Vikas Chaudhary53698872010-04-28 11:41:59 +0530367 srb->cmd->host_scribble = (unsigned char *)(unsigned long)index;
David Somayajuluafaf5a22006-09-19 10:28:00 -0700368
369 /* update counters */
370 srb->state = SRB_ACTIVE_STATE;
371 srb->flags |= SRB_DMA_VALID;
372
373 /* Track IOCB used */
374 ha->iocb_cnt += req_cnt;
375 srb->iocb_cnt = req_cnt;
376 ha->req_q_count -= req_cnt;
377
Vikas Chaudharyf4f5df232010-07-28 15:53:44 +0530378 ha->isp_ops->queue_iocb(ha);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700379 spin_unlock_irqrestore(&ha->hardware_lock, flags);
380
381 return QLA_SUCCESS;
382
383queuing_error:
FUJITA Tomonori5f7186c2007-05-26 14:08:20 +0900384 if (tot_dsds)
385 scsi_dma_unmap(cmd);
David Somayajuluafaf5a22006-09-19 10:28:00 -0700386
David Somayajuluafaf5a22006-09-19 10:28:00 -0700387 spin_unlock_irqrestore(&ha->hardware_lock, flags);
388
389 return QLA_ERROR;
390}
391