blob: 7404c6e7865e04dbb333e3def47fd5d5d97223fa [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez07e264b2011-03-30 11:46:23 -07003 * Copyright (c) 2003-2011 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 Chakraborty59e0b8b2009-06-03 09:55:19 -070014static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/**
16 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
17 * @cmd: SCSI command
18 *
19 * Returns the proper CF_* direction based on CDB.
20 */
21static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070022qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 uint16_t cflags;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080025 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040026 struct scsi_qla_host *vha = sp->fcport->vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28 cflags = 0;
29
30 /* Set transfer direction */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080031 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 cflags = CF_WRITE;
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040033 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080034 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 cflags = CF_READ;
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040036 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 return (cflags);
39}
40
41/**
42 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
43 * Continuation Type 0 IOCBs to allocate.
44 *
45 * @dsds: number of data segment decriptors needed
46 *
47 * Returns the number of IOCB entries needed to store @dsds.
48 */
49uint16_t
50qla2x00_calc_iocbs_32(uint16_t dsds)
51{
52 uint16_t iocbs;
53
54 iocbs = 1;
55 if (dsds > 3) {
56 iocbs += (dsds - 3) / 7;
57 if ((dsds - 3) % 7)
58 iocbs++;
59 }
60 return (iocbs);
61}
62
63/**
64 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
65 * Continuation Type 1 IOCBs to allocate.
66 *
67 * @dsds: number of data segment decriptors needed
68 *
69 * Returns the number of IOCB entries needed to store @dsds.
70 */
71uint16_t
72qla2x00_calc_iocbs_64(uint16_t dsds)
73{
74 uint16_t iocbs;
75
76 iocbs = 1;
77 if (dsds > 2) {
78 iocbs += (dsds - 2) / 5;
79 if ((dsds - 2) % 5)
80 iocbs++;
81 }
82 return (iocbs);
83}
84
85/**
86 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
87 * @ha: HA context
88 *
89 * Returns a pointer to the Continuation Type 0 IOCB packet.
90 */
91static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070092qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070095 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080097 req->ring_index++;
98 if (req->ring_index == req->length) {
99 req->ring_index = 0;
100 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800102 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
104
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800105 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* Load packet defaults. */
108 *((uint32_t *)(&cont_pkt->entry_type)) =
109 __constant_cpu_to_le32(CONTINUE_TYPE);
110
111 return (cont_pkt);
112}
113
114/**
115 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
116 * @ha: HA context
117 *
118 * Returns a pointer to the continuation type 1 IOCB packet.
119 */
120static inline cont_a64_entry_t *
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800121qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
123 cont_a64_entry_t *cont_pkt;
124
125 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800126 req->ring_index++;
127 if (req->ring_index == req->length) {
128 req->ring_index = 0;
129 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800131 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800134 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /* Load packet defaults. */
137 *((uint32_t *)(&cont_pkt->entry_type)) =
138 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
139
140 return (cont_pkt);
141}
142
Arun Easibad75002010-05-04 15:01:30 -0700143static inline int
144qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
145{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800146 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
147 uint8_t guard = scsi_host_get_guard(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700148
149 /* We only support T10 DIF right now */
150 if (guard != SHOST_DIX_GUARD_CRC) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700151 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800152 "Unsupported guard: %d for cmd=%p.\n", guard, cmd);
Arun Easibad75002010-05-04 15:01:30 -0700153 return 0;
154 }
155
156 /* We always use DIFF Bundling for best performance */
157 *fw_prot_opts = 0;
158
159 /* Translate SCSI opcode to a protection opcode */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800160 switch (scsi_get_prot_op(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700161 case SCSI_PROT_READ_STRIP:
162 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
163 break;
164 case SCSI_PROT_WRITE_INSERT:
165 *fw_prot_opts |= PO_MODE_DIF_INSERT;
166 break;
167 case SCSI_PROT_READ_INSERT:
168 *fw_prot_opts |= PO_MODE_DIF_INSERT;
169 break;
170 case SCSI_PROT_WRITE_STRIP:
171 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
172 break;
173 case SCSI_PROT_READ_PASS:
174 *fw_prot_opts |= PO_MODE_DIF_PASS;
175 break;
176 case SCSI_PROT_WRITE_PASS:
177 *fw_prot_opts |= PO_MODE_DIF_PASS;
178 break;
179 default: /* Normal Request */
180 *fw_prot_opts |= PO_MODE_DIF_PASS;
181 break;
182 }
183
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800184 return scsi_prot_sg_count(cmd);
Arun Easibad75002010-05-04 15:01:30 -0700185}
186
187/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
189 * capable IOCB types.
190 *
191 * @sp: SRB command to process
192 * @cmd_pkt: Command type 2 IOCB
193 * @tot_dsds: Total number of segments to transfer
194 */
195void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
196 uint16_t tot_dsds)
197{
198 uint16_t avail_dsds;
199 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800200 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900202 struct scatterlist *sg;
203 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800205 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207 /* Update entry type to indicate Command Type 2 IOCB */
208 *((uint32_t *)(&cmd_pkt->entry_type)) =
209 __constant_cpu_to_le32(COMMAND_TYPE);
210
211 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900212 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
214 return;
215 }
216
Andrew Vasquez444786d2009-01-05 11:18:10 -0800217 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700218 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Three DSDs are available in the Command Type 2 IOCB */
221 avail_dsds = 3;
222 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
223
224 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900225 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
226 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900228 /* Allocate additional continuation packets? */
229 if (avail_dsds == 0) {
230 /*
231 * Seven DSDs are available in the Continuation
232 * Type 0 IOCB.
233 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700234 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900235 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
236 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900238
239 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
240 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
241 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243}
244
245/**
246 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
247 * capable IOCB types.
248 *
249 * @sp: SRB command to process
250 * @cmd_pkt: Command type 3 IOCB
251 * @tot_dsds: Total number of segments to transfer
252 */
253void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
254 uint16_t tot_dsds)
255{
256 uint16_t avail_dsds;
257 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800258 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900260 struct scatterlist *sg;
261 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800263 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 /* Update entry type to indicate Command Type 3 IOCB */
266 *((uint32_t *)(&cmd_pkt->entry_type)) =
267 __constant_cpu_to_le32(COMMAND_A64_TYPE);
268
269 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900270 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
272 return;
273 }
274
Andrew Vasquez444786d2009-01-05 11:18:10 -0800275 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700276 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 /* Two DSDs are available in the Command Type 3 IOCB */
279 avail_dsds = 2;
280 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
281
282 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900283 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
284 dma_addr_t sle_dma;
285 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900287 /* Allocate additional continuation packets? */
288 if (avail_dsds == 0) {
289 /*
290 * Five DSDs are available in the Continuation
291 * Type 1 IOCB.
292 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800293 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900294 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
295 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900297
298 sle_dma = sg_dma_address(sg);
299 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
300 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
301 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
302 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
304}
305
306/**
307 * qla2x00_start_scsi() - Send a SCSI command to the ISP
308 * @sp: command to send to the ISP
309 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700310 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312int
313qla2x00_start_scsi(srb_t *sp)
314{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900315 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800317 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 struct scsi_cmnd *cmd;
319 uint32_t *clr_ptr;
320 uint32_t index;
321 uint32_t handle;
322 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 uint16_t cnt;
324 uint16_t req_cnt;
325 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700326 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800327 struct qla_hw_data *ha;
328 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800329 struct rsp_que *rsp;
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800330 char tag[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Setup device pointers. */
333 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800334 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800335 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700336 reg = &ha->iobase->isp;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800337 cmd = GET_CMD_SP(sp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800338 req = ha->req_q_map[0];
339 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500340 /* So we know we haven't pci_map'ed anything yet */
341 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800344 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700345 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
346 QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return (QLA_FUNCTION_FAILED);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700348 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800349 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700353 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800356 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
358 handle++;
359 if (handle == MAX_OUTSTANDING_COMMANDS)
360 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800361 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 break;
363 }
364 if (index == MAX_OUTSTANDING_COMMANDS)
365 goto queuing_error;
366
83021922005-04-17 15:10:41 -0500367 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700368 if (scsi_sg_count(cmd)) {
369 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
370 scsi_sg_count(cmd), cmd->sc_data_direction);
371 if (unlikely(!nseg))
372 goto queuing_error;
373 } else
374 nseg = 0;
375
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900376 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700379 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800380 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800382 if (req->ring_index < cnt)
383 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800385 req->cnt = req->length -
386 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -0400387 /* If still no head room then bail out */
388 if (req->cnt < (req_cnt + 2))
389 goto queuing_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800393 req->current_outstanding_cmd = handle;
394 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700395 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800396 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800399 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 cmd_pkt->handle = handle;
401 /* Zero out remaining portion of packet. */
402 clr_ptr = (uint32_t *)cmd_pkt + 2;
403 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
404 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
405
bdf79622005-04-17 15:06:53 -0500406 /* Set target ID and LUN number*/
407 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800408 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Update tagged queuing modifier */
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800411 if (scsi_populate_tag_msg(cmd, tag)) {
412 switch (tag[0]) {
413 case HEAD_OF_QUEUE_TAG:
414 cmd_pkt->control_flags =
415 __constant_cpu_to_le16(CF_HEAD_TAG);
416 break;
417 case ORDERED_QUEUE_TAG:
418 cmd_pkt->control_flags =
419 __constant_cpu_to_le16(CF_ORDERED_TAG);
420 break;
421 default:
422 cmd_pkt->control_flags =
423 __constant_cpu_to_le16(CF_SIMPLE_TAG);
424 break;
425 }
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Load SCSI command packet. */
429 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900430 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700433 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Set total data segment count. */
436 cmd_pkt->entry_count = (uint8_t)req_cnt;
437 wmb();
438
439 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800440 req->ring_index++;
441 if (req->ring_index == req->length) {
442 req->ring_index = 0;
443 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800445 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800450 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
452
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700453 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800454 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800455 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
456 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700457
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return (QLA_SUCCESS);
460
461queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900462 if (tot_dsds)
463 scsi_dma_unmap(cmd);
464
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700465 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return (QLA_FUNCTION_FAILED);
468}
469
470/**
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800471 * qla2x00_start_iocbs() - Execute the IOCB command
472 */
473static void
474qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
475{
476 struct qla_hw_data *ha = vha->hw;
477 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800478
479 if (IS_QLA82XX(ha)) {
480 qla82xx_start_iocbs(vha);
481 } else {
482 /* Adjust ring index. */
483 req->ring_index++;
484 if (req->ring_index == req->length) {
485 req->ring_index = 0;
486 req->ring_ptr = req->ring;
487 } else
488 req->ring_ptr++;
489
490 /* Set chip new ring index. */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800491 if (ha->mqenable || IS_QLA83XX(ha)) {
492 WRT_REG_DWORD(req->req_q_in, req->ring_index);
Arun Easi98878a12012-02-09 11:15:59 -0800493 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800494 } else if (IS_FWI2_CAPABLE(ha)) {
495 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
496 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
497 } else {
498 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
499 req->ring_index);
500 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
501 }
502 }
503}
504
505/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * qla2x00_marker() - Send a marker IOCB to the firmware.
507 * @ha: HA context
508 * @loop_id: loop ID
509 * @lun: LUN
510 * @type: marker modifier
511 *
512 * Can be called from both normal and interrupt context.
513 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700514 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500516static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800517__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
518 struct rsp_que *rsp, uint16_t loop_id,
519 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700521 mrk_entry_t *mrk;
522 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800523 struct qla_hw_data *ha = vha->hw;
524 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700526 mrk24 = NULL;
Giridhar Malavali99b82122011-11-18 09:03:17 -0800527 req = ha->req_q_map[0];
Giridhar Malavalid94d10e2010-07-23 15:28:23 +0500528 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700529 if (mrk == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700530 ql_log(ql_log_warn, base_vha, 0x3026,
531 "Failed to allocate Marker IOCB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 return (QLA_FUNCTION_FAILED);
534 }
535
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700536 mrk->entry_type = MARKER_TYPE;
537 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700539 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700540 mrk24 = (struct mrk_entry_24xx *) mrk;
541 mrk24->nport_handle = cpu_to_le16(loop_id);
542 mrk24->lun[1] = LSB(lun);
543 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700544 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800545 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700546 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700547 } else {
548 SET_TARGET_ID(ha, mrk->target, loop_id);
549 mrk->lun = cpu_to_le16(lun);
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 wmb();
553
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800554 qla2x00_start_iocbs(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return (QLA_SUCCESS);
557}
558
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700559int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800560qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
561 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
562 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int ret;
565 unsigned long flags = 0;
566
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800567 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
568 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
569 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 return (ret);
572}
573
574/**
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700575 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
576 * Continuation Type 1 IOCBs to allocate.
577 *
578 * @dsds: number of data segment decriptors needed
579 *
580 * Returns the number of IOCB entries needed to store @dsds.
581 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700582inline uint16_t
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700583qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700584{
585 uint16_t iocbs;
586
587 iocbs = 1;
588 if (dsds > 1) {
589 iocbs += (dsds - 1) / 5;
590 if ((dsds - 1) % 5)
591 iocbs++;
592 }
593 return iocbs;
594}
595
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800596static inline int
597qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
598 uint16_t tot_dsds)
599{
600 uint32_t *cur_dsd = NULL;
601 scsi_qla_host_t *vha;
602 struct qla_hw_data *ha;
603 struct scsi_cmnd *cmd;
604 struct scatterlist *cur_seg;
605 uint32_t *dsd_seg;
606 void *next_dsd;
607 uint8_t avail_dsds;
608 uint8_t first_iocb = 1;
609 uint32_t dsd_list_len;
610 struct dsd_dma *dsd_ptr;
611 struct ct6_dsd *ctx;
612
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800613 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800614
615 /* Update entry type to indicate Command Type 3 IOCB */
616 *((uint32_t *)(&cmd_pkt->entry_type)) =
617 __constant_cpu_to_le32(COMMAND_TYPE_6);
618
619 /* No data transfer */
620 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
621 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
622 return 0;
623 }
624
625 vha = sp->fcport->vha;
626 ha = vha->hw;
627
628 /* Set transfer direction */
629 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
630 cmd_pkt->control_flags =
631 __constant_cpu_to_le16(CF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400632 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800633 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
634 cmd_pkt->control_flags =
635 __constant_cpu_to_le16(CF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400636 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800637 }
638
639 cur_seg = scsi_sglist(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800640 ctx = GET_CMD_CTX_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800641
642 while (tot_dsds) {
643 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
644 QLA_DSDS_PER_IOCB : tot_dsds;
645 tot_dsds -= avail_dsds;
646 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
647
648 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
649 struct dsd_dma, list);
650 next_dsd = dsd_ptr->dsd_addr;
651 list_del(&dsd_ptr->list);
652 ha->gbl_dsd_avail--;
653 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
654 ctx->dsd_use_cnt++;
655 ha->gbl_dsd_inuse++;
656
657 if (first_iocb) {
658 first_iocb = 0;
659 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
660 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
661 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
662 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
663 } else {
664 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
665 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
666 *cur_dsd++ = cpu_to_le32(dsd_list_len);
667 }
668 cur_dsd = (uint32_t *)next_dsd;
669 while (avail_dsds) {
670 dma_addr_t sle_dma;
671
672 sle_dma = sg_dma_address(cur_seg);
673 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
674 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
675 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
676 cur_seg = sg_next(cur_seg);
677 avail_dsds--;
678 }
679 }
680
681 /* Null termination */
682 *cur_dsd++ = 0;
683 *cur_dsd++ = 0;
684 *cur_dsd++ = 0;
685 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
686 return 0;
687}
688
689/*
690 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
691 * for Command Type 6.
692 *
693 * @dsds: number of data segment decriptors needed
694 *
695 * Returns the number of dsd list needed to store @dsds.
696 */
697inline uint16_t
698qla24xx_calc_dsd_lists(uint16_t dsds)
699{
700 uint16_t dsd_lists = 0;
701
702 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
703 if (dsds % QLA_DSDS_PER_IOCB)
704 dsd_lists++;
705 return dsd_lists;
706}
707
708
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700709/**
710 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
711 * IOCB types.
712 *
713 * @sp: SRB command to process
714 * @cmd_pkt: Command type 3 IOCB
715 * @tot_dsds: Total number of segments to transfer
716 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700717inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700718qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
719 uint16_t tot_dsds)
720{
721 uint16_t avail_dsds;
722 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800723 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700724 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900725 struct scatterlist *sg;
726 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800727 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700728
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800729 cmd = GET_CMD_SP(sp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700730
731 /* Update entry type to indicate Command Type 3 IOCB */
732 *((uint32_t *)(&cmd_pkt->entry_type)) =
733 __constant_cpu_to_le32(COMMAND_TYPE_7);
734
735 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900736 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700737 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
738 return;
739 }
740
Andrew Vasquez444786d2009-01-05 11:18:10 -0800741 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700742 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700743
744 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700745 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700746 cmd_pkt->task_mgmt_flags =
747 __constant_cpu_to_le16(TMF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400748 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700749 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700750 cmd_pkt->task_mgmt_flags =
751 __constant_cpu_to_le16(TMF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400752 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700753 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700754
755 /* One DSD is available in the Command Type 3 IOCB */
756 avail_dsds = 1;
757 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
758
759 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700760
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900761 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
762 dma_addr_t sle_dma;
763 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700764
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900765 /* Allocate additional continuation packets? */
766 if (avail_dsds == 0) {
767 /*
768 * Five DSDs are available in the Continuation
769 * Type 1 IOCB.
770 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800771 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900772 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
773 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700774 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900775
776 sle_dma = sg_dma_address(sg);
777 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
778 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
779 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
780 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700781 }
782}
783
Arun Easibad75002010-05-04 15:01:30 -0700784struct fw_dif_context {
785 uint32_t ref_tag;
786 uint16_t app_tag;
787 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
788 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
789};
790
791/*
792 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
793 *
794 */
795static inline void
Arun Easie02587d2011-08-16 11:29:23 -0700796qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
Arun Easibad75002010-05-04 15:01:30 -0700797 unsigned int protcnt)
798{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800799 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700800 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700801
802 switch (scsi_get_prot_type(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700803 case SCSI_PROT_DIF_TYPE0:
Arun Easi8cb20492011-08-16 11:29:22 -0700804 /*
805 * No check for ql2xenablehba_err_chk, as it would be an
806 * I/O error if hba tag generation is not done.
807 */
808 pkt->ref_tag = cpu_to_le32((uint32_t)
809 (0xffffffff & scsi_get_lba(cmd)));
Arun Easie02587d2011-08-16 11:29:23 -0700810
811 if (!qla2x00_hba_err_chk_enabled(sp))
812 break;
813
Arun Easi8cb20492011-08-16 11:29:22 -0700814 pkt->ref_tag_mask[0] = 0xff;
815 pkt->ref_tag_mask[1] = 0xff;
816 pkt->ref_tag_mask[2] = 0xff;
817 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700818 break;
819
820 /*
821 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
822 * match LBA in CDB + N
823 */
824 case SCSI_PROT_DIF_TYPE2:
Arun Easie02587d2011-08-16 11:29:23 -0700825 pkt->app_tag = __constant_cpu_to_le16(0);
826 pkt->app_tag_mask[0] = 0x0;
827 pkt->app_tag_mask[1] = 0x0;
Arun Easi0c470872010-07-23 15:28:38 +0500828
829 pkt->ref_tag = cpu_to_le32((uint32_t)
830 (0xffffffff & scsi_get_lba(cmd)));
831
Arun Easie02587d2011-08-16 11:29:23 -0700832 if (!qla2x00_hba_err_chk_enabled(sp))
833 break;
834
Arun Easi0c470872010-07-23 15:28:38 +0500835 /* enable ALL bytes of the ref tag */
836 pkt->ref_tag_mask[0] = 0xff;
837 pkt->ref_tag_mask[1] = 0xff;
838 pkt->ref_tag_mask[2] = 0xff;
839 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700840 break;
841
842 /* For Type 3 protection: 16 bit GUARD only */
843 case SCSI_PROT_DIF_TYPE3:
844 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
845 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
846 0x00;
847 break;
848
849 /*
850 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
851 * 16 bit app tag.
852 */
853 case SCSI_PROT_DIF_TYPE1:
Arun Easie02587d2011-08-16 11:29:23 -0700854 pkt->ref_tag = cpu_to_le32((uint32_t)
855 (0xffffffff & scsi_get_lba(cmd)));
856 pkt->app_tag = __constant_cpu_to_le16(0);
857 pkt->app_tag_mask[0] = 0x0;
858 pkt->app_tag_mask[1] = 0x0;
859
860 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -0700861 break;
862
Arun Easibad75002010-05-04 15:01:30 -0700863 /* enable ALL bytes of the ref tag */
864 pkt->ref_tag_mask[0] = 0xff;
865 pkt->ref_tag_mask[1] = 0xff;
866 pkt->ref_tag_mask[2] = 0xff;
867 pkt->ref_tag_mask[3] = 0xff;
868 break;
869 }
870
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700871 ql_dbg(ql_dbg_io, vha, 0x3009,
872 "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
873 "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
874 pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
875 scsi_get_prot_type(cmd), cmd);
Arun Easibad75002010-05-04 15:01:30 -0700876}
877
Arun Easi8cb20492011-08-16 11:29:22 -0700878struct qla2_sgx {
879 dma_addr_t dma_addr; /* OUT */
880 uint32_t dma_len; /* OUT */
Arun Easibad75002010-05-04 15:01:30 -0700881
Arun Easi8cb20492011-08-16 11:29:22 -0700882 uint32_t tot_bytes; /* IN */
883 struct scatterlist *cur_sg; /* IN */
884
885 /* for book keeping, bzero on initial invocation */
886 uint32_t bytes_consumed;
887 uint32_t num_bytes;
888 uint32_t tot_partial;
889
890 /* for debugging */
891 uint32_t num_sg;
892 srb_t *sp;
893};
894
895static int
896qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
897 uint32_t *partial)
898{
899 struct scatterlist *sg;
900 uint32_t cumulative_partial, sg_len;
901 dma_addr_t sg_dma_addr;
902
903 if (sgx->num_bytes == sgx->tot_bytes)
904 return 0;
905
906 sg = sgx->cur_sg;
907 cumulative_partial = sgx->tot_partial;
908
909 sg_dma_addr = sg_dma_address(sg);
910 sg_len = sg_dma_len(sg);
911
912 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
913
914 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
915 sgx->dma_len = (blk_sz - cumulative_partial);
916 sgx->tot_partial = 0;
917 sgx->num_bytes += blk_sz;
918 *partial = 0;
919 } else {
920 sgx->dma_len = sg_len - sgx->bytes_consumed;
921 sgx->tot_partial += sgx->dma_len;
922 *partial = 1;
923 }
924
925 sgx->bytes_consumed += sgx->dma_len;
926
927 if (sg_len == sgx->bytes_consumed) {
928 sg = sg_next(sg);
929 sgx->num_sg++;
930 sgx->cur_sg = sg;
931 sgx->bytes_consumed = 0;
932 }
933
934 return 1;
935}
936
937static int
938qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
939 uint32_t *dsd, uint16_t tot_dsds)
940{
941 void *next_dsd;
942 uint8_t avail_dsds = 0;
943 uint32_t dsd_list_len;
944 struct dsd_dma *dsd_ptr;
945 struct scatterlist *sg_prot;
946 uint32_t *cur_dsd = dsd;
947 uint16_t used_dsds = tot_dsds;
948
949 uint32_t prot_int;
950 uint32_t partial;
951 struct qla2_sgx sgx;
952 dma_addr_t sle_dma;
953 uint32_t sle_dma_len, tot_prot_dma_len = 0;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800954 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easi8cb20492011-08-16 11:29:22 -0700955
956 prot_int = cmd->device->sector_size;
957
958 memset(&sgx, 0, sizeof(struct qla2_sgx));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800959 sgx.tot_bytes = scsi_bufflen(cmd);
960 sgx.cur_sg = scsi_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700961 sgx.sp = sp;
962
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800963 sg_prot = scsi_prot_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700964
965 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
966
967 sle_dma = sgx.dma_addr;
968 sle_dma_len = sgx.dma_len;
969alloc_and_fill:
970 /* Allocate additional continuation packets? */
971 if (avail_dsds == 0) {
972 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
973 QLA_DSDS_PER_IOCB : used_dsds;
974 dsd_list_len = (avail_dsds + 1) * 12;
975 used_dsds -= avail_dsds;
976
977 /* allocate tracking DS */
978 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
979 if (!dsd_ptr)
980 return 1;
981
982 /* allocate new list */
983 dsd_ptr->dsd_addr = next_dsd =
984 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
985 &dsd_ptr->dsd_list_dma);
986
987 if (!next_dsd) {
988 /*
989 * Need to cleanup only this dsd_ptr, rest
990 * will be done by sp_free_dma()
991 */
992 kfree(dsd_ptr);
993 return 1;
994 }
995
996 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800997 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easi8cb20492011-08-16 11:29:22 -0700998
999 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1000
1001 /* add new list to cmd iocb or last list */
1002 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1003 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1004 *cur_dsd++ = dsd_list_len;
1005 cur_dsd = (uint32_t *)next_dsd;
1006 }
1007 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1008 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1009 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1010 avail_dsds--;
1011
1012 if (partial == 0) {
1013 /* Got a full protection interval */
1014 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1015 sle_dma_len = 8;
1016
1017 tot_prot_dma_len += sle_dma_len;
1018 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1019 tot_prot_dma_len = 0;
1020 sg_prot = sg_next(sg_prot);
1021 }
1022
1023 partial = 1; /* So as to not re-enter this block */
1024 goto alloc_and_fill;
1025 }
1026 }
1027 /* Null termination */
1028 *cur_dsd++ = 0;
1029 *cur_dsd++ = 0;
1030 *cur_dsd++ = 0;
1031 return 0;
1032}
Giridhar Malavali5162cf02011-11-18 09:03:18 -08001033
Arun Easibad75002010-05-04 15:01:30 -07001034static int
1035qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1036 uint16_t tot_dsds)
1037{
1038 void *next_dsd;
1039 uint8_t avail_dsds = 0;
1040 uint32_t dsd_list_len;
1041 struct dsd_dma *dsd_ptr;
1042 struct scatterlist *sg;
1043 uint32_t *cur_dsd = dsd;
1044 int i;
1045 uint16_t used_dsds = tot_dsds;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001046 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
1047 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -07001048
1049 uint8_t *cp;
1050
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001051 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
Arun Easibad75002010-05-04 15:01:30 -07001052 dma_addr_t sle_dma;
1053
1054 /* Allocate additional continuation packets? */
1055 if (avail_dsds == 0) {
1056 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1057 QLA_DSDS_PER_IOCB : used_dsds;
1058 dsd_list_len = (avail_dsds + 1) * 12;
1059 used_dsds -= avail_dsds;
1060
1061 /* allocate tracking DS */
1062 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1063 if (!dsd_ptr)
1064 return 1;
1065
1066 /* allocate new list */
1067 dsd_ptr->dsd_addr = next_dsd =
1068 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1069 &dsd_ptr->dsd_list_dma);
1070
1071 if (!next_dsd) {
1072 /*
1073 * Need to cleanup only this dsd_ptr, rest
1074 * will be done by sp_free_dma()
1075 */
1076 kfree(dsd_ptr);
1077 return 1;
1078 }
1079
1080 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001081 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001082
1083 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1084
1085 /* add new list to cmd iocb or last list */
1086 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1087 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1088 *cur_dsd++ = dsd_list_len;
1089 cur_dsd = (uint32_t *)next_dsd;
1090 }
1091 sle_dma = sg_dma_address(sg);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001092 ql_dbg(ql_dbg_io, vha, 0x300a,
1093 "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001094 i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg), cmd);
Arun Easibad75002010-05-04 15:01:30 -07001095 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1096 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1097 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1098 avail_dsds--;
1099
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001100 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Arun Easibad75002010-05-04 15:01:30 -07001101 cp = page_address(sg_page(sg)) + sg->offset;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001102 ql_dbg(ql_dbg_io, vha, 0x300b,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001103 "User data buffer=%p for cmd=%p.\n", cp, cmd);
Arun Easibad75002010-05-04 15:01:30 -07001104 }
1105 }
1106 /* Null termination */
1107 *cur_dsd++ = 0;
1108 *cur_dsd++ = 0;
1109 *cur_dsd++ = 0;
1110 return 0;
1111}
1112
1113static int
1114qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1115 uint32_t *dsd,
1116 uint16_t tot_dsds)
1117{
1118 void *next_dsd;
1119 uint8_t avail_dsds = 0;
1120 uint32_t dsd_list_len;
1121 struct dsd_dma *dsd_ptr;
1122 struct scatterlist *sg;
1123 int i;
1124 struct scsi_cmnd *cmd;
1125 uint32_t *cur_dsd = dsd;
1126 uint16_t used_dsds = tot_dsds;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001127 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Arun Easibad75002010-05-04 15:01:30 -07001128 uint8_t *cp;
1129
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001130 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001131 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1132 dma_addr_t sle_dma;
1133
1134 /* Allocate additional continuation packets? */
1135 if (avail_dsds == 0) {
1136 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1137 QLA_DSDS_PER_IOCB : used_dsds;
1138 dsd_list_len = (avail_dsds + 1) * 12;
1139 used_dsds -= avail_dsds;
1140
1141 /* allocate tracking DS */
1142 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1143 if (!dsd_ptr)
1144 return 1;
1145
1146 /* allocate new list */
1147 dsd_ptr->dsd_addr = next_dsd =
1148 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1149 &dsd_ptr->dsd_list_dma);
1150
1151 if (!next_dsd) {
1152 /*
1153 * Need to cleanup only this dsd_ptr, rest
1154 * will be done by sp_free_dma()
1155 */
1156 kfree(dsd_ptr);
1157 return 1;
1158 }
1159
1160 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001161 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001162
1163 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1164
1165 /* add new list to cmd iocb or last list */
1166 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1167 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1168 *cur_dsd++ = dsd_list_len;
1169 cur_dsd = (uint32_t *)next_dsd;
1170 }
1171 sle_dma = sg_dma_address(sg);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001172 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001173 ql_dbg(ql_dbg_io, vha, 0x3027,
1174 "%s(): %p, sg_entry %d - "
1175 "addr=0x%x0x%x, len=%d.\n",
1176 __func__, cur_dsd, i,
1177 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
Arun Easibad75002010-05-04 15:01:30 -07001178 }
1179 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1180 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1181 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1182
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001183 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Arun Easibad75002010-05-04 15:01:30 -07001184 cp = page_address(sg_page(sg)) + sg->offset;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001185 ql_dbg(ql_dbg_io, vha, 0x3028,
1186 "%s(): Protection Data buffer = %p.\n", __func__,
1187 cp);
Arun Easibad75002010-05-04 15:01:30 -07001188 }
1189 avail_dsds--;
1190 }
1191 /* Null termination */
1192 *cur_dsd++ = 0;
1193 *cur_dsd++ = 0;
1194 *cur_dsd++ = 0;
1195 return 0;
1196}
1197
1198/**
1199 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1200 * Type 6 IOCB types.
1201 *
1202 * @sp: SRB command to process
1203 * @cmd_pkt: Command type 3 IOCB
1204 * @tot_dsds: Total number of segments to transfer
1205 */
1206static inline int
1207qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1208 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1209{
1210 uint32_t *cur_dsd, *fcp_dl;
1211 scsi_qla_host_t *vha;
1212 struct scsi_cmnd *cmd;
1213 struct scatterlist *cur_seg;
1214 int sgc;
Arun Easi8cb20492011-08-16 11:29:22 -07001215 uint32_t total_bytes = 0;
Arun Easibad75002010-05-04 15:01:30 -07001216 uint32_t data_bytes;
1217 uint32_t dif_bytes;
1218 uint8_t bundling = 1;
1219 uint16_t blk_size;
1220 uint8_t *clr_ptr;
1221 struct crc_context *crc_ctx_pkt = NULL;
1222 struct qla_hw_data *ha;
1223 uint8_t additional_fcpcdb_len;
1224 uint16_t fcp_cmnd_len;
1225 struct fcp_cmnd *fcp_cmnd;
1226 dma_addr_t crc_ctx_dma;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001227 char tag[2];
Arun Easibad75002010-05-04 15:01:30 -07001228
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001229 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001230
1231 sgc = 0;
1232 /* Update entry type to indicate Command Type CRC_2 IOCB */
1233 *((uint32_t *)(&cmd_pkt->entry_type)) =
1234 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1235
Arun Easibad75002010-05-04 15:01:30 -07001236 vha = sp->fcport->vha;
1237 ha = vha->hw;
1238
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001239 /* No data transfer */
1240 data_bytes = scsi_bufflen(cmd);
1241 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1242 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1243 return QLA_SUCCESS;
1244 }
Arun Easibad75002010-05-04 15:01:30 -07001245
1246 cmd_pkt->vp_index = sp->fcport->vp_idx;
1247
1248 /* Set transfer direction */
1249 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1250 cmd_pkt->control_flags =
1251 __constant_cpu_to_le16(CF_WRITE_DATA);
1252 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1253 cmd_pkt->control_flags =
1254 __constant_cpu_to_le16(CF_READ_DATA);
1255 }
1256
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001257 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1258 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1259 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1260 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
Arun Easibad75002010-05-04 15:01:30 -07001261 bundling = 0;
1262
1263 /* Allocate CRC context from global pool */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001264 crc_ctx_pkt = sp->u.scmd.ctx =
1265 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Arun Easibad75002010-05-04 15:01:30 -07001266
1267 if (!crc_ctx_pkt)
1268 goto crc_queuing_error;
1269
1270 /* Zero out CTX area. */
1271 clr_ptr = (uint8_t *)crc_ctx_pkt;
1272 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1273
1274 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1275
1276 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1277
1278 /* Set handle */
1279 crc_ctx_pkt->handle = cmd_pkt->handle;
1280
1281 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1282
Arun Easie02587d2011-08-16 11:29:23 -07001283 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
Arun Easibad75002010-05-04 15:01:30 -07001284 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1285
1286 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1287 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1288 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1289
1290 /* Determine SCSI command length -- align to 4 byte boundary */
1291 if (cmd->cmd_len > 16) {
Arun Easibad75002010-05-04 15:01:30 -07001292 additional_fcpcdb_len = cmd->cmd_len - 16;
1293 if ((cmd->cmd_len % 4) != 0) {
1294 /* SCSI cmd > 16 bytes must be multiple of 4 */
1295 goto crc_queuing_error;
1296 }
1297 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1298 } else {
1299 additional_fcpcdb_len = 0;
1300 fcp_cmnd_len = 12 + 16 + 4;
1301 }
1302
1303 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1304
1305 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1306 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1307 fcp_cmnd->additional_cdb_len |= 1;
1308 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1309 fcp_cmnd->additional_cdb_len |= 2;
1310
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001311 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
Arun Easibad75002010-05-04 15:01:30 -07001312 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1313 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1314 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1315 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1316 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1317 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001318 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001319
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001320 /*
1321 * Update tagged queuing modifier if using command tag queuing
1322 */
1323 if (scsi_populate_tag_msg(cmd, tag)) {
1324 switch (tag[0]) {
1325 case HEAD_OF_QUEUE_TAG:
1326 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1327 break;
1328 case ORDERED_QUEUE_TAG:
1329 fcp_cmnd->task_attribute = TSK_ORDERED;
1330 break;
1331 default:
1332 fcp_cmnd->task_attribute = 0;
1333 break;
1334 }
1335 } else {
1336 fcp_cmnd->task_attribute = 0;
1337 }
1338
Arun Easibad75002010-05-04 15:01:30 -07001339 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1340
Arun Easibad75002010-05-04 15:01:30 -07001341 /* Compute dif len and adjust data len to incude protection */
Arun Easibad75002010-05-04 15:01:30 -07001342 dif_bytes = 0;
1343 blk_size = cmd->device->sector_size;
Arun Easi8cb20492011-08-16 11:29:22 -07001344 dif_bytes = (data_bytes / blk_size) * 8;
1345
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001346 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
Arun Easi8cb20492011-08-16 11:29:22 -07001347 case SCSI_PROT_READ_INSERT:
1348 case SCSI_PROT_WRITE_STRIP:
1349 total_bytes = data_bytes;
1350 data_bytes += dif_bytes;
1351 break;
1352
1353 case SCSI_PROT_READ_STRIP:
1354 case SCSI_PROT_WRITE_INSERT:
1355 case SCSI_PROT_READ_PASS:
1356 case SCSI_PROT_WRITE_PASS:
1357 total_bytes = data_bytes + dif_bytes;
1358 break;
1359 default:
1360 BUG();
Arun Easibad75002010-05-04 15:01:30 -07001361 }
1362
Arun Easie02587d2011-08-16 11:29:23 -07001363 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -07001364 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1365
1366 if (!bundling) {
1367 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1368 } else {
1369 /*
1370 * Configure Bundling if we need to fetch interlaving
1371 * protection PCI accesses
1372 */
1373 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1374 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1375 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1376 tot_prot_dsds);
1377 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1378 }
1379
1380 /* Finish the common fields of CRC pkt */
1381 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1382 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1383 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1384 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1385 /* Fibre channel byte count */
1386 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1387 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1388 additional_fcpcdb_len);
1389 *fcp_dl = htonl(total_bytes);
1390
Arun Easi0c470872010-07-23 15:28:38 +05001391 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
Arun Easi0c470872010-07-23 15:28:38 +05001392 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1393 return QLA_SUCCESS;
1394 }
Arun Easibad75002010-05-04 15:01:30 -07001395 /* Walks data segments */
1396
1397 cmd_pkt->control_flags |=
1398 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
Arun Easi8cb20492011-08-16 11:29:22 -07001399
1400 if (!bundling && tot_prot_dsds) {
1401 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1402 cur_dsd, tot_dsds))
1403 goto crc_queuing_error;
1404 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
Arun Easibad75002010-05-04 15:01:30 -07001405 (tot_dsds - tot_prot_dsds)))
1406 goto crc_queuing_error;
1407
1408 if (bundling && tot_prot_dsds) {
1409 /* Walks dif segments */
1410 cur_seg = scsi_prot_sglist(cmd);
1411 cmd_pkt->control_flags |=
1412 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1413 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1414 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1415 tot_prot_dsds))
1416 goto crc_queuing_error;
1417 }
1418 return QLA_SUCCESS;
1419
1420crc_queuing_error:
Arun Easibad75002010-05-04 15:01:30 -07001421 /* Cleanup will be performed by the caller */
1422
1423 return QLA_FUNCTION_FAILED;
1424}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001425
1426/**
1427 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1428 * @sp: command to send to the ISP
1429 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001430 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001431 */
1432int
1433qla24xx_start_scsi(srb_t *sp)
1434{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001435 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001436 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001437 uint32_t *clr_ptr;
1438 uint32_t index;
1439 uint32_t handle;
1440 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001441 uint16_t cnt;
1442 uint16_t req_cnt;
1443 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001444 struct req_que *req = NULL;
1445 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001446 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Andrew Vasquez444786d2009-01-05 11:18:10 -08001447 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001448 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001449 char tag[2];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001450
1451 /* Setup device pointers. */
1452 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001453
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001454 qla25xx_set_que(sp, &rsp);
1455 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001456
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001457 /* So we know we haven't pci_map'ed anything yet */
1458 tot_dsds = 0;
1459
1460 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001461 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001462 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1463 QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001464 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001465 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001466 }
1467
1468 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001469 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001470
1471 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001472 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001473 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1474 handle++;
1475 if (handle == MAX_OUTSTANDING_COMMANDS)
1476 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001477 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001478 break;
1479 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001480 if (index == MAX_OUTSTANDING_COMMANDS) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001481 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001482 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001483
1484 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001485 if (scsi_sg_count(cmd)) {
1486 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1487 scsi_sg_count(cmd), cmd->sc_data_direction);
1488 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001489 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001490 } else
1491 nseg = 0;
1492
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001493 tot_dsds = nseg;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001494 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001495 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001496 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001497
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001498 if (req->ring_index < cnt)
1499 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001500 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001501 req->cnt = req->length -
1502 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001503 if (req->cnt < (req_cnt + 2))
1504 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001505 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001506
1507 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001508 req->current_outstanding_cmd = handle;
1509 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001510 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001511 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001512 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001513
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001514 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001515 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001516
1517 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001518 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001519 clr_ptr = (uint32_t *)cmd_pkt + 2;
1520 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1521 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1522
1523 /* Set NPORT-ID and LUN number*/
1524 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1525 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1526 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1527 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001528 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001529
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001530 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001531 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001532
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001533 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1534 if (scsi_populate_tag_msg(cmd, tag)) {
1535 switch (tag[0]) {
1536 case HEAD_OF_QUEUE_TAG:
1537 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1538 break;
1539 case ORDERED_QUEUE_TAG:
1540 cmd_pkt->task = TSK_ORDERED;
1541 break;
1542 }
1543 }
1544
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001545 /* Load SCSI command packet. */
1546 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1547 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1548
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001549 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001550
1551 /* Build IOCB segments */
1552 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1553
1554 /* Set total data segment count. */
1555 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001556 /* Specify response queue number where completion should happen */
1557 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001558 wmb();
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001559 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001560 req->ring_index++;
1561 if (req->ring_index == req->length) {
1562 req->ring_index = 0;
1563 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001564 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001565 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001566
1567 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001568
1569 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001570 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1571 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001572
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001573 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001574 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001575 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001576 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001577
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001578 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001579 return QLA_SUCCESS;
1580
1581queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001582 if (tot_dsds)
1583 scsi_dma_unmap(cmd);
1584
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001585 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001586
1587 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001589
Arun Easibad75002010-05-04 15:01:30 -07001590
1591/**
1592 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1593 * @sp: command to send to the ISP
1594 *
1595 * Returns non-zero if a failure occurred, else zero.
1596 */
1597int
1598qla24xx_dif_start_scsi(srb_t *sp)
1599{
1600 int nseg;
1601 unsigned long flags;
1602 uint32_t *clr_ptr;
1603 uint32_t index;
1604 uint32_t handle;
1605 uint16_t cnt;
1606 uint16_t req_cnt = 0;
1607 uint16_t tot_dsds;
1608 uint16_t tot_prot_dsds;
1609 uint16_t fw_prot_opts = 0;
1610 struct req_que *req = NULL;
1611 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001612 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001613 struct scsi_qla_host *vha = sp->fcport->vha;
1614 struct qla_hw_data *ha = vha->hw;
1615 struct cmd_type_crc_2 *cmd_pkt;
1616 uint32_t status = 0;
1617
1618#define QDSS_GOT_Q_SPACE BIT_0
1619
Arun Easi0c470872010-07-23 15:28:38 +05001620 /* Only process protection or >16 cdb in this routine */
1621 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1622 if (cmd->cmd_len <= 16)
1623 return qla24xx_start_scsi(sp);
1624 }
Arun Easibad75002010-05-04 15:01:30 -07001625
1626 /* Setup device pointers. */
1627
1628 qla25xx_set_que(sp, &rsp);
1629 req = vha->req;
1630
1631 /* So we know we haven't pci_map'ed anything yet */
1632 tot_dsds = 0;
1633
1634 /* Send marker if required */
1635 if (vha->marker_needed != 0) {
1636 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1637 QLA_SUCCESS)
1638 return QLA_FUNCTION_FAILED;
1639 vha->marker_needed = 0;
1640 }
1641
1642 /* Acquire ring specific lock */
1643 spin_lock_irqsave(&ha->hardware_lock, flags);
1644
1645 /* Check for room in outstanding command list. */
1646 handle = req->current_outstanding_cmd;
1647 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1648 handle++;
1649 if (handle == MAX_OUTSTANDING_COMMANDS)
1650 handle = 1;
1651 if (!req->outstanding_cmds[handle])
1652 break;
1653 }
1654
1655 if (index == MAX_OUTSTANDING_COMMANDS)
1656 goto queuing_error;
1657
1658 /* Compute number of required data segments */
1659 /* Map the sg table so we have an accurate count of sg entries needed */
1660 if (scsi_sg_count(cmd)) {
1661 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1662 scsi_sg_count(cmd), cmd->sc_data_direction);
1663 if (unlikely(!nseg))
1664 goto queuing_error;
1665 else
1666 sp->flags |= SRB_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001667
1668 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1669 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1670 struct qla2_sgx sgx;
1671 uint32_t partial;
1672
1673 memset(&sgx, 0, sizeof(struct qla2_sgx));
1674 sgx.tot_bytes = scsi_bufflen(cmd);
1675 sgx.cur_sg = scsi_sglist(cmd);
1676 sgx.sp = sp;
1677
1678 nseg = 0;
1679 while (qla24xx_get_one_block_sg(
1680 cmd->device->sector_size, &sgx, &partial))
1681 nseg++;
1682 }
Arun Easibad75002010-05-04 15:01:30 -07001683 } else
1684 nseg = 0;
1685
1686 /* number of required data segments */
1687 tot_dsds = nseg;
1688
1689 /* Compute number of required protection segments */
1690 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1691 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1692 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1693 if (unlikely(!nseg))
1694 goto queuing_error;
1695 else
1696 sp->flags |= SRB_CRC_PROT_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001697
1698 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1699 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1700 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1701 }
Arun Easibad75002010-05-04 15:01:30 -07001702 } else {
1703 nseg = 0;
1704 }
1705
1706 req_cnt = 1;
1707 /* Total Data and protection sg segment(s) */
1708 tot_prot_dsds = nseg;
1709 tot_dsds += nseg;
1710 if (req->cnt < (req_cnt + 2)) {
1711 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1712
1713 if (req->ring_index < cnt)
1714 req->cnt = cnt - req->ring_index;
1715 else
1716 req->cnt = req->length -
1717 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001718 if (req->cnt < (req_cnt + 2))
1719 goto queuing_error;
Arun Easibad75002010-05-04 15:01:30 -07001720 }
1721
Arun Easibad75002010-05-04 15:01:30 -07001722 status |= QDSS_GOT_Q_SPACE;
1723
1724 /* Build header part of command packet (excluding the OPCODE). */
1725 req->current_outstanding_cmd = handle;
1726 req->outstanding_cmds[handle] = sp;
Arun Easi8cb20492011-08-16 11:29:22 -07001727 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001728 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Arun Easibad75002010-05-04 15:01:30 -07001729 req->cnt -= req_cnt;
1730
1731 /* Fill-in common area */
1732 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1733 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1734
1735 clr_ptr = (uint32_t *)cmd_pkt + 2;
1736 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1737
1738 /* Set NPORT-ID and LUN number*/
1739 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1740 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1741 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1742 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1743
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001744 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Arun Easibad75002010-05-04 15:01:30 -07001745 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1746
1747 /* Total Data and protection segment(s) */
1748 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1749
1750 /* Build IOCB segments and adjust for data protection segments */
1751 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1752 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1753 QLA_SUCCESS)
1754 goto queuing_error;
1755
1756 cmd_pkt->entry_count = (uint8_t)req_cnt;
1757 /* Specify response queue number where completion should happen */
1758 cmd_pkt->entry_status = (uint8_t) rsp->id;
1759 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1760 wmb();
1761
1762 /* Adjust ring index. */
1763 req->ring_index++;
1764 if (req->ring_index == req->length) {
1765 req->ring_index = 0;
1766 req->ring_ptr = req->ring;
1767 } else
1768 req->ring_ptr++;
1769
1770 /* Set chip new ring index. */
1771 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1772 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1773
1774 /* Manage unprocessed RIO/ZIO commands in response queue. */
1775 if (vha->flags.process_response_queue &&
1776 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1777 qla24xx_process_response_queue(vha, rsp);
1778
1779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1780
1781 return QLA_SUCCESS;
1782
1783queuing_error:
1784 if (status & QDSS_GOT_Q_SPACE) {
1785 req->outstanding_cmds[handle] = NULL;
1786 req->cnt += req_cnt;
1787 }
1788 /* Cleanup will be performed by the caller (queuecommand) */
1789
1790 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Arun Easibad75002010-05-04 15:01:30 -07001791 return QLA_FUNCTION_FAILED;
1792}
1793
1794
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001795static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001796{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001797 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001798 struct qla_hw_data *ha = sp->fcport->vha->hw;
1799 int affinity = cmd->request->cpu;
1800
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001801 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001802 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001803 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001804 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001805 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001806}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001807
1808/* Generic Control-SRB manipulation functions. */
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001809void *
1810qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001811{
Andrew Vasquezac280b62009-08-20 11:06:05 -07001812 struct qla_hw_data *ha = vha->hw;
1813 struct req_que *req = ha->req_q_map[0];
1814 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1815 uint32_t index, handle;
1816 request_t *pkt;
1817 uint16_t cnt, req_cnt;
1818
1819 pkt = NULL;
1820 req_cnt = 1;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001821 handle = 0;
1822
1823 if (!sp)
1824 goto skip_cmd_array;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001825
1826 /* Check for room in outstanding command list. */
1827 handle = req->current_outstanding_cmd;
1828 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1829 handle++;
1830 if (handle == MAX_OUTSTANDING_COMMANDS)
1831 handle = 1;
1832 if (!req->outstanding_cmds[handle])
1833 break;
1834 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001835 if (index == MAX_OUTSTANDING_COMMANDS) {
1836 ql_log(ql_log_warn, vha, 0x700b,
1837 "No room on oustanding cmd array.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07001838 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001839 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001840
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001841 /* Prep command array. */
1842 req->current_outstanding_cmd = handle;
1843 req->outstanding_cmds[handle] = sp;
1844 sp->handle = handle;
1845
Andrew Vasquez57807902011-11-18 09:03:20 -08001846 /* Adjust entry-counts as needed. */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001847 if (sp->type != SRB_SCSI_CMD)
1848 req_cnt = sp->iocbs;
Andrew Vasquez57807902011-11-18 09:03:20 -08001849
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001850skip_cmd_array:
Andrew Vasquezac280b62009-08-20 11:06:05 -07001851 /* Check for room on request queue. */
1852 if (req->cnt < req_cnt) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001853 if (ha->mqenable || IS_QLA83XX(ha))
Andrew Vasquezac280b62009-08-20 11:06:05 -07001854 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001855 else if (IS_QLA82XX(ha))
1856 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001857 else if (IS_FWI2_CAPABLE(ha))
1858 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
1859 else
1860 cnt = qla2x00_debounce_register(
1861 ISP_REQ_Q_OUT(ha, &reg->isp));
1862
1863 if (req->ring_index < cnt)
1864 req->cnt = cnt - req->ring_index;
1865 else
1866 req->cnt = req->length -
1867 (req->ring_index - cnt);
1868 }
1869 if (req->cnt < req_cnt)
1870 goto queuing_error;
1871
1872 /* Prep packet */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001873 req->cnt -= req_cnt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001874 pkt = req->ring_ptr;
1875 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1876 pkt->entry_count = req_cnt;
1877 pkt->handle = handle;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001878
1879queuing_error:
1880 return pkt;
1881}
1882
1883static void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001884qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1885{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001886 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001887
1888 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1889 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001890 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001891 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001892 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001893 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1894 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1895 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1896 logio->port_id[1] = sp->fcport->d_id.b.area;
1897 logio->port_id[2] = sp->fcport->d_id.b.domain;
1898 logio->vp_index = sp->fcport->vp_idx;
1899}
1900
1901static void
1902qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1903{
1904 struct qla_hw_data *ha = sp->fcport->vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001905 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001906 uint16_t opts;
1907
Giridhar Malavalib9637522010-05-28 15:08:15 -07001908 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001909 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1910 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001911 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1912 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001913 if (HAS_EXTENDED_IDS(ha)) {
1914 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1915 mbx->mb10 = cpu_to_le16(opts);
1916 } else {
1917 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1918 }
1919 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1920 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1921 sp->fcport->d_id.b.al_pa);
1922 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1923}
1924
1925static void
1926qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1927{
1928 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1929 logio->control_flags =
1930 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1931 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1932 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1933 logio->port_id[1] = sp->fcport->d_id.b.area;
1934 logio->port_id[2] = sp->fcport->d_id.b.domain;
1935 logio->vp_index = sp->fcport->vp_idx;
1936}
1937
1938static void
1939qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1940{
1941 struct qla_hw_data *ha = sp->fcport->vha->hw;
1942
Giridhar Malavalib9637522010-05-28 15:08:15 -07001943 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001944 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1945 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1946 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1947 cpu_to_le16(sp->fcport->loop_id):
1948 cpu_to_le16(sp->fcport->loop_id << 8);
1949 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1950 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1951 sp->fcport->d_id.b.al_pa);
1952 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1953 /* Implicit: mbx->mbx10 = 0. */
1954}
1955
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001956static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001957qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1958{
1959 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1960 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1961 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1962 logio->vp_index = sp->fcport->vp_idx;
1963}
1964
1965static void
1966qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1967{
1968 struct qla_hw_data *ha = sp->fcport->vha->hw;
1969
1970 mbx->entry_type = MBX_IOCB_TYPE;
1971 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1972 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1973 if (HAS_EXTENDED_IDS(ha)) {
1974 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1975 mbx->mb10 = cpu_to_le16(BIT_0);
1976 } else {
1977 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1978 }
1979 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1980 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1981 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1982 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1983 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1984}
1985
1986static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001987qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1988{
1989 uint32_t flags;
1990 unsigned int lun;
1991 struct fc_port *fcport = sp->fcport;
1992 scsi_qla_host_t *vha = fcport->vha;
1993 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001994 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001995 struct req_que *req = vha->req;
1996
1997 flags = iocb->u.tmf.flags;
1998 lun = iocb->u.tmf.lun;
1999
2000 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2001 tsk->entry_count = 1;
2002 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2003 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2004 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2005 tsk->control_flags = cpu_to_le32(flags);
2006 tsk->port_id[0] = fcport->d_id.b.al_pa;
2007 tsk->port_id[1] = fcport->d_id.b.area;
2008 tsk->port_id[2] = fcport->d_id.b.domain;
2009 tsk->vp_index = fcport->vp_idx;
2010
2011 if (flags == TCF_LUN_RESET) {
2012 int_to_scsilun(lun, &tsk->lun);
2013 host_to_fcp_swap((uint8_t *)&tsk->lun,
2014 sizeof(tsk->lun));
2015 }
2016}
2017
2018static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002019qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2020{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002021 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002022
2023 els_iocb->entry_type = ELS_IOCB_TYPE;
2024 els_iocb->entry_count = 1;
2025 els_iocb->sys_define = 0;
2026 els_iocb->entry_status = 0;
2027 els_iocb->handle = sp->handle;
2028 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2029 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2030 els_iocb->vp_index = sp->fcport->vp_idx;
2031 els_iocb->sof_type = EST_SOFI3;
2032 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2033
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002034 els_iocb->opcode =
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002035 sp->type == SRB_ELS_CMD_RPT ?
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002036 bsg_job->request->rqst_data.r_els.els_code :
2037 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002038 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2039 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2040 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2041 els_iocb->control_flags = 0;
2042 els_iocb->rx_byte_count =
2043 cpu_to_le32(bsg_job->reply_payload.payload_len);
2044 els_iocb->tx_byte_count =
2045 cpu_to_le32(bsg_job->request_payload.payload_len);
2046
2047 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2048 (bsg_job->request_payload.sg_list)));
2049 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2050 (bsg_job->request_payload.sg_list)));
2051 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2052 (bsg_job->request_payload.sg_list));
2053
2054 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2055 (bsg_job->reply_payload.sg_list)));
2056 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2057 (bsg_job->reply_payload.sg_list)));
2058 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2059 (bsg_job->reply_payload.sg_list));
2060}
2061
2062static void
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002063qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2064{
2065 uint16_t avail_dsds;
2066 uint32_t *cur_dsd;
2067 struct scatterlist *sg;
2068 int index;
2069 uint16_t tot_dsds;
2070 scsi_qla_host_t *vha = sp->fcport->vha;
2071 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002072 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002073 int loop_iterartion = 0;
2074 int cont_iocb_prsnt = 0;
2075 int entry_count = 1;
2076
2077 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2078 ct_iocb->entry_type = CT_IOCB_TYPE;
2079 ct_iocb->entry_status = 0;
2080 ct_iocb->handle1 = sp->handle;
2081 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2082 ct_iocb->status = __constant_cpu_to_le16(0);
2083 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2084 ct_iocb->timeout = 0;
2085 ct_iocb->cmd_dsd_count =
2086 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2087 ct_iocb->total_dsd_count =
2088 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2089 ct_iocb->req_bytecount =
2090 cpu_to_le32(bsg_job->request_payload.payload_len);
2091 ct_iocb->rsp_bytecount =
2092 cpu_to_le32(bsg_job->reply_payload.payload_len);
2093
2094 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2095 (bsg_job->request_payload.sg_list)));
2096 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2097 (bsg_job->request_payload.sg_list)));
2098 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2099
2100 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2101 (bsg_job->reply_payload.sg_list)));
2102 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2103 (bsg_job->reply_payload.sg_list)));
2104 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2105
2106 avail_dsds = 1;
2107 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2108 index = 0;
2109 tot_dsds = bsg_job->reply_payload.sg_cnt;
2110
2111 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2112 dma_addr_t sle_dma;
2113 cont_a64_entry_t *cont_pkt;
2114
2115 /* Allocate additional continuation packets? */
2116 if (avail_dsds == 0) {
2117 /*
2118 * Five DSDs are available in the Cont.
2119 * Type 1 IOCB.
2120 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002121 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2122 vha->hw->req_q_map[0]);
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002123 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2124 avail_dsds = 5;
2125 cont_iocb_prsnt = 1;
2126 entry_count++;
2127 }
2128
2129 sle_dma = sg_dma_address(sg);
2130 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2131 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2132 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2133 loop_iterartion++;
2134 avail_dsds--;
2135 }
2136 ct_iocb->entry_count = entry_count;
2137}
2138
2139static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002140qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2141{
2142 uint16_t avail_dsds;
2143 uint32_t *cur_dsd;
2144 struct scatterlist *sg;
2145 int index;
2146 uint16_t tot_dsds;
2147 scsi_qla_host_t *vha = sp->fcport->vha;
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002148 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002149 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002150 int loop_iterartion = 0;
2151 int cont_iocb_prsnt = 0;
2152 int entry_count = 1;
2153
2154 ct_iocb->entry_type = CT_IOCB_TYPE;
2155 ct_iocb->entry_status = 0;
2156 ct_iocb->sys_define = 0;
2157 ct_iocb->handle = sp->handle;
2158
2159 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2160 ct_iocb->vp_index = sp->fcport->vp_idx;
2161 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2162
2163 ct_iocb->cmd_dsd_count =
2164 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2165 ct_iocb->timeout = 0;
2166 ct_iocb->rsp_dsd_count =
2167 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2168 ct_iocb->rsp_byte_count =
2169 cpu_to_le32(bsg_job->reply_payload.payload_len);
2170 ct_iocb->cmd_byte_count =
2171 cpu_to_le32(bsg_job->request_payload.payload_len);
2172 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2173 (bsg_job->request_payload.sg_list)));
2174 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2175 (bsg_job->request_payload.sg_list)));
2176 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2177 (bsg_job->request_payload.sg_list));
2178
2179 avail_dsds = 1;
2180 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2181 index = 0;
2182 tot_dsds = bsg_job->reply_payload.sg_cnt;
2183
2184 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2185 dma_addr_t sle_dma;
2186 cont_a64_entry_t *cont_pkt;
2187
2188 /* Allocate additional continuation packets? */
2189 if (avail_dsds == 0) {
2190 /*
2191 * Five DSDs are available in the Cont.
2192 * Type 1 IOCB.
2193 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002194 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2195 ha->req_q_map[0]);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002196 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2197 avail_dsds = 5;
2198 cont_iocb_prsnt = 1;
2199 entry_count++;
2200 }
2201
2202 sle_dma = sg_dma_address(sg);
2203 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2204 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2205 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2206 loop_iterartion++;
2207 avail_dsds--;
2208 }
2209 ct_iocb->entry_count = entry_count;
2210}
2211
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002212/*
2213 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2214 * @sp: command to send to the ISP
2215 *
2216 * Returns non-zero if a failure occurred, else zero.
2217 */
2218int
2219qla82xx_start_scsi(srb_t *sp)
2220{
2221 int ret, nseg;
2222 unsigned long flags;
2223 struct scsi_cmnd *cmd;
2224 uint32_t *clr_ptr;
2225 uint32_t index;
2226 uint32_t handle;
2227 uint16_t cnt;
2228 uint16_t req_cnt;
2229 uint16_t tot_dsds;
2230 struct device_reg_82xx __iomem *reg;
2231 uint32_t dbval;
2232 uint32_t *fcp_dl;
2233 uint8_t additional_cdb_len;
2234 struct ct6_dsd *ctx;
2235 struct scsi_qla_host *vha = sp->fcport->vha;
2236 struct qla_hw_data *ha = vha->hw;
2237 struct req_que *req = NULL;
2238 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002239 char tag[2];
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002240
2241 /* Setup device pointers. */
2242 ret = 0;
2243 reg = &ha->iobase->isp82;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002244 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002245 req = vha->req;
2246 rsp = ha->rsp_q_map[0];
2247
2248 /* So we know we haven't pci_map'ed anything yet */
2249 tot_dsds = 0;
2250
2251 dbval = 0x04 | (ha->portnum << 5);
2252
2253 /* Send marker if required */
2254 if (vha->marker_needed != 0) {
2255 if (qla2x00_marker(vha, req,
2256 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2257 ql_log(ql_log_warn, vha, 0x300c,
2258 "qla2x00_marker failed for cmd=%p.\n", cmd);
2259 return QLA_FUNCTION_FAILED;
2260 }
2261 vha->marker_needed = 0;
2262 }
2263
2264 /* Acquire ring specific lock */
2265 spin_lock_irqsave(&ha->hardware_lock, flags);
2266
2267 /* Check for room in outstanding command list. */
2268 handle = req->current_outstanding_cmd;
2269 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
2270 handle++;
2271 if (handle == MAX_OUTSTANDING_COMMANDS)
2272 handle = 1;
2273 if (!req->outstanding_cmds[handle])
2274 break;
2275 }
2276 if (index == MAX_OUTSTANDING_COMMANDS)
2277 goto queuing_error;
2278
2279 /* Map the sg table so we have an accurate count of sg entries needed */
2280 if (scsi_sg_count(cmd)) {
2281 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2282 scsi_sg_count(cmd), cmd->sc_data_direction);
2283 if (unlikely(!nseg))
2284 goto queuing_error;
2285 } else
2286 nseg = 0;
2287
2288 tot_dsds = nseg;
2289
2290 if (tot_dsds > ql2xshiftctondsd) {
2291 struct cmd_type_6 *cmd_pkt;
2292 uint16_t more_dsd_lists = 0;
2293 struct dsd_dma *dsd_ptr;
2294 uint16_t i;
2295
2296 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2297 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2298 ql_dbg(ql_dbg_io, vha, 0x300d,
2299 "Num of DSD list %d is than %d for cmd=%p.\n",
2300 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2301 cmd);
2302 goto queuing_error;
2303 }
2304
2305 if (more_dsd_lists <= ha->gbl_dsd_avail)
2306 goto sufficient_dsds;
2307 else
2308 more_dsd_lists -= ha->gbl_dsd_avail;
2309
2310 for (i = 0; i < more_dsd_lists; i++) {
2311 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2312 if (!dsd_ptr) {
2313 ql_log(ql_log_fatal, vha, 0x300e,
2314 "Failed to allocate memory for dsd_dma "
2315 "for cmd=%p.\n", cmd);
2316 goto queuing_error;
2317 }
2318
2319 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2320 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2321 if (!dsd_ptr->dsd_addr) {
2322 kfree(dsd_ptr);
2323 ql_log(ql_log_fatal, vha, 0x300f,
2324 "Failed to allocate memory for dsd_addr "
2325 "for cmd=%p.\n", cmd);
2326 goto queuing_error;
2327 }
2328 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2329 ha->gbl_dsd_avail++;
2330 }
2331
2332sufficient_dsds:
2333 req_cnt = 1;
2334
2335 if (req->cnt < (req_cnt + 2)) {
2336 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2337 &reg->req_q_out[0]);
2338 if (req->ring_index < cnt)
2339 req->cnt = cnt - req->ring_index;
2340 else
2341 req->cnt = req->length -
2342 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04002343 if (req->cnt < (req_cnt + 2))
2344 goto queuing_error;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002345 }
2346
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002347 ctx = sp->u.scmd.ctx =
2348 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2349 if (!ctx) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002350 ql_log(ql_log_fatal, vha, 0x3010,
2351 "Failed to allocate ctx for cmd=%p.\n", cmd);
2352 goto queuing_error;
2353 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002354
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002355 memset(ctx, 0, sizeof(struct ct6_dsd));
2356 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2357 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2358 if (!ctx->fcp_cmnd) {
2359 ql_log(ql_log_fatal, vha, 0x3011,
2360 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
2361 goto queuing_error_fcp_cmnd;
2362 }
2363
2364 /* Initialize the DSD list and dma handle */
2365 INIT_LIST_HEAD(&ctx->dsd_list);
2366 ctx->dsd_use_cnt = 0;
2367
2368 if (cmd->cmd_len > 16) {
2369 additional_cdb_len = cmd->cmd_len - 16;
2370 if ((cmd->cmd_len % 4) != 0) {
2371 /* SCSI command bigger than 16 bytes must be
2372 * multiple of 4
2373 */
2374 ql_log(ql_log_warn, vha, 0x3012,
2375 "scsi cmd len %d not multiple of 4 "
2376 "for cmd=%p.\n", cmd->cmd_len, cmd);
2377 goto queuing_error_fcp_cmnd;
2378 }
2379 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2380 } else {
2381 additional_cdb_len = 0;
2382 ctx->fcp_cmnd_len = 12 + 16 + 4;
2383 }
2384
2385 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2386 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2387
2388 /* Zero out remaining portion of packet. */
2389 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2390 clr_ptr = (uint32_t *)cmd_pkt + 2;
2391 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2392 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2393
2394 /* Set NPORT-ID and LUN number*/
2395 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2396 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2397 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2398 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2399 cmd_pkt->vp_index = sp->fcport->vp_idx;
2400
2401 /* Build IOCB segments */
2402 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2403 goto queuing_error_fcp_cmnd;
2404
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002405 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002406 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2407
2408 /* build FCP_CMND IU */
2409 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002410 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002411 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2412
2413 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2414 ctx->fcp_cmnd->additional_cdb_len |= 1;
2415 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2416 ctx->fcp_cmnd->additional_cdb_len |= 2;
2417
2418 /*
2419 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2420 */
2421 if (scsi_populate_tag_msg(cmd, tag)) {
2422 switch (tag[0]) {
2423 case HEAD_OF_QUEUE_TAG:
2424 ctx->fcp_cmnd->task_attribute =
2425 TSK_HEAD_OF_QUEUE;
2426 break;
2427 case ORDERED_QUEUE_TAG:
2428 ctx->fcp_cmnd->task_attribute =
2429 TSK_ORDERED;
2430 break;
2431 }
2432 }
2433
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002434 /* Populate the FCP_PRIO. */
2435 if (ha->flags.fcp_prio_enabled)
2436 ctx->fcp_cmnd->task_attribute |=
2437 sp->fcport->fcp_prio << 3;
2438
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002439 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2440
2441 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2442 additional_cdb_len);
2443 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2444
2445 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2446 cmd_pkt->fcp_cmnd_dseg_address[0] =
2447 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2448 cmd_pkt->fcp_cmnd_dseg_address[1] =
2449 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2450
2451 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2452 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2453 /* Set total data segment count. */
2454 cmd_pkt->entry_count = (uint8_t)req_cnt;
2455 /* Specify response queue number where
2456 * completion should happen
2457 */
2458 cmd_pkt->entry_status = (uint8_t) rsp->id;
2459 } else {
2460 struct cmd_type_7 *cmd_pkt;
2461 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2462 if (req->cnt < (req_cnt + 2)) {
2463 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2464 &reg->req_q_out[0]);
2465 if (req->ring_index < cnt)
2466 req->cnt = cnt - req->ring_index;
2467 else
2468 req->cnt = req->length -
2469 (req->ring_index - cnt);
2470 }
2471 if (req->cnt < (req_cnt + 2))
2472 goto queuing_error;
2473
2474 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2475 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2476
2477 /* Zero out remaining portion of packet. */
2478 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2479 clr_ptr = (uint32_t *)cmd_pkt + 2;
2480 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2481 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2482
2483 /* Set NPORT-ID and LUN number*/
2484 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2485 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2486 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2487 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2488 cmd_pkt->vp_index = sp->fcport->vp_idx;
2489
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002490 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002491 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002492 sizeof(cmd_pkt->lun));
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002493
2494 /*
2495 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2496 */
2497 if (scsi_populate_tag_msg(cmd, tag)) {
2498 switch (tag[0]) {
2499 case HEAD_OF_QUEUE_TAG:
2500 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
2501 break;
2502 case ORDERED_QUEUE_TAG:
2503 cmd_pkt->task = TSK_ORDERED;
2504 break;
2505 }
2506 }
2507
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002508 /* Populate the FCP_PRIO. */
2509 if (ha->flags.fcp_prio_enabled)
2510 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2511
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002512 /* Load SCSI command packet. */
2513 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2514 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2515
2516 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2517
2518 /* Build IOCB segments */
2519 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2520
2521 /* Set total data segment count. */
2522 cmd_pkt->entry_count = (uint8_t)req_cnt;
2523 /* Specify response queue number where
2524 * completion should happen.
2525 */
2526 cmd_pkt->entry_status = (uint8_t) rsp->id;
2527
2528 }
2529 /* Build command packet. */
2530 req->current_outstanding_cmd = handle;
2531 req->outstanding_cmds[handle] = sp;
2532 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002533 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002534 req->cnt -= req_cnt;
2535 wmb();
2536
2537 /* Adjust ring index. */
2538 req->ring_index++;
2539 if (req->ring_index == req->length) {
2540 req->ring_index = 0;
2541 req->ring_ptr = req->ring;
2542 } else
2543 req->ring_ptr++;
2544
2545 sp->flags |= SRB_DMA_VALID;
2546
2547 /* Set chip new ring index. */
2548 /* write, read and verify logic */
2549 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2550 if (ql2xdbwr)
2551 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2552 else {
2553 WRT_REG_DWORD(
2554 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2555 dbval);
2556 wmb();
2557 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
2558 WRT_REG_DWORD(
2559 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2560 dbval);
2561 wmb();
2562 }
2563 }
2564
2565 /* Manage unprocessed RIO/ZIO commands in response queue. */
2566 if (vha->flags.process_response_queue &&
2567 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2568 qla24xx_process_response_queue(vha, rsp);
2569
2570 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2571 return QLA_SUCCESS;
2572
2573queuing_error_fcp_cmnd:
2574 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2575queuing_error:
2576 if (tot_dsds)
2577 scsi_dma_unmap(cmd);
2578
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002579 if (sp->u.scmd.ctx) {
2580 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2581 sp->u.scmd.ctx = NULL;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002582 }
2583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2584
2585 return QLA_FUNCTION_FAILED;
2586}
2587
Andrew Vasquezac280b62009-08-20 11:06:05 -07002588int
2589qla2x00_start_sp(srb_t *sp)
2590{
2591 int rval;
2592 struct qla_hw_data *ha = sp->fcport->vha->hw;
2593 void *pkt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002594 unsigned long flags;
2595
2596 rval = QLA_FUNCTION_FAILED;
2597 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05002598 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002599 if (!pkt) {
2600 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2601 "qla2x00_alloc_iocbs failed.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07002602 goto done;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002603 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07002604
2605 rval = QLA_SUCCESS;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002606 switch (sp->type) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07002607 case SRB_LOGIN_CMD:
2608 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002609 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002610 qla2x00_login_iocb(sp, pkt);
2611 break;
2612 case SRB_LOGOUT_CMD:
2613 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002614 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002615 qla2x00_logout_iocb(sp, pkt);
2616 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002617 case SRB_ELS_CMD_RPT:
2618 case SRB_ELS_CMD_HST:
2619 qla24xx_els_iocb(sp, pkt);
2620 break;
2621 case SRB_CT_CMD:
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002622 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez57807902011-11-18 09:03:20 -08002623 qla24xx_ct_iocb(sp, pkt) :
2624 qla2x00_ct_iocb(sp, pkt);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002625 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002626 case SRB_ADISC_CMD:
2627 IS_FWI2_CAPABLE(ha) ?
2628 qla24xx_adisc_iocb(sp, pkt) :
2629 qla2x00_adisc_iocb(sp, pkt);
2630 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002631 case SRB_TM_CMD:
2632 qla24xx_tm_iocb(sp, pkt);
2633 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002634 default:
2635 break;
2636 }
2637
2638 wmb();
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002639 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
Andrew Vasquezac280b62009-08-20 11:06:05 -07002640done:
2641 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2642 return rval;
2643}