blob: eac9509244971ba9e324803ca531014e3f917dad [file] [log] [blame]
Andrew Vasquezfa90c542005-10-27 11:10:08 -07001/*
2 * QLogic Fibre Channel HBA Driver
Andrew Vasquez07e264b2011-03-30 11:46:23 -07003 * Copyright (c) 2003-2011 QLogic Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Andrew Vasquezfa90c542005-10-27 11:10:08 -07005 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "qla_def.h"
8
9#include <linux/blkdev.h>
10#include <linux/delay.h>
11
12#include <scsi/scsi_tcq.h>
13
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -070014static void qla25xx_set_que(srb_t *, struct rsp_que **);
Linus Torvalds1da177e2005-04-16 15:20:36 -070015/**
16 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
17 * @cmd: SCSI command
18 *
19 * Returns the proper CF_* direction based on CDB.
20 */
21static inline uint16_t
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070022qla2x00_get_cmd_direction(srb_t *sp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{
24 uint16_t cflags;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080025 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27 cflags = 0;
28
29 /* Set transfer direction */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080030 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 cflags = CF_WRITE;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080032 sp->fcport->vha->hw->qla_stats.output_bytes +=
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080033 scsi_bufflen(cmd);
34 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 cflags = CF_READ;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080036 sp->fcport->vha->hw->qla_stats.input_bytes +=
Giridhar Malavali9ba56b92012-02-09 11:15:36 -080037 scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -070038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return (cflags);
40}
41
42/**
43 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
44 * Continuation Type 0 IOCBs to allocate.
45 *
46 * @dsds: number of data segment decriptors needed
47 *
48 * Returns the number of IOCB entries needed to store @dsds.
49 */
50uint16_t
51qla2x00_calc_iocbs_32(uint16_t dsds)
52{
53 uint16_t iocbs;
54
55 iocbs = 1;
56 if (dsds > 3) {
57 iocbs += (dsds - 3) / 7;
58 if ((dsds - 3) % 7)
59 iocbs++;
60 }
61 return (iocbs);
62}
63
64/**
65 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
66 * Continuation Type 1 IOCBs to allocate.
67 *
68 * @dsds: number of data segment decriptors needed
69 *
70 * Returns the number of IOCB entries needed to store @dsds.
71 */
72uint16_t
73qla2x00_calc_iocbs_64(uint16_t dsds)
74{
75 uint16_t iocbs;
76
77 iocbs = 1;
78 if (dsds > 2) {
79 iocbs += (dsds - 2) / 5;
80 if ((dsds - 2) % 5)
81 iocbs++;
82 }
83 return (iocbs);
84}
85
86/**
87 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
88 * @ha: HA context
89 *
90 * Returns a pointer to the Continuation Type 0 IOCB packet.
91 */
92static inline cont_entry_t *
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070093qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
95 cont_entry_t *cont_pkt;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -070096 struct req_que *req = vha->req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -080098 req->ring_index++;
99 if (req->ring_index == req->length) {
100 req->ring_index = 0;
101 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800103 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800106 cont_pkt = (cont_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Load packet defaults. */
109 *((uint32_t *)(&cont_pkt->entry_type)) =
110 __constant_cpu_to_le32(CONTINUE_TYPE);
111
112 return (cont_pkt);
113}
114
115/**
116 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
117 * @ha: HA context
118 *
119 * Returns a pointer to the continuation type 1 IOCB packet.
120 */
121static inline cont_a64_entry_t *
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800122qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 cont_a64_entry_t *cont_pkt;
125
126 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800127 req->ring_index++;
128 if (req->ring_index == req->length) {
129 req->ring_index = 0;
130 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 } else {
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800132 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800135 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137 /* Load packet defaults. */
138 *((uint32_t *)(&cont_pkt->entry_type)) =
139 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
140
141 return (cont_pkt);
142}
143
Arun Easibad75002010-05-04 15:01:30 -0700144static inline int
145qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
146{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800147 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
148 uint8_t guard = scsi_host_get_guard(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700149
150 /* We only support T10 DIF right now */
151 if (guard != SHOST_DIX_GUARD_CRC) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700152 ql_dbg(ql_dbg_io, sp->fcport->vha, 0x3007,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800153 "Unsupported guard: %d for cmd=%p.\n", guard, cmd);
Arun Easibad75002010-05-04 15:01:30 -0700154 return 0;
155 }
156
157 /* We always use DIFF Bundling for best performance */
158 *fw_prot_opts = 0;
159
160 /* Translate SCSI opcode to a protection opcode */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800161 switch (scsi_get_prot_op(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700162 case SCSI_PROT_READ_STRIP:
163 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
164 break;
165 case SCSI_PROT_WRITE_INSERT:
166 *fw_prot_opts |= PO_MODE_DIF_INSERT;
167 break;
168 case SCSI_PROT_READ_INSERT:
169 *fw_prot_opts |= PO_MODE_DIF_INSERT;
170 break;
171 case SCSI_PROT_WRITE_STRIP:
172 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
173 break;
174 case SCSI_PROT_READ_PASS:
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
176 break;
177 case SCSI_PROT_WRITE_PASS:
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 break;
180 default: /* Normal Request */
181 *fw_prot_opts |= PO_MODE_DIF_PASS;
182 break;
183 }
184
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800185 return scsi_prot_sg_count(cmd);
Arun Easibad75002010-05-04 15:01:30 -0700186}
187
188/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
190 * capable IOCB types.
191 *
192 * @sp: SRB command to process
193 * @cmd_pkt: Command type 2 IOCB
194 * @tot_dsds: Total number of segments to transfer
195 */
196void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
197 uint16_t tot_dsds)
198{
199 uint16_t avail_dsds;
200 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800201 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900203 struct scatterlist *sg;
204 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800206 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /* Update entry type to indicate Command Type 2 IOCB */
209 *((uint32_t *)(&cmd_pkt->entry_type)) =
210 __constant_cpu_to_le32(COMMAND_TYPE);
211
212 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900213 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
215 return;
216 }
217
Andrew Vasquez444786d2009-01-05 11:18:10 -0800218 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700219 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 /* Three DSDs are available in the Command Type 2 IOCB */
222 avail_dsds = 3;
223 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
224
225 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900226 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
227 cont_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900229 /* Allocate additional continuation packets? */
230 if (avail_dsds == 0) {
231 /*
232 * Seven DSDs are available in the Continuation
233 * Type 0 IOCB.
234 */
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700235 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900236 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
237 avail_dsds = 7;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900239
240 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
241 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
242 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244}
245
246/**
247 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
248 * capable IOCB types.
249 *
250 * @sp: SRB command to process
251 * @cmd_pkt: Command type 3 IOCB
252 * @tot_dsds: Total number of segments to transfer
253 */
254void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
255 uint16_t tot_dsds)
256{
257 uint16_t avail_dsds;
258 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800259 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900261 struct scatterlist *sg;
262 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800264 cmd = GET_CMD_SP(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266 /* Update entry type to indicate Command Type 3 IOCB */
267 *((uint32_t *)(&cmd_pkt->entry_type)) =
268 __constant_cpu_to_le32(COMMAND_A64_TYPE);
269
270 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900271 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
273 return;
274 }
275
Andrew Vasquez444786d2009-01-05 11:18:10 -0800276 vha = sp->fcport->vha;
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700277 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Two DSDs are available in the Command Type 3 IOCB */
280 avail_dsds = 2;
281 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
282
283 /* Load data segments */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900284 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
285 dma_addr_t sle_dma;
286 cont_a64_entry_t *cont_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900288 /* Allocate additional continuation packets? */
289 if (avail_dsds == 0) {
290 /*
291 * Five DSDs are available in the Continuation
292 * Type 1 IOCB.
293 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800294 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900295 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
296 avail_dsds = 5;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900298
299 sle_dma = sg_dma_address(sg);
300 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
301 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
302 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
303 avail_dsds--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305}
306
307/**
308 * qla2x00_start_scsi() - Send a SCSI command to the ISP
309 * @sp: command to send to the ISP
310 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700311 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
313int
314qla2x00_start_scsi(srb_t *sp)
315{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900316 int ret, nseg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 unsigned long flags;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800318 scsi_qla_host_t *vha;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct scsi_cmnd *cmd;
320 uint32_t *clr_ptr;
321 uint32_t index;
322 uint32_t handle;
323 cmd_entry_t *cmd_pkt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 uint16_t cnt;
325 uint16_t req_cnt;
326 uint16_t tot_dsds;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700327 struct device_reg_2xxx __iomem *reg;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800328 struct qla_hw_data *ha;
329 struct req_que *req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800330 struct rsp_que *rsp;
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800331 char tag[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Setup device pointers. */
334 ret = 0;
Andrew Vasquez444786d2009-01-05 11:18:10 -0800335 vha = sp->fcport->vha;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800336 ha = vha->hw;
Andrew Vasquez3d716442005-07-06 10:30:26 -0700337 reg = &ha->iobase->isp;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800338 cmd = GET_CMD_SP(sp);
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800339 req = ha->req_q_map[0];
340 rsp = ha->rsp_q_map[0];
83021922005-04-17 15:10:41 -0500341 /* So we know we haven't pci_map'ed anything yet */
342 tot_dsds = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800345 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700346 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
347 QLA_SUCCESS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return (QLA_FUNCTION_FAILED);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700349 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800350 vha->marker_needed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
353 /* Acquire ring specific lock */
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700354 spin_lock_irqsave(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800357 handle = req->current_outstanding_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
359 handle++;
360 if (handle == MAX_OUTSTANDING_COMMANDS)
361 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800362 if (!req->outstanding_cmds[handle])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 break;
364 }
365 if (index == MAX_OUTSTANDING_COMMANDS)
366 goto queuing_error;
367
83021922005-04-17 15:10:41 -0500368 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -0700369 if (scsi_sg_count(cmd)) {
370 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
371 scsi_sg_count(cmd), cmd->sc_data_direction);
372 if (unlikely(!nseg))
373 goto queuing_error;
374 } else
375 nseg = 0;
376
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900377 tot_dsds = nseg;
83021922005-04-17 15:10:41 -0500378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 /* Calculate the number of request entries needed. */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700380 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800381 if (req->cnt < (req_cnt + 2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800383 if (req->ring_index < cnt)
384 req->cnt = cnt - req->ring_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800386 req->cnt = req->length -
387 (req->ring_index - cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800389 if (req->cnt < (req_cnt + 2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 goto queuing_error;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 /* Build command packet */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800393 req->current_outstanding_cmd = handle;
394 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -0700395 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800396 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800397 req->cnt -= req_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800399 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 cmd_pkt->handle = handle;
401 /* Zero out remaining portion of packet. */
402 clr_ptr = (uint32_t *)cmd_pkt + 2;
403 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
404 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
405
bdf79622005-04-17 15:06:53 -0500406 /* Set target ID and LUN number*/
407 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800408 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 /* Update tagged queuing modifier */
Andrew Vasquezff2fc422011-02-23 15:27:15 -0800411 if (scsi_populate_tag_msg(cmd, tag)) {
412 switch (tag[0]) {
413 case HEAD_OF_QUEUE_TAG:
414 cmd_pkt->control_flags =
415 __constant_cpu_to_le16(CF_HEAD_TAG);
416 break;
417 case ORDERED_QUEUE_TAG:
418 cmd_pkt->control_flags =
419 __constant_cpu_to_le16(CF_ORDERED_TAG);
420 break;
421 default:
422 cmd_pkt->control_flags =
423 __constant_cpu_to_le16(CF_SIMPLE_TAG);
424 break;
425 }
426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 /* Load SCSI command packet. */
429 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900430 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /* Build IOCB segments */
Andrew Vasquezfd34f552007-07-19 15:06:00 -0700433 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* Set total data segment count. */
436 cmd_pkt->entry_count = (uint8_t)req_cnt;
437 wmb();
438
439 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800440 req->ring_index++;
441 if (req->ring_index == req->length) {
442 req->ring_index = 0;
443 req->ring_ptr = req->ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800445 req->ring_ptr++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 sp->flags |= SRB_DMA_VALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* Set chip new ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800450 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
452
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700453 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800454 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800455 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
456 qla2x00_process_response_queue(rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -0700457
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700458 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return (QLA_SUCCESS);
460
461queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900462 if (tot_dsds)
463 scsi_dma_unmap(cmd);
464
Andrew Vasquezc9c5ced2008-07-24 08:31:49 -0700465 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 return (QLA_FUNCTION_FAILED);
468}
469
470/**
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800471 * qla2x00_start_iocbs() - Execute the IOCB command
472 */
473static void
474qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
475{
476 struct qla_hw_data *ha = vha->hw;
477 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800478
479 if (IS_QLA82XX(ha)) {
480 qla82xx_start_iocbs(vha);
481 } else {
482 /* Adjust ring index. */
483 req->ring_index++;
484 if (req->ring_index == req->length) {
485 req->ring_index = 0;
486 req->ring_ptr = req->ring;
487 } else
488 req->ring_ptr++;
489
490 /* Set chip new ring index. */
Giridhar Malavali6246b8a2012-02-09 11:15:34 -0800491 if (ha->mqenable || IS_QLA83XX(ha)) {
492 WRT_REG_DWORD(req->req_q_in, req->ring_index);
Arun Easi98878a12012-02-09 11:15:59 -0800493 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800494 } else if (IS_FWI2_CAPABLE(ha)) {
495 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
496 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
497 } else {
498 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
499 req->ring_index);
500 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
501 }
502 }
503}
504
505/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 * qla2x00_marker() - Send a marker IOCB to the firmware.
507 * @ha: HA context
508 * @loop_id: loop ID
509 * @lun: LUN
510 * @type: marker modifier
511 *
512 * Can be called from both normal and interrupt context.
513 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -0700514 * Returns non-zero if a failure occurred, else zero.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 */
Andrew Vasquez3dbe7562010-07-23 15:28:37 +0500516static int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800517__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
518 struct rsp_que *rsp, uint16_t loop_id,
519 uint16_t lun, uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700521 mrk_entry_t *mrk;
522 struct mrk_entry_24xx *mrk24;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800523 struct qla_hw_data *ha = vha->hw;
524 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700526 mrk24 = NULL;
Giridhar Malavali99b82122011-11-18 09:03:17 -0800527 req = ha->req_q_map[0];
Giridhar Malavalid94d10e2010-07-23 15:28:23 +0500528 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, 0);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700529 if (mrk == NULL) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700530 ql_log(ql_log_warn, base_vha, 0x3026,
531 "Failed to allocate Marker IOCB.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 return (QLA_FUNCTION_FAILED);
534 }
535
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700536 mrk->entry_type = MARKER_TYPE;
537 mrk->modifier = type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (type != MK_SYNC_ALL) {
Andrew Vasqueze4289242007-07-19 15:05:56 -0700539 if (IS_FWI2_CAPABLE(ha)) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700540 mrk24 = (struct mrk_entry_24xx *) mrk;
541 mrk24->nport_handle = cpu_to_le16(loop_id);
542 mrk24->lun[1] = LSB(lun);
543 mrk24->lun[2] = MSB(lun);
Shyam Sundarb797b6d2006-08-01 13:48:13 -0700544 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800545 mrk24->vp_index = vha->vp_idx;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -0700546 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700547 } else {
548 SET_TARGET_ID(ha, mrk->target, loop_id);
549 mrk->lun = cpu_to_le16(lun);
550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 wmb();
553
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800554 qla2x00_start_iocbs(vha, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return (QLA_SUCCESS);
557}
558
Andrew Vasquezfa2a1ce2005-07-06 10:32:07 -0700559int
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800560qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
561 struct rsp_que *rsp, uint16_t loop_id, uint16_t lun,
562 uint8_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563{
564 int ret;
565 unsigned long flags = 0;
566
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800567 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
568 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
569 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 return (ret);
572}
573
574/**
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700575 * qla24xx_calc_iocbs() - Determine number of Command Type 3 and
576 * Continuation Type 1 IOCBs to allocate.
577 *
578 * @dsds: number of data segment decriptors needed
579 *
580 * Returns the number of IOCB entries needed to store @dsds.
581 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700582inline uint16_t
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700583qla24xx_calc_iocbs(scsi_qla_host_t *vha, uint16_t dsds)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700584{
585 uint16_t iocbs;
586
587 iocbs = 1;
588 if (dsds > 1) {
589 iocbs += (dsds - 1) / 5;
590 if ((dsds - 1) % 5)
591 iocbs++;
592 }
593 return iocbs;
594}
595
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800596static inline int
597qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
598 uint16_t tot_dsds)
599{
600 uint32_t *cur_dsd = NULL;
601 scsi_qla_host_t *vha;
602 struct qla_hw_data *ha;
603 struct scsi_cmnd *cmd;
604 struct scatterlist *cur_seg;
605 uint32_t *dsd_seg;
606 void *next_dsd;
607 uint8_t avail_dsds;
608 uint8_t first_iocb = 1;
609 uint32_t dsd_list_len;
610 struct dsd_dma *dsd_ptr;
611 struct ct6_dsd *ctx;
612
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800613 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800614
615 /* Update entry type to indicate Command Type 3 IOCB */
616 *((uint32_t *)(&cmd_pkt->entry_type)) =
617 __constant_cpu_to_le32(COMMAND_TYPE_6);
618
619 /* No data transfer */
620 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
621 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
622 return 0;
623 }
624
625 vha = sp->fcport->vha;
626 ha = vha->hw;
627
628 /* Set transfer direction */
629 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
630 cmd_pkt->control_flags =
631 __constant_cpu_to_le16(CF_WRITE_DATA);
632 ha->qla_stats.output_bytes += scsi_bufflen(cmd);
633 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
634 cmd_pkt->control_flags =
635 __constant_cpu_to_le16(CF_READ_DATA);
636 ha->qla_stats.input_bytes += scsi_bufflen(cmd);
637 }
638
639 cur_seg = scsi_sglist(cmd);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800640 ctx = GET_CMD_CTX_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -0800641
642 while (tot_dsds) {
643 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
644 QLA_DSDS_PER_IOCB : tot_dsds;
645 tot_dsds -= avail_dsds;
646 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
647
648 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
649 struct dsd_dma, list);
650 next_dsd = dsd_ptr->dsd_addr;
651 list_del(&dsd_ptr->list);
652 ha->gbl_dsd_avail--;
653 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
654 ctx->dsd_use_cnt++;
655 ha->gbl_dsd_inuse++;
656
657 if (first_iocb) {
658 first_iocb = 0;
659 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
660 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
661 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
662 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
663 } else {
664 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
665 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
666 *cur_dsd++ = cpu_to_le32(dsd_list_len);
667 }
668 cur_dsd = (uint32_t *)next_dsd;
669 while (avail_dsds) {
670 dma_addr_t sle_dma;
671
672 sle_dma = sg_dma_address(cur_seg);
673 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
674 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
675 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
676 cur_seg = sg_next(cur_seg);
677 avail_dsds--;
678 }
679 }
680
681 /* Null termination */
682 *cur_dsd++ = 0;
683 *cur_dsd++ = 0;
684 *cur_dsd++ = 0;
685 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
686 return 0;
687}
688
689/*
690 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
691 * for Command Type 6.
692 *
693 * @dsds: number of data segment decriptors needed
694 *
695 * Returns the number of dsd list needed to store @dsds.
696 */
697inline uint16_t
698qla24xx_calc_dsd_lists(uint16_t dsds)
699{
700 uint16_t dsd_lists = 0;
701
702 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
703 if (dsds % QLA_DSDS_PER_IOCB)
704 dsd_lists++;
705 return dsd_lists;
706}
707
708
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700709/**
710 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
711 * IOCB types.
712 *
713 * @sp: SRB command to process
714 * @cmd_pkt: Command type 3 IOCB
715 * @tot_dsds: Total number of segments to transfer
716 */
Giridhar Malavalia9083012010-04-12 17:59:55 -0700717inline void
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700718qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
719 uint16_t tot_dsds)
720{
721 uint16_t avail_dsds;
722 uint32_t *cur_dsd;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800723 scsi_qla_host_t *vha;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700724 struct scsi_cmnd *cmd;
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900725 struct scatterlist *sg;
726 int i;
Anirban Chakraborty73208df2008-12-09 16:45:39 -0800727 struct req_que *req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700728
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800729 cmd = GET_CMD_SP(sp);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700730
731 /* Update entry type to indicate Command Type 3 IOCB */
732 *((uint32_t *)(&cmd_pkt->entry_type)) =
733 __constant_cpu_to_le32(COMMAND_TYPE_7);
734
735 /* No data transfer */
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900736 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700737 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
738 return;
739 }
740
Andrew Vasquez444786d2009-01-05 11:18:10 -0800741 vha = sp->fcport->vha;
Anirban Chakraborty67c2e932009-04-06 22:33:42 -0700742 req = vha->req;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700743
744 /* Set transfer direction */
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700745 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700746 cmd_pkt->task_mgmt_flags =
747 __constant_cpu_to_le16(TMF_WRITE_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800748 sp->fcport->vha->hw->qla_stats.output_bytes +=
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800749 scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700750 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700751 cmd_pkt->task_mgmt_flags =
752 __constant_cpu_to_le16(TMF_READ_DATA);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -0800753 sp->fcport->vha->hw->qla_stats.input_bytes +=
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800754 scsi_bufflen(cmd);
Harish Zunjarrao49fd4622008-09-11 21:22:47 -0700755 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700756
757 /* One DSD is available in the Command Type 3 IOCB */
758 avail_dsds = 1;
759 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
760
761 /* Load data segments */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700762
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900763 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
764 dma_addr_t sle_dma;
765 cont_a64_entry_t *cont_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700766
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900767 /* Allocate additional continuation packets? */
768 if (avail_dsds == 0) {
769 /*
770 * Five DSDs are available in the Continuation
771 * Type 1 IOCB.
772 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -0800773 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900774 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
775 avail_dsds = 5;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700776 }
FUJITA Tomonori385d70b2007-05-26 01:55:38 +0900777
778 sle_dma = sg_dma_address(sg);
779 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
780 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
781 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
782 avail_dsds--;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -0700783 }
784}
785
Arun Easibad75002010-05-04 15:01:30 -0700786struct fw_dif_context {
787 uint32_t ref_tag;
788 uint16_t app_tag;
789 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
790 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
791};
792
793/*
794 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
795 *
796 */
797static inline void
Arun Easie02587d2011-08-16 11:29:23 -0700798qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
Arun Easibad75002010-05-04 15:01:30 -0700799 unsigned int protcnt)
800{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -0800801 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700802 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -0700803
804 switch (scsi_get_prot_type(cmd)) {
Arun Easibad75002010-05-04 15:01:30 -0700805 case SCSI_PROT_DIF_TYPE0:
Arun Easi8cb20492011-08-16 11:29:22 -0700806 /*
807 * No check for ql2xenablehba_err_chk, as it would be an
808 * I/O error if hba tag generation is not done.
809 */
810 pkt->ref_tag = cpu_to_le32((uint32_t)
811 (0xffffffff & scsi_get_lba(cmd)));
Arun Easie02587d2011-08-16 11:29:23 -0700812
813 if (!qla2x00_hba_err_chk_enabled(sp))
814 break;
815
Arun Easi8cb20492011-08-16 11:29:22 -0700816 pkt->ref_tag_mask[0] = 0xff;
817 pkt->ref_tag_mask[1] = 0xff;
818 pkt->ref_tag_mask[2] = 0xff;
819 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700820 break;
821
822 /*
823 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
824 * match LBA in CDB + N
825 */
826 case SCSI_PROT_DIF_TYPE2:
Arun Easie02587d2011-08-16 11:29:23 -0700827 pkt->app_tag = __constant_cpu_to_le16(0);
828 pkt->app_tag_mask[0] = 0x0;
829 pkt->app_tag_mask[1] = 0x0;
Arun Easi0c470872010-07-23 15:28:38 +0500830
831 pkt->ref_tag = cpu_to_le32((uint32_t)
832 (0xffffffff & scsi_get_lba(cmd)));
833
Arun Easie02587d2011-08-16 11:29:23 -0700834 if (!qla2x00_hba_err_chk_enabled(sp))
835 break;
836
Arun Easi0c470872010-07-23 15:28:38 +0500837 /* enable ALL bytes of the ref tag */
838 pkt->ref_tag_mask[0] = 0xff;
839 pkt->ref_tag_mask[1] = 0xff;
840 pkt->ref_tag_mask[2] = 0xff;
841 pkt->ref_tag_mask[3] = 0xff;
Arun Easibad75002010-05-04 15:01:30 -0700842 break;
843
844 /* For Type 3 protection: 16 bit GUARD only */
845 case SCSI_PROT_DIF_TYPE3:
846 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
847 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
848 0x00;
849 break;
850
851 /*
852 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
853 * 16 bit app tag.
854 */
855 case SCSI_PROT_DIF_TYPE1:
Arun Easie02587d2011-08-16 11:29:23 -0700856 pkt->ref_tag = cpu_to_le32((uint32_t)
857 (0xffffffff & scsi_get_lba(cmd)));
858 pkt->app_tag = __constant_cpu_to_le16(0);
859 pkt->app_tag_mask[0] = 0x0;
860 pkt->app_tag_mask[1] = 0x0;
861
862 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -0700863 break;
864
Arun Easibad75002010-05-04 15:01:30 -0700865 /* enable ALL bytes of the ref tag */
866 pkt->ref_tag_mask[0] = 0xff;
867 pkt->ref_tag_mask[1] = 0xff;
868 pkt->ref_tag_mask[2] = 0xff;
869 pkt->ref_tag_mask[3] = 0xff;
870 break;
871 }
872
Saurav Kashyap7c3df132011-07-14 12:00:13 -0700873 ql_dbg(ql_dbg_io, vha, 0x3009,
874 "Setting protection Tags: (BIG) ref tag = 0x%x, app tag = 0x%x, "
875 "prot SG count %d, cmd lba 0x%x, prot_type=%u cmd=%p.\n",
876 pkt->ref_tag, pkt->app_tag, protcnt, (int)scsi_get_lba(cmd),
877 scsi_get_prot_type(cmd), cmd);
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);
1049 scsi_qla_host_t *vha = shost_priv(cmd->device->host);
Arun Easibad75002010-05-04 15:01:30 -07001050
1051 uint8_t *cp;
1052
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001053 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
Arun Easibad75002010-05-04 15:01:30 -07001054 dma_addr_t sle_dma;
1055
1056 /* Allocate additional continuation packets? */
1057 if (avail_dsds == 0) {
1058 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1059 QLA_DSDS_PER_IOCB : used_dsds;
1060 dsd_list_len = (avail_dsds + 1) * 12;
1061 used_dsds -= avail_dsds;
1062
1063 /* allocate tracking DS */
1064 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1065 if (!dsd_ptr)
1066 return 1;
1067
1068 /* allocate new list */
1069 dsd_ptr->dsd_addr = next_dsd =
1070 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1071 &dsd_ptr->dsd_list_dma);
1072
1073 if (!next_dsd) {
1074 /*
1075 * Need to cleanup only this dsd_ptr, rest
1076 * will be done by sp_free_dma()
1077 */
1078 kfree(dsd_ptr);
1079 return 1;
1080 }
1081
1082 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001083 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001084
1085 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1086
1087 /* add new list to cmd iocb or last list */
1088 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1089 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1090 *cur_dsd++ = dsd_list_len;
1091 cur_dsd = (uint32_t *)next_dsd;
1092 }
1093 sle_dma = sg_dma_address(sg);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001094 ql_dbg(ql_dbg_io, vha, 0x300a,
1095 "sg entry %d - addr=0x%x 0x%x, " "len=%d for cmd=%p.\n",
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001096 i, LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg), cmd);
Arun Easibad75002010-05-04 15:01:30 -07001097 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1098 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1099 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1100 avail_dsds--;
1101
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001102 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Arun Easibad75002010-05-04 15:01:30 -07001103 cp = page_address(sg_page(sg)) + sg->offset;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001104 ql_dbg(ql_dbg_io, vha, 0x300b,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001105 "User data buffer=%p for cmd=%p.\n", cp, cmd);
Arun Easibad75002010-05-04 15:01:30 -07001106 }
1107 }
1108 /* Null termination */
1109 *cur_dsd++ = 0;
1110 *cur_dsd++ = 0;
1111 *cur_dsd++ = 0;
1112 return 0;
1113}
1114
1115static int
1116qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
1117 uint32_t *dsd,
1118 uint16_t tot_dsds)
1119{
1120 void *next_dsd;
1121 uint8_t avail_dsds = 0;
1122 uint32_t dsd_list_len;
1123 struct dsd_dma *dsd_ptr;
1124 struct scatterlist *sg;
1125 int i;
1126 struct scsi_cmnd *cmd;
1127 uint32_t *cur_dsd = dsd;
1128 uint16_t used_dsds = tot_dsds;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001129 scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
Arun Easibad75002010-05-04 15:01:30 -07001130 uint8_t *cp;
1131
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001132 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001133 scsi_for_each_prot_sg(cmd, sg, tot_dsds, i) {
1134 dma_addr_t sle_dma;
1135
1136 /* Allocate additional continuation packets? */
1137 if (avail_dsds == 0) {
1138 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1139 QLA_DSDS_PER_IOCB : used_dsds;
1140 dsd_list_len = (avail_dsds + 1) * 12;
1141 used_dsds -= avail_dsds;
1142
1143 /* allocate tracking DS */
1144 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1145 if (!dsd_ptr)
1146 return 1;
1147
1148 /* allocate new list */
1149 dsd_ptr->dsd_addr = next_dsd =
1150 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1151 &dsd_ptr->dsd_list_dma);
1152
1153 if (!next_dsd) {
1154 /*
1155 * Need to cleanup only this dsd_ptr, rest
1156 * will be done by sp_free_dma()
1157 */
1158 kfree(dsd_ptr);
1159 return 1;
1160 }
1161
1162 list_add_tail(&dsd_ptr->list,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001163 &((struct crc_context *)sp->u.scmd.ctx)->dsd_list);
Arun Easibad75002010-05-04 15:01:30 -07001164
1165 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1166
1167 /* add new list to cmd iocb or last list */
1168 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1169 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1170 *cur_dsd++ = dsd_list_len;
1171 cur_dsd = (uint32_t *)next_dsd;
1172 }
1173 sle_dma = sg_dma_address(sg);
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001174 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001175 ql_dbg(ql_dbg_io, vha, 0x3027,
1176 "%s(): %p, sg_entry %d - "
1177 "addr=0x%x0x%x, len=%d.\n",
1178 __func__, cur_dsd, i,
1179 LSD(sle_dma), MSD(sle_dma), sg_dma_len(sg));
Arun Easibad75002010-05-04 15:01:30 -07001180 }
1181 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1182 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1183 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1184
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001185 if (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_PASS) {
Arun Easibad75002010-05-04 15:01:30 -07001186 cp = page_address(sg_page(sg)) + sg->offset;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001187 ql_dbg(ql_dbg_io, vha, 0x3028,
1188 "%s(): Protection Data buffer = %p.\n", __func__,
1189 cp);
Arun Easibad75002010-05-04 15:01:30 -07001190 }
1191 avail_dsds--;
1192 }
1193 /* Null termination */
1194 *cur_dsd++ = 0;
1195 *cur_dsd++ = 0;
1196 *cur_dsd++ = 0;
1197 return 0;
1198}
1199
1200/**
1201 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1202 * Type 6 IOCB types.
1203 *
1204 * @sp: SRB command to process
1205 * @cmd_pkt: Command type 3 IOCB
1206 * @tot_dsds: Total number of segments to transfer
1207 */
1208static inline int
1209qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1210 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1211{
1212 uint32_t *cur_dsd, *fcp_dl;
1213 scsi_qla_host_t *vha;
1214 struct scsi_cmnd *cmd;
1215 struct scatterlist *cur_seg;
1216 int sgc;
Arun Easi8cb20492011-08-16 11:29:22 -07001217 uint32_t total_bytes = 0;
Arun Easibad75002010-05-04 15:01:30 -07001218 uint32_t data_bytes;
1219 uint32_t dif_bytes;
1220 uint8_t bundling = 1;
1221 uint16_t blk_size;
1222 uint8_t *clr_ptr;
1223 struct crc_context *crc_ctx_pkt = NULL;
1224 struct qla_hw_data *ha;
1225 uint8_t additional_fcpcdb_len;
1226 uint16_t fcp_cmnd_len;
1227 struct fcp_cmnd *fcp_cmnd;
1228 dma_addr_t crc_ctx_dma;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001229 char tag[2];
Arun Easibad75002010-05-04 15:01:30 -07001230
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001231 cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001232
1233 sgc = 0;
1234 /* Update entry type to indicate Command Type CRC_2 IOCB */
1235 *((uint32_t *)(&cmd_pkt->entry_type)) =
1236 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1237
Arun Easibad75002010-05-04 15:01:30 -07001238 vha = sp->fcport->vha;
1239 ha = vha->hw;
1240
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001241 /* No data transfer */
1242 data_bytes = scsi_bufflen(cmd);
1243 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
1244 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1245 return QLA_SUCCESS;
1246 }
Arun Easibad75002010-05-04 15:01:30 -07001247
1248 cmd_pkt->vp_index = sp->fcport->vp_idx;
1249
1250 /* Set transfer direction */
1251 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1252 cmd_pkt->control_flags =
1253 __constant_cpu_to_le16(CF_WRITE_DATA);
1254 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1255 cmd_pkt->control_flags =
1256 __constant_cpu_to_le16(CF_READ_DATA);
1257 }
1258
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001259 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1260 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1261 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1262 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
Arun Easibad75002010-05-04 15:01:30 -07001263 bundling = 0;
1264
1265 /* Allocate CRC context from global pool */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001266 crc_ctx_pkt = sp->u.scmd.ctx =
1267 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
Arun Easibad75002010-05-04 15:01:30 -07001268
1269 if (!crc_ctx_pkt)
1270 goto crc_queuing_error;
1271
1272 /* Zero out CTX area. */
1273 clr_ptr = (uint8_t *)crc_ctx_pkt;
1274 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1275
1276 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1277
1278 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1279
1280 /* Set handle */
1281 crc_ctx_pkt->handle = cmd_pkt->handle;
1282
1283 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1284
Arun Easie02587d2011-08-16 11:29:23 -07001285 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
Arun Easibad75002010-05-04 15:01:30 -07001286 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1287
1288 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1289 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1290 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1291
1292 /* Determine SCSI command length -- align to 4 byte boundary */
1293 if (cmd->cmd_len > 16) {
Arun Easibad75002010-05-04 15:01:30 -07001294 additional_fcpcdb_len = cmd->cmd_len - 16;
1295 if ((cmd->cmd_len % 4) != 0) {
1296 /* SCSI cmd > 16 bytes must be multiple of 4 */
1297 goto crc_queuing_error;
1298 }
1299 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1300 } else {
1301 additional_fcpcdb_len = 0;
1302 fcp_cmnd_len = 12 + 16 + 4;
1303 }
1304
1305 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1306
1307 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1308 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1309 fcp_cmnd->additional_cdb_len |= 1;
1310 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1311 fcp_cmnd->additional_cdb_len |= 2;
1312
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001313 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
Arun Easibad75002010-05-04 15:01:30 -07001314 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1315 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1316 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1317 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1318 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1319 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
Uwe Kleine-König65155b32010-06-11 12:17:01 +02001320 fcp_cmnd->task_management = 0;
Arun Easibad75002010-05-04 15:01:30 -07001321
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001322 /*
1323 * Update tagged queuing modifier if using command tag queuing
1324 */
1325 if (scsi_populate_tag_msg(cmd, tag)) {
1326 switch (tag[0]) {
1327 case HEAD_OF_QUEUE_TAG:
1328 fcp_cmnd->task_attribute = TSK_HEAD_OF_QUEUE;
1329 break;
1330 case ORDERED_QUEUE_TAG:
1331 fcp_cmnd->task_attribute = TSK_ORDERED;
1332 break;
1333 default:
1334 fcp_cmnd->task_attribute = 0;
1335 break;
1336 }
1337 } else {
1338 fcp_cmnd->task_attribute = 0;
1339 }
1340
Arun Easibad75002010-05-04 15:01:30 -07001341 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1342
Arun Easibad75002010-05-04 15:01:30 -07001343 /* Compute dif len and adjust data len to incude protection */
Arun Easibad75002010-05-04 15:01:30 -07001344 dif_bytes = 0;
1345 blk_size = cmd->device->sector_size;
Arun Easi8cb20492011-08-16 11:29:22 -07001346 dif_bytes = (data_bytes / blk_size) * 8;
1347
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001348 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
Arun Easi8cb20492011-08-16 11:29:22 -07001349 case SCSI_PROT_READ_INSERT:
1350 case SCSI_PROT_WRITE_STRIP:
1351 total_bytes = data_bytes;
1352 data_bytes += dif_bytes;
1353 break;
1354
1355 case SCSI_PROT_READ_STRIP:
1356 case SCSI_PROT_WRITE_INSERT:
1357 case SCSI_PROT_READ_PASS:
1358 case SCSI_PROT_WRITE_PASS:
1359 total_bytes = data_bytes + dif_bytes;
1360 break;
1361 default:
1362 BUG();
Arun Easibad75002010-05-04 15:01:30 -07001363 }
1364
Arun Easie02587d2011-08-16 11:29:23 -07001365 if (!qla2x00_hba_err_chk_enabled(sp))
Arun Easibad75002010-05-04 15:01:30 -07001366 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
1367
1368 if (!bundling) {
1369 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1370 } else {
1371 /*
1372 * Configure Bundling if we need to fetch interlaving
1373 * protection PCI accesses
1374 */
1375 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1376 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1377 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1378 tot_prot_dsds);
1379 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1380 }
1381
1382 /* Finish the common fields of CRC pkt */
1383 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1384 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1385 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1386 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1387 /* Fibre channel byte count */
1388 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1389 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1390 additional_fcpcdb_len);
1391 *fcp_dl = htonl(total_bytes);
1392
Arun Easi0c470872010-07-23 15:28:38 +05001393 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
Arun Easi0c470872010-07-23 15:28:38 +05001394 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1395 return QLA_SUCCESS;
1396 }
Arun Easibad75002010-05-04 15:01:30 -07001397 /* Walks data segments */
1398
1399 cmd_pkt->control_flags |=
1400 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
Arun Easi8cb20492011-08-16 11:29:22 -07001401
1402 if (!bundling && tot_prot_dsds) {
1403 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
1404 cur_dsd, tot_dsds))
1405 goto crc_queuing_error;
1406 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
Arun Easibad75002010-05-04 15:01:30 -07001407 (tot_dsds - tot_prot_dsds)))
1408 goto crc_queuing_error;
1409
1410 if (bundling && tot_prot_dsds) {
1411 /* Walks dif segments */
1412 cur_seg = scsi_prot_sglist(cmd);
1413 cmd_pkt->control_flags |=
1414 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1415 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1416 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
1417 tot_prot_dsds))
1418 goto crc_queuing_error;
1419 }
1420 return QLA_SUCCESS;
1421
1422crc_queuing_error:
Arun Easibad75002010-05-04 15:01:30 -07001423 /* Cleanup will be performed by the caller */
1424
1425 return QLA_FUNCTION_FAILED;
1426}
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001427
1428/**
1429 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1430 * @sp: command to send to the ISP
1431 *
Bjorn Helgaascc3ef7b2008-09-11 21:22:51 -07001432 * Returns non-zero if a failure occurred, else zero.
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001433 */
1434int
1435qla24xx_start_scsi(srb_t *sp)
1436{
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001437 int ret, nseg;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001438 unsigned long flags;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001439 uint32_t *clr_ptr;
1440 uint32_t index;
1441 uint32_t handle;
1442 struct cmd_type_7 *cmd_pkt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001443 uint16_t cnt;
1444 uint16_t req_cnt;
1445 uint16_t tot_dsds;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001446 struct req_que *req = NULL;
1447 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001448 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Andrew Vasquez444786d2009-01-05 11:18:10 -08001449 struct scsi_qla_host *vha = sp->fcport->vha;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001450 struct qla_hw_data *ha = vha->hw;
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001451 char tag[2];
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001452
1453 /* Setup device pointers. */
1454 ret = 0;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001455
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001456 qla25xx_set_que(sp, &rsp);
1457 req = vha->req;
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001458
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001459 /* So we know we haven't pci_map'ed anything yet */
1460 tot_dsds = 0;
1461
1462 /* Send marker if required */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001463 if (vha->marker_needed != 0) {
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001464 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1465 QLA_SUCCESS)
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001466 return QLA_FUNCTION_FAILED;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001467 vha->marker_needed = 0;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001468 }
1469
1470 /* Acquire ring specific lock */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001471 spin_lock_irqsave(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001472
1473 /* Check for room in outstanding command list. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001474 handle = req->current_outstanding_cmd;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001475 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1476 handle++;
1477 if (handle == MAX_OUTSTANDING_COMMANDS)
1478 handle = 1;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001479 if (!req->outstanding_cmds[handle])
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001480 break;
1481 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001482 if (index == MAX_OUTSTANDING_COMMANDS) {
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001483 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001484 }
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001485
1486 /* Map the sg table so we have an accurate count of sg entries needed */
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001487 if (scsi_sg_count(cmd)) {
1488 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1489 scsi_sg_count(cmd), cmd->sc_data_direction);
1490 if (unlikely(!nseg))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001491 goto queuing_error;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001492 } else
1493 nseg = 0;
1494
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001495 tot_dsds = nseg;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001496 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001497 if (req->cnt < (req_cnt + 2)) {
Andrew Vasquez08029992009-03-24 09:07:55 -07001498 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001499
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001500 if (req->ring_index < cnt)
1501 req->cnt = cnt - req->ring_index;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001502 else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001503 req->cnt = req->length -
1504 (req->ring_index - cnt);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001505 }
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001506 if (req->cnt < (req_cnt + 2))
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001507 goto queuing_error;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001508
1509 /* Build command packet. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001510 req->current_outstanding_cmd = handle;
1511 req->outstanding_cmds[handle] = sp;
Andrew Vasquezcf53b062009-08-20 11:06:04 -07001512 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001513 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001514 req->cnt -= req_cnt;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001515
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001516 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001517 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001518
1519 /* Zero out remaining portion of packet. */
James Bottomley72df8322005-10-28 14:41:19 -05001520 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001521 clr_ptr = (uint32_t *)cmd_pkt + 2;
1522 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1523 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1524
1525 /* Set NPORT-ID and LUN number*/
1526 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1527 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1528 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1529 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
Seokmann Ju2c3dfe32007-07-05 13:16:51 -07001530 cmd_pkt->vp_index = sp->fcport->vp_idx;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001531
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001532 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
andrew.vasquez@qlogic.com0d4be122006-02-07 08:45:35 -08001533 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001534
Andrew Vasquezff2fc422011-02-23 15:27:15 -08001535 /* Update tagged queuing modifier -- default is TSK_SIMPLE (0). */
1536 if (scsi_populate_tag_msg(cmd, tag)) {
1537 switch (tag[0]) {
1538 case HEAD_OF_QUEUE_TAG:
1539 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
1540 break;
1541 case ORDERED_QUEUE_TAG:
1542 cmd_pkt->task = TSK_ORDERED;
1543 break;
1544 }
1545 }
1546
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001547 /* Load SCSI command packet. */
1548 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1549 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1550
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001551 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001552
1553 /* Build IOCB segments */
1554 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1555
1556 /* Set total data segment count. */
1557 cmd_pkt->entry_count = (uint8_t)req_cnt;
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001558 /* Specify response queue number where completion should happen */
1559 cmd_pkt->entry_status = (uint8_t) rsp->id;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001560 wmb();
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001561 /* Adjust ring index. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001562 req->ring_index++;
1563 if (req->ring_index == req->length) {
1564 req->ring_index = 0;
1565 req->ring_ptr = req->ring;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001566 } else
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001567 req->ring_ptr++;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001568
1569 sp->flags |= SRB_DMA_VALID;
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001570
1571 /* Set chip new ring index. */
Andrew Vasquez08029992009-03-24 09:07:55 -07001572 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1573 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001574
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001575 /* Manage unprocessed RIO/ZIO commands in response queue. */
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001576 if (vha->flags.process_response_queue &&
Anirban Chakraborty73208df2008-12-09 16:45:39 -08001577 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
Anirban Chakraborty2afa19a2009-04-06 22:33:40 -07001578 qla24xx_process_response_queue(vha, rsp);
Andrew Vasquez4fdfefe2005-10-27 11:09:48 -07001579
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001580 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001581 return QLA_SUCCESS;
1582
1583queuing_error:
FUJITA Tomonori385d70b2007-05-26 01:55:38 +09001584 if (tot_dsds)
1585 scsi_dma_unmap(cmd);
1586
Anirban Chakrabortye315cd22008-11-06 10:40:51 -08001587 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Andrew Vasquez2b6c0ce2005-07-06 10:31:17 -07001588
1589 return QLA_FUNCTION_FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001591
Arun Easibad75002010-05-04 15:01:30 -07001592
1593/**
1594 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1595 * @sp: command to send to the ISP
1596 *
1597 * Returns non-zero if a failure occurred, else zero.
1598 */
1599int
1600qla24xx_dif_start_scsi(srb_t *sp)
1601{
1602 int nseg;
1603 unsigned long flags;
1604 uint32_t *clr_ptr;
1605 uint32_t index;
1606 uint32_t handle;
1607 uint16_t cnt;
1608 uint16_t req_cnt = 0;
1609 uint16_t tot_dsds;
1610 uint16_t tot_prot_dsds;
1611 uint16_t fw_prot_opts = 0;
1612 struct req_que *req = NULL;
1613 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001614 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Arun Easibad75002010-05-04 15:01:30 -07001615 struct scsi_qla_host *vha = sp->fcport->vha;
1616 struct qla_hw_data *ha = vha->hw;
1617 struct cmd_type_crc_2 *cmd_pkt;
1618 uint32_t status = 0;
1619
1620#define QDSS_GOT_Q_SPACE BIT_0
1621
Arun Easi0c470872010-07-23 15:28:38 +05001622 /* Only process protection or >16 cdb in this routine */
1623 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1624 if (cmd->cmd_len <= 16)
1625 return qla24xx_start_scsi(sp);
1626 }
Arun Easibad75002010-05-04 15:01:30 -07001627
1628 /* Setup device pointers. */
1629
1630 qla25xx_set_que(sp, &rsp);
1631 req = vha->req;
1632
1633 /* So we know we haven't pci_map'ed anything yet */
1634 tot_dsds = 0;
1635
1636 /* Send marker if required */
1637 if (vha->marker_needed != 0) {
1638 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1639 QLA_SUCCESS)
1640 return QLA_FUNCTION_FAILED;
1641 vha->marker_needed = 0;
1642 }
1643
1644 /* Acquire ring specific lock */
1645 spin_lock_irqsave(&ha->hardware_lock, flags);
1646
1647 /* Check for room in outstanding command list. */
1648 handle = req->current_outstanding_cmd;
1649 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1650 handle++;
1651 if (handle == MAX_OUTSTANDING_COMMANDS)
1652 handle = 1;
1653 if (!req->outstanding_cmds[handle])
1654 break;
1655 }
1656
1657 if (index == MAX_OUTSTANDING_COMMANDS)
1658 goto queuing_error;
1659
1660 /* Compute number of required data segments */
1661 /* Map the sg table so we have an accurate count of sg entries needed */
1662 if (scsi_sg_count(cmd)) {
1663 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1664 scsi_sg_count(cmd), cmd->sc_data_direction);
1665 if (unlikely(!nseg))
1666 goto queuing_error;
1667 else
1668 sp->flags |= SRB_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001669
1670 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1671 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1672 struct qla2_sgx sgx;
1673 uint32_t partial;
1674
1675 memset(&sgx, 0, sizeof(struct qla2_sgx));
1676 sgx.tot_bytes = scsi_bufflen(cmd);
1677 sgx.cur_sg = scsi_sglist(cmd);
1678 sgx.sp = sp;
1679
1680 nseg = 0;
1681 while (qla24xx_get_one_block_sg(
1682 cmd->device->sector_size, &sgx, &partial))
1683 nseg++;
1684 }
Arun Easibad75002010-05-04 15:01:30 -07001685 } else
1686 nseg = 0;
1687
1688 /* number of required data segments */
1689 tot_dsds = nseg;
1690
1691 /* Compute number of required protection segments */
1692 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1693 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1694 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1695 if (unlikely(!nseg))
1696 goto queuing_error;
1697 else
1698 sp->flags |= SRB_CRC_PROT_DMA_VALID;
Arun Easi8cb20492011-08-16 11:29:22 -07001699
1700 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1701 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1702 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1703 }
Arun Easibad75002010-05-04 15:01:30 -07001704 } else {
1705 nseg = 0;
1706 }
1707
1708 req_cnt = 1;
1709 /* Total Data and protection sg segment(s) */
1710 tot_prot_dsds = nseg;
1711 tot_dsds += nseg;
1712 if (req->cnt < (req_cnt + 2)) {
1713 cnt = RD_REG_DWORD_RELAXED(req->req_q_out);
1714
1715 if (req->ring_index < cnt)
1716 req->cnt = cnt - req->ring_index;
1717 else
1718 req->cnt = req->length -
1719 (req->ring_index - cnt);
1720 }
1721
1722 if (req->cnt < (req_cnt + 2))
1723 goto queuing_error;
1724
1725 status |= QDSS_GOT_Q_SPACE;
1726
1727 /* Build header part of command packet (excluding the OPCODE). */
1728 req->current_outstanding_cmd = handle;
1729 req->outstanding_cmds[handle] = sp;
Arun Easi8cb20492011-08-16 11:29:22 -07001730 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001731 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Arun Easibad75002010-05-04 15:01:30 -07001732 req->cnt -= req_cnt;
1733
1734 /* Fill-in common area */
1735 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1736 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1737
1738 clr_ptr = (uint32_t *)cmd_pkt + 2;
1739 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1740
1741 /* Set NPORT-ID and LUN number*/
1742 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1743 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1744 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1745 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1746
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001747 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Arun Easibad75002010-05-04 15:01:30 -07001748 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1749
1750 /* Total Data and protection segment(s) */
1751 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1752
1753 /* Build IOCB segments and adjust for data protection segments */
1754 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1755 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1756 QLA_SUCCESS)
1757 goto queuing_error;
1758
1759 cmd_pkt->entry_count = (uint8_t)req_cnt;
1760 /* Specify response queue number where completion should happen */
1761 cmd_pkt->entry_status = (uint8_t) rsp->id;
1762 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1763 wmb();
1764
1765 /* Adjust ring index. */
1766 req->ring_index++;
1767 if (req->ring_index == req->length) {
1768 req->ring_index = 0;
1769 req->ring_ptr = req->ring;
1770 } else
1771 req->ring_ptr++;
1772
1773 /* Set chip new ring index. */
1774 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1775 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1776
1777 /* Manage unprocessed RIO/ZIO commands in response queue. */
1778 if (vha->flags.process_response_queue &&
1779 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1780 qla24xx_process_response_queue(vha, rsp);
1781
1782 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1783
1784 return QLA_SUCCESS;
1785
1786queuing_error:
1787 if (status & QDSS_GOT_Q_SPACE) {
1788 req->outstanding_cmds[handle] = NULL;
1789 req->cnt += req_cnt;
1790 }
1791 /* Cleanup will be performed by the caller (queuecommand) */
1792
1793 spin_unlock_irqrestore(&ha->hardware_lock, flags);
Arun Easibad75002010-05-04 15:01:30 -07001794 return QLA_FUNCTION_FAILED;
1795}
1796
1797
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001798static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001799{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001800 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001801 struct qla_hw_data *ha = sp->fcport->vha->hw;
1802 int affinity = cmd->request->cpu;
1803
Anirban Chakraborty7163ea82009-08-05 09:18:40 -07001804 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001805 affinity < ha->max_rsp_queues - 1)
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001806 *rsp = ha->rsp_q_map[affinity + 1];
Anirban Chakraborty59e0b8b2009-06-03 09:55:19 -07001807 else
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001808 *rsp = ha->rsp_q_map[0];
Anirban Chakraborty68ca9492009-04-06 22:33:41 -07001809}
Andrew Vasquezac280b62009-08-20 11:06:05 -07001810
1811/* Generic Control-SRB manipulation functions. */
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001812void *
1813qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001814{
Andrew Vasquezac280b62009-08-20 11:06:05 -07001815 struct qla_hw_data *ha = vha->hw;
1816 struct req_que *req = ha->req_q_map[0];
1817 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1818 uint32_t index, handle;
1819 request_t *pkt;
1820 uint16_t cnt, req_cnt;
1821
1822 pkt = NULL;
1823 req_cnt = 1;
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001824 handle = 0;
1825
1826 if (!sp)
1827 goto skip_cmd_array;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001828
1829 /* Check for room in outstanding command list. */
1830 handle = req->current_outstanding_cmd;
1831 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
1832 handle++;
1833 if (handle == MAX_OUTSTANDING_COMMANDS)
1834 handle = 1;
1835 if (!req->outstanding_cmds[handle])
1836 break;
1837 }
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001838 if (index == MAX_OUTSTANDING_COMMANDS) {
1839 ql_log(ql_log_warn, vha, 0x700b,
1840 "No room on oustanding cmd array.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07001841 goto queuing_error;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07001842 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07001843
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001844 /* Prep command array. */
1845 req->current_outstanding_cmd = handle;
1846 req->outstanding_cmds[handle] = sp;
1847 sp->handle = handle;
1848
Andrew Vasquez57807902011-11-18 09:03:20 -08001849 /* Adjust entry-counts as needed. */
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001850 if (sp->type != SRB_SCSI_CMD)
1851 req_cnt = sp->iocbs;
Andrew Vasquez57807902011-11-18 09:03:20 -08001852
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001853skip_cmd_array:
Andrew Vasquezac280b62009-08-20 11:06:05 -07001854 /* Check for room on request queue. */
1855 if (req->cnt < req_cnt) {
Giridhar Malavali6246b8a2012-02-09 11:15:34 -08001856 if (ha->mqenable || IS_QLA83XX(ha))
Andrew Vasquezac280b62009-08-20 11:06:05 -07001857 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05001858 else if (IS_QLA82XX(ha))
1859 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
Andrew Vasquezac280b62009-08-20 11:06:05 -07001860 else if (IS_FWI2_CAPABLE(ha))
1861 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
1862 else
1863 cnt = qla2x00_debounce_register(
1864 ISP_REQ_Q_OUT(ha, &reg->isp));
1865
1866 if (req->ring_index < cnt)
1867 req->cnt = cnt - req->ring_index;
1868 else
1869 req->cnt = req->length -
1870 (req->ring_index - cnt);
1871 }
1872 if (req->cnt < req_cnt)
1873 goto queuing_error;
1874
1875 /* Prep packet */
Andrew Vasquezac280b62009-08-20 11:06:05 -07001876 req->cnt -= req_cnt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001877 pkt = req->ring_ptr;
1878 memset(pkt, 0, REQUEST_ENTRY_SIZE);
1879 pkt->entry_count = req_cnt;
1880 pkt->handle = handle;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001881
1882queuing_error:
1883 return pkt;
1884}
1885
1886static void
Andrew Vasquezac280b62009-08-20 11:06:05 -07001887qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1888{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001889 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001890
1891 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1892 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001893 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001894 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001895 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
Andrew Vasquezac280b62009-08-20 11:06:05 -07001896 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1897 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1898 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1899 logio->port_id[1] = sp->fcport->d_id.b.area;
1900 logio->port_id[2] = sp->fcport->d_id.b.domain;
1901 logio->vp_index = sp->fcport->vp_idx;
1902}
1903
1904static void
1905qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1906{
1907 struct qla_hw_data *ha = sp->fcport->vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001908 struct srb_iocb *lio = &sp->u.iocb_cmd;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001909 uint16_t opts;
1910
Giridhar Malavalib9637522010-05-28 15:08:15 -07001911 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001912 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1913 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
Madhuranath Iyengar49163922010-05-04 15:01:28 -07001914 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1915 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001916 if (HAS_EXTENDED_IDS(ha)) {
1917 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1918 mbx->mb10 = cpu_to_le16(opts);
1919 } else {
1920 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1921 }
1922 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1923 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1924 sp->fcport->d_id.b.al_pa);
1925 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1926}
1927
1928static void
1929qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1930{
1931 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1932 logio->control_flags =
1933 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1934 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1935 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1936 logio->port_id[1] = sp->fcport->d_id.b.area;
1937 logio->port_id[2] = sp->fcport->d_id.b.domain;
1938 logio->vp_index = sp->fcport->vp_idx;
1939}
1940
1941static void
1942qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1943{
1944 struct qla_hw_data *ha = sp->fcport->vha->hw;
1945
Giridhar Malavalib9637522010-05-28 15:08:15 -07001946 mbx->entry_type = MBX_IOCB_TYPE;
Andrew Vasquezac280b62009-08-20 11:06:05 -07001947 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1948 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1949 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1950 cpu_to_le16(sp->fcport->loop_id):
1951 cpu_to_le16(sp->fcport->loop_id << 8);
1952 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1953 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1954 sp->fcport->d_id.b.al_pa);
1955 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1956 /* Implicit: mbx->mbx10 = 0. */
1957}
1958
Giridhar Malavali9a069e12010-01-12 13:02:47 -08001959static void
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07001960qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1961{
1962 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1963 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1964 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1965 logio->vp_index = sp->fcport->vp_idx;
1966}
1967
1968static void
1969qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1970{
1971 struct qla_hw_data *ha = sp->fcport->vha->hw;
1972
1973 mbx->entry_type = MBX_IOCB_TYPE;
1974 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1975 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1976 if (HAS_EXTENDED_IDS(ha)) {
1977 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1978 mbx->mb10 = cpu_to_le16(BIT_0);
1979 } else {
1980 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1981 }
1982 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1983 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1984 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1985 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
1986 mbx->mb9 = cpu_to_le16(sp->fcport->vp_idx);
1987}
1988
1989static void
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001990qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
1991{
1992 uint32_t flags;
1993 unsigned int lun;
1994 struct fc_port *fcport = sp->fcport;
1995 scsi_qla_host_t *vha = fcport->vha;
1996 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08001997 struct srb_iocb *iocb = &sp->u.iocb_cmd;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07001998 struct req_que *req = vha->req;
1999
2000 flags = iocb->u.tmf.flags;
2001 lun = iocb->u.tmf.lun;
2002
2003 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2004 tsk->entry_count = 1;
2005 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2006 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2007 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2008 tsk->control_flags = cpu_to_le32(flags);
2009 tsk->port_id[0] = fcport->d_id.b.al_pa;
2010 tsk->port_id[1] = fcport->d_id.b.area;
2011 tsk->port_id[2] = fcport->d_id.b.domain;
2012 tsk->vp_index = fcport->vp_idx;
2013
2014 if (flags == TCF_LUN_RESET) {
2015 int_to_scsilun(lun, &tsk->lun);
2016 host_to_fcp_swap((uint8_t *)&tsk->lun,
2017 sizeof(tsk->lun));
2018 }
2019}
2020
2021static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002022qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2023{
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002024 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002025
2026 els_iocb->entry_type = ELS_IOCB_TYPE;
2027 els_iocb->entry_count = 1;
2028 els_iocb->sys_define = 0;
2029 els_iocb->entry_status = 0;
2030 els_iocb->handle = sp->handle;
2031 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2032 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2033 els_iocb->vp_index = sp->fcport->vp_idx;
2034 els_iocb->sof_type = EST_SOFI3;
2035 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2036
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002037 els_iocb->opcode =
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002038 sp->type == SRB_ELS_CMD_RPT ?
Madhuranath Iyengar49163922010-05-04 15:01:28 -07002039 bsg_job->request->rqst_data.r_els.els_code :
2040 bsg_job->request->rqst_data.h_els.command_code;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002041 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2042 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2043 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2044 els_iocb->control_flags = 0;
2045 els_iocb->rx_byte_count =
2046 cpu_to_le32(bsg_job->reply_payload.payload_len);
2047 els_iocb->tx_byte_count =
2048 cpu_to_le32(bsg_job->request_payload.payload_len);
2049
2050 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2051 (bsg_job->request_payload.sg_list)));
2052 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2053 (bsg_job->request_payload.sg_list)));
2054 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2055 (bsg_job->request_payload.sg_list));
2056
2057 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2058 (bsg_job->reply_payload.sg_list)));
2059 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2060 (bsg_job->reply_payload.sg_list)));
2061 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2062 (bsg_job->reply_payload.sg_list));
2063}
2064
2065static void
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002066qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2067{
2068 uint16_t avail_dsds;
2069 uint32_t *cur_dsd;
2070 struct scatterlist *sg;
2071 int index;
2072 uint16_t tot_dsds;
2073 scsi_qla_host_t *vha = sp->fcport->vha;
2074 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002075 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002076 int loop_iterartion = 0;
2077 int cont_iocb_prsnt = 0;
2078 int entry_count = 1;
2079
2080 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2081 ct_iocb->entry_type = CT_IOCB_TYPE;
2082 ct_iocb->entry_status = 0;
2083 ct_iocb->handle1 = sp->handle;
2084 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2085 ct_iocb->status = __constant_cpu_to_le16(0);
2086 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2087 ct_iocb->timeout = 0;
2088 ct_iocb->cmd_dsd_count =
2089 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2090 ct_iocb->total_dsd_count =
2091 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2092 ct_iocb->req_bytecount =
2093 cpu_to_le32(bsg_job->request_payload.payload_len);
2094 ct_iocb->rsp_bytecount =
2095 cpu_to_le32(bsg_job->reply_payload.payload_len);
2096
2097 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2098 (bsg_job->request_payload.sg_list)));
2099 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2100 (bsg_job->request_payload.sg_list)));
2101 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2102
2103 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2104 (bsg_job->reply_payload.sg_list)));
2105 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2106 (bsg_job->reply_payload.sg_list)));
2107 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2108
2109 avail_dsds = 1;
2110 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2111 index = 0;
2112 tot_dsds = bsg_job->reply_payload.sg_cnt;
2113
2114 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2115 dma_addr_t sle_dma;
2116 cont_a64_entry_t *cont_pkt;
2117
2118 /* Allocate additional continuation packets? */
2119 if (avail_dsds == 0) {
2120 /*
2121 * Five DSDs are available in the Cont.
2122 * Type 1 IOCB.
2123 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002124 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2125 vha->hw->req_q_map[0]);
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002126 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2127 avail_dsds = 5;
2128 cont_iocb_prsnt = 1;
2129 entry_count++;
2130 }
2131
2132 sle_dma = sg_dma_address(sg);
2133 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2134 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2135 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2136 loop_iterartion++;
2137 avail_dsds--;
2138 }
2139 ct_iocb->entry_count = entry_count;
2140}
2141
2142static void
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002143qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2144{
2145 uint16_t avail_dsds;
2146 uint32_t *cur_dsd;
2147 struct scatterlist *sg;
2148 int index;
2149 uint16_t tot_dsds;
2150 scsi_qla_host_t *vha = sp->fcport->vha;
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002151 struct qla_hw_data *ha = vha->hw;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002152 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002153 int loop_iterartion = 0;
2154 int cont_iocb_prsnt = 0;
2155 int entry_count = 1;
2156
2157 ct_iocb->entry_type = CT_IOCB_TYPE;
2158 ct_iocb->entry_status = 0;
2159 ct_iocb->sys_define = 0;
2160 ct_iocb->handle = sp->handle;
2161
2162 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2163 ct_iocb->vp_index = sp->fcport->vp_idx;
2164 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2165
2166 ct_iocb->cmd_dsd_count =
2167 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2168 ct_iocb->timeout = 0;
2169 ct_iocb->rsp_dsd_count =
2170 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2171 ct_iocb->rsp_byte_count =
2172 cpu_to_le32(bsg_job->reply_payload.payload_len);
2173 ct_iocb->cmd_byte_count =
2174 cpu_to_le32(bsg_job->request_payload.payload_len);
2175 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2176 (bsg_job->request_payload.sg_list)));
2177 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2178 (bsg_job->request_payload.sg_list)));
2179 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2180 (bsg_job->request_payload.sg_list));
2181
2182 avail_dsds = 1;
2183 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2184 index = 0;
2185 tot_dsds = bsg_job->reply_payload.sg_cnt;
2186
2187 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2188 dma_addr_t sle_dma;
2189 cont_a64_entry_t *cont_pkt;
2190
2191 /* Allocate additional continuation packets? */
2192 if (avail_dsds == 0) {
2193 /*
2194 * Five DSDs are available in the Cont.
2195 * Type 1 IOCB.
2196 */
Giridhar Malavali0d2aa382011-11-18 09:02:21 -08002197 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2198 ha->req_q_map[0]);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002199 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2200 avail_dsds = 5;
2201 cont_iocb_prsnt = 1;
2202 entry_count++;
2203 }
2204
2205 sle_dma = sg_dma_address(sg);
2206 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2207 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2208 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2209 loop_iterartion++;
2210 avail_dsds--;
2211 }
2212 ct_iocb->entry_count = entry_count;
2213}
2214
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002215/*
2216 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2217 * @sp: command to send to the ISP
2218 *
2219 * Returns non-zero if a failure occurred, else zero.
2220 */
2221int
2222qla82xx_start_scsi(srb_t *sp)
2223{
2224 int ret, nseg;
2225 unsigned long flags;
2226 struct scsi_cmnd *cmd;
2227 uint32_t *clr_ptr;
2228 uint32_t index;
2229 uint32_t handle;
2230 uint16_t cnt;
2231 uint16_t req_cnt;
2232 uint16_t tot_dsds;
2233 struct device_reg_82xx __iomem *reg;
2234 uint32_t dbval;
2235 uint32_t *fcp_dl;
2236 uint8_t additional_cdb_len;
2237 struct ct6_dsd *ctx;
2238 struct scsi_qla_host *vha = sp->fcport->vha;
2239 struct qla_hw_data *ha = vha->hw;
2240 struct req_que *req = NULL;
2241 struct rsp_que *rsp = NULL;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002242 char tag[2];
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002243
2244 /* Setup device pointers. */
2245 ret = 0;
2246 reg = &ha->iobase->isp82;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002247 cmd = GET_CMD_SP(sp);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002248 req = vha->req;
2249 rsp = ha->rsp_q_map[0];
2250
2251 /* So we know we haven't pci_map'ed anything yet */
2252 tot_dsds = 0;
2253
2254 dbval = 0x04 | (ha->portnum << 5);
2255
2256 /* Send marker if required */
2257 if (vha->marker_needed != 0) {
2258 if (qla2x00_marker(vha, req,
2259 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2260 ql_log(ql_log_warn, vha, 0x300c,
2261 "qla2x00_marker failed for cmd=%p.\n", cmd);
2262 return QLA_FUNCTION_FAILED;
2263 }
2264 vha->marker_needed = 0;
2265 }
2266
2267 /* Acquire ring specific lock */
2268 spin_lock_irqsave(&ha->hardware_lock, flags);
2269
2270 /* Check for room in outstanding command list. */
2271 handle = req->current_outstanding_cmd;
2272 for (index = 1; index < MAX_OUTSTANDING_COMMANDS; index++) {
2273 handle++;
2274 if (handle == MAX_OUTSTANDING_COMMANDS)
2275 handle = 1;
2276 if (!req->outstanding_cmds[handle])
2277 break;
2278 }
2279 if (index == MAX_OUTSTANDING_COMMANDS)
2280 goto queuing_error;
2281
2282 /* Map the sg table so we have an accurate count of sg entries needed */
2283 if (scsi_sg_count(cmd)) {
2284 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2285 scsi_sg_count(cmd), cmd->sc_data_direction);
2286 if (unlikely(!nseg))
2287 goto queuing_error;
2288 } else
2289 nseg = 0;
2290
2291 tot_dsds = nseg;
2292
2293 if (tot_dsds > ql2xshiftctondsd) {
2294 struct cmd_type_6 *cmd_pkt;
2295 uint16_t more_dsd_lists = 0;
2296 struct dsd_dma *dsd_ptr;
2297 uint16_t i;
2298
2299 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2300 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2301 ql_dbg(ql_dbg_io, vha, 0x300d,
2302 "Num of DSD list %d is than %d for cmd=%p.\n",
2303 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2304 cmd);
2305 goto queuing_error;
2306 }
2307
2308 if (more_dsd_lists <= ha->gbl_dsd_avail)
2309 goto sufficient_dsds;
2310 else
2311 more_dsd_lists -= ha->gbl_dsd_avail;
2312
2313 for (i = 0; i < more_dsd_lists; i++) {
2314 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2315 if (!dsd_ptr) {
2316 ql_log(ql_log_fatal, vha, 0x300e,
2317 "Failed to allocate memory for dsd_dma "
2318 "for cmd=%p.\n", cmd);
2319 goto queuing_error;
2320 }
2321
2322 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2323 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2324 if (!dsd_ptr->dsd_addr) {
2325 kfree(dsd_ptr);
2326 ql_log(ql_log_fatal, vha, 0x300f,
2327 "Failed to allocate memory for dsd_addr "
2328 "for cmd=%p.\n", cmd);
2329 goto queuing_error;
2330 }
2331 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2332 ha->gbl_dsd_avail++;
2333 }
2334
2335sufficient_dsds:
2336 req_cnt = 1;
2337
2338 if (req->cnt < (req_cnt + 2)) {
2339 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2340 &reg->req_q_out[0]);
2341 if (req->ring_index < cnt)
2342 req->cnt = cnt - req->ring_index;
2343 else
2344 req->cnt = req->length -
2345 (req->ring_index - cnt);
2346 }
2347
2348 if (req->cnt < (req_cnt + 2))
2349 goto queuing_error;
2350
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002351 ctx = sp->u.scmd.ctx =
2352 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2353 if (!ctx) {
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002354 ql_log(ql_log_fatal, vha, 0x3010,
2355 "Failed to allocate ctx for cmd=%p.\n", cmd);
2356 goto queuing_error;
2357 }
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002358
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002359 memset(ctx, 0, sizeof(struct ct6_dsd));
2360 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2361 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2362 if (!ctx->fcp_cmnd) {
2363 ql_log(ql_log_fatal, vha, 0x3011,
2364 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
2365 goto queuing_error_fcp_cmnd;
2366 }
2367
2368 /* Initialize the DSD list and dma handle */
2369 INIT_LIST_HEAD(&ctx->dsd_list);
2370 ctx->dsd_use_cnt = 0;
2371
2372 if (cmd->cmd_len > 16) {
2373 additional_cdb_len = cmd->cmd_len - 16;
2374 if ((cmd->cmd_len % 4) != 0) {
2375 /* SCSI command bigger than 16 bytes must be
2376 * multiple of 4
2377 */
2378 ql_log(ql_log_warn, vha, 0x3012,
2379 "scsi cmd len %d not multiple of 4 "
2380 "for cmd=%p.\n", cmd->cmd_len, cmd);
2381 goto queuing_error_fcp_cmnd;
2382 }
2383 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2384 } else {
2385 additional_cdb_len = 0;
2386 ctx->fcp_cmnd_len = 12 + 16 + 4;
2387 }
2388
2389 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2390 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2391
2392 /* Zero out remaining portion of packet. */
2393 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2394 clr_ptr = (uint32_t *)cmd_pkt + 2;
2395 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2396 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2397
2398 /* Set NPORT-ID and LUN number*/
2399 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2400 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2401 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2402 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2403 cmd_pkt->vp_index = sp->fcport->vp_idx;
2404
2405 /* Build IOCB segments */
2406 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2407 goto queuing_error_fcp_cmnd;
2408
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002409 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002410 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2411
2412 /* build FCP_CMND IU */
2413 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002414 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002415 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2416
2417 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2418 ctx->fcp_cmnd->additional_cdb_len |= 1;
2419 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2420 ctx->fcp_cmnd->additional_cdb_len |= 2;
2421
2422 /*
2423 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2424 */
2425 if (scsi_populate_tag_msg(cmd, tag)) {
2426 switch (tag[0]) {
2427 case HEAD_OF_QUEUE_TAG:
2428 ctx->fcp_cmnd->task_attribute =
2429 TSK_HEAD_OF_QUEUE;
2430 break;
2431 case ORDERED_QUEUE_TAG:
2432 ctx->fcp_cmnd->task_attribute =
2433 TSK_ORDERED;
2434 break;
2435 }
2436 }
2437
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002438 /* Populate the FCP_PRIO. */
2439 if (ha->flags.fcp_prio_enabled)
2440 ctx->fcp_cmnd->task_attribute |=
2441 sp->fcport->fcp_prio << 3;
2442
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002443 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2444
2445 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2446 additional_cdb_len);
2447 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2448
2449 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2450 cmd_pkt->fcp_cmnd_dseg_address[0] =
2451 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2452 cmd_pkt->fcp_cmnd_dseg_address[1] =
2453 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2454
2455 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2456 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2457 /* Set total data segment count. */
2458 cmd_pkt->entry_count = (uint8_t)req_cnt;
2459 /* Specify response queue number where
2460 * completion should happen
2461 */
2462 cmd_pkt->entry_status = (uint8_t) rsp->id;
2463 } else {
2464 struct cmd_type_7 *cmd_pkt;
2465 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2466 if (req->cnt < (req_cnt + 2)) {
2467 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2468 &reg->req_q_out[0]);
2469 if (req->ring_index < cnt)
2470 req->cnt = cnt - req->ring_index;
2471 else
2472 req->cnt = req->length -
2473 (req->ring_index - cnt);
2474 }
2475 if (req->cnt < (req_cnt + 2))
2476 goto queuing_error;
2477
2478 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2479 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2480
2481 /* Zero out remaining portion of packet. */
2482 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2483 clr_ptr = (uint32_t *)cmd_pkt + 2;
2484 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2485 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2486
2487 /* Set NPORT-ID and LUN number*/
2488 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2489 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2490 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2491 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
2492 cmd_pkt->vp_index = sp->fcport->vp_idx;
2493
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002494 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002495 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002496 sizeof(cmd_pkt->lun));
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002497
2498 /*
2499 * Update tagged queuing modifier -- default is TSK_SIMPLE (0).
2500 */
2501 if (scsi_populate_tag_msg(cmd, tag)) {
2502 switch (tag[0]) {
2503 case HEAD_OF_QUEUE_TAG:
2504 cmd_pkt->task = TSK_HEAD_OF_QUEUE;
2505 break;
2506 case ORDERED_QUEUE_TAG:
2507 cmd_pkt->task = TSK_ORDERED;
2508 break;
2509 }
2510 }
2511
Saurav Kashyapa00f6292011-11-18 09:03:19 -08002512 /* Populate the FCP_PRIO. */
2513 if (ha->flags.fcp_prio_enabled)
2514 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2515
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002516 /* Load SCSI command packet. */
2517 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2518 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2519
2520 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2521
2522 /* Build IOCB segments */
2523 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2524
2525 /* Set total data segment count. */
2526 cmd_pkt->entry_count = (uint8_t)req_cnt;
2527 /* Specify response queue number where
2528 * completion should happen.
2529 */
2530 cmd_pkt->entry_status = (uint8_t) rsp->id;
2531
2532 }
2533 /* Build command packet. */
2534 req->current_outstanding_cmd = handle;
2535 req->outstanding_cmds[handle] = sp;
2536 sp->handle = handle;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002537 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002538 req->cnt -= req_cnt;
2539 wmb();
2540
2541 /* Adjust ring index. */
2542 req->ring_index++;
2543 if (req->ring_index == req->length) {
2544 req->ring_index = 0;
2545 req->ring_ptr = req->ring;
2546 } else
2547 req->ring_ptr++;
2548
2549 sp->flags |= SRB_DMA_VALID;
2550
2551 /* Set chip new ring index. */
2552 /* write, read and verify logic */
2553 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2554 if (ql2xdbwr)
2555 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2556 else {
2557 WRT_REG_DWORD(
2558 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2559 dbval);
2560 wmb();
2561 while (RD_REG_DWORD(ha->nxdb_rd_ptr) != dbval) {
2562 WRT_REG_DWORD(
2563 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2564 dbval);
2565 wmb();
2566 }
2567 }
2568
2569 /* Manage unprocessed RIO/ZIO commands in response queue. */
2570 if (vha->flags.process_response_queue &&
2571 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2572 qla24xx_process_response_queue(vha, rsp);
2573
2574 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2575 return QLA_SUCCESS;
2576
2577queuing_error_fcp_cmnd:
2578 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2579queuing_error:
2580 if (tot_dsds)
2581 scsi_dma_unmap(cmd);
2582
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002583 if (sp->u.scmd.ctx) {
2584 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2585 sp->u.scmd.ctx = NULL;
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002586 }
2587 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2588
2589 return QLA_FUNCTION_FAILED;
2590}
2591
Andrew Vasquezac280b62009-08-20 11:06:05 -07002592int
2593qla2x00_start_sp(srb_t *sp)
2594{
2595 int rval;
2596 struct qla_hw_data *ha = sp->fcport->vha->hw;
2597 void *pkt;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002598 unsigned long flags;
2599
2600 rval = QLA_FUNCTION_FAILED;
2601 spin_lock_irqsave(&ha->hardware_lock, flags);
Giridhar Malavalid94d10e2010-07-23 15:28:23 +05002602 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002603 if (!pkt) {
2604 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2605 "qla2x00_alloc_iocbs failed.\n");
Andrew Vasquezac280b62009-08-20 11:06:05 -07002606 goto done;
Saurav Kashyap7c3df132011-07-14 12:00:13 -07002607 }
Andrew Vasquezac280b62009-08-20 11:06:05 -07002608
2609 rval = QLA_SUCCESS;
Giridhar Malavali9ba56b92012-02-09 11:15:36 -08002610 switch (sp->type) {
Andrew Vasquezac280b62009-08-20 11:06:05 -07002611 case SRB_LOGIN_CMD:
2612 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002613 qla24xx_login_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002614 qla2x00_login_iocb(sp, pkt);
2615 break;
2616 case SRB_LOGOUT_CMD:
2617 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002618 qla24xx_logout_iocb(sp, pkt) :
Andrew Vasquezac280b62009-08-20 11:06:05 -07002619 qla2x00_logout_iocb(sp, pkt);
2620 break;
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002621 case SRB_ELS_CMD_RPT:
2622 case SRB_ELS_CMD_HST:
2623 qla24xx_els_iocb(sp, pkt);
2624 break;
2625 case SRB_CT_CMD:
Harish Zunjarrao9bc4f4f2010-07-23 15:28:32 +05002626 IS_FWI2_CAPABLE(ha) ?
Andrew Vasquez57807902011-11-18 09:03:20 -08002627 qla24xx_ct_iocb(sp, pkt) :
2628 qla2x00_ct_iocb(sp, pkt);
Giridhar Malavali9a069e12010-01-12 13:02:47 -08002629 break;
Andrew Vasquez5ff1d582010-05-04 15:01:26 -07002630 case SRB_ADISC_CMD:
2631 IS_FWI2_CAPABLE(ha) ?
2632 qla24xx_adisc_iocb(sp, pkt) :
2633 qla2x00_adisc_iocb(sp, pkt);
2634 break;
Madhuranath Iyengar38222632010-05-04 15:01:29 -07002635 case SRB_TM_CMD:
2636 qla24xx_tm_iocb(sp, pkt);
2637 break;
Andrew Vasquezac280b62009-08-20 11:06:05 -07002638 default:
2639 break;
2640 }
2641
2642 wmb();
Giridhar Malavali5162cf02011-11-18 09:03:18 -08002643 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
Andrew Vasquezac280b62009-08-20 11:06:05 -07002644done:
2645 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2646 return rval;
2647}