blob: 6d2bd97c3b1133617ef22d494c16b537ab4bf44b [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 Chakraborty73208df2008-12-09 16:45:39 -080014static request_t *qla2x00_req_pkt(struct scsi_qla_host *, struct req_que *,
15 struct rsp_que *rsp);
16static void qla2x00_isp_cmd(struct scsi_qla_host *, struct req_que *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/**
19 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
20 * @cmd: SCSI command
21 *
22 * Returns the proper CF_* direction based on CDB.
23 */
24static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070025qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 uint16_t cflags;
28
29 cflags = 0;
30
31 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070032 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080034 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070035 scsi_bufflen(sp->cmd);
36 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080038 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070039 scsi_bufflen(sp->cmd);
40 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return (cflags);
42}
43
44/**
45 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46 * Continuation Type 0 IOCBs to allocate.
47 *
48 * @dsds: number of data segment decriptors needed
49 *
50 * Returns the number of IOCB entries needed to store @dsds.
51 */
52uint16_t
53qla2x00_calc_iocbs_32(uint16_t dsds)
54{
55 uint16_t iocbs;
56
57 iocbs = 1;
58 if (dsds > 3) {
59 iocbs += (dsds - 3) / 7;
60 if ((dsds - 3) % 7)
61 iocbs++;
62 }
63 return (iocbs);
64}
65
66/**
67 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68 * Continuation Type 1 IOCBs to allocate.
69 *
70 * @dsds: number of data segment decriptors needed
71 *
72 * Returns the number of IOCB entries needed to store @dsds.
73 */
74uint16_t
75qla2x00_calc_iocbs_64(uint16_t dsds)
76{
77 uint16_t iocbs;
78
79 iocbs = 1;
80 if (dsds > 2) {
81 iocbs += (dsds - 2) / 5;
82 if ((dsds - 2) % 5)
83 iocbs++;
84 }
85 return (iocbs);
86}
87
88/**
89 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90 * @ha: HA context
91 *
92 * Returns a pointer to the Continuation Type 0 IOCB packet.
93 */
94static inline cont_entry_t *
Anirban Chakraborty73208df2008-12-09 16:45:39 -080095qla2x00_prep_cont_type0_iocb(struct req_que *req, struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 cont_entry_t *cont_pkt;
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 Chakraborty73208df2008-12-09 16:45:39 -0800123qla2x00_prep_cont_type1_iocb(struct req_que *req, scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 cont_a64_entry_t *cont_pkt;
126
127 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800128 req->ring_index++;
129 if (req->ring_index == req->length) {
130 req->ring_index = 0;
131 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800133 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800136 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 /* Load packet defaults. */
139 *((uint32_t *)(&cont_pkt->entry_type)) =
140 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
141
142 return (cont_pkt);
143}
144
145/**
146 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
147 * capable IOCB types.
148 *
149 * @sp: SRB command to process
150 * @cmd_pkt: Command type 2 IOCB
151 * @tot_dsds: Total number of segments to transfer
152 */
153void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
154 uint16_t tot_dsds)
155{
156 uint16_t avail_dsds;
157 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800158 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900160 struct scatterlist *sg;
161 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800162 struct req_que *req;
163 uint16_t que_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 cmd = sp->cmd;
166
167 /* Update entry type to indicate Command Type 2 IOCB */
168 *((uint32_t *)(&cmd_pkt->entry_type)) =
169 __constant_cpu_to_le32(COMMAND_TYPE);
170
171 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900172 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
174 return;
175 }
176
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800177 vha = sp->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800178 que_id = vha->req_ques[0];
179 req = vha->hw->req_q_map[que_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700181 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Three DSDs are available in the Command Type 2 IOCB */
184 avail_dsds = 3;
185 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
186
187 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900188 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
189 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900191 /* Allocate additional continuation packets? */
192 if (avail_dsds == 0) {
193 /*
194 * Seven DSDs are available in the Continuation
195 * Type 0 IOCB.
196 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800197 cont_pkt = qla2x00_prep_cont_type0_iocb(req, vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900198 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
199 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900201
202 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
203 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
204 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206}
207
208/**
209 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
210 * capable IOCB types.
211 *
212 * @sp: SRB command to process
213 * @cmd_pkt: Command type 3 IOCB
214 * @tot_dsds: Total number of segments to transfer
215 */
216void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
217 uint16_t tot_dsds)
218{
219 uint16_t avail_dsds;
220 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800221 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900223 struct scatterlist *sg;
224 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800225 struct req_que *req;
226 uint16_t que_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 cmd = sp->cmd;
229
230 /* Update entry type to indicate Command Type 3 IOCB */
231 *((uint32_t *)(&cmd_pkt->entry_type)) =
232 __constant_cpu_to_le32(COMMAND_A64_TYPE);
233
234 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900235 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
237 return;
238 }
239
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800240 vha = sp->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800241 que_id = vha->req_ques[0];
242 req = vha->hw->req_q_map[que_id];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700244 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
246 /* Two DSDs are available in the Command Type 3 IOCB */
247 avail_dsds = 2;
248 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
249
250 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900251 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
252 dma_addr_t sle_dma;
253 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900255 /* Allocate additional continuation packets? */
256 if (avail_dsds == 0) {
257 /*
258 * Five DSDs are available in the Continuation
259 * Type 1 IOCB.
260 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800261 cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900262 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
263 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900265
266 sle_dma = sg_dma_address(sg);
267 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
268 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
269 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
270 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 }
272}
273
274/**
275 * qla2x00_start_scsi() - Send a SCSI command to the ISP
276 * @sp: command to send to the ISP
277 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700278 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280int
281qla2x00_start_scsi(srb_t *sp)
282{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900283 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800285 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct scsi_cmnd *cmd;
287 uint32_t *clr_ptr;
288 uint32_t index;
289 uint32_t handle;
290 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 uint16_t cnt;
292 uint16_t req_cnt;
293 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700294 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800295 struct qla_hw_data *ha;
296 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800297 struct rsp_que *rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 /* Setup device pointers. */
300 ret = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800301 vha = sp->vha;
302 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700303 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 cmd = sp->cmd;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800305 req = ha->req_q_map[0];
306 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500307 /* So we know we haven't pci_map'ed anything yet */
308 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800311 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800312 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
313 != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800315 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
317
318 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700319 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800322 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
324 handle++;
325 if (handle == MAX_OUTSTANDING_COMMANDS)
326 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800327 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 break;
329 }
330 if (index == MAX_OUTSTANDING_COMMANDS)
331 goto queuing_error;
332
83021922005-04-17 15:10:41 -0500333 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700334 if (scsi_sg_count(cmd)) {
335 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
336 scsi_sg_count(cmd), cmd->sc_data_direction);
337 if (unlikely(!nseg))
338 goto queuing_error;
339 } else
340 nseg = 0;
341
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900342 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700345 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800346 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800348 if (req->ring_index < cnt)
349 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800351 req->cnt = req->length -
352 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800354 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 goto queuing_error;
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800358 req->current_outstanding_cmd = handle;
359 req->outstanding_cmds[handle] = sp;
360 sp->vha = vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800362 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800364 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 cmd_pkt->handle = handle;
366 /* Zero out remaining portion of packet. */
367 clr_ptr = (uint32_t *)cmd_pkt + 2;
368 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
369 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
370
bdf79622005-04-17 15:06:53 -0500371 /* Set target ID and LUN number*/
372 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
373 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 /* Update tagged queuing modifier */
376 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Load SCSI command packet. */
379 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900380 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700383 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 /* Set total data segment count. */
386 cmd_pkt->entry_count = (uint8_t)req_cnt;
387 wmb();
388
389 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800390 req->ring_index++;
391 if (req->ring_index == req->length) {
392 req->ring_index = 0;
393 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800395 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800400 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
402
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700403 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800404 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800405 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
406 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700407
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700408 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return (QLA_SUCCESS);
410
411queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900412 if (tot_dsds)
413 scsi_dma_unmap(cmd);
414
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700415 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 return (QLA_FUNCTION_FAILED);
418}
419
420/**
421 * qla2x00_marker() - Send a marker IOCB to the firmware.
422 * @ha: HA context
423 * @loop_id: loop ID
424 * @lun: LUN
425 * @type: marker modifier
426 *
427 * Can be called from both normal and interrupt context.
428 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700429 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700431int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800432__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
433 struct rsp_que *rsp, uint16_t loop_id,
434 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700436 mrk_entry_t *mrk;
437 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800438 struct qla_hw_data *ha = vha->hw;
439 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700441 mrk24 = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800442 mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700443 if (mrk == NULL) {
444 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800445 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return (QLA_FUNCTION_FAILED);
448 }
449
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700450 mrk->entry_type = MARKER_TYPE;
451 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700453 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700454 mrk24 = (struct mrk_entry_24xx *) mrk;
455 mrk24->nport_handle = cpu_to_le16(loop_id);
456 mrk24->lun[1] = LSB(lun);
457 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700458 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800459 mrk24->vp_index = vha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700460 } else {
461 SET_TARGET_ID(ha, mrk->target, loop_id);
462 mrk->lun = cpu_to_le16(lun);
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465 wmb();
466
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800467 qla2x00_isp_cmd(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 return (QLA_SUCCESS);
470}
471
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700472int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800473qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
474 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
475 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
477 int ret;
478 unsigned long flags = 0;
479
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800480 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
481 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
482 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return (ret);
485}
486
487/**
488 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
489 * @ha: HA context
490 *
491 * Note: The caller must hold the hardware lock before calling this routine.
492 *
493 * Returns NULL if function failed, else, a pointer to the request packet.
494 */
495static request_t *
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800496qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
497 struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800499 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800500 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 request_t *pkt = NULL;
502 uint16_t cnt;
503 uint32_t *dword_ptr;
504 uint32_t timer;
505 uint16_t req_cnt = 1;
506
507 /* Wait 1 second for slot. */
508 for (timer = HZ; timer; timer--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800509 if ((req_cnt + 2) >= req->cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Calculate number of free request entries. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800511 if (ha->mqenable)
512 cnt = (uint16_t)
513 RD_REG_DWORD(&reg->isp25mq.req_q_out);
514 else {
515 if (IS_FWI2_CAPABLE(ha))
516 cnt = (uint16_t)RD_REG_DWORD(
517 &reg->isp24.req_q_out);
518 else
519 cnt = qla2x00_debounce_register(
520 ISP_REQ_Q_OUT(ha, &reg->isp));
521 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800522 if (req->ring_index < cnt)
523 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800525 req->cnt = req->length -
526 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528 /* If room for request in request ring. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800529 if ((req_cnt + 2) < req->cnt) {
530 req->cnt--;
531 pkt = req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 /* Zero out packet. */
534 dword_ptr = (uint32_t *)pkt;
535 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
536 *dword_ptr++ = 0;
537
538 /* Set system defined field. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800539 pkt->sys_define = (uint8_t)req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* Set entry count. */
542 pkt->entry_count = 1;
543
544 break;
545 }
546
547 /* Release ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800548 spin_unlock_irq(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 udelay(2); /* 2 us */
551
552 /* Check for pending interrupts. */
553 /* During init we issue marker directly */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800554 if (!vha->marker_needed && !vha->flags.init_done)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800555 qla2x00_poll(rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 spin_lock_irq(&ha->hardware_lock);
557 }
558 if (!pkt) {
559 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
560 }
561
562 return (pkt);
563}
564
565/**
566 * qla2x00_isp_cmd() - Modify the request ring pointer.
567 * @ha: HA context
568 *
569 * Note: The caller must hold the hardware lock before calling this routine.
570 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700571static void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800572qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800574 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800575 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 DEBUG5(printk("%s(): IOCB data:\n", __func__));
578 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800579 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
581 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800582 req->ring_index++;
583 if (req->ring_index == req->length) {
584 req->ring_index = 0;
585 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800587 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* Set chip new ring index. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800590 if (ha->mqenable)
591 RD_REG_DWORD(&reg->isp25mq.req_q_out);
592 else {
593 if (IS_FWI2_CAPABLE(ha)) {
594 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
595 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
596 } else {
597 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
598 req->ring_index);
599 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
600 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700601 }
602
603}
604
605/**
606 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
607 * Continuation Type 1 IOCBs to allocate.
608 *
609 * @dsds: number of data segment decriptors needed
610 *
611 * Returns the number of IOCB entries needed to store @dsds.
612 */
613static inline uint16_t
614qla24xx_calc_iocbs(uint16_t dsds)
615{
616 uint16_t iocbs;
617
618 iocbs = 1;
619 if (dsds > 1) {
620 iocbs += (dsds - 1) / 5;
621 if ((dsds - 1) % 5)
622 iocbs++;
623 }
624 return iocbs;
625}
626
627/**
628 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
629 * IOCB types.
630 *
631 * @sp: SRB command to process
632 * @cmd_pkt: Command type 3 IOCB
633 * @tot_dsds: Total number of segments to transfer
634 */
635static inline void
636qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
637 uint16_t tot_dsds)
638{
639 uint16_t avail_dsds;
640 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800641 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700642 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900643 struct scatterlist *sg;
644 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800645 uint16_t que_id;
646 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700647
648 cmd = sp->cmd;
649
650 /* Update entry type to indicate Command Type 3 IOCB */
651 *((uint32_t *)(&cmd_pkt->entry_type)) =
652 __constant_cpu_to_le32(COMMAND_TYPE_7);
653
654 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900655 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700656 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
657 return;
658 }
659
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800660 vha = sp->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800661 que_id = vha->req_ques[0];
662 req = vha->hw->req_q_map[que_id];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700663
664 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700665 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700666 cmd_pkt->task_mgmt_flags =
667 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800668 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700669 scsi_bufflen(sp->cmd);
670 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700671 cmd_pkt->task_mgmt_flags =
672 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800673 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700674 scsi_bufflen(sp->cmd);
675 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700676
677 /* One DSD is available in the Command Type 3 IOCB */
678 avail_dsds = 1;
679 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
680
681 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700682
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900683 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
684 dma_addr_t sle_dma;
685 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700686
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900687 /* Allocate additional continuation packets? */
688 if (avail_dsds == 0) {
689 /*
690 * Five DSDs are available in the Continuation
691 * Type 1 IOCB.
692 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800693 cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900694 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
695 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700696 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900697
698 sle_dma = sg_dma_address(sg);
699 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
700 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
701 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
702 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700703 }
704}
705
706
707/**
708 * qla24xx_start_scsi() - Send a SCSI command to the ISP
709 * @sp: command to send to the ISP
710 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700711 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700712 */
713int
714qla24xx_start_scsi(srb_t *sp)
715{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900716 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700717 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700718 uint32_t *clr_ptr;
719 uint32_t index;
720 uint32_t handle;
721 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700722 uint16_t cnt;
723 uint16_t req_cnt;
724 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800725 struct req_que *req = NULL;
726 struct rsp_que *rsp = NULL;
727 struct scsi_cmnd *cmd = sp->cmd;
728 struct scsi_qla_host *vha = sp->vha;
729 struct qla_hw_data *ha = vha->hw;
730 device_reg_t __iomem *reg;
731 uint16_t que_id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700732
733 /* Setup device pointers. */
734 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800735 que_id = vha->req_ques[0];
736
737 req = ha->req_q_map[que_id];
738 reg = ISP_QUE_REG(ha, req->id);
739
740 if (req->rsp)
741 rsp = req->rsp;
742 else
743 rsp = ha->rsp_q_map[que_id];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700744 /* So we know we haven't pci_map'ed anything yet */
745 tot_dsds = 0;
746
747 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800748 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800749 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
750 != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700751 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800752 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700753 }
754
755 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800756 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700757
758 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800759 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700760 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
761 handle++;
762 if (handle == MAX_OUTSTANDING_COMMANDS)
763 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800764 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700765 break;
766 }
767 if (index == MAX_OUTSTANDING_COMMANDS)
768 goto queuing_error;
769
770 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700771 if (scsi_sg_count(cmd)) {
772 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
773 scsi_sg_count(cmd), cmd->sc_data_direction);
774 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700775 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700776 } else
777 nseg = 0;
778
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900779 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700780
781 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800782 if (req->cnt < (req_cnt + 2)) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800783 if (ha->mqenable)
784 cnt = (uint16_t)
785 RD_REG_DWORD_RELAXED(&reg->isp25mq.req_q_out);
786 else
787 cnt = (uint16_t)
788 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_out);
789
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800790 if (req->ring_index < cnt)
791 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700792 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800793 req->cnt = req->length -
794 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700795 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800796 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700797 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700798
799 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800800 req->current_outstanding_cmd = handle;
801 req->outstanding_cmds[handle] = sp;
802 sp->vha = vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700803 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800804 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700805
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800806 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700807 cmd_pkt->handle = handle;
808
809 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500810 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700811 clr_ptr = (uint32_t *)cmd_pkt + 2;
812 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
813 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
814
815 /* Set NPORT-ID and LUN number*/
816 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
817 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
818 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
819 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700820 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700821
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700822 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800823 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700824
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700825 /* Load SCSI command packet. */
826 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
827 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
828
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900829 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700830
831 /* Build IOCB segments */
832 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
833
834 /* Set total data segment count. */
835 cmd_pkt->entry_count = (uint8_t)req_cnt;
836 wmb();
837
838 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800839 req->ring_index++;
840 if (req->ring_index == req->length) {
841 req->ring_index = 0;
842 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700843 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800844 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700845
846 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700847
848 /* Set chip new ring index. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800849 if (ha->mqenable)
850 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
851 else {
852 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
853 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
854 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700855
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700856 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800857 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800858 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
859 qla24xx_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700860
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800861 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700862 return QLA_SUCCESS;
863
864queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900865 if (tot_dsds)
866 scsi_dma_unmap(cmd);
867
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800868 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700869
870 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800872