blob: 0aad5e412aa7f41f243aa0cba7dacd62e3d65ebb [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);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -040035 vha->qla_stats.output_requests++;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080036 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 cflags = CF_READ;
Saurav Kashyap2be21fa2012-05-15 14:34:16 -040038 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -040039 vha->qla_stats.input_requests++;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 return (cflags);
42}
43
44/**
45 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46 * Continuation Type 0 IOCBs to allocate.
47 *
48 * @dsds: number of data segment decriptors needed
49 *
50 * Returns the number of IOCB entries needed to store @dsds.
51 */
52uint16_t
53qla2x00_calc_iocbs_32(uint16_t dsds)
54{
55 uint16_t iocbs;
56
57 iocbs = 1;
58 if (dsds > 3) {
59 iocbs += (dsds - 3) / 7;
60 if ((dsds - 3) % 7)
61 iocbs++;
62 }
63 return (iocbs);
64}
65
66/**
67 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68 * Continuation Type 1 IOCBs to allocate.
69 *
70 * @dsds: number of data segment decriptors needed
71 *
72 * Returns the number of IOCB entries needed to store @dsds.
73 */
74uint16_t
75qla2x00_calc_iocbs_64(uint16_t dsds)
76{
77 uint16_t iocbs;
78
79 iocbs = 1;
80 if (dsds > 2) {
81 iocbs += (dsds - 2) / 5;
82 if ((dsds - 2) % 5)
83 iocbs++;
84 }
85 return (iocbs);
86}
87
88/**
89 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90 * @ha: HA context
91 *
92 * Returns a pointer to the Continuation Type 0 IOCB packet.
93 */
94static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070095qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070098 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800100 req->ring_index++;
101 if (req->ring_index == req->length) {
102 req->ring_index = 0;
103 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800105 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800108 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 /* Load packet defaults. */
111 *((uint32_t *)(&cont_pkt->entry_type)) =
112 __constant_cpu_to_le32(CONTINUE_TYPE);
113
114 return (cont_pkt);
115}
116
117/**
118 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
119 * @ha: HA context
120 *
121 * Returns a pointer to the continuation type 1 IOCB packet.
122 */
123static inline cont_a64_entry_t *
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800124qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 cont_a64_entry_t *cont_pkt;
127
128 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800134 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Load packet defaults. */
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400140 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
143
144 return (cont_pkt);
145}
146
Arun Easibad75002010-05-04 15:01:30 -0700147static inline int
148qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
149{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800150 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
151 uint8_t guard = scsi_host_get_guard(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700152
Arun Easibad75002010-05-04 15:01:30 -0700153 /* We always use DIFF Bundling for best performance */
154 *fw_prot_opts = 0;
155
156 /* Translate SCSI opcode to a protection opcode */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800157 switch (scsi_get_prot_op(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700158 case SCSI_PROT_READ_STRIP:
159 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
160 break;
161 case SCSI_PROT_WRITE_INSERT:
162 *fw_prot_opts |= PO_MODE_DIF_INSERT;
163 break;
164 case SCSI_PROT_READ_INSERT:
165 *fw_prot_opts |= PO_MODE_DIF_INSERT;
166 break;
167 case SCSI_PROT_WRITE_STRIP:
168 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
169 break;
170 case SCSI_PROT_READ_PASS:
Arun Easibad75002010-05-04 15:01:30 -0700171 case SCSI_PROT_WRITE_PASS:
Arun Easi9e522cd2012-08-22 14:21:31 -0400172 if (guard & SHOST_DIX_GUARD_IP)
173 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
174 else
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
Arun Easibad75002010-05-04 15:01:30 -0700176 break;
177 default: /* Normal Request */
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 break;
180 }
181
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800182 return scsi_prot_sg_count(cmd);
Arun Easibad75002010-05-04 15:01:30 -0700183}
184
185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
187 * capable IOCB types.
188 *
189 * @sp: SRB command to process
190 * @cmd_pkt: Command type 2 IOCB
191 * @tot_dsds: Total number of segments to transfer
192 */
193void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
194 uint16_t tot_dsds)
195{
196 uint16_t avail_dsds;
197 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800198 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900200 struct scatterlist *sg;
201 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800203 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205 /* Update entry type to indicate Command Type 2 IOCB */
206 *((uint32_t *)(&cmd_pkt->entry_type)) =
207 __constant_cpu_to_le32(COMMAND_TYPE);
208
209 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900210 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
212 return;
213 }
214
Andrew Vasquez444786d2009-01-05 11:18:10 -0800215 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700216 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 /* Three DSDs are available in the Command Type 2 IOCB */
219 avail_dsds = 3;
220 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
221
222 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900223 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
224 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900226 /* Allocate additional continuation packets? */
227 if (avail_dsds == 0) {
228 /*
229 * Seven DSDs are available in the Continuation
230 * Type 0 IOCB.
231 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700232 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900233 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
234 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900236
237 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
238 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
239 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241}
242
243/**
244 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
245 * capable IOCB types.
246 *
247 * @sp: SRB command to process
248 * @cmd_pkt: Command type 3 IOCB
249 * @tot_dsds: Total number of segments to transfer
250 */
251void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
252 uint16_t tot_dsds)
253{
254 uint16_t avail_dsds;
255 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800256 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900258 struct scatterlist *sg;
259 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800261 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
263 /* Update entry type to indicate Command Type 3 IOCB */
264 *((uint32_t *)(&cmd_pkt->entry_type)) =
265 __constant_cpu_to_le32(COMMAND_A64_TYPE);
266
267 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900268 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
270 return;
271 }
272
Andrew Vasquez444786d2009-01-05 11:18:10 -0800273 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700274 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* Two DSDs are available in the Command Type 3 IOCB */
277 avail_dsds = 2;
278 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
279
280 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900281 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
282 dma_addr_t sle_dma;
283 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900285 /* Allocate additional continuation packets? */
286 if (avail_dsds == 0) {
287 /*
288 * Five DSDs are available in the Continuation
289 * Type 1 IOCB.
290 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800291 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900292 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
293 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900295
296 sle_dma = sg_dma_address(sg);
297 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
298 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
299 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
300 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 }
302}
303
304/**
305 * qla2x00_start_scsi() - Send a SCSI command to the ISP
306 * @sp: command to send to the ISP
307 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700308 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310int
311qla2x00_start_scsi(srb_t *sp)
312{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900313 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800315 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct scsi_cmnd *cmd;
317 uint32_t *clr_ptr;
318 uint32_t index;
319 uint32_t handle;
320 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 uint16_t cnt;
322 uint16_t req_cnt;
323 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700324 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800325 struct qla_hw_data *ha;
326 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800327 struct rsp_que *rsp;
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800328 char tag[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 /* Setup device pointers. */
331 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800332 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800333 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700334 reg = &ha->iobase->isp;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800335 cmd = GET_CMD_SP(sp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800336 req = ha->req_q_map[0];
337 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500338 /* So we know we haven't pci_map'ed anything yet */
339 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800342 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700343 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
344 QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return (QLA_FUNCTION_FAILED);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700346 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800347 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700351 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800354 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -0500355 for (index = 1; index < req->num_outstanding_cmds; index++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -0500357 if (handle == req->num_outstanding_cmds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800359 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 break;
361 }
Chad Dupuis8d93f552013-01-30 03:34:37 -0500362 if (index == req->num_outstanding_cmds)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 goto queuing_error;
364
83021922005-04-17 15:10:41 -0500365 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700366 if (scsi_sg_count(cmd)) {
367 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
368 scsi_sg_count(cmd), cmd->sc_data_direction);
369 if (unlikely(!nseg))
370 goto queuing_error;
371 } else
372 nseg = 0;
373
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900374 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700377 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800378 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800380 if (req->ring_index < cnt)
381 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800383 req->cnt = req->length -
384 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -0400385 /* If still no head room then bail out */
386 if (req->cnt < (req_cnt + 2))
387 goto queuing_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800391 req->current_outstanding_cmd = handle;
392 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700393 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800394 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800395 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 cmd_pkt->handle = handle;
399 /* Zero out remaining portion of packet. */
400 clr_ptr = (uint32_t *)cmd_pkt + 2;
401 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
402 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
403
bdf79622005-04-17 15:06:53 -0500404 /* Set target ID and LUN number*/
405 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800406 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* Update tagged queuing modifier */
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800409 if (scsi_populate_tag_msg(cmd, tag)) {
410 switch (tag[0]) {
411 case HEAD_OF_QUEUE_TAG:
412 cmd_pkt->control_flags =
413 __constant_cpu_to_le16(CF_HEAD_TAG);
414 break;
415 case ORDERED_QUEUE_TAG:
416 cmd_pkt->control_flags =
417 __constant_cpu_to_le16(CF_ORDERED_TAG);
418 break;
419 default:
420 cmd_pkt->control_flags =
421 __constant_cpu_to_le16(CF_SIMPLE_TAG);
422 break;
423 }
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -0400424 } else {
425 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Load SCSI command packet. */
429 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900430 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700433 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Set total data segment count. */
436 cmd_pkt->entry_count = (uint8_t)req_cnt;
437 wmb();
438
439 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800440 req->ring_index++;
441 if (req->ring_index == req->length) {
442 req->ring_index = 0;
443 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800445 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800450 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
452
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700453 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800454 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800455 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
456 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700457
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return (QLA_SUCCESS);
460
461queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900462 if (tot_dsds)
463 scsi_dma_unmap(cmd);
464
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700465 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return (QLA_FUNCTION_FAILED);
468}
469
470/**
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800471 * qla2x00_start_iocbs() - Execute the IOCB command
472 */
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400473void
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800474qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
475{
476 struct qla_hw_data *ha = vha->hw;
477 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800478
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -0400479 if (IS_P3P_TYPE(ha)) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800480 qla82xx_start_iocbs(vha);
481 } else {
482 /* Adjust ring index. */
483 req->ring_index++;
484 if (req->ring_index == req->length) {
485 req->ring_index = 0;
486 req->ring_ptr = req->ring;
487 } else
488 req->ring_ptr++;
489
490 /* Set chip new ring index. */
Chad Dupuisf73cb692014-02-26 04:15:06 -0500491 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800492 WRT_REG_DWORD(req->req_q_in, req->ring_index);
Arun Easi98878a12012-02-09 11:15:59 -0800493 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400494 } else if (IS_QLAFX00(ha)) {
495 WRT_REG_DWORD(&reg->ispfx00.req_q_in, req->ring_index);
496 RD_REG_DWORD_RELAXED(&reg->ispfx00.req_q_in);
497 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800498 } else if (IS_FWI2_CAPABLE(ha)) {
499 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
500 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
501 } else {
502 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
503 req->ring_index);
504 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
505 }
506 }
507}
508
509/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 * qla2x00_marker() - Send a marker IOCB to the firmware.
511 * @ha: HA context
512 * @loop_id: loop ID
513 * @lun: LUN
514 * @type: marker modifier
515 *
516 * Can be called from both normal and interrupt context.
517 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700518 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 */
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500520static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800521__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
522 struct rsp_que *rsp, uint16_t loop_id,
523 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700525 mrk_entry_t *mrk;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400526 struct mrk_entry_24xx *mrk24 = NULL;
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -0400527
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800528 struct qla_hw_data *ha = vha->hw;
529 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
Giridhar Malavali99b82122011-11-18 09:03:17 -0800531 req = ha->req_q_map[0];
Saurav Kashyapfa492632012-11-21 02:40:29 -0500532 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700533 if (mrk == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700534 ql_log(ql_log_warn, base_vha, 0x3026,
535 "Failed to allocate Marker IOCB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537 return (QLA_FUNCTION_FAILED);
538 }
539
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700540 mrk->entry_type = MARKER_TYPE;
541 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (type != MK_SYNC_ALL) {
Armen Baloyanbfd73342014-02-26 04:15:07 -0500543 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700544 mrk24 = (struct mrk_entry_24xx *) mrk;
545 mrk24->nport_handle = cpu_to_le16(loop_id);
546 mrk24->lun[1] = LSB(lun);
547 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700548 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800549 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700550 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700551 } else {
552 SET_TARGET_ID(ha, mrk->target, loop_id);
553 mrk->lun = cpu_to_le16(lun);
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 wmb();
557
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800558 qla2x00_start_iocbs(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return (QLA_SUCCESS);
561}
562
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700563int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800564qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
565 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
566 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 int ret;
569 unsigned long flags = 0;
570
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800571 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
572 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
573 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 return (ret);
576}
577
Nicholas Bellinger2d70c102012-05-15 14:34:28 -0400578/*
579 * qla2x00_issue_marker
580 *
581 * Issue marker
582 * Caller CAN have hardware lock held as specified by ha_locked parameter.
583 * Might release it, then reaquire.
584 */
585int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
586{
587 if (ha_locked) {
588 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
589 MK_SYNC_ALL) != QLA_SUCCESS)
590 return QLA_FUNCTION_FAILED;
591 } else {
592 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
593 MK_SYNC_ALL) != QLA_SUCCESS)
594 return QLA_FUNCTION_FAILED;
595 }
596 vha->marker_needed = 0;
597
598 return QLA_SUCCESS;
599}
600
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800601static inline int
602qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
603 uint16_t tot_dsds)
604{
605 uint32_t *cur_dsd = NULL;
606 scsi_qla_host_t *vha;
607 struct qla_hw_data *ha;
608 struct scsi_cmnd *cmd;
609 struct scatterlist *cur_seg;
610 uint32_t *dsd_seg;
611 void *next_dsd;
612 uint8_t avail_dsds;
613 uint8_t first_iocb = 1;
614 uint32_t dsd_list_len;
615 struct dsd_dma *dsd_ptr;
616 struct ct6_dsd *ctx;
617
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800618 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800619
620 /* Update entry type to indicate Command Type 3 IOCB */
621 *((uint32_t *)(&cmd_pkt->entry_type)) =
622 __constant_cpu_to_le32(COMMAND_TYPE_6);
623
624 /* No data transfer */
625 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
626 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
627 return 0;
628 }
629
630 vha = sp->fcport->vha;
631 ha = vha->hw;
632
633 /* Set transfer direction */
634 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
635 cmd_pkt->control_flags =
636 __constant_cpu_to_le16(CF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400637 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -0400638 vha->qla_stats.output_requests++;
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800639 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
640 cmd_pkt->control_flags =
641 __constant_cpu_to_le16(CF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400642 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -0400643 vha->qla_stats.input_requests++;
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800644 }
645
646 cur_seg = scsi_sglist(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800647 ctx = GET_CMD_CTX_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800648
649 while (tot_dsds) {
650 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
651 QLA_DSDS_PER_IOCB : tot_dsds;
652 tot_dsds -= avail_dsds;
653 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
654
655 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
656 struct dsd_dma, list);
657 next_dsd = dsd_ptr->dsd_addr;
658 list_del(&dsd_ptr->list);
659 ha->gbl_dsd_avail--;
660 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
661 ctx->dsd_use_cnt++;
662 ha->gbl_dsd_inuse++;
663
664 if (first_iocb) {
665 first_iocb = 0;
666 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
667 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
668 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
669 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
670 } else {
671 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
672 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
673 *cur_dsd++ = cpu_to_le32(dsd_list_len);
674 }
675 cur_dsd = (uint32_t *)next_dsd;
676 while (avail_dsds) {
677 dma_addr_t sle_dma;
678
679 sle_dma = sg_dma_address(cur_seg);
680 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
681 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
682 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
683 cur_seg = sg_next(cur_seg);
684 avail_dsds--;
685 }
686 }
687
688 /* Null termination */
689 *cur_dsd++ = 0;
690 *cur_dsd++ = 0;
691 *cur_dsd++ = 0;
692 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
693 return 0;
694}
695
696/*
697 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
698 * for Command Type 6.
699 *
700 * @dsds: number of data segment decriptors needed
701 *
702 * Returns the number of dsd list needed to store @dsds.
703 */
704inline uint16_t
705qla24xx_calc_dsd_lists(uint16_t dsds)
706{
707 uint16_t dsd_lists = 0;
708
709 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
710 if (dsds % QLA_DSDS_PER_IOCB)
711 dsd_lists++;
712 return dsd_lists;
713}
714
715
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700716/**
717 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
718 * IOCB types.
719 *
720 * @sp: SRB command to process
721 * @cmd_pkt: Command type 3 IOCB
722 * @tot_dsds: Total number of segments to transfer
723 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700724inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700725qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
726 uint16_t tot_dsds)
727{
728 uint16_t avail_dsds;
729 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800730 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700731 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900732 struct scatterlist *sg;
733 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800734 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700735
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800736 cmd = GET_CMD_SP(sp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700737
738 /* Update entry type to indicate Command Type 3 IOCB */
739 *((uint32_t *)(&cmd_pkt->entry_type)) =
740 __constant_cpu_to_le32(COMMAND_TYPE_7);
741
742 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900743 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700744 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
745 return;
746 }
747
Andrew Vasquez444786d2009-01-05 11:18:10 -0800748 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700749 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700750
751 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700752 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700753 cmd_pkt->task_mgmt_flags =
754 __constant_cpu_to_le16(TMF_WRITE_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400755 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -0400756 vha->qla_stats.output_requests++;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700757 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700758 cmd_pkt->task_mgmt_flags =
759 __constant_cpu_to_le16(TMF_READ_DATA);
Saurav Kashyap2be21fa2012-05-15 14:34:16 -0400760 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
Joe Carnucciofabbb8d2013-08-27 01:37:40 -0400761 vha->qla_stats.input_requests++;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700762 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700763
764 /* One DSD is available in the Command Type 3 IOCB */
765 avail_dsds = 1;
766 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
767
768 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700769
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900770 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
771 dma_addr_t sle_dma;
772 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700773
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900774 /* Allocate additional continuation packets? */
775 if (avail_dsds == 0) {
776 /*
777 * Five DSDs are available in the Continuation
778 * Type 1 IOCB.
779 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800780 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900781 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
782 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700783 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900784
785 sle_dma = sg_dma_address(sg);
786 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
787 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
788 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
789 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700790 }
791}
792
Arun Easibad75002010-05-04 15:01:30 -0700793struct fw_dif_context {
794 uint32_t ref_tag;
795 uint16_t app_tag;
796 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
797 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
798};
799
800/*
801 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
802 *
803 */
804static inline void
Arun Easie02587d2011-08-16 11:29:23 -0700805qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
Arun Easibad75002010-05-04 15:01:30 -0700806 unsigned int protcnt)
807{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800808 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -0700809
810 switch (scsi_get_prot_type(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700811 case SCSI_PROT_DIF_TYPE0:
Arun Easi8cb20492011-08-16 11:29:22 -0700812 /*
813 * No check for ql2xenablehba_err_chk, as it would be an
814 * I/O error if hba tag generation is not done.
815 */
816 pkt->ref_tag = cpu_to_le32((uint32_t)
817 (0xffffffff & scsi_get_lba(cmd)));
Arun Easie02587d2011-08-16 11:29:23 -0700818
819 if (!qla2x00_hba_err_chk_enabled(sp))
820 break;
821
Arun Easi8cb20492011-08-16 11:29:22 -0700822 pkt->ref_tag_mask[0] = 0xff;
823 pkt->ref_tag_mask[1] = 0xff;
824 pkt->ref_tag_mask[2] = 0xff;
825 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700826 break;
827
828 /*
829 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
830 * match LBA in CDB + N
831 */
832 case SCSI_PROT_DIF_TYPE2:
Arun Easie02587d2011-08-16 11:29:23 -0700833 pkt->app_tag = __constant_cpu_to_le16(0);
834 pkt->app_tag_mask[0] = 0x0;
835 pkt->app_tag_mask[1] = 0x0;
Arun Easi0c470872010-07-23 15:28:38 +0500836
837 pkt->ref_tag = cpu_to_le32((uint32_t)
838 (0xffffffff & scsi_get_lba(cmd)));
839
Arun Easie02587d2011-08-16 11:29:23 -0700840 if (!qla2x00_hba_err_chk_enabled(sp))
841 break;
842
Arun Easi0c470872010-07-23 15:28:38 +0500843 /* enable ALL bytes of the ref tag */
844 pkt->ref_tag_mask[0] = 0xff;
845 pkt->ref_tag_mask[1] = 0xff;
846 pkt->ref_tag_mask[2] = 0xff;
847 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700848 break;
849
850 /* For Type 3 protection: 16 bit GUARD only */
851 case SCSI_PROT_DIF_TYPE3:
852 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
853 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
854 0x00;
855 break;
856
857 /*
858 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
859 * 16 bit app tag.
860 */
861 case SCSI_PROT_DIF_TYPE1:
Arun Easie02587d2011-08-16 11:29:23 -0700862 pkt->ref_tag = cpu_to_le32((uint32_t)
863 (0xffffffff & scsi_get_lba(cmd)));
864 pkt->app_tag = __constant_cpu_to_le16(0);
865 pkt->app_tag_mask[0] = 0x0;
866 pkt->app_tag_mask[1] = 0x0;
867
868 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -0700869 break;
870
Arun Easibad75002010-05-04 15:01:30 -0700871 /* enable ALL bytes of the ref tag */
872 pkt->ref_tag_mask[0] = 0xff;
873 pkt->ref_tag_mask[1] = 0xff;
874 pkt->ref_tag_mask[2] = 0xff;
875 pkt->ref_tag_mask[3] = 0xff;
876 break;
877 }
Arun Easibad75002010-05-04 15:01:30 -0700878}
879
Arun Easi8cb20492011-08-16 11:29:22 -0700880struct qla2_sgx {
881 dma_addr_t dma_addr; /* OUT */
882 uint32_t dma_len; /* OUT */
Arun Easibad75002010-05-04 15:01:30 -0700883
Arun Easi8cb20492011-08-16 11:29:22 -0700884 uint32_t tot_bytes; /* IN */
885 struct scatterlist *cur_sg; /* IN */
886
887 /* for book keeping, bzero on initial invocation */
888 uint32_t bytes_consumed;
889 uint32_t num_bytes;
890 uint32_t tot_partial;
891
892 /* for debugging */
893 uint32_t num_sg;
894 srb_t *sp;
895};
896
897static int
898qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
899 uint32_t *partial)
900{
901 struct scatterlist *sg;
902 uint32_t cumulative_partial, sg_len;
903 dma_addr_t sg_dma_addr;
904
905 if (sgx->num_bytes == sgx->tot_bytes)
906 return 0;
907
908 sg = sgx->cur_sg;
909 cumulative_partial = sgx->tot_partial;
910
911 sg_dma_addr = sg_dma_address(sg);
912 sg_len = sg_dma_len(sg);
913
914 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
915
916 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
917 sgx->dma_len = (blk_sz - cumulative_partial);
918 sgx->tot_partial = 0;
919 sgx->num_bytes += blk_sz;
920 *partial = 0;
921 } else {
922 sgx->dma_len = sg_len - sgx->bytes_consumed;
923 sgx->tot_partial += sgx->dma_len;
924 *partial = 1;
925 }
926
927 sgx->bytes_consumed += sgx->dma_len;
928
929 if (sg_len == sgx->bytes_consumed) {
930 sg = sg_next(sg);
931 sgx->num_sg++;
932 sgx->cur_sg = sg;
933 sgx->bytes_consumed = 0;
934 }
935
936 return 1;
937}
938
939static int
940qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
941 uint32_t *dsd, uint16_t tot_dsds)
942{
943 void *next_dsd;
944 uint8_t avail_dsds = 0;
945 uint32_t dsd_list_len;
946 struct dsd_dma *dsd_ptr;
947 struct scatterlist *sg_prot;
948 uint32_t *cur_dsd = dsd;
949 uint16_t used_dsds = tot_dsds;
950
951 uint32_t prot_int;
952 uint32_t partial;
953 struct qla2_sgx sgx;
954 dma_addr_t sle_dma;
955 uint32_t sle_dma_len, tot_prot_dma_len = 0;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800956 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easi8cb20492011-08-16 11:29:22 -0700957
958 prot_int = cmd->device->sector_size;
959
960 memset(&sgx, 0, sizeof(struct qla2_sgx));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800961 sgx.tot_bytes = scsi_bufflen(cmd);
962 sgx.cur_sg = scsi_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700963 sgx.sp = sp;
964
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800965 sg_prot = scsi_prot_sglist(cmd);
Arun Easi8cb20492011-08-16 11:29:22 -0700966
967 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
968
969 sle_dma = sgx.dma_addr;
970 sle_dma_len = sgx.dma_len;
971alloc_and_fill:
972 /* Allocate additional continuation packets? */
973 if (avail_dsds == 0) {
974 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
975 QLA_DSDS_PER_IOCB : used_dsds;
976 dsd_list_len = (avail_dsds + 1) * 12;
977 used_dsds -= avail_dsds;
978
979 /* allocate tracking DS */
980 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
981 if (!dsd_ptr)
982 return 1;
983
984 /* allocate new list */
985 dsd_ptr->dsd_addr = next_dsd =
986 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
987 &dsd_ptr->dsd_list_dma);
988
989 if (!next_dsd) {
990 /*
991 * Need to cleanup only this dsd_ptr, rest
992 * will be done by sp_free_dma()
993 */
994 kfree(dsd_ptr);
995 return 1;
996 }
997
998 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800999 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easi8cb20492011-08-16 11:29:22 -07001000
1001 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1002
1003 /* add new list to cmd iocb or last list */
1004 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1005 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1006 *cur_dsd++ = dsd_list_len;
1007 cur_dsd = (uint32_t *)next_dsd;
1008 }
1009 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1010 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1011 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1012 avail_dsds--;
1013
1014 if (partial == 0) {
1015 /* Got a full protection interval */
1016 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1017 sle_dma_len = 8;
1018
1019 tot_prot_dma_len += sle_dma_len;
1020 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1021 tot_prot_dma_len = 0;
1022 sg_prot = sg_next(sg_prot);
1023 }
1024
1025 partial = 1; /* So as to not re-enter this block */
1026 goto alloc_and_fill;
1027 }
1028 }
1029 /* Null termination */
1030 *cur_dsd++ = 0;
1031 *cur_dsd++ = 0;
1032 *cur_dsd++ = 0;
1033 return 0;
1034}
Giridhar Malavali5162cf02011-11-18 09:03:18 -08001035
Arun Easibad75002010-05-04 15:01:30 -07001036static int
1037qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
1038 uint16_t tot_dsds)
1039{
1040 void *next_dsd;
1041 uint8_t avail_dsds = 0;
1042 uint32_t dsd_list_len;
1043 struct dsd_dma *dsd_ptr;
1044 struct scatterlist *sg;
1045 uint32_t *cur_dsd = dsd;
1046 int i;
1047 uint16_t used_dsds = tot_dsds;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001048 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001049
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001050 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
Arun Easibad75002010-05-04 15:01:30 -07001051 dma_addr_t sle_dma;
1052
1053 /* Allocate additional continuation packets? */
1054 if (avail_dsds == 0) {
1055 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1056 QLA_DSDS_PER_IOCB : used_dsds;
1057 dsd_list_len = (avail_dsds + 1) * 12;
1058 used_dsds -= avail_dsds;
1059
1060 /* allocate tracking DS */
1061 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1062 if (!dsd_ptr)
1063 return 1;
1064
1065 /* allocate new list */
1066 dsd_ptr->dsd_addr = next_dsd =
1067 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1068 &dsd_ptr->dsd_list_dma);
1069
1070 if (!next_dsd) {
1071 /*
1072 * Need to cleanup only this dsd_ptr, rest
1073 * will be done by sp_free_dma()
1074 */
1075 kfree(dsd_ptr);
1076 return 1;
1077 }
1078
1079 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001080 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001081
1082 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1083
1084 /* add new list to cmd iocb or last list */
1085 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1086 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1087 *cur_dsd++ = dsd_list_len;
1088 cur_dsd = (uint32_t *)next_dsd;
1089 }
1090 sle_dma = sg_dma_address(sg);
Arun Easi9e522cd2012-08-22 14:21:31 -04001091
Arun Easibad75002010-05-04 15:01:30 -07001092 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1093 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1094 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1095 avail_dsds--;
1096
Arun Easibad75002010-05-04 15:01:30 -07001097 }
1098 /* Null termination */
1099 *cur_dsd++ = 0;
1100 *cur_dsd++ = 0;
1101 *cur_dsd++ = 0;
1102 return 0;
1103}
1104
1105static int
1106qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1107 uint32_t *dsd,
1108 uint16_t tot_dsds)
1109{
1110 void *next_dsd;
1111 uint8_t avail_dsds = 0;
1112 uint32_t dsd_list_len;
1113 struct dsd_dma *dsd_ptr;
1114 struct scatterlist *sg;
1115 int i;
1116 struct scsi_cmnd *cmd;
1117 uint32_t *cur_dsd = dsd;
1118 uint16_t used_dsds = tot_dsds;
Arun Easibad75002010-05-04 15:01:30 -07001119
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001120 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001121 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1122 dma_addr_t sle_dma;
1123
1124 /* Allocate additional continuation packets? */
1125 if (avail_dsds == 0) {
1126 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1127 QLA_DSDS_PER_IOCB : used_dsds;
1128 dsd_list_len = (avail_dsds + 1) * 12;
1129 used_dsds -= avail_dsds;
1130
1131 /* allocate tracking DS */
1132 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1133 if (!dsd_ptr)
1134 return 1;
1135
1136 /* allocate new list */
1137 dsd_ptr->dsd_addr = next_dsd =
1138 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1139 &dsd_ptr->dsd_list_dma);
1140
1141 if (!next_dsd) {
1142 /*
1143 * Need to cleanup only this dsd_ptr, rest
1144 * will be done by sp_free_dma()
1145 */
1146 kfree(dsd_ptr);
1147 return 1;
1148 }
1149
1150 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001151 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001152
1153 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1154
1155 /* add new list to cmd iocb or last list */
1156 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1157 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1158 *cur_dsd++ = dsd_list_len;
1159 cur_dsd = (uint32_t *)next_dsd;
1160 }
1161 sle_dma = sg_dma_address(sg);
Arun Easi9e522cd2012-08-22 14:21:31 -04001162
Arun Easibad75002010-05-04 15:01:30 -07001163 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1164 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1165 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1166
Arun Easibad75002010-05-04 15:01:30 -07001167 avail_dsds--;
1168 }
1169 /* Null termination */
1170 *cur_dsd++ = 0;
1171 *cur_dsd++ = 0;
1172 *cur_dsd++ = 0;
1173 return 0;
1174}
1175
1176/**
1177 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1178 * Type 6 IOCB types.
1179 *
1180 * @sp: SRB command to process
1181 * @cmd_pkt: Command type 3 IOCB
1182 * @tot_dsds: Total number of segments to transfer
1183 */
1184static inline int
1185qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1186 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1187{
1188 uint32_t *cur_dsd, *fcp_dl;
1189 scsi_qla_host_t *vha;
1190 struct scsi_cmnd *cmd;
Arun Easibad75002010-05-04 15:01:30 -07001191 int sgc;
Arun Easi8cb20492011-08-16 11:29:22 -07001192 uint32_t total_bytes = 0;
Arun Easibad75002010-05-04 15:01:30 -07001193 uint32_t data_bytes;
1194 uint32_t dif_bytes;
1195 uint8_t bundling = 1;
1196 uint16_t blk_size;
1197 uint8_t *clr_ptr;
1198 struct crc_context *crc_ctx_pkt = NULL;
1199 struct qla_hw_data *ha;
1200 uint8_t additional_fcpcdb_len;
1201 uint16_t fcp_cmnd_len;
1202 struct fcp_cmnd *fcp_cmnd;
1203 dma_addr_t crc_ctx_dma;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001204 char tag[2];
Arun Easibad75002010-05-04 15:01:30 -07001205
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001206 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001207
1208 sgc = 0;
1209 /* Update entry type to indicate Command Type CRC_2 IOCB */
1210 *((uint32_t *)(&cmd_pkt->entry_type)) =
1211 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1212
Arun Easibad75002010-05-04 15:01:30 -07001213 vha = sp->fcport->vha;
1214 ha = vha->hw;
1215
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001216 /* No data transfer */
1217 data_bytes = scsi_bufflen(cmd);
1218 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1219 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1220 return QLA_SUCCESS;
1221 }
Arun Easibad75002010-05-04 15:01:30 -07001222
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001223 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Arun Easibad75002010-05-04 15:01:30 -07001224
1225 /* Set transfer direction */
1226 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1227 cmd_pkt->control_flags =
1228 __constant_cpu_to_le16(CF_WRITE_DATA);
1229 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1230 cmd_pkt->control_flags =
1231 __constant_cpu_to_le16(CF_READ_DATA);
1232 }
1233
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001234 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1235 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1236 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1237 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
Arun Easibad75002010-05-04 15:01:30 -07001238 bundling = 0;
1239
1240 /* Allocate CRC context from global pool */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001241 crc_ctx_pkt = sp->u.scmd.ctx =
1242 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Arun Easibad75002010-05-04 15:01:30 -07001243
1244 if (!crc_ctx_pkt)
1245 goto crc_queuing_error;
1246
1247 /* Zero out CTX area. */
1248 clr_ptr = (uint8_t *)crc_ctx_pkt;
1249 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1250
1251 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1252
1253 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1254
1255 /* Set handle */
1256 crc_ctx_pkt->handle = cmd_pkt->handle;
1257
1258 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1259
Arun Easie02587d2011-08-16 11:29:23 -07001260 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
Arun Easibad75002010-05-04 15:01:30 -07001261 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1262
1263 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1264 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1265 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1266
1267 /* Determine SCSI command length -- align to 4 byte boundary */
1268 if (cmd->cmd_len > 16) {
Arun Easibad75002010-05-04 15:01:30 -07001269 additional_fcpcdb_len = cmd->cmd_len - 16;
1270 if ((cmd->cmd_len % 4) != 0) {
1271 /* SCSI cmd > 16 bytes must be multiple of 4 */
1272 goto crc_queuing_error;
1273 }
1274 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1275 } else {
1276 additional_fcpcdb_len = 0;
1277 fcp_cmnd_len = 12 + 16 + 4;
1278 }
1279
1280 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1281
1282 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1283 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1284 fcp_cmnd->additional_cdb_len |= 1;
1285 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1286 fcp_cmnd->additional_cdb_len |= 2;
1287
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001288 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
Arun Easibad75002010-05-04 15:01:30 -07001289 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1290 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1291 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1292 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1293 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1294 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001295 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001296
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001297 /*
1298 * Update tagged queuing modifier if using command tag queuing
1299 */
1300 if (scsi_populate_tag_msg(cmd, tag)) {
1301 switch (tag[0]) {
1302 case HEAD_OF_QUEUE_TAG:
1303 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1304 break;
1305 case ORDERED_QUEUE_TAG:
1306 fcp_cmnd->task_attribute = TSK_ORDERED;
1307 break;
1308 default:
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001309 fcp_cmnd->task_attribute = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001310 break;
1311 }
1312 } else {
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001313 fcp_cmnd->task_attribute = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001314 }
1315
Arun Easibad75002010-05-04 15:01:30 -07001316 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1317
Arun Easibad75002010-05-04 15:01:30 -07001318 /* Compute dif len and adjust data len to incude protection */
Arun Easibad75002010-05-04 15:01:30 -07001319 dif_bytes = 0;
1320 blk_size = cmd->device->sector_size;
Arun Easi8cb20492011-08-16 11:29:22 -07001321 dif_bytes = (data_bytes / blk_size) * 8;
1322
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001323 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
Arun Easi8cb20492011-08-16 11:29:22 -07001324 case SCSI_PROT_READ_INSERT:
1325 case SCSI_PROT_WRITE_STRIP:
1326 total_bytes = data_bytes;
1327 data_bytes += dif_bytes;
1328 break;
1329
1330 case SCSI_PROT_READ_STRIP:
1331 case SCSI_PROT_WRITE_INSERT:
1332 case SCSI_PROT_READ_PASS:
1333 case SCSI_PROT_WRITE_PASS:
1334 total_bytes = data_bytes + dif_bytes;
1335 break;
1336 default:
1337 BUG();
Arun Easibad75002010-05-04 15:01:30 -07001338 }
1339
Arun Easie02587d2011-08-16 11:29:23 -07001340 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -07001341 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
Arun Easi9e522cd2012-08-22 14:21:31 -04001342 /* HBA error checking enabled */
1343 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1344 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1345 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1346 SCSI_PROT_DIF_TYPE2))
1347 fw_prot_opts |= BIT_10;
1348 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1349 SCSI_PROT_DIF_TYPE3)
1350 fw_prot_opts |= BIT_11;
1351 }
Arun Easibad75002010-05-04 15:01:30 -07001352
1353 if (!bundling) {
1354 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1355 } else {
1356 /*
1357 * Configure Bundling if we need to fetch interlaving
1358 * protection PCI accesses
1359 */
1360 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1361 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1362 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1363 tot_prot_dsds);
1364 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1365 }
1366
1367 /* Finish the common fields of CRC pkt */
1368 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1369 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1370 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1371 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1372 /* Fibre channel byte count */
1373 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1374 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1375 additional_fcpcdb_len);
1376 *fcp_dl = htonl(total_bytes);
1377
Arun Easi0c470872010-07-23 15:28:38 +05001378 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
Arun Easi0c470872010-07-23 15:28:38 +05001379 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1380 return QLA_SUCCESS;
1381 }
Arun Easibad75002010-05-04 15:01:30 -07001382 /* Walks data segments */
1383
1384 cmd_pkt->control_flags |=
1385 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
Arun Easi8cb20492011-08-16 11:29:22 -07001386
1387 if (!bundling && tot_prot_dsds) {
1388 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1389 cur_dsd, tot_dsds))
1390 goto crc_queuing_error;
1391 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
Arun Easibad75002010-05-04 15:01:30 -07001392 (tot_dsds - tot_prot_dsds)))
1393 goto crc_queuing_error;
1394
1395 if (bundling && tot_prot_dsds) {
1396 /* Walks dif segments */
Arun Easibad75002010-05-04 15:01:30 -07001397 cmd_pkt->control_flags |=
1398 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1399 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1400 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1401 tot_prot_dsds))
1402 goto crc_queuing_error;
1403 }
1404 return QLA_SUCCESS;
1405
1406crc_queuing_error:
Arun Easibad75002010-05-04 15:01:30 -07001407 /* Cleanup will be performed by the caller */
1408
1409 return QLA_FUNCTION_FAILED;
1410}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001411
1412/**
1413 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1414 * @sp: command to send to the ISP
1415 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001416 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001417 */
1418int
1419qla24xx_start_scsi(srb_t *sp)
1420{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001421 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001422 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001423 uint32_t *clr_ptr;
1424 uint32_t index;
1425 uint32_t handle;
1426 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001427 uint16_t cnt;
1428 uint16_t req_cnt;
1429 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001430 struct req_que *req = NULL;
1431 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001432 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Andrew Vasquez444786d2009-01-05 11:18:10 -08001433 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001434 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001435 char tag[2];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001436
1437 /* Setup device pointers. */
1438 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001439
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001440 qla25xx_set_que(sp, &rsp);
1441 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001442
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001443 /* So we know we haven't pci_map'ed anything yet */
1444 tot_dsds = 0;
1445
1446 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001447 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001448 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1449 QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001450 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001451 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001452 }
1453
1454 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001455 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001456
1457 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001458 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001459 for (index = 1; index < req->num_outstanding_cmds; index++) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001460 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001461 if (handle == req->num_outstanding_cmds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001462 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001463 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001464 break;
1465 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05001466 if (index == req->num_outstanding_cmds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001467 goto queuing_error;
1468
1469 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001470 if (scsi_sg_count(cmd)) {
1471 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1472 scsi_sg_count(cmd), cmd->sc_data_direction);
1473 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001474 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001475 } else
1476 nseg = 0;
1477
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001478 tot_dsds = nseg;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001479 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001480 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001481 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001482
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001483 if (req->ring_index < cnt)
1484 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001485 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001486 req->cnt = req->length -
1487 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001488 if (req->cnt < (req_cnt + 2))
1489 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001490 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001491
1492 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001493 req->current_outstanding_cmd = handle;
1494 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001495 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001496 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001497 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001498
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001499 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001500 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001501
1502 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001503 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001504 clr_ptr = (uint32_t *)cmd_pkt + 2;
1505 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1506 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1507
1508 /* Set NPORT-ID and LUN number*/
1509 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1510 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1511 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1512 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001513 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001514
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001515 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001516 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001517
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001518 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1519 if (scsi_populate_tag_msg(cmd, tag)) {
1520 switch (tag[0]) {
1521 case HEAD_OF_QUEUE_TAG:
1522 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1523 break;
1524 case ORDERED_QUEUE_TAG:
1525 cmd_pkt->task = TSK_ORDERED;
1526 break;
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001527 default:
1528 cmd_pkt->task = TSK_SIMPLE;
1529 break;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001530 }
Saurav Kashyapc3ccb1d2013-07-12 14:47:51 -04001531 } else {
1532 cmd_pkt->task = TSK_SIMPLE;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001533 }
1534
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001535 /* Load SCSI command packet. */
1536 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1537 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1538
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001539 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001540
1541 /* Build IOCB segments */
1542 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1543
1544 /* Set total data segment count. */
1545 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001546 /* Specify response queue number where completion should happen */
1547 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001548 wmb();
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001549 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001550 req->ring_index++;
1551 if (req->ring_index == req->length) {
1552 req->ring_index = 0;
1553 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001554 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001555 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001556
1557 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001558
1559 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001560 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1561 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001562
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001563 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001564 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001565 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001566 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001567
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001568 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001569 return QLA_SUCCESS;
1570
1571queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001572 if (tot_dsds)
1573 scsi_dma_unmap(cmd);
1574
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001575 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001576
1577 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001579
Arun Easibad75002010-05-04 15:01:30 -07001580/**
1581 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1582 * @sp: command to send to the ISP
1583 *
1584 * Returns non-zero if a failure occurred, else zero.
1585 */
1586int
1587qla24xx_dif_start_scsi(srb_t *sp)
1588{
1589 int nseg;
1590 unsigned long flags;
1591 uint32_t *clr_ptr;
1592 uint32_t index;
1593 uint32_t handle;
1594 uint16_t cnt;
1595 uint16_t req_cnt = 0;
1596 uint16_t tot_dsds;
1597 uint16_t tot_prot_dsds;
1598 uint16_t fw_prot_opts = 0;
1599 struct req_que *req = NULL;
1600 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001601 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001602 struct scsi_qla_host *vha = sp->fcport->vha;
1603 struct qla_hw_data *ha = vha->hw;
1604 struct cmd_type_crc_2 *cmd_pkt;
1605 uint32_t status = 0;
1606
1607#define QDSS_GOT_Q_SPACE BIT_0
1608
Arun Easi0c470872010-07-23 15:28:38 +05001609 /* Only process protection or >16 cdb in this routine */
1610 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1611 if (cmd->cmd_len <= 16)
1612 return qla24xx_start_scsi(sp);
1613 }
Arun Easibad75002010-05-04 15:01:30 -07001614
1615 /* Setup device pointers. */
1616
1617 qla25xx_set_que(sp, &rsp);
1618 req = vha->req;
1619
1620 /* So we know we haven't pci_map'ed anything yet */
1621 tot_dsds = 0;
1622
1623 /* Send marker if required */
1624 if (vha->marker_needed != 0) {
1625 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1626 QLA_SUCCESS)
1627 return QLA_FUNCTION_FAILED;
1628 vha->marker_needed = 0;
1629 }
1630
1631 /* Acquire ring specific lock */
1632 spin_lock_irqsave(&ha->hardware_lock, flags);
1633
1634 /* Check for room in outstanding command list. */
1635 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001636 for (index = 1; index < req->num_outstanding_cmds; index++) {
Arun Easibad75002010-05-04 15:01:30 -07001637 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001638 if (handle == req->num_outstanding_cmds)
Arun Easibad75002010-05-04 15:01:30 -07001639 handle = 1;
1640 if (!req->outstanding_cmds[handle])
1641 break;
1642 }
1643
Chad Dupuis8d93f552013-01-30 03:34:37 -05001644 if (index == req->num_outstanding_cmds)
Arun Easibad75002010-05-04 15:01:30 -07001645 goto queuing_error;
1646
1647 /* Compute number of required data segments */
1648 /* Map the sg table so we have an accurate count of sg entries needed */
1649 if (scsi_sg_count(cmd)) {
1650 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1651 scsi_sg_count(cmd), cmd->sc_data_direction);
1652 if (unlikely(!nseg))
1653 goto queuing_error;
1654 else
1655 sp->flags |= SRB_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001656
1657 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1658 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1659 struct qla2_sgx sgx;
1660 uint32_t partial;
1661
1662 memset(&sgx, 0, sizeof(struct qla2_sgx));
1663 sgx.tot_bytes = scsi_bufflen(cmd);
1664 sgx.cur_sg = scsi_sglist(cmd);
1665 sgx.sp = sp;
1666
1667 nseg = 0;
1668 while (qla24xx_get_one_block_sg(
1669 cmd->device->sector_size, &sgx, &partial))
1670 nseg++;
1671 }
Arun Easibad75002010-05-04 15:01:30 -07001672 } else
1673 nseg = 0;
1674
1675 /* number of required data segments */
1676 tot_dsds = nseg;
1677
1678 /* Compute number of required protection segments */
1679 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1680 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1681 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1682 if (unlikely(!nseg))
1683 goto queuing_error;
1684 else
1685 sp->flags |= SRB_CRC_PROT_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001686
1687 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1688 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1689 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1690 }
Arun Easibad75002010-05-04 15:01:30 -07001691 } else {
1692 nseg = 0;
1693 }
1694
1695 req_cnt = 1;
1696 /* Total Data and protection sg segment(s) */
1697 tot_prot_dsds = nseg;
1698 tot_dsds += nseg;
1699 if (req->cnt < (req_cnt + 2)) {
1700 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1701
1702 if (req->ring_index < cnt)
1703 req->cnt = cnt - req->ring_index;
1704 else
1705 req->cnt = req->length -
1706 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04001707 if (req->cnt < (req_cnt + 2))
1708 goto queuing_error;
Arun Easibad75002010-05-04 15:01:30 -07001709 }
1710
Arun Easibad75002010-05-04 15:01:30 -07001711 status |= QDSS_GOT_Q_SPACE;
1712
1713 /* Build header part of command packet (excluding the OPCODE). */
1714 req->current_outstanding_cmd = handle;
1715 req->outstanding_cmds[handle] = sp;
Arun Easi8cb20492011-08-16 11:29:22 -07001716 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001717 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Arun Easibad75002010-05-04 15:01:30 -07001718 req->cnt -= req_cnt;
1719
1720 /* Fill-in common area */
1721 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1722 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1723
1724 clr_ptr = (uint32_t *)cmd_pkt + 2;
1725 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1726
1727 /* Set NPORT-ID and LUN number*/
1728 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1729 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1730 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1731 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1732
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001733 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Arun Easibad75002010-05-04 15:01:30 -07001734 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1735
1736 /* Total Data and protection segment(s) */
1737 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1738
1739 /* Build IOCB segments and adjust for data protection segments */
1740 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1741 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1742 QLA_SUCCESS)
1743 goto queuing_error;
1744
1745 cmd_pkt->entry_count = (uint8_t)req_cnt;
1746 /* Specify response queue number where completion should happen */
1747 cmd_pkt->entry_status = (uint8_t) rsp->id;
1748 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1749 wmb();
1750
1751 /* Adjust ring index. */
1752 req->ring_index++;
1753 if (req->ring_index == req->length) {
1754 req->ring_index = 0;
1755 req->ring_ptr = req->ring;
1756 } else
1757 req->ring_ptr++;
1758
1759 /* Set chip new ring index. */
1760 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1761 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1762
1763 /* Manage unprocessed RIO/ZIO commands in response queue. */
1764 if (vha->flags.process_response_queue &&
1765 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1766 qla24xx_process_response_queue(vha, rsp);
1767
1768 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1769
1770 return QLA_SUCCESS;
1771
1772queuing_error:
1773 if (status & QDSS_GOT_Q_SPACE) {
1774 req->outstanding_cmds[handle] = NULL;
1775 req->cnt += req_cnt;
1776 }
1777 /* Cleanup will be performed by the caller (queuecommand) */
1778
1779 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Arun Easibad75002010-05-04 15:01:30 -07001780 return QLA_FUNCTION_FAILED;
1781}
1782
1783
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001784static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001785{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001786 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001787 struct qla_hw_data *ha = sp->fcport->vha->hw;
1788 int affinity = cmd->request->cpu;
1789
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001790 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001791 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001792 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001793 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001794 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001795}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001796
1797/* Generic Control-SRB manipulation functions. */
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001798void *
1799qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001800{
Andrew Vasquezac280b62009-08-20 11:06:05 -07001801 struct qla_hw_data *ha = vha->hw;
1802 struct req_que *req = ha->req_q_map[0];
1803 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1804 uint32_t index, handle;
1805 request_t *pkt;
1806 uint16_t cnt, req_cnt;
1807
1808 pkt = NULL;
1809 req_cnt = 1;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001810 handle = 0;
1811
1812 if (!sp)
1813 goto skip_cmd_array;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001814
1815 /* Check for room in outstanding command list. */
1816 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001817 for (index = 1; req->num_outstanding_cmds; index++) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07001818 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05001819 if (handle == req->num_outstanding_cmds)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001820 handle = 1;
1821 if (!req->outstanding_cmds[handle])
1822 break;
1823 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05001824 if (index == req->num_outstanding_cmds) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001825 ql_log(ql_log_warn, vha, 0x700b,
Masanari Iidad6a03582012-08-22 14:20:58 -04001826 "No room on outstanding cmd array.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07001827 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001828 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001829
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001830 /* Prep command array. */
1831 req->current_outstanding_cmd = handle;
1832 req->outstanding_cmds[handle] = sp;
1833 sp->handle = handle;
1834
Andrew Vasquez57807902011-11-18 09:03:20 -08001835 /* Adjust entry-counts as needed. */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001836 if (sp->type != SRB_SCSI_CMD)
1837 req_cnt = sp->iocbs;
Andrew Vasquez57807902011-11-18 09:03:20 -08001838
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001839skip_cmd_array:
Andrew Vasquezac280b62009-08-20 11:06:05 -07001840 /* Check for room on request queue. */
1841 if (req->cnt < req_cnt) {
Chad Dupuisf73cb692014-02-26 04:15:06 -05001842 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha))
Andrew Vasquezac280b62009-08-20 11:06:05 -07001843 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
Atul Deshmukh7ec0eff2013-08-27 01:37:28 -04001844 else if (IS_P3P_TYPE(ha))
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001845 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001846 else if (IS_FWI2_CAPABLE(ha))
1847 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001848 else if (IS_QLAFX00(ha))
1849 cnt = RD_REG_DWORD(&reg->ispfx00.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001850 else
1851 cnt = qla2x00_debounce_register(
1852 ISP_REQ_Q_OUT(ha, &reg->isp));
1853
1854 if (req->ring_index < cnt)
1855 req->cnt = cnt - req->ring_index;
1856 else
1857 req->cnt = req->length -
1858 (req->ring_index - cnt);
1859 }
1860 if (req->cnt < req_cnt)
1861 goto queuing_error;
1862
1863 /* Prep packet */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001864 req->cnt -= req_cnt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001865 pkt = req->ring_ptr;
1866 memset(pkt, 0, REQUEST_ENTRY_SIZE);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001867 if (IS_QLAFX00(ha)) {
Saurav Kashyap1f8deef2013-06-25 11:27:21 -04001868 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
1869 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04001870 } else {
1871 pkt->entry_count = req_cnt;
1872 pkt->handle = handle;
1873 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001874
1875queuing_error:
1876 return pkt;
1877}
1878
1879static void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001880qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1881{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001882 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001883
1884 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1885 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001886 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001887 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001888 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001889 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1890 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1891 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1892 logio->port_id[1] = sp->fcport->d_id.b.area;
1893 logio->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001894 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001895}
1896
1897static void
1898qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1899{
1900 struct qla_hw_data *ha = sp->fcport->vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001901 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001902 uint16_t opts;
1903
Giridhar Malavalib9637522010-05-28 15:08:15 -07001904 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001905 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1906 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001907 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1908 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001909 if (HAS_EXTENDED_IDS(ha)) {
1910 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1911 mbx->mb10 = cpu_to_le16(opts);
1912 } else {
1913 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1914 }
1915 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1916 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1917 sp->fcport->d_id.b.al_pa);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001918 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001919}
1920
1921static void
1922qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1923{
1924 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1925 logio->control_flags =
1926 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1927 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1928 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1929 logio->port_id[1] = sp->fcport->d_id.b.area;
1930 logio->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001931 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001932}
1933
1934static void
1935qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1936{
1937 struct qla_hw_data *ha = sp->fcport->vha->hw;
1938
Giridhar Malavalib9637522010-05-28 15:08:15 -07001939 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001940 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1941 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1942 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1943 cpu_to_le16(sp->fcport->loop_id):
1944 cpu_to_le16(sp->fcport->loop_id << 8);
1945 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1946 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1947 sp->fcport->d_id.b.al_pa);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001948 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001949 /* Implicit: mbx->mbx10 = 0. */
1950}
1951
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001952static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001953qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1954{
1955 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1956 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1957 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001958 logio->vp_index = sp->fcport->vha->vp_idx;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001959}
1960
1961static void
1962qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1963{
1964 struct qla_hw_data *ha = sp->fcport->vha->hw;
1965
1966 mbx->entry_type = MBX_IOCB_TYPE;
1967 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1968 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1969 if (HAS_EXTENDED_IDS(ha)) {
1970 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1971 mbx->mb10 = cpu_to_le16(BIT_0);
1972 } else {
1973 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1974 }
1975 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1976 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1977 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1978 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04001979 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001980}
1981
1982static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001983qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1984{
1985 uint32_t flags;
1986 unsigned int lun;
1987 struct fc_port *fcport = sp->fcport;
1988 scsi_qla_host_t *vha = fcport->vha;
1989 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001990 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001991 struct req_que *req = vha->req;
1992
1993 flags = iocb->u.tmf.flags;
1994 lun = iocb->u.tmf.lun;
1995
1996 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
1997 tsk->entry_count = 1;
1998 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
1999 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2000 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2001 tsk->control_flags = cpu_to_le32(flags);
2002 tsk->port_id[0] = fcport->d_id.b.al_pa;
2003 tsk->port_id[1] = fcport->d_id.b.area;
2004 tsk->port_id[2] = fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002005 tsk->vp_index = fcport->vha->vp_idx;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002006
2007 if (flags == TCF_LUN_RESET) {
2008 int_to_scsilun(lun, &tsk->lun);
2009 host_to_fcp_swap((uint8_t *)&tsk->lun,
2010 sizeof(tsk->lun));
2011 }
2012}
2013
2014static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002015qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2016{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002017 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002018
2019 els_iocb->entry_type = ELS_IOCB_TYPE;
2020 els_iocb->entry_count = 1;
2021 els_iocb->sys_define = 0;
2022 els_iocb->entry_status = 0;
2023 els_iocb->handle = sp->handle;
2024 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2025 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002026 els_iocb->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002027 els_iocb->sof_type = EST_SOFI3;
2028 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2029
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002030 els_iocb->opcode =
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002031 sp->type == SRB_ELS_CMD_RPT ?
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002032 bsg_job->request->rqst_data.r_els.els_code :
2033 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002034 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2035 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2036 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2037 els_iocb->control_flags = 0;
2038 els_iocb->rx_byte_count =
2039 cpu_to_le32(bsg_job->reply_payload.payload_len);
2040 els_iocb->tx_byte_count =
2041 cpu_to_le32(bsg_job->request_payload.payload_len);
2042
2043 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2044 (bsg_job->request_payload.sg_list)));
2045 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2046 (bsg_job->request_payload.sg_list)));
2047 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2048 (bsg_job->request_payload.sg_list));
2049
2050 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2051 (bsg_job->reply_payload.sg_list)));
2052 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2053 (bsg_job->reply_payload.sg_list)));
2054 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2055 (bsg_job->reply_payload.sg_list));
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04002056
2057 sp->fcport->vha->qla_stats.control_requests++;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002058}
2059
2060static void
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002061qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2062{
2063 uint16_t avail_dsds;
2064 uint32_t *cur_dsd;
2065 struct scatterlist *sg;
2066 int index;
2067 uint16_t tot_dsds;
2068 scsi_qla_host_t *vha = sp->fcport->vha;
2069 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002070 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002071 int loop_iterartion = 0;
2072 int cont_iocb_prsnt = 0;
2073 int entry_count = 1;
2074
2075 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2076 ct_iocb->entry_type = CT_IOCB_TYPE;
2077 ct_iocb->entry_status = 0;
2078 ct_iocb->handle1 = sp->handle;
2079 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2080 ct_iocb->status = __constant_cpu_to_le16(0);
2081 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2082 ct_iocb->timeout = 0;
2083 ct_iocb->cmd_dsd_count =
2084 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2085 ct_iocb->total_dsd_count =
2086 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2087 ct_iocb->req_bytecount =
2088 cpu_to_le32(bsg_job->request_payload.payload_len);
2089 ct_iocb->rsp_bytecount =
2090 cpu_to_le32(bsg_job->reply_payload.payload_len);
2091
2092 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2093 (bsg_job->request_payload.sg_list)));
2094 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2095 (bsg_job->request_payload.sg_list)));
2096 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2097
2098 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2099 (bsg_job->reply_payload.sg_list)));
2100 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2101 (bsg_job->reply_payload.sg_list)));
2102 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2103
2104 avail_dsds = 1;
2105 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2106 index = 0;
2107 tot_dsds = bsg_job->reply_payload.sg_cnt;
2108
2109 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2110 dma_addr_t sle_dma;
2111 cont_a64_entry_t *cont_pkt;
2112
2113 /* Allocate additional continuation packets? */
2114 if (avail_dsds == 0) {
2115 /*
2116 * Five DSDs are available in the Cont.
2117 * Type 1 IOCB.
2118 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002119 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2120 vha->hw->req_q_map[0]);
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002121 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2122 avail_dsds = 5;
2123 cont_iocb_prsnt = 1;
2124 entry_count++;
2125 }
2126
2127 sle_dma = sg_dma_address(sg);
2128 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2129 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2130 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2131 loop_iterartion++;
2132 avail_dsds--;
2133 }
2134 ct_iocb->entry_count = entry_count;
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04002135
2136 sp->fcport->vha->qla_stats.control_requests++;
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002137}
2138
2139static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002140qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2141{
2142 uint16_t avail_dsds;
2143 uint32_t *cur_dsd;
2144 struct scatterlist *sg;
2145 int index;
2146 uint16_t tot_dsds;
2147 scsi_qla_host_t *vha = sp->fcport->vha;
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002148 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002149 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002150 int loop_iterartion = 0;
2151 int cont_iocb_prsnt = 0;
2152 int entry_count = 1;
2153
2154 ct_iocb->entry_type = CT_IOCB_TYPE;
2155 ct_iocb->entry_status = 0;
2156 ct_iocb->sys_define = 0;
2157 ct_iocb->handle = sp->handle;
2158
2159 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002160 ct_iocb->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002161 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2162
2163 ct_iocb->cmd_dsd_count =
2164 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2165 ct_iocb->timeout = 0;
2166 ct_iocb->rsp_dsd_count =
2167 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2168 ct_iocb->rsp_byte_count =
2169 cpu_to_le32(bsg_job->reply_payload.payload_len);
2170 ct_iocb->cmd_byte_count =
2171 cpu_to_le32(bsg_job->request_payload.payload_len);
2172 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2173 (bsg_job->request_payload.sg_list)));
2174 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2175 (bsg_job->request_payload.sg_list)));
2176 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2177 (bsg_job->request_payload.sg_list));
2178
2179 avail_dsds = 1;
2180 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2181 index = 0;
2182 tot_dsds = bsg_job->reply_payload.sg_cnt;
2183
2184 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2185 dma_addr_t sle_dma;
2186 cont_a64_entry_t *cont_pkt;
2187
2188 /* Allocate additional continuation packets? */
2189 if (avail_dsds == 0) {
2190 /*
2191 * Five DSDs are available in the Cont.
2192 * Type 1 IOCB.
2193 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002194 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2195 ha->req_q_map[0]);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002196 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2197 avail_dsds = 5;
2198 cont_iocb_prsnt = 1;
2199 entry_count++;
2200 }
2201
2202 sle_dma = sg_dma_address(sg);
2203 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2204 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2205 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2206 loop_iterartion++;
2207 avail_dsds--;
2208 }
2209 ct_iocb->entry_count = entry_count;
2210}
2211
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002212/*
2213 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2214 * @sp: command to send to the ISP
2215 *
2216 * Returns non-zero if a failure occurred, else zero.
2217 */
2218int
2219qla82xx_start_scsi(srb_t *sp)
2220{
2221 int ret, nseg;
2222 unsigned long flags;
2223 struct scsi_cmnd *cmd;
2224 uint32_t *clr_ptr;
2225 uint32_t index;
2226 uint32_t handle;
2227 uint16_t cnt;
2228 uint16_t req_cnt;
2229 uint16_t tot_dsds;
2230 struct device_reg_82xx __iomem *reg;
2231 uint32_t dbval;
2232 uint32_t *fcp_dl;
2233 uint8_t additional_cdb_len;
2234 struct ct6_dsd *ctx;
2235 struct scsi_qla_host *vha = sp->fcport->vha;
2236 struct qla_hw_data *ha = vha->hw;
2237 struct req_que *req = NULL;
2238 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002239 char tag[2];
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002240
2241 /* Setup device pointers. */
2242 ret = 0;
2243 reg = &ha->iobase->isp82;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002244 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002245 req = vha->req;
2246 rsp = ha->rsp_q_map[0];
2247
2248 /* So we know we haven't pci_map'ed anything yet */
2249 tot_dsds = 0;
2250
2251 dbval = 0x04 | (ha->portnum << 5);
2252
2253 /* Send marker if required */
2254 if (vha->marker_needed != 0) {
2255 if (qla2x00_marker(vha, req,
2256 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2257 ql_log(ql_log_warn, vha, 0x300c,
2258 "qla2x00_marker failed for cmd=%p.\n", cmd);
2259 return QLA_FUNCTION_FAILED;
2260 }
2261 vha->marker_needed = 0;
2262 }
2263
2264 /* Acquire ring specific lock */
2265 spin_lock_irqsave(&ha->hardware_lock, flags);
2266
2267 /* Check for room in outstanding command list. */
2268 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002269 for (index = 1; index < req->num_outstanding_cmds; index++) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002270 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002271 if (handle == req->num_outstanding_cmds)
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002272 handle = 1;
2273 if (!req->outstanding_cmds[handle])
2274 break;
2275 }
Chad Dupuis8d93f552013-01-30 03:34:37 -05002276 if (index == req->num_outstanding_cmds)
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002277 goto queuing_error;
2278
2279 /* Map the sg table so we have an accurate count of sg entries needed */
2280 if (scsi_sg_count(cmd)) {
2281 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2282 scsi_sg_count(cmd), cmd->sc_data_direction);
2283 if (unlikely(!nseg))
2284 goto queuing_error;
2285 } else
2286 nseg = 0;
2287
2288 tot_dsds = nseg;
2289
2290 if (tot_dsds > ql2xshiftctondsd) {
2291 struct cmd_type_6 *cmd_pkt;
2292 uint16_t more_dsd_lists = 0;
2293 struct dsd_dma *dsd_ptr;
2294 uint16_t i;
2295
2296 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2297 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2298 ql_dbg(ql_dbg_io, vha, 0x300d,
2299 "Num of DSD list %d is than %d for cmd=%p.\n",
2300 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2301 cmd);
2302 goto queuing_error;
2303 }
2304
2305 if (more_dsd_lists <= ha->gbl_dsd_avail)
2306 goto sufficient_dsds;
2307 else
2308 more_dsd_lists -= ha->gbl_dsd_avail;
2309
2310 for (i = 0; i < more_dsd_lists; i++) {
2311 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2312 if (!dsd_ptr) {
2313 ql_log(ql_log_fatal, vha, 0x300e,
2314 "Failed to allocate memory for dsd_dma "
2315 "for cmd=%p.\n", cmd);
2316 goto queuing_error;
2317 }
2318
2319 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2320 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2321 if (!dsd_ptr->dsd_addr) {
2322 kfree(dsd_ptr);
2323 ql_log(ql_log_fatal, vha, 0x300f,
2324 "Failed to allocate memory for dsd_addr "
2325 "for cmd=%p.\n", cmd);
2326 goto queuing_error;
2327 }
2328 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2329 ha->gbl_dsd_avail++;
2330 }
2331
2332sufficient_dsds:
2333 req_cnt = 1;
2334
2335 if (req->cnt < (req_cnt + 2)) {
2336 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2337 &reg->req_q_out[0]);
2338 if (req->ring_index < cnt)
2339 req->cnt = cnt - req->ring_index;
2340 else
2341 req->cnt = req->length -
2342 (req->ring_index - cnt);
Chetan Lokea6eb3c92012-05-15 14:34:09 -04002343 if (req->cnt < (req_cnt + 2))
2344 goto queuing_error;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002345 }
2346
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002347 ctx = sp->u.scmd.ctx =
2348 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2349 if (!ctx) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002350 ql_log(ql_log_fatal, vha, 0x3010,
2351 "Failed to allocate ctx for cmd=%p.\n", cmd);
2352 goto queuing_error;
2353 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002354
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002355 memset(ctx, 0, sizeof(struct ct6_dsd));
2356 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2357 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2358 if (!ctx->fcp_cmnd) {
2359 ql_log(ql_log_fatal, vha, 0x3011,
2360 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
Dan Carpenter841f97b2012-05-17 10:13:40 +03002361 goto queuing_error;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002362 }
2363
2364 /* Initialize the DSD list and dma handle */
2365 INIT_LIST_HEAD(&ctx->dsd_list);
2366 ctx->dsd_use_cnt = 0;
2367
2368 if (cmd->cmd_len > 16) {
2369 additional_cdb_len = cmd->cmd_len - 16;
2370 if ((cmd->cmd_len % 4) != 0) {
2371 /* SCSI command bigger than 16 bytes must be
2372 * multiple of 4
2373 */
2374 ql_log(ql_log_warn, vha, 0x3012,
2375 "scsi cmd len %d not multiple of 4 "
2376 "for cmd=%p.\n", cmd->cmd_len, cmd);
2377 goto queuing_error_fcp_cmnd;
2378 }
2379 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2380 } else {
2381 additional_cdb_len = 0;
2382 ctx->fcp_cmnd_len = 12 + 16 + 4;
2383 }
2384
2385 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2386 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2387
2388 /* Zero out remaining portion of packet. */
2389 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2390 clr_ptr = (uint32_t *)cmd_pkt + 2;
2391 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2392 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2393
2394 /* Set NPORT-ID and LUN number*/
2395 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2396 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2397 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2398 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002399 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002400
2401 /* Build IOCB segments */
2402 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2403 goto queuing_error_fcp_cmnd;
2404
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002405 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002406 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2407
2408 /* build FCP_CMND IU */
2409 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002410 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002411 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2412
2413 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2414 ctx->fcp_cmnd->additional_cdb_len |= 1;
2415 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2416 ctx->fcp_cmnd->additional_cdb_len |= 2;
2417
2418 /*
2419 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2420 */
2421 if (scsi_populate_tag_msg(cmd, tag)) {
2422 switch (tag[0]) {
2423 case HEAD_OF_QUEUE_TAG:
2424 ctx->fcp_cmnd->task_attribute =
2425 TSK_HEAD_OF_QUEUE;
2426 break;
2427 case ORDERED_QUEUE_TAG:
2428 ctx->fcp_cmnd->task_attribute =
2429 TSK_ORDERED;
2430 break;
2431 }
2432 }
2433
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002434 /* Populate the FCP_PRIO. */
2435 if (ha->flags.fcp_prio_enabled)
2436 ctx->fcp_cmnd->task_attribute |=
2437 sp->fcport->fcp_prio << 3;
2438
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002439 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2440
2441 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2442 additional_cdb_len);
2443 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2444
2445 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2446 cmd_pkt->fcp_cmnd_dseg_address[0] =
2447 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2448 cmd_pkt->fcp_cmnd_dseg_address[1] =
2449 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2450
2451 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2452 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2453 /* Set total data segment count. */
2454 cmd_pkt->entry_count = (uint8_t)req_cnt;
2455 /* Specify response queue number where
2456 * completion should happen
2457 */
2458 cmd_pkt->entry_status = (uint8_t) rsp->id;
2459 } else {
2460 struct cmd_type_7 *cmd_pkt;
2461 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2462 if (req->cnt < (req_cnt + 2)) {
2463 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2464 &reg->req_q_out[0]);
2465 if (req->ring_index < cnt)
2466 req->cnt = cnt - req->ring_index;
2467 else
2468 req->cnt = req->length -
2469 (req->ring_index - cnt);
2470 }
2471 if (req->cnt < (req_cnt + 2))
2472 goto queuing_error;
2473
2474 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2475 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2476
2477 /* Zero out remaining portion of packet. */
2478 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2479 clr_ptr = (uint32_t *)cmd_pkt + 2;
2480 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2481 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2482
2483 /* Set NPORT-ID and LUN number*/
2484 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2485 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2486 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2487 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Joe Carnuccioc6d39e22012-05-15 14:34:20 -04002488 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002489
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002490 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002491 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002492 sizeof(cmd_pkt->lun));
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002493
2494 /*
2495 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2496 */
2497 if (scsi_populate_tag_msg(cmd, tag)) {
2498 switch (tag[0]) {
2499 case HEAD_OF_QUEUE_TAG:
2500 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
2501 break;
2502 case ORDERED_QUEUE_TAG:
2503 cmd_pkt->task = TSK_ORDERED;
2504 break;
2505 }
2506 }
2507
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002508 /* Populate the FCP_PRIO. */
2509 if (ha->flags.fcp_prio_enabled)
2510 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2511
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002512 /* Load SCSI command packet. */
2513 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2514 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2515
2516 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2517
2518 /* Build IOCB segments */
2519 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2520
2521 /* Set total data segment count. */
2522 cmd_pkt->entry_count = (uint8_t)req_cnt;
2523 /* Specify response queue number where
2524 * completion should happen.
2525 */
2526 cmd_pkt->entry_status = (uint8_t) rsp->id;
2527
2528 }
2529 /* Build command packet. */
2530 req->current_outstanding_cmd = handle;
2531 req->outstanding_cmds[handle] = sp;
2532 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002533 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002534 req->cnt -= req_cnt;
2535 wmb();
2536
2537 /* Adjust ring index. */
2538 req->ring_index++;
2539 if (req->ring_index == req->length) {
2540 req->ring_index = 0;
2541 req->ring_ptr = req->ring;
2542 } else
2543 req->ring_ptr++;
2544
2545 sp->flags |= SRB_DMA_VALID;
2546
2547 /* Set chip new ring index. */
2548 /* write, read and verify logic */
2549 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2550 if (ql2xdbwr)
2551 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2552 else {
2553 WRT_REG_DWORD(
2554 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2555 dbval);
2556 wmb();
Saurav Kashyapfa492632012-11-21 02:40:29 -05002557 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002558 WRT_REG_DWORD(
2559 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2560 dbval);
2561 wmb();
2562 }
2563 }
2564
2565 /* Manage unprocessed RIO/ZIO commands in response queue. */
2566 if (vha->flags.process_response_queue &&
2567 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2568 qla24xx_process_response_queue(vha, rsp);
2569
2570 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2571 return QLA_SUCCESS;
2572
2573queuing_error_fcp_cmnd:
2574 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2575queuing_error:
2576 if (tot_dsds)
2577 scsi_dma_unmap(cmd);
2578
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002579 if (sp->u.scmd.ctx) {
2580 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2581 sp->u.scmd.ctx = NULL;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002582 }
2583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2584
2585 return QLA_FUNCTION_FAILED;
2586}
2587
Andrew Vasquezac280b62009-08-20 11:06:05 -07002588int
2589qla2x00_start_sp(srb_t *sp)
2590{
2591 int rval;
2592 struct qla_hw_data *ha = sp->fcport->vha->hw;
2593 void *pkt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002594 unsigned long flags;
2595
2596 rval = QLA_FUNCTION_FAILED;
2597 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05002598 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002599 if (!pkt) {
2600 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2601 "qla2x00_alloc_iocbs failed.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07002602 goto done;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002603 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07002604
2605 rval = QLA_SUCCESS;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002606 switch (sp->type) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07002607 case SRB_LOGIN_CMD:
2608 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002609 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002610 qla2x00_login_iocb(sp, pkt);
2611 break;
2612 case SRB_LOGOUT_CMD:
2613 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002614 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002615 qla2x00_logout_iocb(sp, pkt);
2616 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002617 case SRB_ELS_CMD_RPT:
2618 case SRB_ELS_CMD_HST:
2619 qla24xx_els_iocb(sp, pkt);
2620 break;
2621 case SRB_CT_CMD:
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002622 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez57807902011-11-18 09:03:20 -08002623 qla24xx_ct_iocb(sp, pkt) :
2624 qla2x00_ct_iocb(sp, pkt);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002625 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002626 case SRB_ADISC_CMD:
2627 IS_FWI2_CAPABLE(ha) ?
2628 qla24xx_adisc_iocb(sp, pkt) :
2629 qla2x00_adisc_iocb(sp, pkt);
2630 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002631 case SRB_TM_CMD:
Giridhar Malavali8ae6d9c2013-03-28 08:21:23 -04002632 IS_QLAFX00(ha) ?
2633 qlafx00_tm_iocb(sp, pkt) :
2634 qla24xx_tm_iocb(sp, pkt);
2635 break;
2636 case SRB_FXIOCB_DCMD:
2637 case SRB_FXIOCB_BCMD:
2638 qlafx00_fxdisc_iocb(sp, pkt);
2639 break;
2640 case SRB_ABT_CMD:
2641 qlafx00_abort_iocb(sp, pkt);
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002642 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002643 default:
2644 break;
2645 }
2646
2647 wmb();
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002648 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
Andrew Vasquezac280b62009-08-20 11:06:05 -07002649done:
2650 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2651 return rval;
2652}
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002653
2654static void
2655qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
2656 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
2657{
2658 uint16_t avail_dsds;
2659 uint32_t *cur_dsd;
2660 uint32_t req_data_len = 0;
2661 uint32_t rsp_data_len = 0;
2662 struct scatterlist *sg;
2663 int index;
2664 int entry_count = 1;
2665 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2666
2667 /*Update entry type to indicate bidir command */
2668 *((uint32_t *)(&cmd_pkt->entry_type)) =
2669 __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
2670
2671 /* Set the transfer direction, in this set both flags
2672 * Also set the BD_WRAP_BACK flag, firmware will take care
2673 * assigning DID=SID for outgoing pkts.
2674 */
2675 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2676 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2677 cmd_pkt->control_flags =
2678 __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
2679 BD_WRAP_BACK);
2680
2681 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2682 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
2683 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
2684 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
2685
2686 vha->bidi_stats.transfer_bytes += req_data_len;
2687 vha->bidi_stats.io_count++;
2688
Joe Carnucciofabbb8d2013-08-27 01:37:40 -04002689 vha->qla_stats.output_bytes += req_data_len;
2690 vha->qla_stats.output_requests++;
2691
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002692 /* Only one dsd is available for bidirectional IOCB, remaining dsds
2693 * are bundled in continuation iocb
2694 */
2695 avail_dsds = 1;
2696 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
2697
2698 index = 0;
2699
2700 for_each_sg(bsg_job->request_payload.sg_list, sg,
2701 bsg_job->request_payload.sg_cnt, index) {
2702 dma_addr_t sle_dma;
2703 cont_a64_entry_t *cont_pkt;
2704
2705 /* Allocate additional continuation packets */
2706 if (avail_dsds == 0) {
2707 /* Continuation type 1 IOCB can accomodate
2708 * 5 DSDS
2709 */
2710 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2711 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2712 avail_dsds = 5;
2713 entry_count++;
2714 }
2715 sle_dma = sg_dma_address(sg);
2716 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2717 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2718 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2719 avail_dsds--;
2720 }
2721 /* For read request DSD will always goes to continuation IOCB
2722 * and follow the write DSD. If there is room on the current IOCB
2723 * then it is added to that IOCB else new continuation IOCB is
2724 * allocated.
2725 */
2726 for_each_sg(bsg_job->reply_payload.sg_list, sg,
2727 bsg_job->reply_payload.sg_cnt, index) {
2728 dma_addr_t sle_dma;
2729 cont_a64_entry_t *cont_pkt;
2730
2731 /* Allocate additional continuation packets */
2732 if (avail_dsds == 0) {
2733 /* Continuation type 1 IOCB can accomodate
2734 * 5 DSDS
2735 */
2736 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2737 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2738 avail_dsds = 5;
2739 entry_count++;
2740 }
2741 sle_dma = sg_dma_address(sg);
2742 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2743 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2744 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2745 avail_dsds--;
2746 }
2747 /* This value should be same as number of IOCB required for this cmd */
2748 cmd_pkt->entry_count = entry_count;
2749}
2750
2751int
2752qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
2753{
2754
2755 struct qla_hw_data *ha = vha->hw;
2756 unsigned long flags;
2757 uint32_t handle;
2758 uint32_t index;
2759 uint16_t req_cnt;
2760 uint16_t cnt;
2761 uint32_t *clr_ptr;
2762 struct cmd_bidir *cmd_pkt = NULL;
2763 struct rsp_que *rsp;
2764 struct req_que *req;
2765 int rval = EXT_STATUS_OK;
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002766
2767 rval = QLA_SUCCESS;
2768
2769 rsp = ha->rsp_q_map[0];
2770 req = vha->req;
2771
2772 /* Send marker if required */
2773 if (vha->marker_needed != 0) {
2774 if (qla2x00_marker(vha, req,
2775 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
2776 return EXT_STATUS_MAILBOX;
2777 vha->marker_needed = 0;
2778 }
2779
2780 /* Acquire ring specific lock */
2781 spin_lock_irqsave(&ha->hardware_lock, flags);
2782
2783 /* Check for room in outstanding command list. */
2784 handle = req->current_outstanding_cmd;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002785 for (index = 1; index < req->num_outstanding_cmds; index++) {
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002786 handle++;
Chad Dupuis8d93f552013-01-30 03:34:37 -05002787 if (handle == req->num_outstanding_cmds)
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002788 handle = 1;
2789 if (!req->outstanding_cmds[handle])
2790 break;
2791 }
2792
Chad Dupuis8d93f552013-01-30 03:34:37 -05002793 if (index == req->num_outstanding_cmds) {
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002794 rval = EXT_STATUS_BUSY;
2795 goto queuing_error;
2796 }
2797
2798 /* Calculate number of IOCB required */
2799 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2800
2801 /* Check for room on request queue. */
2802 if (req->cnt < req_cnt + 2) {
Andrew Vasquez7e98df22012-11-21 02:40:33 -05002803 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Saurav Kashyapa9b6f7222012-08-22 14:21:01 -04002804
2805 if (req->ring_index < cnt)
2806 req->cnt = cnt - req->ring_index;
2807 else
2808 req->cnt = req->length -
2809 (req->ring_index - cnt);
2810 }
2811 if (req->cnt < req_cnt + 2) {
2812 rval = EXT_STATUS_BUSY;
2813 goto queuing_error;
2814 }
2815
2816 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
2817 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2818
2819 /* Zero out remaining portion of packet. */
2820 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2821 clr_ptr = (uint32_t *)cmd_pkt + 2;
2822 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2823
2824 /* Set NPORT-ID (of vha)*/
2825 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
2826 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
2827 cmd_pkt->port_id[1] = vha->d_id.b.area;
2828 cmd_pkt->port_id[2] = vha->d_id.b.domain;
2829
2830 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
2831 cmd_pkt->entry_status = (uint8_t) rsp->id;
2832 /* Build command packet. */
2833 req->current_outstanding_cmd = handle;
2834 req->outstanding_cmds[handle] = sp;
2835 sp->handle = handle;
2836 req->cnt -= req_cnt;
2837
2838 /* Send the command to the firmware */
2839 wmb();
2840 qla2x00_start_iocbs(vha, req);
2841queuing_error:
2842 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2843 return rval;
2844}