blob: ef0a5481b9dd46ce0efbad796fa9a6cf04744296 [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Saurav Kashyap1e633952013-02-08 01:57:54 -05003 * Copyright (c) 2003-2013 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"
Nicholas Bellinger2d70c102012-05-15 14:34:28 -04008#include "qla_target.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
10#include <linux/blkdev.h>
11#include <linux/delay.h>
12
13#include <scsi/scsi_tcq.h>
14
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -070015static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070016/**
17 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
18 * @cmd: SCSI command
19 *
20 * Returns the proper CF_* direction based on CDB.
21 */
22static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070023qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
25 uint16_t cflags;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080026 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040027 struct scsi_qla_host *vha = sp->fcport->vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29 cflags = 0;
30
31 /* Set transfer direction */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080032 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 cflags = CF_WRITE;
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040034 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080035 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 cflags = CF_READ;
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040037 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return (cflags);
40}
41
42/**
43 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
44 * Continuation Type 0 IOCBs to allocate.
45 *
46 * @dsds: number of data segment decriptors needed
47 *
48 * Returns the number of IOCB entries needed to store @dsds.
49 */
50uint16_t
51qla2x00_calc_iocbs_32(uint16_t dsds)
52{
53 uint16_t iocbs;
54
55 iocbs = 1;
56 if (dsds > 3) {
57 iocbs += (dsds - 3) / 7;
58 if ((dsds - 3) % 7)
59 iocbs++;
60 }
61 return (iocbs);
62}
63
64/**
65 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
66 * Continuation Type 1 IOCBs to allocate.
67 *
68 * @dsds: number of data segment decriptors needed
69 *
70 * Returns the number of IOCB entries needed to store @dsds.
71 */
72uint16_t
73qla2x00_calc_iocbs_64(uint16_t dsds)
74{
75 uint16_t iocbs;
76
77 iocbs = 1;
78 if (dsds > 2) {
79 iocbs += (dsds - 2) / 5;
80 if ((dsds - 2) % 5)
81 iocbs++;
82 }
83 return (iocbs);
84}
85
86/**
87 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
88 * @ha: HA context
89 *
90 * Returns a pointer to the Continuation Type 0 IOCB packet.
91 */
92static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070093qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070096 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080098 req->ring_index++;
99 if (req->ring_index == req->length) {
100 req->ring_index = 0;
101 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800103 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800106 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Load packet defaults. */
109 *((uint32_t *)(&cont_pkt->entry_type)) =
110 __constant_cpu_to_le32(CONTINUE_TYPE);
111
112 return (cont_pkt);
113}
114
115/**
116 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
117 * @ha: HA context
118 *
119 * Returns a pointer to the continuation type 1 IOCB packet.
120 */
121static inline cont_a64_entry_t *
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800122qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 cont_a64_entry_t *cont_pkt;
125
126 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800127 req->ring_index++;
128 if (req->ring_index == req->length) {
129 req->ring_index = 0;
130 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800132 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800135 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* Load packet defaults. */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400138 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
139 __constant_cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
141
142 return (cont_pkt);
143}
144
Arun Easibad75002010-05-04 15:01:30 -0700145static inline int
146qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
147{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800148 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
149 uint8_t guard = scsi_host_get_guard(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700150
Arun Easibad75002010-05-04 15:01:30 -0700151 /* We always use DIFF Bundling for best performance */
152 *fw_prot_opts = 0;
153
154 /* Translate SCSI opcode to a protection opcode */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800155 switch (scsi_get_prot_op(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700156 case SCSI_PROT_READ_STRIP:
157 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
158 break;
159 case SCSI_PROT_WRITE_INSERT:
160 *fw_prot_opts |= PO_MODE_DIF_INSERT;
161 break;
162 case SCSI_PROT_READ_INSERT:
163 *fw_prot_opts |= PO_MODE_DIF_INSERT;
164 break;
165 case SCSI_PROT_WRITE_STRIP:
166 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
167 break;
168 case SCSI_PROT_READ_PASS:
Arun Easibad75002010-05-04 15:01:30 -0700169 case SCSI_PROT_WRITE_PASS:
Arun Easi9e522cd2012-08-22 14:21:31 -0400170 if (guard & SHOST_DIX_GUARD_IP)
171 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
172 else
173 *fw_prot_opts |= PO_MODE_DIF_PASS;
Arun Easibad75002010-05-04 15:01:30 -0700174 break;
175 default: /* Normal Request */
176 *fw_prot_opts |= PO_MODE_DIF_PASS;
177 break;
178 }
179
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800180 return scsi_prot_sg_count(cmd);
Arun Easibad75002010-05-04 15:01:30 -0700181}
182
183/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
185 * capable IOCB types.
186 *
187 * @sp: SRB command to process
188 * @cmd_pkt: Command type 2 IOCB
189 * @tot_dsds: Total number of segments to transfer
190 */
191void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
192 uint16_t tot_dsds)
193{
194 uint16_t avail_dsds;
195 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800196 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900198 struct scatterlist *sg;
199 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800201 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 /* Update entry type to indicate Command Type 2 IOCB */
204 *((uint32_t *)(&cmd_pkt->entry_type)) =
205 __constant_cpu_to_le32(COMMAND_TYPE);
206
207 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900208 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
210 return;
211 }
212
Andrew Vasquez444786d2009-01-05 11:18:10 -0800213 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700214 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 /* Three DSDs are available in the Command Type 2 IOCB */
217 avail_dsds = 3;
218 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
219
220 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900221 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
222 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900224 /* Allocate additional continuation packets? */
225 if (avail_dsds == 0) {
226 /*
227 * Seven DSDs are available in the Continuation
228 * Type 0 IOCB.
229 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700230 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900231 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
232 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900234
235 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
236 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
237 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239}
240
241/**
242 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
243 * capable IOCB types.
244 *
245 * @sp: SRB command to process
246 * @cmd_pkt: Command type 3 IOCB
247 * @tot_dsds: Total number of segments to transfer
248 */
249void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
250 uint16_t tot_dsds)
251{
252 uint16_t avail_dsds;
253 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800254 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900256 struct scatterlist *sg;
257 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800259 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 /* Update entry type to indicate Command Type 3 IOCB */
262 *((uint32_t *)(&cmd_pkt->entry_type)) =
263 __constant_cpu_to_le32(COMMAND_A64_TYPE);
264
265 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900266 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
268 return;
269 }
270
Andrew Vasquez444786d2009-01-05 11:18:10 -0800271 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700272 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 /* Two DSDs are available in the Command Type 3 IOCB */
275 avail_dsds = 2;
276 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
277
278 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900279 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
280 dma_addr_t sle_dma;
281 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900283 /* Allocate additional continuation packets? */
284 if (avail_dsds == 0) {
285 /*
286 * Five DSDs are available in the Continuation
287 * Type 1 IOCB.
288 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800289 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900290 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
291 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900293
294 sle_dma = sg_dma_address(sg);
295 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
296 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
297 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
298 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300}
301
302/**
303 * qla2x00_start_scsi() - Send a SCSI command to the ISP
304 * @sp: command to send to the ISP
305 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700306 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 */
308int
309qla2x00_start_scsi(srb_t *sp)
310{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900311 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800313 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct scsi_cmnd *cmd;
315 uint32_t *clr_ptr;
316 uint32_t index;
317 uint32_t handle;
318 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 uint16_t cnt;
320 uint16_t req_cnt;
321 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700322 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800323 struct qla_hw_data *ha;
324 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800325 struct rsp_que *rsp;
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800326 char tag[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Setup device pointers. */
329 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800330 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800331 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700332 reg = &ha->iobase->isp;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800333 cmd = GET_CMD_SP(sp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800334 req = ha->req_q_map[0];
335 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500336 /* So we know we haven't pci_map'ed anything yet */
337 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800340 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700341 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
342 QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 return (QLA_FUNCTION_FAILED);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700344 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800345 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700349 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800352 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -0500353 for (index = 1; index < req->num_outstanding_cmds; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -0500355 if (handle == req->num_outstanding_cmds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800357 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 break;
359 }
Chad Dupuis8d93f552013-01-30 03:34:37 -0500360 if (index == req->num_outstanding_cmds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto queuing_error;
362
83021922005-04-17 15:10:41 -0500363 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700364 if (scsi_sg_count(cmd)) {
365 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
366 scsi_sg_count(cmd), cmd->sc_data_direction);
367 if (unlikely(!nseg))
368 goto queuing_error;
369 } else
370 nseg = 0;
371
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900372 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700375 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800376 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800378 if (req->ring_index < cnt)
379 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800381 req->cnt = req->length -
382 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -0400383 /* If still no head room then bail out */
384 if (req->cnt < (req_cnt + 2))
385 goto queuing_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800389 req->current_outstanding_cmd = handle;
390 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700391 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800392 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800393 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800395 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 cmd_pkt->handle = handle;
397 /* Zero out remaining portion of packet. */
398 clr_ptr = (uint32_t *)cmd_pkt + 2;
399 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
400 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
401
bdf79622005-04-17 15:06:53 -0500402 /* Set target ID and LUN number*/
403 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800404 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* Update tagged queuing modifier */
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800407 if (scsi_populate_tag_msg(cmd, tag)) {
408 switch (tag[0]) {
409 case HEAD_OF_QUEUE_TAG:
410 cmd_pkt->control_flags =
411 __constant_cpu_to_le16(CF_HEAD_TAG);
412 break;
413 case ORDERED_QUEUE_TAG:
414 cmd_pkt->control_flags =
415 __constant_cpu_to_le16(CF_ORDERED_TAG);
416 break;
417 default:
418 cmd_pkt->control_flags =
419 __constant_cpu_to_le16(CF_SIMPLE_TAG);
420 break;
421 }
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -0400422 } else {
423 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 /* Load SCSI command packet. */
427 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900428 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700431 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /* Set total data segment count. */
434 cmd_pkt->entry_count = (uint8_t)req_cnt;
435 wmb();
436
437 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800438 req->ring_index++;
439 if (req->ring_index == req->length) {
440 req->ring_index = 0;
441 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800443 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800448 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
450
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700451 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800452 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800453 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
454 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700455
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700456 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 return (QLA_SUCCESS);
458
459queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900460 if (tot_dsds)
461 scsi_dma_unmap(cmd);
462
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700463 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 return (QLA_FUNCTION_FAILED);
466}
467
468/**
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800469 * qla2x00_start_iocbs() - Execute the IOCB command
470 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400471void
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800472qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
473{
474 struct qla_hw_data *ha = vha->hw;
475 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800476
477 if (IS_QLA82XX(ha)) {
478 qla82xx_start_iocbs(vha);
479 } else {
480 /* Adjust ring index. */
481 req->ring_index++;
482 if (req->ring_index == req->length) {
483 req->ring_index = 0;
484 req->ring_ptr = req->ring;
485 } else
486 req->ring_ptr++;
487
488 /* Set chip new ring index. */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800489 if (ha->mqenable || IS_QLA83XX(ha)) {
490 WRT_REG_DWORD(req->req_q_in, req->ring_index);
Arun Easi98878a12012-02-09 11:15:59 -0800491 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400492 } else if (IS_QLAFX00(ha)) {
493 WRT_REG_DWORD(&reg->ispfx00.req_q_in, req->ring_index);
494 RD_REG_DWORD_RELAXED(&reg->ispfx00.req_q_in);
495 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800496 } else if (IS_FWI2_CAPABLE(ha)) {
497 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
498 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
499 } else {
500 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
501 req->ring_index);
502 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
503 }
504 }
505}
506
507/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * qla2x00_marker() - Send a marker IOCB to the firmware.
509 * @ha: HA context
510 * @loop_id: loop ID
511 * @lun: LUN
512 * @type: marker modifier
513 *
514 * Can be called from both normal and interrupt context.
515 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700516 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500518static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800519__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
520 struct rsp_que *rsp, uint16_t loop_id,
521 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700523 mrk_entry_t *mrk;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400524 struct mrk_entry_24xx *mrk24 = NULL;
525 struct mrk_entry_fx00 *mrkfx = NULL;
526
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800527 struct qla_hw_data *ha = vha->hw;
528 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Giridhar Malavali99b82122011-11-18 09:03:17 -0800530 req = ha->req_q_map[0];
Saurav Kashyapfa492632012-11-21 02:40:29 -0500531 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700532 if (mrk == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700533 ql_log(ql_log_warn, base_vha, 0x3026,
534 "Failed to allocate Marker IOCB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 return (QLA_FUNCTION_FAILED);
537 }
538
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700539 mrk->entry_type = MARKER_TYPE;
540 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (type != MK_SYNC_ALL) {
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400542 if (IS_QLAFX00(ha)) {
543 mrkfx = (struct mrk_entry_fx00 *) mrk;
544 mrkfx->handle = MAKE_HANDLE(req->id, mrkfx->handle);
545 mrkfx->handle_hi = 0;
546 mrkfx->tgt_id = cpu_to_le16(loop_id);
547 mrkfx->lun[1] = LSB(lun);
548 mrkfx->lun[2] = MSB(lun);
549 host_to_fcp_swap(mrkfx->lun, sizeof(mrkfx->lun));
550 } else if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700551 mrk24 = (struct mrk_entry_24xx *) mrk;
552 mrk24->nport_handle = cpu_to_le16(loop_id);
553 mrk24->lun[1] = LSB(lun);
554 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700555 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800556 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700557 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700558 } else {
559 SET_TARGET_ID(ha, mrk->target, loop_id);
560 mrk->lun = cpu_to_le16(lun);
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 }
563 wmb();
564
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800565 qla2x00_start_iocbs(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 return (QLA_SUCCESS);
568}
569
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700570int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800571qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
572 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
573 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 int ret;
576 unsigned long flags = 0;
577
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800578 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
579 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
580 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 return (ret);
583}
584
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400585/*
586 * qla2x00_issue_marker
587 *
588 * Issue marker
589 * Caller CAN have hardware lock held as specified by ha_locked parameter.
590 * Might release it, then reaquire.
591 */
592int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
593{
594 if (ha_locked) {
595 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
596 MK_SYNC_ALL) != QLA_SUCCESS)
597 return QLA_FUNCTION_FAILED;
598 } else {
599 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
600 MK_SYNC_ALL) != QLA_SUCCESS)
601 return QLA_FUNCTION_FAILED;
602 }
603 vha->marker_needed = 0;
604
605 return QLA_SUCCESS;
606}
607
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800608static inline int
609qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
610 uint16_t tot_dsds)
611{
612 uint32_t *cur_dsd = NULL;
613 scsi_qla_host_t *vha;
614 struct qla_hw_data *ha;
615 struct scsi_cmnd *cmd;
616 struct scatterlist *cur_seg;
617 uint32_t *dsd_seg;
618 void *next_dsd;
619 uint8_t avail_dsds;
620 uint8_t first_iocb = 1;
621 uint32_t dsd_list_len;
622 struct dsd_dma *dsd_ptr;
623 struct ct6_dsd *ctx;
624
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800625 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800626
627 /* Update entry type to indicate Command Type 3 IOCB */
628 *((uint32_t *)(&cmd_pkt->entry_type)) =
629 __constant_cpu_to_le32(COMMAND_TYPE_6);
630
631 /* No data transfer */
632 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
633 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
634 return 0;
635 }
636
637 vha = sp->fcport->vha;
638 ha = vha->hw;
639
640 /* Set transfer direction */
641 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
642 cmd_pkt->control_flags =
643 __constant_cpu_to_le16(CF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400644 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800645 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
646 cmd_pkt->control_flags =
647 __constant_cpu_to_le16(CF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400648 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800649 }
650
651 cur_seg = scsi_sglist(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800652 ctx = GET_CMD_CTX_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800653
654 while (tot_dsds) {
655 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
656 QLA_DSDS_PER_IOCB : tot_dsds;
657 tot_dsds -= avail_dsds;
658 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
659
660 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
661 struct dsd_dma, list);
662 next_dsd = dsd_ptr->dsd_addr;
663 list_del(&dsd_ptr->list);
664 ha->gbl_dsd_avail--;
665 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
666 ctx->dsd_use_cnt++;
667 ha->gbl_dsd_inuse++;
668
669 if (first_iocb) {
670 first_iocb = 0;
671 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
672 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
673 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
674 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
675 } else {
676 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
677 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
678 *cur_dsd++ = cpu_to_le32(dsd_list_len);
679 }
680 cur_dsd = (uint32_t *)next_dsd;
681 while (avail_dsds) {
682 dma_addr_t sle_dma;
683
684 sle_dma = sg_dma_address(cur_seg);
685 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
686 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
687 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
688 cur_seg = sg_next(cur_seg);
689 avail_dsds--;
690 }
691 }
692
693 /* Null termination */
694 *cur_dsd++ = 0;
695 *cur_dsd++ = 0;
696 *cur_dsd++ = 0;
697 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
698 return 0;
699}
700
701/*
702 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
703 * for Command Type 6.
704 *
705 * @dsds: number of data segment decriptors needed
706 *
707 * Returns the number of dsd list needed to store @dsds.
708 */
709inline uint16_t
710qla24xx_calc_dsd_lists(uint16_t dsds)
711{
712 uint16_t dsd_lists = 0;
713
714 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
715 if (dsds % QLA_DSDS_PER_IOCB)
716 dsd_lists++;
717 return dsd_lists;
718}
719
720
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700721/**
722 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
723 * IOCB types.
724 *
725 * @sp: SRB command to process
726 * @cmd_pkt: Command type 3 IOCB
727 * @tot_dsds: Total number of segments to transfer
728 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700729inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700730qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
731 uint16_t tot_dsds)
732{
733 uint16_t avail_dsds;
734 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800735 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700736 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900737 struct scatterlist *sg;
738 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800739 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700740
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800741 cmd = GET_CMD_SP(sp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700742
743 /* Update entry type to indicate Command Type 3 IOCB */
744 *((uint32_t *)(&cmd_pkt->entry_type)) =
745 __constant_cpu_to_le32(COMMAND_TYPE_7);
746
747 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900748 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700749 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
750 return;
751 }
752
Andrew Vasquez444786d2009-01-05 11:18:10 -0800753 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700754 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700755
756 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700757 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700758 cmd_pkt->task_mgmt_flags =
759 __constant_cpu_to_le16(TMF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400760 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700761 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700762 cmd_pkt->task_mgmt_flags =
763 __constant_cpu_to_le16(TMF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400764 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700765 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700766
767 /* One DSD is available in the Command Type 3 IOCB */
768 avail_dsds = 1;
769 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
770
771 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700772
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900773 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
774 dma_addr_t sle_dma;
775 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700776
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900777 /* Allocate additional continuation packets? */
778 if (avail_dsds == 0) {
779 /*
780 * Five DSDs are available in the Continuation
781 * Type 1 IOCB.
782 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800783 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900784 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
785 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700786 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900787
788 sle_dma = sg_dma_address(sg);
789 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
790 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
791 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
792 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700793 }
794}
795
Arun Easibad75002010-05-04 15:01:30 -0700796struct fw_dif_context {
797 uint32_t ref_tag;
798 uint16_t app_tag;
799 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
800 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
801};
802
803/*
804 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
805 *
806 */
807static inline void
Arun Easie02587d2011-08-16 11:29:23 -0700808qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
Arun Easibad75002010-05-04 15:01:30 -0700809 unsigned int protcnt)
810{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800811 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -0700812
813 switch (scsi_get_prot_type(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700814 case SCSI_PROT_DIF_TYPE0:
Arun Easi8cb20492011-08-16 11:29:22 -0700815 /*
816 * No check for ql2xenablehba_err_chk, as it would be an
817 * I/O error if hba tag generation is not done.
818 */
819 pkt->ref_tag = cpu_to_le32((uint32_t)
820 (0xffffffff & scsi_get_lba(cmd)));
Arun Easie02587d2011-08-16 11:29:23 -0700821
822 if (!qla2x00_hba_err_chk_enabled(sp))
823 break;
824
Arun Easi8cb20492011-08-16 11:29:22 -0700825 pkt->ref_tag_mask[0] = 0xff;
826 pkt->ref_tag_mask[1] = 0xff;
827 pkt->ref_tag_mask[2] = 0xff;
828 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700829 break;
830
831 /*
832 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
833 * match LBA in CDB + N
834 */
835 case SCSI_PROT_DIF_TYPE2:
Arun Easie02587d2011-08-16 11:29:23 -0700836 pkt->app_tag = __constant_cpu_to_le16(0);
837 pkt->app_tag_mask[0] = 0x0;
838 pkt->app_tag_mask[1] = 0x0;
Arun Easi0c470872010-07-23 15:28:38 +0500839
840 pkt->ref_tag = cpu_to_le32((uint32_t)
841 (0xffffffff & scsi_get_lba(cmd)));
842
Arun Easie02587d2011-08-16 11:29:23 -0700843 if (!qla2x00_hba_err_chk_enabled(sp))
844 break;
845
Arun Easi0c470872010-07-23 15:28:38 +0500846 /* enable ALL bytes of the ref tag */
847 pkt->ref_tag_mask[0] = 0xff;
848 pkt->ref_tag_mask[1] = 0xff;
849 pkt->ref_tag_mask[2] = 0xff;
850 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700851 break;
852
853 /* For Type 3 protection: 16 bit GUARD only */
854 case SCSI_PROT_DIF_TYPE3:
855 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
856 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
857 0x00;
858 break;
859
860 /*
861 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
862 * 16 bit app tag.
863 */
864 case SCSI_PROT_DIF_TYPE1:
Arun Easie02587d2011-08-16 11:29:23 -0700865 pkt->ref_tag = cpu_to_le32((uint32_t)
866 (0xffffffff & scsi_get_lba(cmd)));
867 pkt->app_tag = __constant_cpu_to_le16(0);
868 pkt->app_tag_mask[0] = 0x0;
869 pkt->app_tag_mask[1] = 0x0;
870
871 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -0700872 break;
873
Arun Easibad75002010-05-04 15:01:30 -0700874 /* enable ALL bytes of the ref tag */
875 pkt->ref_tag_mask[0] = 0xff;
876 pkt->ref_tag_mask[1] = 0xff;
877 pkt->ref_tag_mask[2] = 0xff;
878 pkt->ref_tag_mask[3] = 0xff;
879 break;
880 }
Arun Easibad75002010-05-04 15:01:30 -0700881}
882
Arun Easi8cb20492011-08-16 11:29:22 -0700883struct qla2_sgx {
884 dma_addr_t dma_addr; /* OUT */
885 uint32_t dma_len; /* OUT */
Arun Easibad75002010-05-04 15:01:30 -0700886
Arun Easi8cb20492011-08-16 11:29:22 -0700887 uint32_t tot_bytes; /* IN */
888 struct scatterlist *cur_sg; /* IN */
889
890 /* for book keeping, bzero on initial invocation */
891 uint32_t bytes_consumed;
892 uint32_t num_bytes;
893 uint32_t tot_partial;
894
895 /* for debugging */
896 uint32_t num_sg;
897 srb_t *sp;
898};
899
900static int
901qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
902 uint32_t *partial)
903{
904 struct scatterlist *sg;
905 uint32_t cumulative_partial, sg_len;
906 dma_addr_t sg_dma_addr;
907
908 if (sgx->num_bytes == sgx->tot_bytes)
909 return 0;
910
911 sg = sgx->cur_sg;
912 cumulative_partial = sgx->tot_partial;
913
914 sg_dma_addr = sg_dma_address(sg);
915 sg_len = sg_dma_len(sg);
916
917 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
918
919 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
920 sgx->dma_len = (blk_sz - cumulative_partial);
921 sgx->tot_partial = 0;
922 sgx->num_bytes += blk_sz;
923 *partial = 0;
924 } else {
925 sgx->dma_len = sg_len - sgx->bytes_consumed;
926 sgx->tot_partial += sgx->dma_len;
927 *partial = 1;
928 }
929
930 sgx->bytes_consumed += sgx->dma_len;
931
932 if (sg_len == sgx->bytes_consumed) {
933 sg = sg_next(sg);
934 sgx->num_sg++;
935 sgx->cur_sg = sg;
936 sgx->bytes_consumed = 0;
937 }
938
939 return 1;
940}
941
942static int
943qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
944 uint32_t *dsd, uint16_t tot_dsds)
945{
946 void *next_dsd;
947 uint8_t avail_dsds = 0;
948 uint32_t dsd_list_len;
949 struct dsd_dma *dsd_ptr;
950 struct scatterlist *sg_prot;
951 uint32_t *cur_dsd = dsd;
952 uint16_t used_dsds = tot_dsds;
953
954 uint32_t prot_int;
955 uint32_t partial;
956 struct qla2_sgx sgx;
957 dma_addr_t sle_dma;
958 uint32_t sle_dma_len, tot_prot_dma_len = 0;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800959 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easi8cb20492011-08-16 11:29:22 -0700960
961 prot_int = cmd->device->sector_size;
962
963 memset(&sgx, 0, sizeof(struct qla2_sgx));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800964 sgx.tot_bytes = scsi_bufflen(cmd);
965 sgx.cur_sg = scsi_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700966 sgx.sp = sp;
967
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800968 sg_prot = scsi_prot_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700969
970 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
971
972 sle_dma = sgx.dma_addr;
973 sle_dma_len = sgx.dma_len;
974alloc_and_fill:
975 /* Allocate additional continuation packets? */
976 if (avail_dsds == 0) {
977 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
978 QLA_DSDS_PER_IOCB : used_dsds;
979 dsd_list_len = (avail_dsds + 1) * 12;
980 used_dsds -= avail_dsds;
981
982 /* allocate tracking DS */
983 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
984 if (!dsd_ptr)
985 return 1;
986
987 /* allocate new list */
988 dsd_ptr->dsd_addr = next_dsd =
989 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
990 &dsd_ptr->dsd_list_dma);
991
992 if (!next_dsd) {
993 /*
994 * Need to cleanup only this dsd_ptr, rest
995 * will be done by sp_free_dma()
996 */
997 kfree(dsd_ptr);
998 return 1;
999 }
1000
1001 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001002 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easi8cb20492011-08-16 11:29:22 -07001003
1004 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1005
1006 /* add new list to cmd iocb or last list */
1007 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1008 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1009 *cur_dsd++ = dsd_list_len;
1010 cur_dsd = (uint32_t *)next_dsd;
1011 }
1012 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1013 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1014 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1015 avail_dsds--;
1016
1017 if (partial == 0) {
1018 /* Got a full protection interval */
1019 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1020 sle_dma_len = 8;
1021
1022 tot_prot_dma_len += sle_dma_len;
1023 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1024 tot_prot_dma_len = 0;
1025 sg_prot = sg_next(sg_prot);
1026 }
1027
1028 partial = 1; /* So as to not re-enter this block */
1029 goto alloc_and_fill;
1030 }
1031 }
1032 /* Null termination */
1033 *cur_dsd++ = 0;
1034 *cur_dsd++ = 0;
1035 *cur_dsd++ = 0;
1036 return 0;
1037}
Giridhar Malavali5162cf02011-11-18 09:03:18 -08001038
Arun Easibad75002010-05-04 15:01:30 -07001039static int
1040qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1041 uint16_t tot_dsds)
1042{
1043 void *next_dsd;
1044 uint8_t avail_dsds = 0;
1045 uint32_t dsd_list_len;
1046 struct dsd_dma *dsd_ptr;
1047 struct scatterlist *sg;
1048 uint32_t *cur_dsd = dsd;
1049 int i;
1050 uint16_t used_dsds = tot_dsds;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001051 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001052
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001053 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
Arun Easibad75002010-05-04 15:01:30 -07001054 dma_addr_t sle_dma;
1055
1056 /* Allocate additional continuation packets? */
1057 if (avail_dsds == 0) {
1058 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1059 QLA_DSDS_PER_IOCB : used_dsds;
1060 dsd_list_len = (avail_dsds + 1) * 12;
1061 used_dsds -= avail_dsds;
1062
1063 /* allocate tracking DS */
1064 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1065 if (!dsd_ptr)
1066 return 1;
1067
1068 /* allocate new list */
1069 dsd_ptr->dsd_addr = next_dsd =
1070 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1071 &dsd_ptr->dsd_list_dma);
1072
1073 if (!next_dsd) {
1074 /*
1075 * Need to cleanup only this dsd_ptr, rest
1076 * will be done by sp_free_dma()
1077 */
1078 kfree(dsd_ptr);
1079 return 1;
1080 }
1081
1082 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001083 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001084
1085 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1086
1087 /* add new list to cmd iocb or last list */
1088 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1089 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1090 *cur_dsd++ = dsd_list_len;
1091 cur_dsd = (uint32_t *)next_dsd;
1092 }
1093 sle_dma = sg_dma_address(sg);
Arun Easi9e522cd2012-08-22 14:21:31 -04001094
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
Arun Easibad75002010-05-04 15:01:30 -07001100 }
1101 /* Null termination */
1102 *cur_dsd++ = 0;
1103 *cur_dsd++ = 0;
1104 *cur_dsd++ = 0;
1105 return 0;
1106}
1107
1108static int
1109qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1110 uint32_t *dsd,
1111 uint16_t tot_dsds)
1112{
1113 void *next_dsd;
1114 uint8_t avail_dsds = 0;
1115 uint32_t dsd_list_len;
1116 struct dsd_dma *dsd_ptr;
1117 struct scatterlist *sg;
1118 int i;
1119 struct scsi_cmnd *cmd;
1120 uint32_t *cur_dsd = dsd;
1121 uint16_t used_dsds = tot_dsds;
Arun Easibad75002010-05-04 15:01:30 -07001122
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001123 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001124 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1125 dma_addr_t sle_dma;
1126
1127 /* Allocate additional continuation packets? */
1128 if (avail_dsds == 0) {
1129 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1130 QLA_DSDS_PER_IOCB : used_dsds;
1131 dsd_list_len = (avail_dsds + 1) * 12;
1132 used_dsds -= avail_dsds;
1133
1134 /* allocate tracking DS */
1135 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1136 if (!dsd_ptr)
1137 return 1;
1138
1139 /* allocate new list */
1140 dsd_ptr->dsd_addr = next_dsd =
1141 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1142 &dsd_ptr->dsd_list_dma);
1143
1144 if (!next_dsd) {
1145 /*
1146 * Need to cleanup only this dsd_ptr, rest
1147 * will be done by sp_free_dma()
1148 */
1149 kfree(dsd_ptr);
1150 return 1;
1151 }
1152
1153 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001154 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001155
1156 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1157
1158 /* add new list to cmd iocb or last list */
1159 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1160 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1161 *cur_dsd++ = dsd_list_len;
1162 cur_dsd = (uint32_t *)next_dsd;
1163 }
1164 sle_dma = sg_dma_address(sg);
Arun Easi9e522cd2012-08-22 14:21:31 -04001165
Arun Easibad75002010-05-04 15:01:30 -07001166 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1167 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1168 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1169
Arun Easibad75002010-05-04 15:01:30 -07001170 avail_dsds--;
1171 }
1172 /* Null termination */
1173 *cur_dsd++ = 0;
1174 *cur_dsd++ = 0;
1175 *cur_dsd++ = 0;
1176 return 0;
1177}
1178
1179/**
1180 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1181 * Type 6 IOCB types.
1182 *
1183 * @sp: SRB command to process
1184 * @cmd_pkt: Command type 3 IOCB
1185 * @tot_dsds: Total number of segments to transfer
1186 */
1187static inline int
1188qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1189 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1190{
1191 uint32_t *cur_dsd, *fcp_dl;
1192 scsi_qla_host_t *vha;
1193 struct scsi_cmnd *cmd;
Arun Easibad75002010-05-04 15:01:30 -07001194 int sgc;
Arun Easi8cb20492011-08-16 11:29:22 -07001195 uint32_t total_bytes = 0;
Arun Easibad75002010-05-04 15:01:30 -07001196 uint32_t data_bytes;
1197 uint32_t dif_bytes;
1198 uint8_t bundling = 1;
1199 uint16_t blk_size;
1200 uint8_t *clr_ptr;
1201 struct crc_context *crc_ctx_pkt = NULL;
1202 struct qla_hw_data *ha;
1203 uint8_t additional_fcpcdb_len;
1204 uint16_t fcp_cmnd_len;
1205 struct fcp_cmnd *fcp_cmnd;
1206 dma_addr_t crc_ctx_dma;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001207 char tag[2];
Arun Easibad75002010-05-04 15:01:30 -07001208
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001209 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001210
1211 sgc = 0;
1212 /* Update entry type to indicate Command Type CRC_2 IOCB */
1213 *((uint32_t *)(&cmd_pkt->entry_type)) =
1214 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1215
Arun Easibad75002010-05-04 15:01:30 -07001216 vha = sp->fcport->vha;
1217 ha = vha->hw;
1218
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001219 /* No data transfer */
1220 data_bytes = scsi_bufflen(cmd);
1221 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1222 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1223 return QLA_SUCCESS;
1224 }
Arun Easibad75002010-05-04 15:01:30 -07001225
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001226 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Arun Easibad75002010-05-04 15:01:30 -07001227
1228 /* Set transfer direction */
1229 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1230 cmd_pkt->control_flags =
1231 __constant_cpu_to_le16(CF_WRITE_DATA);
1232 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1233 cmd_pkt->control_flags =
1234 __constant_cpu_to_le16(CF_READ_DATA);
1235 }
1236
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001237 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1238 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1239 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1240 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
Arun Easibad75002010-05-04 15:01:30 -07001241 bundling = 0;
1242
1243 /* Allocate CRC context from global pool */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001244 crc_ctx_pkt = sp->u.scmd.ctx =
1245 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Arun Easibad75002010-05-04 15:01:30 -07001246
1247 if (!crc_ctx_pkt)
1248 goto crc_queuing_error;
1249
1250 /* Zero out CTX area. */
1251 clr_ptr = (uint8_t *)crc_ctx_pkt;
1252 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1253
1254 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1255
1256 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1257
1258 /* Set handle */
1259 crc_ctx_pkt->handle = cmd_pkt->handle;
1260
1261 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1262
Arun Easie02587d2011-08-16 11:29:23 -07001263 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
Arun Easibad75002010-05-04 15:01:30 -07001264 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1265
1266 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1267 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1268 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1269
1270 /* Determine SCSI command length -- align to 4 byte boundary */
1271 if (cmd->cmd_len > 16) {
Arun Easibad75002010-05-04 15:01:30 -07001272 additional_fcpcdb_len = cmd->cmd_len - 16;
1273 if ((cmd->cmd_len % 4) != 0) {
1274 /* SCSI cmd > 16 bytes must be multiple of 4 */
1275 goto crc_queuing_error;
1276 }
1277 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1278 } else {
1279 additional_fcpcdb_len = 0;
1280 fcp_cmnd_len = 12 + 16 + 4;
1281 }
1282
1283 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1284
1285 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1286 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1287 fcp_cmnd->additional_cdb_len |= 1;
1288 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1289 fcp_cmnd->additional_cdb_len |= 2;
1290
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001291 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
Arun Easibad75002010-05-04 15:01:30 -07001292 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1293 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1294 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1295 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1296 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1297 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001298 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001299
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001300 /*
1301 * Update tagged queuing modifier if using command tag queuing
1302 */
1303 if (scsi_populate_tag_msg(cmd, tag)) {
1304 switch (tag[0]) {
1305 case HEAD_OF_QUEUE_TAG:
1306 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1307 break;
1308 case ORDERED_QUEUE_TAG:
1309 fcp_cmnd->task_attribute = TSK_ORDERED;
1310 break;
1311 default:
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001312 fcp_cmnd->task_attribute = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001313 break;
1314 }
1315 } else {
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001316 fcp_cmnd->task_attribute = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001317 }
1318
Arun Easibad75002010-05-04 15:01:30 -07001319 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1320
Arun Easibad75002010-05-04 15:01:30 -07001321 /* Compute dif len and adjust data len to incude protection */
Arun Easibad75002010-05-04 15:01:30 -07001322 dif_bytes = 0;
1323 blk_size = cmd->device->sector_size;
Arun Easi8cb20492011-08-16 11:29:22 -07001324 dif_bytes = (data_bytes / blk_size) * 8;
1325
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001326 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
Arun Easi8cb20492011-08-16 11:29:22 -07001327 case SCSI_PROT_READ_INSERT:
1328 case SCSI_PROT_WRITE_STRIP:
1329 total_bytes = data_bytes;
1330 data_bytes += dif_bytes;
1331 break;
1332
1333 case SCSI_PROT_READ_STRIP:
1334 case SCSI_PROT_WRITE_INSERT:
1335 case SCSI_PROT_READ_PASS:
1336 case SCSI_PROT_WRITE_PASS:
1337 total_bytes = data_bytes + dif_bytes;
1338 break;
1339 default:
1340 BUG();
Arun Easibad75002010-05-04 15:01:30 -07001341 }
1342
Arun Easie02587d2011-08-16 11:29:23 -07001343 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -07001344 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
Arun Easi9e522cd2012-08-22 14:21:31 -04001345 /* HBA error checking enabled */
1346 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1347 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1348 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1349 SCSI_PROT_DIF_TYPE2))
1350 fw_prot_opts |= BIT_10;
1351 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1352 SCSI_PROT_DIF_TYPE3)
1353 fw_prot_opts |= BIT_11;
1354 }
Arun Easibad75002010-05-04 15:01:30 -07001355
1356 if (!bundling) {
1357 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1358 } else {
1359 /*
1360 * Configure Bundling if we need to fetch interlaving
1361 * protection PCI accesses
1362 */
1363 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1364 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1365 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1366 tot_prot_dsds);
1367 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1368 }
1369
1370 /* Finish the common fields of CRC pkt */
1371 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1372 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1373 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1374 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1375 /* Fibre channel byte count */
1376 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1377 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1378 additional_fcpcdb_len);
1379 *fcp_dl = htonl(total_bytes);
1380
Arun Easi0c470872010-07-23 15:28:38 +05001381 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
Arun Easi0c470872010-07-23 15:28:38 +05001382 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1383 return QLA_SUCCESS;
1384 }
Arun Easibad75002010-05-04 15:01:30 -07001385 /* Walks data segments */
1386
1387 cmd_pkt->control_flags |=
1388 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
Arun Easi8cb20492011-08-16 11:29:22 -07001389
1390 if (!bundling && tot_prot_dsds) {
1391 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1392 cur_dsd, tot_dsds))
1393 goto crc_queuing_error;
1394 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
Arun Easibad75002010-05-04 15:01:30 -07001395 (tot_dsds - tot_prot_dsds)))
1396 goto crc_queuing_error;
1397
1398 if (bundling && tot_prot_dsds) {
1399 /* Walks dif segments */
Arun Easibad75002010-05-04 15:01:30 -07001400 cmd_pkt->control_flags |=
1401 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1402 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1403 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1404 tot_prot_dsds))
1405 goto crc_queuing_error;
1406 }
1407 return QLA_SUCCESS;
1408
1409crc_queuing_error:
Arun Easibad75002010-05-04 15:01:30 -07001410 /* Cleanup will be performed by the caller */
1411
1412 return QLA_FUNCTION_FAILED;
1413}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001414
1415/**
1416 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1417 * @sp: command to send to the ISP
1418 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001419 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001420 */
1421int
1422qla24xx_start_scsi(srb_t *sp)
1423{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001424 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001425 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001426 uint32_t *clr_ptr;
1427 uint32_t index;
1428 uint32_t handle;
1429 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001430 uint16_t cnt;
1431 uint16_t req_cnt;
1432 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001433 struct req_que *req = NULL;
1434 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001435 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Andrew Vasquez444786d2009-01-05 11:18:10 -08001436 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001437 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001438 char tag[2];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001439
1440 /* Setup device pointers. */
1441 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001442
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001443 qla25xx_set_que(sp, &rsp);
1444 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001445
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001446 /* So we know we haven't pci_map'ed anything yet */
1447 tot_dsds = 0;
1448
1449 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001450 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001451 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1452 QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001453 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001454 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001455 }
1456
1457 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001458 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001459
1460 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001461 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001462 for (index = 1; index < req->num_outstanding_cmds; index++) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001463 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001464 if (handle == req->num_outstanding_cmds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001465 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001466 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001467 break;
1468 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05001469 if (index == req->num_outstanding_cmds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001470 goto queuing_error;
1471
1472 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001473 if (scsi_sg_count(cmd)) {
1474 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1475 scsi_sg_count(cmd), cmd->sc_data_direction);
1476 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001477 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001478 } else
1479 nseg = 0;
1480
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001481 tot_dsds = nseg;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001482 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001483 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001484 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001485
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001486 if (req->ring_index < cnt)
1487 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001488 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001489 req->cnt = req->length -
1490 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001491 if (req->cnt < (req_cnt + 2))
1492 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001493 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001494
1495 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001496 req->current_outstanding_cmd = handle;
1497 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001498 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001499 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001500 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001501
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001502 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001503 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001504
1505 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001506 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001507 clr_ptr = (uint32_t *)cmd_pkt + 2;
1508 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1509 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1510
1511 /* Set NPORT-ID and LUN number*/
1512 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1513 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1514 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1515 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001516 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001517
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001518 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001519 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001520
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001521 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1522 if (scsi_populate_tag_msg(cmd, tag)) {
1523 switch (tag[0]) {
1524 case HEAD_OF_QUEUE_TAG:
1525 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1526 break;
1527 case ORDERED_QUEUE_TAG:
1528 cmd_pkt->task = TSK_ORDERED;
1529 break;
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001530 default:
1531 cmd_pkt->task = TSK_SIMPLE;
1532 break;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001533 }
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001534 } else {
1535 cmd_pkt->task = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001536 }
1537
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001538 /* Load SCSI command packet. */
1539 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1540 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1541
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001542 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001543
1544 /* Build IOCB segments */
1545 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1546
1547 /* Set total data segment count. */
1548 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001549 /* Specify response queue number where completion should happen */
1550 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001551 wmb();
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001552 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001553 req->ring_index++;
1554 if (req->ring_index == req->length) {
1555 req->ring_index = 0;
1556 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001557 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001558 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001559
1560 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001561
1562 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001563 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1564 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001565
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001566 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001567 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001568 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001569 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001570
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001571 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001572 return QLA_SUCCESS;
1573
1574queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001575 if (tot_dsds)
1576 scsi_dma_unmap(cmd);
1577
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001578 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001579
1580 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001582
Arun Easibad75002010-05-04 15:01:30 -07001583/**
1584 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1585 * @sp: command to send to the ISP
1586 *
1587 * Returns non-zero if a failure occurred, else zero.
1588 */
1589int
1590qla24xx_dif_start_scsi(srb_t *sp)
1591{
1592 int nseg;
1593 unsigned long flags;
1594 uint32_t *clr_ptr;
1595 uint32_t index;
1596 uint32_t handle;
1597 uint16_t cnt;
1598 uint16_t req_cnt = 0;
1599 uint16_t tot_dsds;
1600 uint16_t tot_prot_dsds;
1601 uint16_t fw_prot_opts = 0;
1602 struct req_que *req = NULL;
1603 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001604 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001605 struct scsi_qla_host *vha = sp->fcport->vha;
1606 struct qla_hw_data *ha = vha->hw;
1607 struct cmd_type_crc_2 *cmd_pkt;
1608 uint32_t status = 0;
1609
1610#define QDSS_GOT_Q_SPACE BIT_0
1611
Arun Easi0c470872010-07-23 15:28:38 +05001612 /* Only process protection or >16 cdb in this routine */
1613 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1614 if (cmd->cmd_len <= 16)
1615 return qla24xx_start_scsi(sp);
1616 }
Arun Easibad75002010-05-04 15:01:30 -07001617
1618 /* Setup device pointers. */
1619
1620 qla25xx_set_que(sp, &rsp);
1621 req = vha->req;
1622
1623 /* So we know we haven't pci_map'ed anything yet */
1624 tot_dsds = 0;
1625
1626 /* Send marker if required */
1627 if (vha->marker_needed != 0) {
1628 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1629 QLA_SUCCESS)
1630 return QLA_FUNCTION_FAILED;
1631 vha->marker_needed = 0;
1632 }
1633
1634 /* Acquire ring specific lock */
1635 spin_lock_irqsave(&ha->hardware_lock, flags);
1636
1637 /* Check for room in outstanding command list. */
1638 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001639 for (index = 1; index < req->num_outstanding_cmds; index++) {
Arun Easibad75002010-05-04 15:01:30 -07001640 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001641 if (handle == req->num_outstanding_cmds)
Arun Easibad75002010-05-04 15:01:30 -07001642 handle = 1;
1643 if (!req->outstanding_cmds[handle])
1644 break;
1645 }
1646
Chad Dupuis8d93f552013-01-30 03:34:37 -05001647 if (index == req->num_outstanding_cmds)
Arun Easibad75002010-05-04 15:01:30 -07001648 goto queuing_error;
1649
1650 /* Compute number of required data segments */
1651 /* Map the sg table so we have an accurate count of sg entries needed */
1652 if (scsi_sg_count(cmd)) {
1653 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1654 scsi_sg_count(cmd), cmd->sc_data_direction);
1655 if (unlikely(!nseg))
1656 goto queuing_error;
1657 else
1658 sp->flags |= SRB_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001659
1660 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1661 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1662 struct qla2_sgx sgx;
1663 uint32_t partial;
1664
1665 memset(&sgx, 0, sizeof(struct qla2_sgx));
1666 sgx.tot_bytes = scsi_bufflen(cmd);
1667 sgx.cur_sg = scsi_sglist(cmd);
1668 sgx.sp = sp;
1669
1670 nseg = 0;
1671 while (qla24xx_get_one_block_sg(
1672 cmd->device->sector_size, &sgx, &partial))
1673 nseg++;
1674 }
Arun Easibad75002010-05-04 15:01:30 -07001675 } else
1676 nseg = 0;
1677
1678 /* number of required data segments */
1679 tot_dsds = nseg;
1680
1681 /* Compute number of required protection segments */
1682 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1683 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1684 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1685 if (unlikely(!nseg))
1686 goto queuing_error;
1687 else
1688 sp->flags |= SRB_CRC_PROT_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001689
1690 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1691 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1692 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1693 }
Arun Easibad75002010-05-04 15:01:30 -07001694 } else {
1695 nseg = 0;
1696 }
1697
1698 req_cnt = 1;
1699 /* Total Data and protection sg segment(s) */
1700 tot_prot_dsds = nseg;
1701 tot_dsds += nseg;
1702 if (req->cnt < (req_cnt + 2)) {
1703 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1704
1705 if (req->ring_index < cnt)
1706 req->cnt = cnt - req->ring_index;
1707 else
1708 req->cnt = req->length -
1709 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001710 if (req->cnt < (req_cnt + 2))
1711 goto queuing_error;
Arun Easibad75002010-05-04 15:01:30 -07001712 }
1713
Arun Easibad75002010-05-04 15:01:30 -07001714 status |= QDSS_GOT_Q_SPACE;
1715
1716 /* Build header part of command packet (excluding the OPCODE). */
1717 req->current_outstanding_cmd = handle;
1718 req->outstanding_cmds[handle] = sp;
Arun Easi8cb20492011-08-16 11:29:22 -07001719 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001720 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Arun Easibad75002010-05-04 15:01:30 -07001721 req->cnt -= req_cnt;
1722
1723 /* Fill-in common area */
1724 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1725 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1726
1727 clr_ptr = (uint32_t *)cmd_pkt + 2;
1728 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1729
1730 /* Set NPORT-ID and LUN number*/
1731 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1732 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1733 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1734 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1735
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001736 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Arun Easibad75002010-05-04 15:01:30 -07001737 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1738
1739 /* Total Data and protection segment(s) */
1740 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1741
1742 /* Build IOCB segments and adjust for data protection segments */
1743 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1744 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1745 QLA_SUCCESS)
1746 goto queuing_error;
1747
1748 cmd_pkt->entry_count = (uint8_t)req_cnt;
1749 /* Specify response queue number where completion should happen */
1750 cmd_pkt->entry_status = (uint8_t) rsp->id;
1751 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1752 wmb();
1753
1754 /* Adjust ring index. */
1755 req->ring_index++;
1756 if (req->ring_index == req->length) {
1757 req->ring_index = 0;
1758 req->ring_ptr = req->ring;
1759 } else
1760 req->ring_ptr++;
1761
1762 /* Set chip new ring index. */
1763 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1764 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1765
1766 /* Manage unprocessed RIO/ZIO commands in response queue. */
1767 if (vha->flags.process_response_queue &&
1768 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1769 qla24xx_process_response_queue(vha, rsp);
1770
1771 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1772
1773 return QLA_SUCCESS;
1774
1775queuing_error:
1776 if (status & QDSS_GOT_Q_SPACE) {
1777 req->outstanding_cmds[handle] = NULL;
1778 req->cnt += req_cnt;
1779 }
1780 /* Cleanup will be performed by the caller (queuecommand) */
1781
1782 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Arun Easibad75002010-05-04 15:01:30 -07001783 return QLA_FUNCTION_FAILED;
1784}
1785
1786
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001787static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001788{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001789 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001790 struct qla_hw_data *ha = sp->fcport->vha->hw;
1791 int affinity = cmd->request->cpu;
1792
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001793 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001794 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001795 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001796 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001797 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001798}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001799
1800/* Generic Control-SRB manipulation functions. */
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001801void *
1802qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001803{
Andrew Vasquezac280b62009-08-20 11:06:05 -07001804 struct qla_hw_data *ha = vha->hw;
1805 struct req_que *req = ha->req_q_map[0];
1806 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1807 uint32_t index, handle;
1808 request_t *pkt;
1809 uint16_t cnt, req_cnt;
1810
1811 pkt = NULL;
1812 req_cnt = 1;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001813 handle = 0;
1814
1815 if (!sp)
1816 goto skip_cmd_array;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001817
1818 /* Check for room in outstanding command list. */
1819 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001820 for (index = 1; req->num_outstanding_cmds; index++) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001821 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001822 if (handle == req->num_outstanding_cmds)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001823 handle = 1;
1824 if (!req->outstanding_cmds[handle])
1825 break;
1826 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05001827 if (index == req->num_outstanding_cmds) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001828 ql_log(ql_log_warn, vha, 0x700b,
Masanari Iidad6a03582012-08-22 14:20:58 -04001829 "No room on outstanding cmd array.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07001830 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001831 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001832
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001833 /* Prep command array. */
1834 req->current_outstanding_cmd = handle;
1835 req->outstanding_cmds[handle] = sp;
1836 sp->handle = handle;
1837
Andrew Vasquez57807902011-11-18 09:03:20 -08001838 /* Adjust entry-counts as needed. */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001839 if (sp->type != SRB_SCSI_CMD)
1840 req_cnt = sp->iocbs;
Andrew Vasquez57807902011-11-18 09:03:20 -08001841
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001842skip_cmd_array:
Andrew Vasquezac280b62009-08-20 11:06:05 -07001843 /* Check for room on request queue. */
1844 if (req->cnt < req_cnt) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001845 if (ha->mqenable || IS_QLA83XX(ha))
Andrew Vasquezac280b62009-08-20 11:06:05 -07001846 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001847 else if (IS_QLA82XX(ha))
1848 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001849 else if (IS_FWI2_CAPABLE(ha))
1850 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001851 else if (IS_QLAFX00(ha))
1852 cnt = RD_REG_DWORD(&reg->ispfx00.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001853 else
1854 cnt = qla2x00_debounce_register(
1855 ISP_REQ_Q_OUT(ha, &reg->isp));
1856
1857 if (req->ring_index < cnt)
1858 req->cnt = cnt - req->ring_index;
1859 else
1860 req->cnt = req->length -
1861 (req->ring_index - cnt);
1862 }
1863 if (req->cnt < req_cnt)
1864 goto queuing_error;
1865
1866 /* Prep packet */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001867 req->cnt -= req_cnt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001868 pkt = req->ring_ptr;
1869 memset(pkt, 0, REQUEST_ENTRY_SIZE);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001870 if (IS_QLAFX00(ha)) {
Saurav Kashyap1f8deef2013-06-25 11:27:21 -04001871 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
1872 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001873 } else {
1874 pkt->entry_count = req_cnt;
1875 pkt->handle = handle;
1876 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001877
1878queuing_error:
1879 return pkt;
1880}
1881
1882static void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001883qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1884{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001885 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001886
1887 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1888 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001889 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001890 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001891 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001892 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1893 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1894 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1895 logio->port_id[1] = sp->fcport->d_id.b.area;
1896 logio->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001897 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001898}
1899
1900static void
1901qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1902{
1903 struct qla_hw_data *ha = sp->fcport->vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001904 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001905 uint16_t opts;
1906
Giridhar Malavalib9637522010-05-28 15:08:15 -07001907 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001908 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1909 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001910 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1911 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001912 if (HAS_EXTENDED_IDS(ha)) {
1913 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1914 mbx->mb10 = cpu_to_le16(opts);
1915 } else {
1916 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1917 }
1918 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1919 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1920 sp->fcport->d_id.b.al_pa);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001921 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001922}
1923
1924static void
1925qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1926{
1927 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1928 logio->control_flags =
1929 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1930 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1931 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1932 logio->port_id[1] = sp->fcport->d_id.b.area;
1933 logio->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001934 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001935}
1936
1937static void
1938qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1939{
1940 struct qla_hw_data *ha = sp->fcport->vha->hw;
1941
Giridhar Malavalib9637522010-05-28 15:08:15 -07001942 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001943 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1944 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1945 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1946 cpu_to_le16(sp->fcport->loop_id):
1947 cpu_to_le16(sp->fcport->loop_id << 8);
1948 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1949 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1950 sp->fcport->d_id.b.al_pa);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001951 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001952 /* Implicit: mbx->mbx10 = 0. */
1953}
1954
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001955static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001956qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1957{
1958 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1959 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1960 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001961 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001962}
1963
1964static void
1965qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1966{
1967 struct qla_hw_data *ha = sp->fcport->vha->hw;
1968
1969 mbx->entry_type = MBX_IOCB_TYPE;
1970 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1971 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1972 if (HAS_EXTENDED_IDS(ha)) {
1973 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1974 mbx->mb10 = cpu_to_le16(BIT_0);
1975 } else {
1976 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1977 }
1978 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1979 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1980 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1981 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001982 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001983}
1984
1985static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001986qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1987{
1988 uint32_t flags;
1989 unsigned int lun;
1990 struct fc_port *fcport = sp->fcport;
1991 scsi_qla_host_t *vha = fcport->vha;
1992 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001993 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001994 struct req_que *req = vha->req;
1995
1996 flags = iocb->u.tmf.flags;
1997 lun = iocb->u.tmf.lun;
1998
1999 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2000 tsk->entry_count = 1;
2001 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2002 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2003 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2004 tsk->control_flags = cpu_to_le32(flags);
2005 tsk->port_id[0] = fcport->d_id.b.al_pa;
2006 tsk->port_id[1] = fcport->d_id.b.area;
2007 tsk->port_id[2] = fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002008 tsk->vp_index = fcport->vha->vp_idx;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002009
2010 if (flags == TCF_LUN_RESET) {
2011 int_to_scsilun(lun, &tsk->lun);
2012 host_to_fcp_swap((uint8_t *)&tsk->lun,
2013 sizeof(tsk->lun));
2014 }
2015}
2016
2017static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002018qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2019{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002020 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002021
2022 els_iocb->entry_type = ELS_IOCB_TYPE;
2023 els_iocb->entry_count = 1;
2024 els_iocb->sys_define = 0;
2025 els_iocb->entry_status = 0;
2026 els_iocb->handle = sp->handle;
2027 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2028 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002029 els_iocb->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002030 els_iocb->sof_type = EST_SOFI3;
2031 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2032
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002033 els_iocb->opcode =
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002034 sp->type == SRB_ELS_CMD_RPT ?
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002035 bsg_job->request->rqst_data.r_els.els_code :
2036 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002037 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2038 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2039 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2040 els_iocb->control_flags = 0;
2041 els_iocb->rx_byte_count =
2042 cpu_to_le32(bsg_job->reply_payload.payload_len);
2043 els_iocb->tx_byte_count =
2044 cpu_to_le32(bsg_job->request_payload.payload_len);
2045
2046 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2047 (bsg_job->request_payload.sg_list)));
2048 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2049 (bsg_job->request_payload.sg_list)));
2050 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2051 (bsg_job->request_payload.sg_list));
2052
2053 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2054 (bsg_job->reply_payload.sg_list)));
2055 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2056 (bsg_job->reply_payload.sg_list)));
2057 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2058 (bsg_job->reply_payload.sg_list));
2059}
2060
2061static void
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002062qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2063{
2064 uint16_t avail_dsds;
2065 uint32_t *cur_dsd;
2066 struct scatterlist *sg;
2067 int index;
2068 uint16_t tot_dsds;
2069 scsi_qla_host_t *vha = sp->fcport->vha;
2070 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002071 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002072 int loop_iterartion = 0;
2073 int cont_iocb_prsnt = 0;
2074 int entry_count = 1;
2075
2076 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2077 ct_iocb->entry_type = CT_IOCB_TYPE;
2078 ct_iocb->entry_status = 0;
2079 ct_iocb->handle1 = sp->handle;
2080 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2081 ct_iocb->status = __constant_cpu_to_le16(0);
2082 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2083 ct_iocb->timeout = 0;
2084 ct_iocb->cmd_dsd_count =
2085 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2086 ct_iocb->total_dsd_count =
2087 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2088 ct_iocb->req_bytecount =
2089 cpu_to_le32(bsg_job->request_payload.payload_len);
2090 ct_iocb->rsp_bytecount =
2091 cpu_to_le32(bsg_job->reply_payload.payload_len);
2092
2093 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2094 (bsg_job->request_payload.sg_list)));
2095 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2096 (bsg_job->request_payload.sg_list)));
2097 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2098
2099 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2100 (bsg_job->reply_payload.sg_list)));
2101 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2102 (bsg_job->reply_payload.sg_list)));
2103 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2104
2105 avail_dsds = 1;
2106 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2107 index = 0;
2108 tot_dsds = bsg_job->reply_payload.sg_cnt;
2109
2110 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2111 dma_addr_t sle_dma;
2112 cont_a64_entry_t *cont_pkt;
2113
2114 /* Allocate additional continuation packets? */
2115 if (avail_dsds == 0) {
2116 /*
2117 * Five DSDs are available in the Cont.
2118 * Type 1 IOCB.
2119 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002120 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2121 vha->hw->req_q_map[0]);
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002122 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2123 avail_dsds = 5;
2124 cont_iocb_prsnt = 1;
2125 entry_count++;
2126 }
2127
2128 sle_dma = sg_dma_address(sg);
2129 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2130 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2131 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2132 loop_iterartion++;
2133 avail_dsds--;
2134 }
2135 ct_iocb->entry_count = entry_count;
2136}
2137
2138static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002139qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2140{
2141 uint16_t avail_dsds;
2142 uint32_t *cur_dsd;
2143 struct scatterlist *sg;
2144 int index;
2145 uint16_t tot_dsds;
2146 scsi_qla_host_t *vha = sp->fcport->vha;
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002147 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002148 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002149 int loop_iterartion = 0;
2150 int cont_iocb_prsnt = 0;
2151 int entry_count = 1;
2152
2153 ct_iocb->entry_type = CT_IOCB_TYPE;
2154 ct_iocb->entry_status = 0;
2155 ct_iocb->sys_define = 0;
2156 ct_iocb->handle = sp->handle;
2157
2158 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002159 ct_iocb->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002160 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2161
2162 ct_iocb->cmd_dsd_count =
2163 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2164 ct_iocb->timeout = 0;
2165 ct_iocb->rsp_dsd_count =
2166 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2167 ct_iocb->rsp_byte_count =
2168 cpu_to_le32(bsg_job->reply_payload.payload_len);
2169 ct_iocb->cmd_byte_count =
2170 cpu_to_le32(bsg_job->request_payload.payload_len);
2171 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2172 (bsg_job->request_payload.sg_list)));
2173 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2174 (bsg_job->request_payload.sg_list)));
2175 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2176 (bsg_job->request_payload.sg_list));
2177
2178 avail_dsds = 1;
2179 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2180 index = 0;
2181 tot_dsds = bsg_job->reply_payload.sg_cnt;
2182
2183 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2184 dma_addr_t sle_dma;
2185 cont_a64_entry_t *cont_pkt;
2186
2187 /* Allocate additional continuation packets? */
2188 if (avail_dsds == 0) {
2189 /*
2190 * Five DSDs are available in the Cont.
2191 * Type 1 IOCB.
2192 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002193 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2194 ha->req_q_map[0]);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002195 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2196 avail_dsds = 5;
2197 cont_iocb_prsnt = 1;
2198 entry_count++;
2199 }
2200
2201 sle_dma = sg_dma_address(sg);
2202 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2203 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2204 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2205 loop_iterartion++;
2206 avail_dsds--;
2207 }
2208 ct_iocb->entry_count = entry_count;
2209}
2210
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002211/*
2212 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2213 * @sp: command to send to the ISP
2214 *
2215 * Returns non-zero if a failure occurred, else zero.
2216 */
2217int
2218qla82xx_start_scsi(srb_t *sp)
2219{
2220 int ret, nseg;
2221 unsigned long flags;
2222 struct scsi_cmnd *cmd;
2223 uint32_t *clr_ptr;
2224 uint32_t index;
2225 uint32_t handle;
2226 uint16_t cnt;
2227 uint16_t req_cnt;
2228 uint16_t tot_dsds;
2229 struct device_reg_82xx __iomem *reg;
2230 uint32_t dbval;
2231 uint32_t *fcp_dl;
2232 uint8_t additional_cdb_len;
2233 struct ct6_dsd *ctx;
2234 struct scsi_qla_host *vha = sp->fcport->vha;
2235 struct qla_hw_data *ha = vha->hw;
2236 struct req_que *req = NULL;
2237 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002238 char tag[2];
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002239
2240 /* Setup device pointers. */
2241 ret = 0;
2242 reg = &ha->iobase->isp82;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002243 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002244 req = vha->req;
2245 rsp = ha->rsp_q_map[0];
2246
2247 /* So we know we haven't pci_map'ed anything yet */
2248 tot_dsds = 0;
2249
2250 dbval = 0x04 | (ha->portnum << 5);
2251
2252 /* Send marker if required */
2253 if (vha->marker_needed != 0) {
2254 if (qla2x00_marker(vha, req,
2255 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2256 ql_log(ql_log_warn, vha, 0x300c,
2257 "qla2x00_marker failed for cmd=%p.\n", cmd);
2258 return QLA_FUNCTION_FAILED;
2259 }
2260 vha->marker_needed = 0;
2261 }
2262
2263 /* Acquire ring specific lock */
2264 spin_lock_irqsave(&ha->hardware_lock, flags);
2265
2266 /* Check for room in outstanding command list. */
2267 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002268 for (index = 1; index < req->num_outstanding_cmds; index++) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002269 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002270 if (handle == req->num_outstanding_cmds)
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002271 handle = 1;
2272 if (!req->outstanding_cmds[handle])
2273 break;
2274 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05002275 if (index == req->num_outstanding_cmds)
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002276 goto queuing_error;
2277
2278 /* Map the sg table so we have an accurate count of sg entries needed */
2279 if (scsi_sg_count(cmd)) {
2280 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2281 scsi_sg_count(cmd), cmd->sc_data_direction);
2282 if (unlikely(!nseg))
2283 goto queuing_error;
2284 } else
2285 nseg = 0;
2286
2287 tot_dsds = nseg;
2288
2289 if (tot_dsds > ql2xshiftctondsd) {
2290 struct cmd_type_6 *cmd_pkt;
2291 uint16_t more_dsd_lists = 0;
2292 struct dsd_dma *dsd_ptr;
2293 uint16_t i;
2294
2295 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2296 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2297 ql_dbg(ql_dbg_io, vha, 0x300d,
2298 "Num of DSD list %d is than %d for cmd=%p.\n",
2299 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2300 cmd);
2301 goto queuing_error;
2302 }
2303
2304 if (more_dsd_lists <= ha->gbl_dsd_avail)
2305 goto sufficient_dsds;
2306 else
2307 more_dsd_lists -= ha->gbl_dsd_avail;
2308
2309 for (i = 0; i < more_dsd_lists; i++) {
2310 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2311 if (!dsd_ptr) {
2312 ql_log(ql_log_fatal, vha, 0x300e,
2313 "Failed to allocate memory for dsd_dma "
2314 "for cmd=%p.\n", cmd);
2315 goto queuing_error;
2316 }
2317
2318 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2319 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2320 if (!dsd_ptr->dsd_addr) {
2321 kfree(dsd_ptr);
2322 ql_log(ql_log_fatal, vha, 0x300f,
2323 "Failed to allocate memory for dsd_addr "
2324 "for cmd=%p.\n", cmd);
2325 goto queuing_error;
2326 }
2327 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2328 ha->gbl_dsd_avail++;
2329 }
2330
2331sufficient_dsds:
2332 req_cnt = 1;
2333
2334 if (req->cnt < (req_cnt + 2)) {
2335 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2336 &reg->req_q_out[0]);
2337 if (req->ring_index < cnt)
2338 req->cnt = cnt - req->ring_index;
2339 else
2340 req->cnt = req->length -
2341 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04002342 if (req->cnt < (req_cnt + 2))
2343 goto queuing_error;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002344 }
2345
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002346 ctx = sp->u.scmd.ctx =
2347 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2348 if (!ctx) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002349 ql_log(ql_log_fatal, vha, 0x3010,
2350 "Failed to allocate ctx for cmd=%p.\n", cmd);
2351 goto queuing_error;
2352 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002353
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002354 memset(ctx, 0, sizeof(struct ct6_dsd));
2355 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2356 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2357 if (!ctx->fcp_cmnd) {
2358 ql_log(ql_log_fatal, vha, 0x3011,
2359 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
Dan Carpenter841f97b2012-05-17 10:13:40 +03002360 goto queuing_error;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002361 }
2362
2363 /* Initialize the DSD list and dma handle */
2364 INIT_LIST_HEAD(&ctx->dsd_list);
2365 ctx->dsd_use_cnt = 0;
2366
2367 if (cmd->cmd_len > 16) {
2368 additional_cdb_len = cmd->cmd_len - 16;
2369 if ((cmd->cmd_len % 4) != 0) {
2370 /* SCSI command bigger than 16 bytes must be
2371 * multiple of 4
2372 */
2373 ql_log(ql_log_warn, vha, 0x3012,
2374 "scsi cmd len %d not multiple of 4 "
2375 "for cmd=%p.\n", cmd->cmd_len, cmd);
2376 goto queuing_error_fcp_cmnd;
2377 }
2378 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2379 } else {
2380 additional_cdb_len = 0;
2381 ctx->fcp_cmnd_len = 12 + 16 + 4;
2382 }
2383
2384 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2385 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2386
2387 /* Zero out remaining portion of packet. */
2388 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2389 clr_ptr = (uint32_t *)cmd_pkt + 2;
2390 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2391 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2392
2393 /* Set NPORT-ID and LUN number*/
2394 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2395 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2396 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2397 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002398 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002399
2400 /* Build IOCB segments */
2401 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2402 goto queuing_error_fcp_cmnd;
2403
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002404 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002405 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2406
2407 /* build FCP_CMND IU */
2408 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002409 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002410 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2411
2412 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2413 ctx->fcp_cmnd->additional_cdb_len |= 1;
2414 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2415 ctx->fcp_cmnd->additional_cdb_len |= 2;
2416
2417 /*
2418 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2419 */
2420 if (scsi_populate_tag_msg(cmd, tag)) {
2421 switch (tag[0]) {
2422 case HEAD_OF_QUEUE_TAG:
2423 ctx->fcp_cmnd->task_attribute =
2424 TSK_HEAD_OF_QUEUE;
2425 break;
2426 case ORDERED_QUEUE_TAG:
2427 ctx->fcp_cmnd->task_attribute =
2428 TSK_ORDERED;
2429 break;
2430 }
2431 }
2432
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002433 /* Populate the FCP_PRIO. */
2434 if (ha->flags.fcp_prio_enabled)
2435 ctx->fcp_cmnd->task_attribute |=
2436 sp->fcport->fcp_prio << 3;
2437
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002438 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2439
2440 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2441 additional_cdb_len);
2442 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2443
2444 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2445 cmd_pkt->fcp_cmnd_dseg_address[0] =
2446 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2447 cmd_pkt->fcp_cmnd_dseg_address[1] =
2448 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2449
2450 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2451 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2452 /* Set total data segment count. */
2453 cmd_pkt->entry_count = (uint8_t)req_cnt;
2454 /* Specify response queue number where
2455 * completion should happen
2456 */
2457 cmd_pkt->entry_status = (uint8_t) rsp->id;
2458 } else {
2459 struct cmd_type_7 *cmd_pkt;
2460 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2461 if (req->cnt < (req_cnt + 2)) {
2462 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2463 &reg->req_q_out[0]);
2464 if (req->ring_index < cnt)
2465 req->cnt = cnt - req->ring_index;
2466 else
2467 req->cnt = req->length -
2468 (req->ring_index - cnt);
2469 }
2470 if (req->cnt < (req_cnt + 2))
2471 goto queuing_error;
2472
2473 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2474 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2475
2476 /* Zero out remaining portion of packet. */
2477 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2478 clr_ptr = (uint32_t *)cmd_pkt + 2;
2479 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2480 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2481
2482 /* Set NPORT-ID and LUN number*/
2483 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2484 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2485 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2486 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002487 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002488
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002489 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002490 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002491 sizeof(cmd_pkt->lun));
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002492
2493 /*
2494 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2495 */
2496 if (scsi_populate_tag_msg(cmd, tag)) {
2497 switch (tag[0]) {
2498 case HEAD_OF_QUEUE_TAG:
2499 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
2500 break;
2501 case ORDERED_QUEUE_TAG:
2502 cmd_pkt->task = TSK_ORDERED;
2503 break;
2504 }
2505 }
2506
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002507 /* Populate the FCP_PRIO. */
2508 if (ha->flags.fcp_prio_enabled)
2509 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2510
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002511 /* Load SCSI command packet. */
2512 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2513 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2514
2515 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2516
2517 /* Build IOCB segments */
2518 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2519
2520 /* Set total data segment count. */
2521 cmd_pkt->entry_count = (uint8_t)req_cnt;
2522 /* Specify response queue number where
2523 * completion should happen.
2524 */
2525 cmd_pkt->entry_status = (uint8_t) rsp->id;
2526
2527 }
2528 /* Build command packet. */
2529 req->current_outstanding_cmd = handle;
2530 req->outstanding_cmds[handle] = sp;
2531 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002532 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002533 req->cnt -= req_cnt;
2534 wmb();
2535
2536 /* Adjust ring index. */
2537 req->ring_index++;
2538 if (req->ring_index == req->length) {
2539 req->ring_index = 0;
2540 req->ring_ptr = req->ring;
2541 } else
2542 req->ring_ptr++;
2543
2544 sp->flags |= SRB_DMA_VALID;
2545
2546 /* Set chip new ring index. */
2547 /* write, read and verify logic */
2548 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2549 if (ql2xdbwr)
2550 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2551 else {
2552 WRT_REG_DWORD(
2553 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2554 dbval);
2555 wmb();
Saurav Kashyapfa492632012-11-21 02:40:29 -05002556 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002557 WRT_REG_DWORD(
2558 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2559 dbval);
2560 wmb();
2561 }
2562 }
2563
2564 /* Manage unprocessed RIO/ZIO commands in response queue. */
2565 if (vha->flags.process_response_queue &&
2566 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2567 qla24xx_process_response_queue(vha, rsp);
2568
2569 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2570 return QLA_SUCCESS;
2571
2572queuing_error_fcp_cmnd:
2573 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2574queuing_error:
2575 if (tot_dsds)
2576 scsi_dma_unmap(cmd);
2577
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002578 if (sp->u.scmd.ctx) {
2579 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2580 sp->u.scmd.ctx = NULL;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002581 }
2582 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2583
2584 return QLA_FUNCTION_FAILED;
2585}
2586
Andrew Vasquezac280b62009-08-20 11:06:05 -07002587int
2588qla2x00_start_sp(srb_t *sp)
2589{
2590 int rval;
2591 struct qla_hw_data *ha = sp->fcport->vha->hw;
2592 void *pkt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002593 unsigned long flags;
2594
2595 rval = QLA_FUNCTION_FAILED;
2596 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05002597 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002598 if (!pkt) {
2599 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2600 "qla2x00_alloc_iocbs failed.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07002601 goto done;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002602 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07002603
2604 rval = QLA_SUCCESS;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002605 switch (sp->type) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07002606 case SRB_LOGIN_CMD:
2607 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002608 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002609 qla2x00_login_iocb(sp, pkt);
2610 break;
2611 case SRB_LOGOUT_CMD:
2612 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002613 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002614 qla2x00_logout_iocb(sp, pkt);
2615 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002616 case SRB_ELS_CMD_RPT:
2617 case SRB_ELS_CMD_HST:
2618 qla24xx_els_iocb(sp, pkt);
2619 break;
2620 case SRB_CT_CMD:
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002621 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez57807902011-11-18 09:03:20 -08002622 qla24xx_ct_iocb(sp, pkt) :
2623 qla2x00_ct_iocb(sp, pkt);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002624 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002625 case SRB_ADISC_CMD:
2626 IS_FWI2_CAPABLE(ha) ?
2627 qla24xx_adisc_iocb(sp, pkt) :
2628 qla2x00_adisc_iocb(sp, pkt);
2629 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002630 case SRB_TM_CMD:
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04002631 IS_QLAFX00(ha) ?
2632 qlafx00_tm_iocb(sp, pkt) :
2633 qla24xx_tm_iocb(sp, pkt);
2634 break;
2635 case SRB_FXIOCB_DCMD:
2636 case SRB_FXIOCB_BCMD:
2637 qlafx00_fxdisc_iocb(sp, pkt);
2638 break;
2639 case SRB_ABT_CMD:
2640 qlafx00_abort_iocb(sp, pkt);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002641 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002642 default:
2643 break;
2644 }
2645
2646 wmb();
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002647 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
Andrew Vasquezac280b62009-08-20 11:06:05 -07002648done:
2649 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2650 return rval;
2651}
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002652
2653static void
2654qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
2655 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
2656{
2657 uint16_t avail_dsds;
2658 uint32_t *cur_dsd;
2659 uint32_t req_data_len = 0;
2660 uint32_t rsp_data_len = 0;
2661 struct scatterlist *sg;
2662 int index;
2663 int entry_count = 1;
2664 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2665
2666 /*Update entry type to indicate bidir command */
2667 *((uint32_t *)(&cmd_pkt->entry_type)) =
2668 __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
2669
2670 /* Set the transfer direction, in this set both flags
2671 * Also set the BD_WRAP_BACK flag, firmware will take care
2672 * assigning DID=SID for outgoing pkts.
2673 */
2674 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2675 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2676 cmd_pkt->control_flags =
2677 __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
2678 BD_WRAP_BACK);
2679
2680 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2681 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
2682 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
2683 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
2684
2685 vha->bidi_stats.transfer_bytes += req_data_len;
2686 vha->bidi_stats.io_count++;
2687
2688 /* Only one dsd is available for bidirectional IOCB, remaining dsds
2689 * are bundled in continuation iocb
2690 */
2691 avail_dsds = 1;
2692 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
2693
2694 index = 0;
2695
2696 for_each_sg(bsg_job->request_payload.sg_list, sg,
2697 bsg_job->request_payload.sg_cnt, index) {
2698 dma_addr_t sle_dma;
2699 cont_a64_entry_t *cont_pkt;
2700
2701 /* Allocate additional continuation packets */
2702 if (avail_dsds == 0) {
2703 /* Continuation type 1 IOCB can accomodate
2704 * 5 DSDS
2705 */
2706 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2707 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2708 avail_dsds = 5;
2709 entry_count++;
2710 }
2711 sle_dma = sg_dma_address(sg);
2712 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2713 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2714 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2715 avail_dsds--;
2716 }
2717 /* For read request DSD will always goes to continuation IOCB
2718 * and follow the write DSD. If there is room on the current IOCB
2719 * then it is added to that IOCB else new continuation IOCB is
2720 * allocated.
2721 */
2722 for_each_sg(bsg_job->reply_payload.sg_list, sg,
2723 bsg_job->reply_payload.sg_cnt, index) {
2724 dma_addr_t sle_dma;
2725 cont_a64_entry_t *cont_pkt;
2726
2727 /* Allocate additional continuation packets */
2728 if (avail_dsds == 0) {
2729 /* Continuation type 1 IOCB can accomodate
2730 * 5 DSDS
2731 */
2732 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2733 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2734 avail_dsds = 5;
2735 entry_count++;
2736 }
2737 sle_dma = sg_dma_address(sg);
2738 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2739 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2740 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2741 avail_dsds--;
2742 }
2743 /* This value should be same as number of IOCB required for this cmd */
2744 cmd_pkt->entry_count = entry_count;
2745}
2746
2747int
2748qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
2749{
2750
2751 struct qla_hw_data *ha = vha->hw;
2752 unsigned long flags;
2753 uint32_t handle;
2754 uint32_t index;
2755 uint16_t req_cnt;
2756 uint16_t cnt;
2757 uint32_t *clr_ptr;
2758 struct cmd_bidir *cmd_pkt = NULL;
2759 struct rsp_que *rsp;
2760 struct req_que *req;
2761 int rval = EXT_STATUS_OK;
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002762
2763 rval = QLA_SUCCESS;
2764
2765 rsp = ha->rsp_q_map[0];
2766 req = vha->req;
2767
2768 /* Send marker if required */
2769 if (vha->marker_needed != 0) {
2770 if (qla2x00_marker(vha, req,
2771 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
2772 return EXT_STATUS_MAILBOX;
2773 vha->marker_needed = 0;
2774 }
2775
2776 /* Acquire ring specific lock */
2777 spin_lock_irqsave(&ha->hardware_lock, flags);
2778
2779 /* Check for room in outstanding command list. */
2780 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002781 for (index = 1; index < req->num_outstanding_cmds; index++) {
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002782 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002783 if (handle == req->num_outstanding_cmds)
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002784 handle = 1;
2785 if (!req->outstanding_cmds[handle])
2786 break;
2787 }
2788
Chad Dupuis8d93f552013-01-30 03:34:37 -05002789 if (index == req->num_outstanding_cmds) {
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002790 rval = EXT_STATUS_BUSY;
2791 goto queuing_error;
2792 }
2793
2794 /* Calculate number of IOCB required */
2795 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2796
2797 /* Check for room on request queue. */
2798 if (req->cnt < req_cnt + 2) {
Andrew Vasquez7e98df22012-11-21 02:40:33 -05002799 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002800
2801 if (req->ring_index < cnt)
2802 req->cnt = cnt - req->ring_index;
2803 else
2804 req->cnt = req->length -
2805 (req->ring_index - cnt);
2806 }
2807 if (req->cnt < req_cnt + 2) {
2808 rval = EXT_STATUS_BUSY;
2809 goto queuing_error;
2810 }
2811
2812 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
2813 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2814
2815 /* Zero out remaining portion of packet. */
2816 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2817 clr_ptr = (uint32_t *)cmd_pkt + 2;
2818 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2819
2820 /* Set NPORT-ID (of vha)*/
2821 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
2822 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
2823 cmd_pkt->port_id[1] = vha->d_id.b.area;
2824 cmd_pkt->port_id[2] = vha->d_id.b.domain;
2825
2826 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
2827 cmd_pkt->entry_status = (uint8_t) rsp->id;
2828 /* Build command packet. */
2829 req->current_outstanding_cmd = handle;
2830 req->outstanding_cmds[handle] = sp;
2831 sp->handle = handle;
2832 req->cnt -= req_cnt;
2833
2834 /* Send the command to the firmware */
2835 wmb();
2836 qla2x00_start_iocbs(vha, req);
2837queuing_error:
2838 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2839 return rval;
2840}