blob: 13396beae2cedcde63b00c6156b44696168e6cc0 [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
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -070018static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070019/**
20 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
21 * @cmd: SCSI command
22 *
23 * Returns the proper CF_* direction based on CDB.
24 */
25static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070026qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 uint16_t cflags;
29
30 cflags = 0;
31
32 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070033 if (sp->cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080035 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070036 scsi_bufflen(sp->cmd);
37 } else if (sp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080039 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070040 scsi_bufflen(sp->cmd);
41 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return (cflags);
43}
44
45/**
46 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
47 * Continuation Type 0 IOCBs to allocate.
48 *
49 * @dsds: number of data segment decriptors needed
50 *
51 * Returns the number of IOCB entries needed to store @dsds.
52 */
53uint16_t
54qla2x00_calc_iocbs_32(uint16_t dsds)
55{
56 uint16_t iocbs;
57
58 iocbs = 1;
59 if (dsds > 3) {
60 iocbs += (dsds - 3) / 7;
61 if ((dsds - 3) % 7)
62 iocbs++;
63 }
64 return (iocbs);
65}
66
67/**
68 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
69 * Continuation Type 1 IOCBs to allocate.
70 *
71 * @dsds: number of data segment decriptors needed
72 *
73 * Returns the number of IOCB entries needed to store @dsds.
74 */
75uint16_t
76qla2x00_calc_iocbs_64(uint16_t dsds)
77{
78 uint16_t iocbs;
79
80 iocbs = 1;
81 if (dsds > 2) {
82 iocbs += (dsds - 2) / 5;
83 if ((dsds - 2) % 5)
84 iocbs++;
85 }
86 return (iocbs);
87}
88
89/**
90 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
91 * @ha: HA context
92 *
93 * Returns a pointer to the Continuation Type 0 IOCB packet.
94 */
95static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070096qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070099 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800101 req->ring_index++;
102 if (req->ring_index == req->length) {
103 req->ring_index = 0;
104 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800106 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800109 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 /* Load packet defaults. */
112 *((uint32_t *)(&cont_pkt->entry_type)) =
113 __constant_cpu_to_le32(CONTINUE_TYPE);
114
115 return (cont_pkt);
116}
117
118/**
119 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
120 * @ha: HA context
121 *
122 * Returns a pointer to the continuation type 1 IOCB packet.
123 */
124static inline cont_a64_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700125qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 cont_a64_entry_t *cont_pkt;
128
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700129 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800131 req->ring_index++;
132 if (req->ring_index == req->length) {
133 req->ring_index = 0;
134 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800136 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800139 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 /* Load packet defaults. */
142 *((uint32_t *)(&cont_pkt->entry_type)) =
143 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
144
145 return (cont_pkt);
146}
147
148/**
149 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
150 * capable IOCB types.
151 *
152 * @sp: SRB command to process
153 * @cmd_pkt: Command type 2 IOCB
154 * @tot_dsds: Total number of segments to transfer
155 */
156void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
157 uint16_t tot_dsds)
158{
159 uint16_t avail_dsds;
160 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800161 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900163 struct scatterlist *sg;
164 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 cmd = sp->cmd;
167
168 /* Update entry type to indicate Command Type 2 IOCB */
169 *((uint32_t *)(&cmd_pkt->entry_type)) =
170 __constant_cpu_to_le32(COMMAND_TYPE);
171
172 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900173 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
175 return;
176 }
177
Andrew Vasquez444786d2009-01-05 11:18:10 -0800178 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700179 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* Three DSDs are available in the Command Type 2 IOCB */
182 avail_dsds = 3;
183 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
184
185 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900186 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
187 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900189 /* Allocate additional continuation packets? */
190 if (avail_dsds == 0) {
191 /*
192 * Seven DSDs are available in the Continuation
193 * Type 0 IOCB.
194 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700195 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900196 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
197 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900199
200 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
201 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
202 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204}
205
206/**
207 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
208 * capable IOCB types.
209 *
210 * @sp: SRB command to process
211 * @cmd_pkt: Command type 3 IOCB
212 * @tot_dsds: Total number of segments to transfer
213 */
214void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
215 uint16_t tot_dsds)
216{
217 uint16_t avail_dsds;
218 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800219 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900221 struct scatterlist *sg;
222 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 cmd = sp->cmd;
225
226 /* Update entry type to indicate Command Type 3 IOCB */
227 *((uint32_t *)(&cmd_pkt->entry_type)) =
228 __constant_cpu_to_le32(COMMAND_A64_TYPE);
229
230 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900231 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
233 return;
234 }
235
Andrew Vasquez444786d2009-01-05 11:18:10 -0800236 vha = sp->fcport->vha;
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 Chakraborty67c2e932009-04-06 22:33:42 -0700254 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;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800290 struct rsp_que *rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Setup device pointers. */
293 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800294 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800295 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700296 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 cmd = sp->cmd;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800298 req = ha->req_q_map[0];
299 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500300 /* So we know we haven't pci_map'ed anything yet */
301 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800304 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800305 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
306 != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800308 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
311 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700312 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800315 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
317 handle++;
318 if (handle == MAX_OUTSTANDING_COMMANDS)
319 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800320 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 }
323 if (index == MAX_OUTSTANDING_COMMANDS)
324 goto queuing_error;
325
83021922005-04-17 15:10:41 -0500326 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700327 if (scsi_sg_count(cmd)) {
328 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
329 scsi_sg_count(cmd), cmd->sc_data_direction);
330 if (unlikely(!nseg))
331 goto queuing_error;
332 } else
333 nseg = 0;
334
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900335 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700338 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800339 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800341 if (req->ring_index < cnt)
342 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800344 req->cnt = req->length -
345 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800347 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 goto queuing_error;
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800351 req->current_outstanding_cmd = handle;
352 req->outstanding_cmds[handle] = sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800354 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800356 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 cmd_pkt->handle = handle;
358 /* Zero out remaining portion of packet. */
359 clr_ptr = (uint32_t *)cmd_pkt + 2;
360 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
361 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
362
bdf79622005-04-17 15:06:53 -0500363 /* Set target ID and LUN number*/
364 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
365 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 /* Update tagged queuing modifier */
368 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 /* Load SCSI command packet. */
371 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900372 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700375 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* Set total data segment count. */
378 cmd_pkt->entry_count = (uint8_t)req_cnt;
379 wmb();
380
381 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800382 req->ring_index++;
383 if (req->ring_index == req->length) {
384 req->ring_index = 0;
385 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800387 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800392 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
394
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700395 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800396 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800397 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
398 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700399
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700400 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 return (QLA_SUCCESS);
402
403queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900404 if (tot_dsds)
405 scsi_dma_unmap(cmd);
406
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700407 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 return (QLA_FUNCTION_FAILED);
410}
411
412/**
413 * qla2x00_marker() - Send a marker IOCB to the firmware.
414 * @ha: HA context
415 * @loop_id: loop ID
416 * @lun: LUN
417 * @type: marker modifier
418 *
419 * Can be called from both normal and interrupt context.
420 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700421 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700423int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800424__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
425 struct rsp_que *rsp, uint16_t loop_id,
426 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700428 mrk_entry_t *mrk;
429 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800430 struct qla_hw_data *ha = vha->hw;
431 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700433 mrk24 = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800434 mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700435 if (mrk == NULL) {
436 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800437 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 return (QLA_FUNCTION_FAILED);
440 }
441
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700442 mrk->entry_type = MARKER_TYPE;
443 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700445 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700446 mrk24 = (struct mrk_entry_24xx *) mrk;
447 mrk24->nport_handle = cpu_to_le16(loop_id);
448 mrk24->lun[1] = LSB(lun);
449 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700450 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800451 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700452 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700453 } else {
454 SET_TARGET_ID(ha, mrk->target, loop_id);
455 mrk->lun = cpu_to_le16(lun);
456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 wmb();
459
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800460 qla2x00_isp_cmd(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 return (QLA_SUCCESS);
463}
464
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700465int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800466qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
467 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
468 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 int ret;
471 unsigned long flags = 0;
472
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800473 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
474 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
475 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return (ret);
478}
479
480/**
481 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
482 * @ha: HA context
483 *
484 * Note: The caller must hold the hardware lock before calling this routine.
485 *
486 * Returns NULL if function failed, else, a pointer to the request packet.
487 */
488static request_t *
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800489qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
490 struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800492 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800493 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 request_t *pkt = NULL;
495 uint16_t cnt;
496 uint32_t *dword_ptr;
497 uint32_t timer;
498 uint16_t req_cnt = 1;
499
500 /* Wait 1 second for slot. */
501 for (timer = HZ; timer; timer--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800502 if ((req_cnt + 2) >= req->cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Calculate number of free request entries. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800504 if (ha->mqenable)
505 cnt = (uint16_t)
506 RD_REG_DWORD(&reg->isp25mq.req_q_out);
507 else {
508 if (IS_FWI2_CAPABLE(ha))
509 cnt = (uint16_t)RD_REG_DWORD(
510 &reg->isp24.req_q_out);
511 else
512 cnt = qla2x00_debounce_register(
513 ISP_REQ_Q_OUT(ha, &reg->isp));
514 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800515 if (req->ring_index < cnt)
516 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800518 req->cnt = req->length -
519 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 /* If room for request in request ring. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800522 if ((req_cnt + 2) < req->cnt) {
523 req->cnt--;
524 pkt = req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* Zero out packet. */
527 dword_ptr = (uint32_t *)pkt;
528 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
529 *dword_ptr++ = 0;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Set entry count. */
532 pkt->entry_count = 1;
533
534 break;
535 }
536
537 /* Release ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800538 spin_unlock_irq(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 udelay(2); /* 2 us */
541
542 /* Check for pending interrupts. */
543 /* During init we issue marker directly */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800544 if (!vha->marker_needed && !vha->flags.init_done)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800545 qla2x00_poll(rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 spin_lock_irq(&ha->hardware_lock);
547 }
548 if (!pkt) {
549 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
550 }
551
552 return (pkt);
553}
554
555/**
556 * qla2x00_isp_cmd() - Modify the request ring pointer.
557 * @ha: HA context
558 *
559 * Note: The caller must hold the hardware lock before calling this routine.
560 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700561static void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800562qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800564 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800565 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800566 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 DEBUG5(printk("%s(): IOCB data:\n", __func__));
569 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800570 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800573 req->ring_index++;
574 if (req->ring_index == req->length) {
575 req->ring_index = 0;
576 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800578 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 /* Set chip new ring index. */
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800581 if (ha->mqenable) {
582 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
583 RD_REG_DWORD(&ioreg->hccr);
584 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800585 else {
586 if (IS_FWI2_CAPABLE(ha)) {
587 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
588 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
589 } else {
590 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
591 req->ring_index);
592 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
593 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700594 }
595
596}
597
598/**
599 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
600 * Continuation Type 1 IOCBs to allocate.
601 *
602 * @dsds: number of data segment decriptors needed
603 *
604 * Returns the number of IOCB entries needed to store @dsds.
605 */
606static inline uint16_t
607qla24xx_calc_iocbs(uint16_t dsds)
608{
609 uint16_t iocbs;
610
611 iocbs = 1;
612 if (dsds > 1) {
613 iocbs += (dsds - 1) / 5;
614 if ((dsds - 1) % 5)
615 iocbs++;
616 }
617 return iocbs;
618}
619
620/**
621 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
622 * IOCB types.
623 *
624 * @sp: SRB command to process
625 * @cmd_pkt: Command type 3 IOCB
626 * @tot_dsds: Total number of segments to transfer
627 */
628static inline void
629qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
630 uint16_t tot_dsds)
631{
632 uint16_t avail_dsds;
633 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800634 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700635 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900636 struct scatterlist *sg;
637 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800638 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700639
640 cmd = sp->cmd;
641
642 /* Update entry type to indicate Command Type 3 IOCB */
643 *((uint32_t *)(&cmd_pkt->entry_type)) =
644 __constant_cpu_to_le32(COMMAND_TYPE_7);
645
646 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900647 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700648 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
649 return;
650 }
651
Andrew Vasquez444786d2009-01-05 11:18:10 -0800652 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700653 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700654
655 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700656 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700657 cmd_pkt->task_mgmt_flags =
658 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800659 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700660 scsi_bufflen(sp->cmd);
661 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700662 cmd_pkt->task_mgmt_flags =
663 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800664 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700665 scsi_bufflen(sp->cmd);
666 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700667
668 /* One DSD is available in the Command Type 3 IOCB */
669 avail_dsds = 1;
670 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
671
672 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700673
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900674 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
675 dma_addr_t sle_dma;
676 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700677
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900678 /* Allocate additional continuation packets? */
679 if (avail_dsds == 0) {
680 /*
681 * Five DSDs are available in the Continuation
682 * Type 1 IOCB.
683 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700684 cont_pkt = qla2x00_prep_cont_type1_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900685 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
686 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700687 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900688
689 sle_dma = sg_dma_address(sg);
690 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
691 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
692 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
693 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700694 }
695}
696
697
698/**
699 * qla24xx_start_scsi() - Send a SCSI command to the ISP
700 * @sp: command to send to the ISP
701 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700702 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700703 */
704int
705qla24xx_start_scsi(srb_t *sp)
706{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900707 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700708 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700709 uint32_t *clr_ptr;
710 uint32_t index;
711 uint32_t handle;
712 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700713 uint16_t cnt;
714 uint16_t req_cnt;
715 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800716 struct req_que *req = NULL;
717 struct rsp_que *rsp = NULL;
718 struct scsi_cmnd *cmd = sp->cmd;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800719 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800720 struct qla_hw_data *ha = vha->hw;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700721
722 /* Setup device pointers. */
723 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800724
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -0700725 qla25xx_set_que(sp, &rsp);
726 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800727
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700728 /* So we know we haven't pci_map'ed anything yet */
729 tot_dsds = 0;
730
731 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800732 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800733 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
734 != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700735 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800736 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700737 }
738
739 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800740 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700741
742 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800743 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700744 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
745 handle++;
746 if (handle == MAX_OUTSTANDING_COMMANDS)
747 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800748 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700749 break;
750 }
751 if (index == MAX_OUTSTANDING_COMMANDS)
752 goto queuing_error;
753
754 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700755 if (scsi_sg_count(cmd)) {
756 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
757 scsi_sg_count(cmd), cmd->sc_data_direction);
758 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700759 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700760 } else
761 nseg = 0;
762
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900763 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700764
765 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800766 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -0700767 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800768
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800769 if (req->ring_index < cnt)
770 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700771 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800772 req->cnt = req->length -
773 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700774 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800775 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700776 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700777
778 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800779 req->current_outstanding_cmd = handle;
780 req->outstanding_cmds[handle] = sp;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700781 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800782 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700783
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800784 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700785 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700786
787 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500788 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700789 clr_ptr = (uint32_t *)cmd_pkt + 2;
790 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
791 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
792
793 /* Set NPORT-ID and LUN number*/
794 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
795 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
796 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
797 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700798 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700799
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700800 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800801 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700802
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700803 /* Load SCSI command packet. */
804 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
805 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
806
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900807 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700808
809 /* Build IOCB segments */
810 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
811
812 /* Set total data segment count. */
813 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700814 /* Specify response queue number where completion should happen */
815 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700816 wmb();
817
818 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800819 req->ring_index++;
820 if (req->ring_index == req->length) {
821 req->ring_index = 0;
822 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700823 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800824 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700825
826 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700827
828 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -0700829 WRT_REG_DWORD(req->req_q_in, req->ring_index);
830 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700831
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700832 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800833 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800834 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700835 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700836
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800837 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700838 return QLA_SUCCESS;
839
840queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900841 if (tot_dsds)
842 scsi_dma_unmap(cmd);
843
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800844 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700845
846 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700848
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -0700849static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700850{
851 struct scsi_cmnd *cmd = sp->cmd;
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700852 struct qla_hw_data *ha = sp->fcport->vha->hw;
853 int affinity = cmd->request->cpu;
854
855 if (ql2xmultique_tag && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -0700856 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700857 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -0700858 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700859 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -0700860}