blob: 5bedc9d05942756a6734d479f3a0b8137eb1a221 [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;
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;
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800177 req = sp->que;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
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 Chakraborty73208df2008-12-09 16:45:39 -0800195 cont_pkt = qla2x00_prep_cont_type0_iocb(req, 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;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800223 struct req_que *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 cmd = sp->cmd;
226
227 /* Update entry type to indicate Command Type 3 IOCB */
228 *((uint32_t *)(&cmd_pkt->entry_type)) =
229 __constant_cpu_to_le32(COMMAND_A64_TYPE);
230
231 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900232 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
234 return;
235 }
236
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800237 vha = sp->vha;
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800238 req = sp->que;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700240 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* Two DSDs are available in the Command Type 3 IOCB */
243 avail_dsds = 2;
244 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
245
246 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900247 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
248 dma_addr_t sle_dma;
249 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900251 /* Allocate additional continuation packets? */
252 if (avail_dsds == 0) {
253 /*
254 * Five DSDs are available in the Continuation
255 * Type 1 IOCB.
256 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800257 cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900258 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
259 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900261
262 sle_dma = sg_dma_address(sg);
263 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
264 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
265 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
266 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268}
269
270/**
271 * qla2x00_start_scsi() - Send a SCSI command to the ISP
272 * @sp: command to send to the ISP
273 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700274 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 */
276int
277qla2x00_start_scsi(srb_t *sp)
278{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900279 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800281 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct scsi_cmnd *cmd;
283 uint32_t *clr_ptr;
284 uint32_t index;
285 uint32_t handle;
286 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 uint16_t cnt;
288 uint16_t req_cnt;
289 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700290 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800291 struct qla_hw_data *ha;
292 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800293 struct rsp_que *rsp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* Setup device pointers. */
296 ret = 0;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800297 vha = sp->vha;
298 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700299 reg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 cmd = sp->cmd;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800301 req = ha->req_q_map[0];
302 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500303 /* So we know we haven't pci_map'ed anything yet */
304 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800307 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800308 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
309 != QLA_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 return (QLA_FUNCTION_FAILED);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800311 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 }
313
314 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700315 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800318 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
320 handle++;
321 if (handle == MAX_OUTSTANDING_COMMANDS)
322 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800323 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 }
326 if (index == MAX_OUTSTANDING_COMMANDS)
327 goto queuing_error;
328
83021922005-04-17 15:10:41 -0500329 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700330 if (scsi_sg_count(cmd)) {
331 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
332 scsi_sg_count(cmd), cmd->sc_data_direction);
333 if (unlikely(!nseg))
334 goto queuing_error;
335 } else
336 nseg = 0;
337
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900338 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700341 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800342 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800344 if (req->ring_index < cnt)
345 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800347 req->cnt = req->length -
348 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800350 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 goto queuing_error;
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800354 req->current_outstanding_cmd = handle;
355 req->outstanding_cmds[handle] = sp;
356 sp->vha = vha;
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800357 sp->que = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800359 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800361 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 cmd_pkt->handle = handle;
363 /* Zero out remaining portion of packet. */
364 clr_ptr = (uint32_t *)cmd_pkt + 2;
365 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
366 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
367
bdf79622005-04-17 15:06:53 -0500368 /* Set target ID and LUN number*/
369 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
370 cmd_pkt->lun = cpu_to_le16(sp->cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Update tagged queuing modifier */
373 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 /* Load SCSI command packet. */
376 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900377 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700380 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* Set total data segment count. */
383 cmd_pkt->entry_count = (uint8_t)req_cnt;
384 wmb();
385
386 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800387 req->ring_index++;
388 if (req->ring_index == req->length) {
389 req->ring_index = 0;
390 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800392 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
399
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700400 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800401 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800402 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
403 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700404
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700405 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 return (QLA_SUCCESS);
407
408queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900409 if (tot_dsds)
410 scsi_dma_unmap(cmd);
411
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700412 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 return (QLA_FUNCTION_FAILED);
415}
416
417/**
418 * qla2x00_marker() - Send a marker IOCB to the firmware.
419 * @ha: HA context
420 * @loop_id: loop ID
421 * @lun: LUN
422 * @type: marker modifier
423 *
424 * Can be called from both normal and interrupt context.
425 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700426 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 */
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700428int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800429__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
430 struct rsp_que *rsp, uint16_t loop_id,
431 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700433 mrk_entry_t *mrk;
434 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800435 struct qla_hw_data *ha = vha->hw;
436 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700438 mrk24 = NULL;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800439 mrk = (mrk_entry_t *)qla2x00_req_pkt(vha, req, rsp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700440 if (mrk == NULL) {
441 DEBUG2_3(printk("%s(%ld): failed to allocate Marker IOCB.\n",
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800442 __func__, base_vha->host_no));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return (QLA_FUNCTION_FAILED);
445 }
446
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700447 mrk->entry_type = MARKER_TYPE;
448 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700450 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700451 mrk24 = (struct mrk_entry_24xx *) mrk;
452 mrk24->nport_handle = cpu_to_le16(loop_id);
453 mrk24->lun[1] = LSB(lun);
454 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700455 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800456 mrk24->vp_index = vha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700457 } else {
458 SET_TARGET_ID(ha, mrk->target, loop_id);
459 mrk->lun = cpu_to_le16(lun);
460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 wmb();
463
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800464 qla2x00_isp_cmd(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 return (QLA_SUCCESS);
467}
468
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700469int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800470qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
471 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
472 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 int ret;
475 unsigned long flags = 0;
476
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800477 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
478 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
479 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 return (ret);
482}
483
484/**
485 * qla2x00_req_pkt() - Retrieve a request packet from the request ring.
486 * @ha: HA context
487 *
488 * Note: The caller must hold the hardware lock before calling this routine.
489 *
490 * Returns NULL if function failed, else, a pointer to the request packet.
491 */
492static request_t *
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800493qla2x00_req_pkt(struct scsi_qla_host *vha, struct req_que *req,
494 struct rsp_que *rsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800496 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800497 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 request_t *pkt = NULL;
499 uint16_t cnt;
500 uint32_t *dword_ptr;
501 uint32_t timer;
502 uint16_t req_cnt = 1;
503
504 /* Wait 1 second for slot. */
505 for (timer = HZ; timer; timer--) {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800506 if ((req_cnt + 2) >= req->cnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 /* Calculate number of free request entries. */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800508 if (ha->mqenable)
509 cnt = (uint16_t)
510 RD_REG_DWORD(&reg->isp25mq.req_q_out);
511 else {
512 if (IS_FWI2_CAPABLE(ha))
513 cnt = (uint16_t)RD_REG_DWORD(
514 &reg->isp24.req_q_out);
515 else
516 cnt = qla2x00_debounce_register(
517 ISP_REQ_Q_OUT(ha, &reg->isp));
518 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800519 if (req->ring_index < cnt)
520 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800522 req->cnt = req->length -
523 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 /* If room for request in request ring. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800526 if ((req_cnt + 2) < req->cnt) {
527 req->cnt--;
528 pkt = req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 /* Zero out packet. */
531 dword_ptr = (uint32_t *)pkt;
532 for (cnt = 0; cnt < REQUEST_ENTRY_SIZE / 4; cnt++)
533 *dword_ptr++ = 0;
534
535 /* Set system defined field. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800536 pkt->sys_define = (uint8_t)req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 /* Set entry count. */
539 pkt->entry_count = 1;
540
541 break;
542 }
543
544 /* Release ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800545 spin_unlock_irq(&ha->hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 udelay(2); /* 2 us */
548
549 /* Check for pending interrupts. */
550 /* During init we issue marker directly */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800551 if (!vha->marker_needed && !vha->flags.init_done)
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800552 qla2x00_poll(rsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 spin_lock_irq(&ha->hardware_lock);
554 }
555 if (!pkt) {
556 DEBUG2_3(printk("%s(): **** FAILED ****\n", __func__));
557 }
558
559 return (pkt);
560}
561
562/**
563 * qla2x00_isp_cmd() - Modify the request ring pointer.
564 * @ha: HA context
565 *
566 * Note: The caller must hold the hardware lock before calling this routine.
567 */
Adrian Bunk413975a2006-06-30 02:33:06 -0700568static void
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800569qla2x00_isp_cmd(struct scsi_qla_host *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800571 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800572 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800573 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 DEBUG5(printk("%s(): IOCB data:\n", __func__));
576 DEBUG5(qla2x00_dump_buffer(
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800577 (uint8_t *)req->ring_ptr, REQUEST_ENTRY_SIZE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800580 req->ring_index++;
581 if (req->ring_index == req->length) {
582 req->ring_index = 0;
583 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800585 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 /* Set chip new ring index. */
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800588 if (ha->mqenable) {
589 WRT_REG_DWORD(&reg->isp25mq.req_q_in, req->ring_index);
590 RD_REG_DWORD(&ioreg->hccr);
591 }
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800592 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 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700646
647 cmd = sp->cmd;
648
649 /* Update entry type to indicate Command Type 3 IOCB */
650 *((uint32_t *)(&cmd_pkt->entry_type)) =
651 __constant_cpu_to_le32(COMMAND_TYPE_7);
652
653 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900654 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700655 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
656 return;
657 }
658
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800659 vha = sp->vha;
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800660 req = sp->que;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700661
662 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700663 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700664 cmd_pkt->task_mgmt_flags =
665 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800666 sp->fcport->vha->hw->qla_stats.output_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700667 scsi_bufflen(sp->cmd);
668 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700669 cmd_pkt->task_mgmt_flags =
670 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800671 sp->fcport->vha->hw->qla_stats.input_bytes +=
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700672 scsi_bufflen(sp->cmd);
673 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700674
675 /* One DSD is available in the Command Type 3 IOCB */
676 avail_dsds = 1;
677 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
678
679 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700680
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900681 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
682 dma_addr_t sle_dma;
683 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700684
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900685 /* Allocate additional continuation packets? */
686 if (avail_dsds == 0) {
687 /*
688 * Five DSDs are available in the Continuation
689 * Type 1 IOCB.
690 */
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800691 cont_pkt = qla2x00_prep_cont_type1_iocb(req, vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900692 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
693 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700694 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900695
696 sle_dma = sg_dma_address(sg);
697 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
698 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
699 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
700 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700701 }
702}
703
704
705/**
706 * qla24xx_start_scsi() - Send a SCSI command to the ISP
707 * @sp: command to send to the ISP
708 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700709 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700710 */
711int
712qla24xx_start_scsi(srb_t *sp)
713{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900714 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700715 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700716 uint32_t *clr_ptr;
717 uint32_t index;
718 uint32_t handle;
719 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700720 uint16_t cnt;
721 uint16_t req_cnt;
722 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800723 struct req_que *req = NULL;
724 struct rsp_que *rsp = NULL;
725 struct scsi_cmnd *cmd = sp->cmd;
726 struct scsi_qla_host *vha = sp->vha;
727 struct qla_hw_data *ha = vha->hw;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800728 uint16_t que_id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700729
730 /* Setup device pointers. */
731 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800732 que_id = vha->req_ques[0];
733
734 req = ha->req_q_map[que_id];
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800735 sp->que = req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800736
737 if (req->rsp)
738 rsp = req->rsp;
739 else
740 rsp = ha->rsp_q_map[que_id];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700741 /* So we know we haven't pci_map'ed anything yet */
742 tot_dsds = 0;
743
744 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800745 if (vha->marker_needed != 0) {
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800746 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL)
747 != QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700748 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800749 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700750 }
751
752 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800753 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700754
755 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800756 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700757 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
758 handle++;
759 if (handle == MAX_OUTSTANDING_COMMANDS)
760 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800761 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700762 break;
763 }
764 if (index == MAX_OUTSTANDING_COMMANDS)
765 goto queuing_error;
766
767 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700768 if (scsi_sg_count(cmd)) {
769 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
770 scsi_sg_count(cmd), cmd->sc_data_direction);
771 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700772 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700773 } else
774 nseg = 0;
775
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900776 tot_dsds = nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700777
778 req_cnt = qla24xx_calc_iocbs(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800779 if (req->cnt < (req_cnt + 2)) {
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800780 cnt = ha->isp_ops->rd_req_reg(ha, req->id);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800781
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800782 if (req->ring_index < cnt)
783 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700784 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800785 req->cnt = req->length -
786 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700787 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800788 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700789 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700790
791 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800792 req->current_outstanding_cmd = handle;
793 req->outstanding_cmds[handle] = sp;
794 sp->vha = vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700795 sp->cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800796 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700797
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800798 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700799 cmd_pkt->handle = handle;
800
801 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -0500802 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700803 clr_ptr = (uint32_t *)cmd_pkt + 2;
804 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
805 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
806
807 /* Set NPORT-ID and LUN number*/
808 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
809 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
810 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
811 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700812 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700813
Andrew Vasquez661c3f62005-10-27 11:09:58 -0700814 int_to_scsilun(sp->cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -0800815 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700816
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700817 /* Load SCSI command packet. */
818 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
819 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
820
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900821 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700822
823 /* Build IOCB segments */
824 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
825
826 /* Set total data segment count. */
827 cmd_pkt->entry_count = (uint8_t)req_cnt;
828 wmb();
829
830 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800831 req->ring_index++;
832 if (req->ring_index == req->length) {
833 req->ring_index = 0;
834 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700835 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800836 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700837
838 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700839
840 /* Set chip new ring index. */
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800841 ha->isp_ops->wrt_req_reg(ha, req->id, req->ring_index);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700842
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700843 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800844 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800845 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
846 qla24xx_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700847
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800848 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700849 return QLA_SUCCESS;
850
851queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900852 if (tot_dsds)
853 scsi_dma_unmap(cmd);
854
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800855 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700856
857 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800859
Anirban Chakraborty17d98632008-12-18 10:06:15 -0800860uint16_t
861qla24xx_rd_req_reg(struct qla_hw_data *ha, uint16_t id)
862{
863 device_reg_t __iomem *reg = (void *) ha->iobase;
864 return RD_REG_DWORD_RELAXED(&reg->isp24.req_q_out);
865}
866
867uint16_t
868qla25xx_rd_req_reg(struct qla_hw_data *ha, uint16_t id)
869{
870 device_reg_t __iomem *reg = (void *) ha->mqiobase + QLA_QUE_PAGE * id;
871 return RD_REG_DWORD_RELAXED(&reg->isp25mq.req_q_out);
872}
873
874void
875qla24xx_wrt_req_reg(struct qla_hw_data *ha, uint16_t id, uint16_t index)
876{
877 device_reg_t __iomem *reg = (void *) ha->iobase;
878 WRT_REG_DWORD(&reg->isp24.req_q_in, index);
879 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
880}
881
882void
883qla25xx_wrt_req_reg(struct qla_hw_data *ha, uint16_t id, uint16_t index)
884{
885 device_reg_t __iomem *reg = (void *) ha->mqiobase + QLA_QUE_PAGE * id;
886 struct device_reg_2xxx __iomem *ioreg = &ha->iobase->isp;
887 WRT_REG_DWORD(&reg->isp25mq.req_q_in, index);
888 RD_REG_DWORD(&ioreg->hccr); /* PCI posting */
889}
890