blob: 0c145c9e0cd9b0382290729a0a64b3dd958cdfec [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez01e58d82008-04-03 13:13:13 -07003 * Copyright (c) 2003-2008 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080014static request_t *qla2x00_req_pkt(scsi_qla_host_t *);
15static void qla2x00_isp_cmd(scsi_qla_host_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/**
18 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
19 * @cmd: SCSI command
20 *
21 * Returns the proper CF_* direction based on CDB.
22 */
23static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070024qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 uint16_t cflags;
27
28 cflags = 0;
29
30 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070031 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080033 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070034 scsi_bufflen(sp->cmd);
35 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080037 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070038 scsi_bufflen(sp->cmd);
39 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 return (cflags);
41}
42
43/**
44 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
45 * Continuation Type 0 IOCBs to allocate.
46 *
47 * @dsds: number of data segment decriptors needed
48 *
49 * Returns the number of IOCB entries needed to store @dsds.
50 */
51uint16_t
52qla2x00_calc_iocbs_32(uint16_t dsds)
53{
54 uint16_t iocbs;
55
56 iocbs = 1;
57 if (dsds > 3) {
58 iocbs += (dsds - 3) / 7;
59 if ((dsds - 3) % 7)
60 iocbs++;
61 }
62 return (iocbs);
63}
64
65/**
66 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
67 * Continuation Type 1 IOCBs to allocate.
68 *
69 * @dsds: number of data segment decriptors needed
70 *
71 * Returns the number of IOCB entries needed to store @dsds.
72 */
73uint16_t
74qla2x00_calc_iocbs_64(uint16_t dsds)
75{
76 uint16_t iocbs;
77
78 iocbs = 1;
79 if (dsds > 2) {
80 iocbs += (dsds - 2) / 5;
81 if ((dsds - 2) % 5)
82 iocbs++;
83 }
84 return (iocbs);
85}
86
87/**
88 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
89 * @ha: HA context
90 *
91 * Returns a pointer to the Continuation Type 0 IOCB packet.
92 */
93static inline cont_entry_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080094qla2x00_prep_cont_type0_iocb(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 cont_entry_t *cont_pkt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080097 struct req_que *req = vha->hw->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080099 req->ring_index++;
100 if (req->ring_index == req->length) {
101 req->ring_index = 0;
102 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800104 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
106
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800107 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Load packet defaults. */
110 *((uint32_t *)(&cont_pkt->entry_type)) =
111 __constant_cpu_to_le32(CONTINUE_TYPE);
112
113 return (cont_pkt);
114}
115
116/**
117 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
118 * @ha: HA context
119 *
120 * Returns a pointer to the continuation type 1 IOCB packet.
121 */
122static inline cont_a64_entry_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800123qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 cont_a64_entry_t *cont_pkt;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800126 struct req_que *req = vha->hw->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800134 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Load packet defaults. */
140 *((uint32_t *)(&cont_pkt->entry_type)) =
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
142
143 return (cont_pkt);
144}
145
146/**
147 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
148 * capable IOCB types.
149 *
150 * @sp: SRB command to process
151 * @cmd_pkt: Command type 2 IOCB
152 * @tot_dsds: Total number of segments to transfer
153 */
154void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
155 uint16_t tot_dsds)
156{
157 uint16_t avail_dsds;
158 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800159 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900161 struct scatterlist *sg;
162 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 cmd = sp->cmd;
165
166 /* Update entry type to indicate Command Type 2 IOCB */
167 *((uint32_t *)(&cmd_pkt->entry_type)) =
168 __constant_cpu_to_le32(COMMAND_TYPE);
169
170 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900171 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
173 return;
174 }
175
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800176 vha = sp->vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700178 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Three DSDs are available in the Command Type 2 IOCB */
181 avail_dsds = 3;
182 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
183
184 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900185 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
186 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900188 /* Allocate additional continuation packets? */
189 if (avail_dsds == 0) {
190 /*
191 * Seven DSDs are available in the Continuation
192 * Type 0 IOCB.
193 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800194 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900195 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
196 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900198
199 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
200 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
201 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203}
204
205/**
206 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
207 * capable IOCB types.
208 *
209 * @sp: SRB command to process
210 * @cmd_pkt: Command type 3 IOCB
211 * @tot_dsds: Total number of segments to transfer
212 */
213void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
214 uint16_t tot_dsds)
215{
216 uint16_t avail_dsds;
217 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800218 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900220 struct scatterlist *sg;
221 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 cmd = sp->cmd;
224
225 /* Update entry type to indicate Command Type 3 IOCB */
226 *((uint32_t *)(&cmd_pkt->entry_type)) =
227 __constant_cpu_to_le32(COMMAND_A64_TYPE);
228
229 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900230 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
232 return;
233 }
234
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800235 vha = sp->vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700237 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 /* Two DSDs are available in the Command Type 3 IOCB */
240 avail_dsds = 2;
241 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
242
243 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900244 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
245 dma_addr_t sle_dma;
246 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900248 /* Allocate additional continuation packets? */
249 if (avail_dsds == 0) {
250 /*
251 * Five DSDs are available in the Continuation
252 * Type 1 IOCB.
253 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800254 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900255 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
256 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900258
259 sle_dma = sg_dma_address(sg);
260 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
261 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
262 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
263 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265}
266
267/**
268 * qla2x00_start_scsi() - Send a SCSI command to the ISP
269 * @sp: command to send to the ISP
270 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700271 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 */
273int
274qla2x00_start_scsi(srb_t *sp)
275{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900276 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800278 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 struct scsi_cmnd *cmd;
280 uint32_t *clr_ptr;
281 uint32_t index;
282 uint32_t handle;
283 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 uint16_t cnt;
285 uint16_t req_cnt;
286 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700287 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800288 struct qla_hw_data *ha;
289 struct req_que *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Setup device pointers. */
292 ret = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800293 vha = sp->vha;
294 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700295 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 cmd = sp->cmd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800297 req = ha->req;
83021922005-04-17 15:10:41 -0500298 /* So we know we haven't pci_map'ed anything yet */
299 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800302 if (vha->marker_needed != 0) {
303 if (qla2x00_marker(vha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800305 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
308 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700309 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800312 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
314 handle++;
315 if (handle == MAX_OUTSTANDING_COMMANDS)
316 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800317 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
319 }
320 if (index == MAX_OUTSTANDING_COMMANDS)
321 goto queuing_error;
322
83021922005-04-17 15:10:41 -0500323 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700324 if (scsi_sg_count(cmd)) {
325 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
326 scsi_sg_count(cmd), cmd->sc_data_direction);
327 if (unlikely(!nseg))
328 goto queuing_error;
329 } else
330 nseg = 0;
331
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900332 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700335 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800336 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800338 if (req->ring_index < cnt)
339 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800341 req->cnt = req->length -
342 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800344 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto queuing_error;
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800348 req->current_outstanding_cmd = handle;
349 req->outstanding_cmds[handle] = sp;
350 sp->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800352 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800354 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 cmd_pkt->handle = handle;
356 /* Zero out remaining portion of packet. */
357 clr_ptr = (uint32_t *)cmd_pkt + 2;
358 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
359 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
360
bdf79622005-04-17 15:06:53 -0500361 /* Set target ID and LUN number*/
362 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
363 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 /* Update tagged queuing modifier */
366 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Load SCSI command packet. */
369 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900370 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700373 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* Set total data segment count. */
376 cmd_pkt->entry_count = (uint8_t)req_cnt;
377 wmb();
378
379 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800380 req->ring_index++;
381 if (req->ring_index == req->length) {
382 req->ring_index = 0;
383 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800385 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800390 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
392
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700393 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800394 if (vha->flags.process_response_queue &&
395 ha->rsp->ring_ptr->signature != RESPONSE_PROCESSED)
396 qla2x00_process_response_queue(vha);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700397
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700398 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return (QLA_SUCCESS);
400
401queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900402 if (tot_dsds)
403 scsi_dma_unmap(cmd);
404
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700405 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 return (QLA_FUNCTION_FAILED);
408}
409
410/**
411 * qla2x00_marker() - Send a marker IOCB to the firmware.
412 * @ha: HA context
413 * @loop_id: loop ID
414 * @lun: LUN
415 * @type: marker modifier
416 *
417 * Can be called from both normal and interrupt context.
418 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700419 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700421int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800422__qla2x00_marker(scsi_qla_host_t *vha, uint16_t loop_id, uint16_t lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 uint8_t type)
424{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700425 mrk_entry_t *mrk;
426 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800427 struct qla_hw_data *ha = vha->hw;
428 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700430 mrk24 = NULL;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800431 mrk = (mrk_entry_t *)qla2x00_req_pkt(base_vha);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700432 if (mrk == NULL) {
433 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800434 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 return (QLA_FUNCTION_FAILED);
437 }
438
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700439 mrk->entry_type = MARKER_TYPE;
440 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700442 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700443 mrk24 = (struct mrk_entry_24xx *) mrk;
444 mrk24->nport_handle = cpu_to_le16(loop_id);
445 mrk24->lun[1] = LSB(lun);
446 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700447 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800448 mrk24->vp_index = vha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700449 } else {
450 SET_TARGET_ID(ha, mrk->target, loop_id);
451 mrk->lun = cpu_to_le16(lun);
452 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 wmb();
455
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800456 qla2x00_isp_cmd(base_vha);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 return (QLA_SUCCESS);
459}
460
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700461int
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800462qla2x00_marker(scsi_qla_host_t *vha, uint16_t loop_id, uint16_t lun,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 uint8_t type)
464{
465 int ret;
466 unsigned long flags = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800467 struct qla_hw_data *ha = vha->hw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800469 spin_lock_irqsave(&ha->hardware_lock, flags);
470 ret = __qla2x00_marker(vha, loop_id, lun, type);
471 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 return (ret);
474}
475
476/**
477 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
478 * @ha: HA context
479 *
480 * Note: The caller must hold the hardware lock before calling this routine.
481 *
482 * Returns NULL if function failed, else, a pointer to the request packet.
483 */
484static request_t *
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800485qla2x00_req_pkt(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800487 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700488 device_reg_t __iomem *reg = ha->iobase;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 request_t *pkt = NULL;
490 uint16_t cnt;
491 uint32_t *dword_ptr;
492 uint32_t timer;
493 uint16_t req_cnt = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800494 struct req_que *req = ha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Wait 1 second for slot. */
497 for (timer = HZ; timer; timer--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800498 if ((req_cnt + 2) >= req->cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /* Calculate number of free request entries. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700500 if (IS_FWI2_CAPABLE(ha))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700501 cnt = (uint16_t)RD_REG_DWORD(
502 &reg->isp24.req_q_out);
503 else
504 cnt = qla2x00_debounce_register(
505 ISP_REQ_Q_OUT(ha, &reg->isp));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800506 if (req->ring_index < cnt)
507 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800509 req->cnt = req->length -
510 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 /* If room for request in request ring. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800513 if ((req_cnt + 2) < req->cnt) {
514 req->cnt--;
515 pkt = req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 /* Zero out packet. */
518 dword_ptr = (uint32_t *)pkt;
519 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
520 *dword_ptr++ = 0;
521
522 /* Set system defined field. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800523 pkt->sys_define = (uint8_t)req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* Set entry count. */
526 pkt->entry_count = 1;
527
528 break;
529 }
530
531 /* Release ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800532 spin_unlock_irq(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 udelay(2); /* 2 us */
535
536 /* Check for pending interrupts. */
537 /* During init we issue marker directly */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800538 if (!vha->marker_needed && !vha->flags.init_done)
539 qla2x00_poll(ha->rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 spin_lock_irq(&ha->hardware_lock);
541 }
542 if (!pkt) {
543 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
544 }
545
546 return (pkt);
547}
548
549/**
550 * qla2x00_isp_cmd() - Modify the request ring pointer.
551 * @ha: HA context
552 *
553 * Note: The caller must hold the hardware lock before calling this routine.
554 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700555static void
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800556qla2x00_isp_cmd(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800558 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700559 device_reg_t __iomem *reg = ha->iobase;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800560 struct req_que *req = ha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 DEBUG5(printk("%s(): IOCB data:\n", __func__));
563 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800564 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800567 req->ring_index++;
568 if (req->ring_index == req->length) {
569 req->ring_index = 0;
570 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800572 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /* Set chip new ring index. */
Andrew Vasqueze4289242007-07-19 15:05:56 -0700575 if (IS_FWI2_CAPABLE(ha)) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800576 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700577 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
578 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800579 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp), req->ring_index);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700580 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
581 }
582
583}
584
585/**
586 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
587 * Continuation Type 1 IOCBs to allocate.
588 *
589 * @dsds: number of data segment decriptors needed
590 *
591 * Returns the number of IOCB entries needed to store @dsds.
592 */
593static inline uint16_t
594qla24xx_calc_iocbs(uint16_t dsds)
595{
596 uint16_t iocbs;
597
598 iocbs = 1;
599 if (dsds > 1) {
600 iocbs += (dsds - 1) / 5;
601 if ((dsds - 1) % 5)
602 iocbs++;
603 }
604 return iocbs;
605}
606
607/**
608 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
609 * IOCB types.
610 *
611 * @sp: SRB command to process
612 * @cmd_pkt: Command type 3 IOCB
613 * @tot_dsds: Total number of segments to transfer
614 */
615static inline void
616qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
617 uint16_t tot_dsds)
618{
619 uint16_t avail_dsds;
620 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800621 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700622 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900623 struct scatterlist *sg;
624 int i;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700625
626 cmd = sp->cmd;
627
628 /* Update entry type to indicate Command Type 3 IOCB */
629 *((uint32_t *)(&cmd_pkt->entry_type)) =
630 __constant_cpu_to_le32(COMMAND_TYPE_7);
631
632 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900633 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700634 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
635 return;
636 }
637
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800638 vha = sp->vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700639
640 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700641 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700642 cmd_pkt->task_mgmt_flags =
643 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800644 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700645 scsi_bufflen(sp->cmd);
646 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700647 cmd_pkt->task_mgmt_flags =
648 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800649 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700650 scsi_bufflen(sp->cmd);
651 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700652
653 /* One DSD is available in the Command Type 3 IOCB */
654 avail_dsds = 1;
655 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
656
657 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700658
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900659 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
660 dma_addr_t sle_dma;
661 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700662
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900663 /* Allocate additional continuation packets? */
664 if (avail_dsds == 0) {
665 /*
666 * Five DSDs are available in the Continuation
667 * Type 1 IOCB.
668 */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800669 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900670 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
671 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700672 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900673
674 sle_dma = sg_dma_address(sg);
675 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
676 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
677 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
678 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700679 }
680}
681
682
683/**
684 * qla24xx_start_scsi() - Send a SCSI command to the ISP
685 * @sp: command to send to the ISP
686 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700687 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700688 */
689int
690qla24xx_start_scsi(srb_t *sp)
691{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900692 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700693 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800694 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700695 struct scsi_cmnd *cmd;
696 uint32_t *clr_ptr;
697 uint32_t index;
698 uint32_t handle;
699 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700700 uint16_t cnt;
701 uint16_t req_cnt;
702 uint16_t tot_dsds;
Linus Torvaldsdb776a12005-07-26 14:50:02 -0700703 struct device_reg_24xx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800704 struct qla_hw_data *ha;
705 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700706
707 /* Setup device pointers. */
708 ret = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800709 vha = sp->vha;
710 ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700711 reg = &ha->iobase->isp24;
712 cmd = sp->cmd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800713 req = ha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700714 /* So we know we haven't pci_map'ed anything yet */
715 tot_dsds = 0;
716
717 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800718 if (vha->marker_needed != 0) {
719 if (qla2x00_marker(vha, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700720 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800721 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700722 }
723
724 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800725 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700726
727 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800728 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700729 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
730 handle++;
731 if (handle == MAX_OUTSTANDING_COMMANDS)
732 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800733 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700734 break;
735 }
736 if (index == MAX_OUTSTANDING_COMMANDS)
737 goto queuing_error;
738
739 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700740 if (scsi_sg_count(cmd)) {
741 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
742 scsi_sg_count(cmd), cmd->sc_data_direction);
743 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700744 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700745 } else
746 nseg = 0;
747
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900748 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700749
750 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800751 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700752 cnt = (uint16_t)RD_REG_DWORD_RELAXED(&reg->req_q_out);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800753 if (req->ring_index < cnt)
754 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700755 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800756 req->cnt = req->length -
757 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700758 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800759 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700760 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700761
762 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800763 req->current_outstanding_cmd = handle;
764 req->outstanding_cmds[handle] = sp;
765 sp->vha = vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700766 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800767 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700768
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800769 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700770 cmd_pkt->handle = handle;
771
772 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500773 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700774 clr_ptr = (uint32_t *)cmd_pkt + 2;
775 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
776 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
777
778 /* Set NPORT-ID and LUN number*/
779 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
780 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
781 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
782 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700783 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700784
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700785 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800786 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700787
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700788 /* Load SCSI command packet. */
789 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
790 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
791
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900792 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700793
794 /* Build IOCB segments */
795 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
796
797 /* Set total data segment count. */
798 cmd_pkt->entry_count = (uint8_t)req_cnt;
799 wmb();
800
801 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800802 req->ring_index++;
803 if (req->ring_index == req->length) {
804 req->ring_index = 0;
805 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700806 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800807 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700808
809 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700810
811 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800812 WRT_REG_DWORD(&reg->req_q_in, req->ring_index);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700813 RD_REG_DWORD_RELAXED(&reg->req_q_in); /* PCI Posting. */
814
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700815 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800816 if (vha->flags.process_response_queue &&
817 ha->rsp->ring_ptr->signature != RESPONSE_PROCESSED)
818 qla24xx_process_response_queue(vha);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700819
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800820 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700821 return QLA_SUCCESS;
822
823queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900824 if (tot_dsds)
825 scsi_dma_unmap(cmd);
826
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800827 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700828
829 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830}