]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/scsi/qla2xxx/qla_iocb.c
qla2xxx: cleanup cmd in qla workqueue before processing TMR
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / qla2xxx / qla_iocb.c
CommitLineData
fa90c54f
AV
1/*
2 * QLogic Fibre Channel HBA Driver
bd21eaf9 3 * Copyright (c) 2003-2014 QLogic Corporation
1da177e4 4 *
fa90c54f
AV
5 * See LICENSE.qla2xxx for copyright and licensing details.
6 */
1da177e4 7#include "qla_def.h"
2d70c103 8#include "qla_target.h"
1da177e4
LT
9
10#include <linux/blkdev.h>
11#include <linux/delay.h>
12
13#include <scsi/scsi_tcq.h>
14
59e0b8b0 15static void qla25xx_set_que(srb_t *, struct rsp_que **);
1da177e4
LT
16/**
17 * qla2x00_get_cmd_direction() - Determine control_flag data direction.
18 * @cmd: SCSI command
19 *
20 * Returns the proper CF_* direction based on CDB.
21 */
22static inline uint16_t
49fd462a 23qla2x00_get_cmd_direction(srb_t *sp)
1da177e4
LT
24{
25 uint16_t cflags;
9ba56b95 26 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
2be21fa2 27 struct scsi_qla_host *vha = sp->fcport->vha;
1da177e4
LT
28
29 cflags = 0;
30
31 /* Set transfer direction */
9ba56b95 32 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4 33 cflags = CF_WRITE;
2be21fa2 34 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 35 vha->qla_stats.output_requests++;
9ba56b95 36 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1da177e4 37 cflags = CF_READ;
2be21fa2 38 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 39 vha->qla_stats.input_requests++;
49fd462a 40 }
1da177e4
LT
41 return (cflags);
42}
43
44/**
45 * qla2x00_calc_iocbs_32() - Determine number of Command Type 2 and
46 * Continuation Type 0 IOCBs to allocate.
47 *
48 * @dsds: number of data segment decriptors needed
49 *
50 * Returns the number of IOCB entries needed to store @dsds.
51 */
52uint16_t
53qla2x00_calc_iocbs_32(uint16_t dsds)
54{
55 uint16_t iocbs;
56
57 iocbs = 1;
58 if (dsds > 3) {
59 iocbs += (dsds - 3) / 7;
60 if ((dsds - 3) % 7)
61 iocbs++;
62 }
63 return (iocbs);
64}
65
66/**
67 * qla2x00_calc_iocbs_64() - Determine number of Command Type 3 and
68 * Continuation Type 1 IOCBs to allocate.
69 *
70 * @dsds: number of data segment decriptors needed
71 *
72 * Returns the number of IOCB entries needed to store @dsds.
73 */
74uint16_t
75qla2x00_calc_iocbs_64(uint16_t dsds)
76{
77 uint16_t iocbs;
78
79 iocbs = 1;
80 if (dsds > 2) {
81 iocbs += (dsds - 2) / 5;
82 if ((dsds - 2) % 5)
83 iocbs++;
84 }
85 return (iocbs);
86}
87
88/**
89 * qla2x00_prep_cont_type0_iocb() - Initialize a Continuation Type 0 IOCB.
90 * @ha: HA context
91 *
92 * Returns a pointer to the Continuation Type 0 IOCB packet.
93 */
94static inline cont_entry_t *
67c2e93a 95qla2x00_prep_cont_type0_iocb(struct scsi_qla_host *vha)
1da177e4
LT
96{
97 cont_entry_t *cont_pkt;
67c2e93a 98 struct req_que *req = vha->req;
1da177e4 99 /* Adjust ring index. */
e315cd28
AC
100 req->ring_index++;
101 if (req->ring_index == req->length) {
102 req->ring_index = 0;
103 req->ring_ptr = req->ring;
1da177e4 104 } else {
e315cd28 105 req->ring_ptr++;
1da177e4
LT
106 }
107
e315cd28 108 cont_pkt = (cont_entry_t *)req->ring_ptr;
1da177e4
LT
109
110 /* Load packet defaults. */
111 *((uint32_t *)(&cont_pkt->entry_type)) =
112 __constant_cpu_to_le32(CONTINUE_TYPE);
113
114 return (cont_pkt);
115}
116
117/**
118 * qla2x00_prep_cont_type1_iocb() - Initialize a Continuation Type 1 IOCB.
119 * @ha: HA context
120 *
121 * Returns a pointer to the continuation type 1 IOCB packet.
122 */
123static inline cont_a64_entry_t *
0d2aa38e 124qla2x00_prep_cont_type1_iocb(scsi_qla_host_t *vha, struct req_que *req)
1da177e4
LT
125{
126 cont_a64_entry_t *cont_pkt;
127
128 /* Adjust ring index. */
e315cd28
AC
129 req->ring_index++;
130 if (req->ring_index == req->length) {
131 req->ring_index = 0;
132 req->ring_ptr = req->ring;
1da177e4 133 } else {
e315cd28 134 req->ring_ptr++;
1da177e4
LT
135 }
136
e315cd28 137 cont_pkt = (cont_a64_entry_t *)req->ring_ptr;
1da177e4
LT
138
139 /* Load packet defaults. */
8ae6d9c7
GM
140 *((uint32_t *)(&cont_pkt->entry_type)) = IS_QLAFX00(vha->hw) ?
141 __constant_cpu_to_le32(CONTINUE_A64_TYPE_FX00) :
1da177e4
LT
142 __constant_cpu_to_le32(CONTINUE_A64_TYPE);
143
144 return (cont_pkt);
145}
146
bad75002
AE
147static inline int
148qla24xx_configure_prot_mode(srb_t *sp, uint16_t *fw_prot_opts)
149{
9ba56b95
GM
150 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
151 uint8_t guard = scsi_host_get_guard(cmd->device->host);
bad75002 152
bad75002
AE
153 /* We always use DIFF Bundling for best performance */
154 *fw_prot_opts = 0;
155
156 /* Translate SCSI opcode to a protection opcode */
9ba56b95 157 switch (scsi_get_prot_op(cmd)) {
bad75002
AE
158 case SCSI_PROT_READ_STRIP:
159 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
160 break;
161 case SCSI_PROT_WRITE_INSERT:
162 *fw_prot_opts |= PO_MODE_DIF_INSERT;
163 break;
164 case SCSI_PROT_READ_INSERT:
165 *fw_prot_opts |= PO_MODE_DIF_INSERT;
166 break;
167 case SCSI_PROT_WRITE_STRIP:
168 *fw_prot_opts |= PO_MODE_DIF_REMOVE;
169 break;
170 case SCSI_PROT_READ_PASS:
bad75002 171 case SCSI_PROT_WRITE_PASS:
9e522cd8
AE
172 if (guard & SHOST_DIX_GUARD_IP)
173 *fw_prot_opts |= PO_MODE_DIF_TCP_CKSUM;
174 else
175 *fw_prot_opts |= PO_MODE_DIF_PASS;
bad75002
AE
176 break;
177 default: /* Normal Request */
178 *fw_prot_opts |= PO_MODE_DIF_PASS;
179 break;
180 }
181
9ba56b95 182 return scsi_prot_sg_count(cmd);
bad75002
AE
183}
184
185/*
1da177e4
LT
186 * qla2x00_build_scsi_iocbs_32() - Build IOCB command utilizing 32bit
187 * capable IOCB types.
188 *
189 * @sp: SRB command to process
190 * @cmd_pkt: Command type 2 IOCB
191 * @tot_dsds: Total number of segments to transfer
192 */
193void qla2x00_build_scsi_iocbs_32(srb_t *sp, cmd_entry_t *cmd_pkt,
194 uint16_t tot_dsds)
195{
196 uint16_t avail_dsds;
197 uint32_t *cur_dsd;
e315cd28 198 scsi_qla_host_t *vha;
1da177e4 199 struct scsi_cmnd *cmd;
385d70b4
FT
200 struct scatterlist *sg;
201 int i;
1da177e4 202
9ba56b95 203 cmd = GET_CMD_SP(sp);
1da177e4
LT
204
205 /* Update entry type to indicate Command Type 2 IOCB */
206 *((uint32_t *)(&cmd_pkt->entry_type)) =
207 __constant_cpu_to_le32(COMMAND_TYPE);
208
209 /* No data transfer */
385d70b4 210 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
211 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
212 return;
213 }
214
444786d7 215 vha = sp->fcport->vha;
49fd462a 216 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
217
218 /* Three DSDs are available in the Command Type 2 IOCB */
219 avail_dsds = 3;
220 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
221
222 /* Load data segments */
385d70b4
FT
223 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
224 cont_entry_t *cont_pkt;
225
226 /* Allocate additional continuation packets? */
227 if (avail_dsds == 0) {
228 /*
229 * Seven DSDs are available in the Continuation
230 * Type 0 IOCB.
231 */
67c2e93a 232 cont_pkt = qla2x00_prep_cont_type0_iocb(vha);
385d70b4
FT
233 cur_dsd = (uint32_t *)&cont_pkt->dseg_0_address;
234 avail_dsds = 7;
1da177e4 235 }
385d70b4
FT
236
237 *cur_dsd++ = cpu_to_le32(sg_dma_address(sg));
238 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
239 avail_dsds--;
1da177e4
LT
240 }
241}
242
243/**
244 * qla2x00_build_scsi_iocbs_64() - Build IOCB command utilizing 64bit
245 * capable IOCB types.
246 *
247 * @sp: SRB command to process
248 * @cmd_pkt: Command type 3 IOCB
249 * @tot_dsds: Total number of segments to transfer
250 */
251void qla2x00_build_scsi_iocbs_64(srb_t *sp, cmd_entry_t *cmd_pkt,
252 uint16_t tot_dsds)
253{
254 uint16_t avail_dsds;
255 uint32_t *cur_dsd;
e315cd28 256 scsi_qla_host_t *vha;
1da177e4 257 struct scsi_cmnd *cmd;
385d70b4
FT
258 struct scatterlist *sg;
259 int i;
1da177e4 260
9ba56b95 261 cmd = GET_CMD_SP(sp);
1da177e4
LT
262
263 /* Update entry type to indicate Command Type 3 IOCB */
264 *((uint32_t *)(&cmd_pkt->entry_type)) =
265 __constant_cpu_to_le32(COMMAND_A64_TYPE);
266
267 /* No data transfer */
385d70b4 268 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
1da177e4
LT
269 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
270 return;
271 }
272
444786d7 273 vha = sp->fcport->vha;
49fd462a 274 cmd_pkt->control_flags |= cpu_to_le16(qla2x00_get_cmd_direction(sp));
1da177e4
LT
275
276 /* Two DSDs are available in the Command Type 3 IOCB */
277 avail_dsds = 2;
278 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
279
280 /* Load data segments */
385d70b4
FT
281 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
282 dma_addr_t sle_dma;
283 cont_a64_entry_t *cont_pkt;
284
285 /* Allocate additional continuation packets? */
286 if (avail_dsds == 0) {
287 /*
288 * Five DSDs are available in the Continuation
289 * Type 1 IOCB.
290 */
0d2aa38e 291 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
385d70b4
FT
292 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
293 avail_dsds = 5;
1da177e4 294 }
385d70b4
FT
295
296 sle_dma = sg_dma_address(sg);
297 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
298 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
299 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
300 avail_dsds--;
1da177e4
LT
301 }
302}
303
304/**
305 * qla2x00_start_scsi() - Send a SCSI command to the ISP
306 * @sp: command to send to the ISP
307 *
cc3ef7bc 308 * Returns non-zero if a failure occurred, else zero.
1da177e4
LT
309 */
310int
311qla2x00_start_scsi(srb_t *sp)
312{
385d70b4 313 int ret, nseg;
1da177e4 314 unsigned long flags;
e315cd28 315 scsi_qla_host_t *vha;
1da177e4
LT
316 struct scsi_cmnd *cmd;
317 uint32_t *clr_ptr;
318 uint32_t index;
319 uint32_t handle;
320 cmd_entry_t *cmd_pkt;
1da177e4
LT
321 uint16_t cnt;
322 uint16_t req_cnt;
323 uint16_t tot_dsds;
3d71644c 324 struct device_reg_2xxx __iomem *reg;
e315cd28
AC
325 struct qla_hw_data *ha;
326 struct req_que *req;
73208dfd 327 struct rsp_que *rsp;
1da177e4
LT
328
329 /* Setup device pointers. */
330 ret = 0;
444786d7 331 vha = sp->fcport->vha;
e315cd28 332 ha = vha->hw;
3d71644c 333 reg = &ha->iobase->isp;
9ba56b95 334 cmd = GET_CMD_SP(sp);
73208dfd
AC
335 req = ha->req_q_map[0];
336 rsp = ha->rsp_q_map[0];
83021920
AV
337 /* So we know we haven't pci_map'ed anything yet */
338 tot_dsds = 0;
1da177e4
LT
339
340 /* Send marker if required */
e315cd28 341 if (vha->marker_needed != 0) {
7c3df132
SK
342 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
343 QLA_SUCCESS) {
1da177e4 344 return (QLA_FUNCTION_FAILED);
7c3df132 345 }
e315cd28 346 vha->marker_needed = 0;
1da177e4
LT
347 }
348
349 /* Acquire ring specific lock */
c9c5ced9 350 spin_lock_irqsave(&ha->hardware_lock, flags);
1da177e4
LT
351
352 /* Check for room in outstanding command list. */
e315cd28 353 handle = req->current_outstanding_cmd;
8d93f550 354 for (index = 1; index < req->num_outstanding_cmds; index++) {
1da177e4 355 handle++;
8d93f550 356 if (handle == req->num_outstanding_cmds)
1da177e4 357 handle = 1;
e315cd28 358 if (!req->outstanding_cmds[handle])
1da177e4
LT
359 break;
360 }
8d93f550 361 if (index == req->num_outstanding_cmds)
1da177e4
LT
362 goto queuing_error;
363
83021920 364 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
365 if (scsi_sg_count(cmd)) {
366 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
367 scsi_sg_count(cmd), cmd->sc_data_direction);
368 if (unlikely(!nseg))
369 goto queuing_error;
370 } else
371 nseg = 0;
372
385d70b4 373 tot_dsds = nseg;
83021920 374
1da177e4 375 /* Calculate the number of request entries needed. */
fd34f556 376 req_cnt = ha->isp_ops->calc_req_entries(tot_dsds);
e315cd28 377 if (req->cnt < (req_cnt + 2)) {
1da177e4 378 cnt = RD_REG_WORD_RELAXED(ISP_REQ_Q_OUT(ha, reg));
e315cd28
AC
379 if (req->ring_index < cnt)
380 req->cnt = cnt - req->ring_index;
1da177e4 381 else
e315cd28
AC
382 req->cnt = req->length -
383 (req->ring_index - cnt);
a6eb3c9f
CL
384 /* If still no head room then bail out */
385 if (req->cnt < (req_cnt + 2))
386 goto queuing_error;
1da177e4 387 }
1da177e4 388
1da177e4 389 /* Build command packet */
e315cd28
AC
390 req->current_outstanding_cmd = handle;
391 req->outstanding_cmds[handle] = sp;
cf53b069 392 sp->handle = handle;
9ba56b95 393 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 394 req->cnt -= req_cnt;
1da177e4 395
e315cd28 396 cmd_pkt = (cmd_entry_t *)req->ring_ptr;
1da177e4
LT
397 cmd_pkt->handle = handle;
398 /* Zero out remaining portion of packet. */
399 clr_ptr = (uint32_t *)cmd_pkt + 2;
400 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
401 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
402
bdf79621
AV
403 /* Set target ID and LUN number*/
404 SET_TARGET_ID(ha, cmd_pkt->target, sp->fcport->loop_id);
9ba56b95 405 cmd_pkt->lun = cpu_to_le16(cmd->device->lun);
50668633 406 cmd_pkt->control_flags = __constant_cpu_to_le16(CF_SIMPLE_TAG);
1da177e4 407
1da177e4
LT
408 /* Load SCSI command packet. */
409 memcpy(cmd_pkt->scsi_cdb, cmd->cmnd, cmd->cmd_len);
385d70b4 410 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
1da177e4
LT
411
412 /* Build IOCB segments */
fd34f556 413 ha->isp_ops->build_iocbs(sp, cmd_pkt, tot_dsds);
1da177e4
LT
414
415 /* Set total data segment count. */
416 cmd_pkt->entry_count = (uint8_t)req_cnt;
417 wmb();
418
419 /* Adjust ring index. */
e315cd28
AC
420 req->ring_index++;
421 if (req->ring_index == req->length) {
422 req->ring_index = 0;
423 req->ring_ptr = req->ring;
1da177e4 424 } else
e315cd28 425 req->ring_ptr++;
1da177e4 426
1da177e4 427 sp->flags |= SRB_DMA_VALID;
1da177e4
LT
428
429 /* Set chip new ring index. */
e315cd28 430 WRT_REG_WORD(ISP_REQ_Q_IN(ha, reg), req->ring_index);
1da177e4
LT
431 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, reg)); /* PCI Posting. */
432
4fdfefe5 433 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 434 if (vha->flags.process_response_queue &&
73208dfd
AC
435 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
436 qla2x00_process_response_queue(rsp);
4fdfefe5 437
c9c5ced9 438 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
439 return (QLA_SUCCESS);
440
441queuing_error:
385d70b4
FT
442 if (tot_dsds)
443 scsi_dma_unmap(cmd);
444
c9c5ced9 445 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1da177e4
LT
446
447 return (QLA_FUNCTION_FAILED);
448}
449
5162cf0c
GM
450/**
451 * qla2x00_start_iocbs() - Execute the IOCB command
452 */
2d70c103 453void
5162cf0c
GM
454qla2x00_start_iocbs(struct scsi_qla_host *vha, struct req_que *req)
455{
456 struct qla_hw_data *ha = vha->hw;
457 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
5162cf0c 458
7ec0effd 459 if (IS_P3P_TYPE(ha)) {
5162cf0c
GM
460 qla82xx_start_iocbs(vha);
461 } else {
462 /* Adjust ring index. */
463 req->ring_index++;
464 if (req->ring_index == req->length) {
465 req->ring_index = 0;
466 req->ring_ptr = req->ring;
467 } else
468 req->ring_ptr++;
469
470 /* Set chip new ring index. */
f73cb695 471 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
6246b8a1 472 WRT_REG_DWORD(req->req_q_in, req->ring_index);
98878a16 473 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
8ae6d9c7
GM
474 } else if (IS_QLAFX00(ha)) {
475 WRT_REG_DWORD(&reg->ispfx00.req_q_in, req->ring_index);
476 RD_REG_DWORD_RELAXED(&reg->ispfx00.req_q_in);
477 QLAFX00_SET_HST_INTR(ha, ha->rqstq_intr_code);
5162cf0c
GM
478 } else if (IS_FWI2_CAPABLE(ha)) {
479 WRT_REG_DWORD(&reg->isp24.req_q_in, req->ring_index);
480 RD_REG_DWORD_RELAXED(&reg->isp24.req_q_in);
481 } else {
482 WRT_REG_WORD(ISP_REQ_Q_IN(ha, &reg->isp),
483 req->ring_index);
484 RD_REG_WORD_RELAXED(ISP_REQ_Q_IN(ha, &reg->isp));
485 }
486 }
487}
488
1da177e4
LT
489/**
490 * qla2x00_marker() - Send a marker IOCB to the firmware.
491 * @ha: HA context
492 * @loop_id: loop ID
493 * @lun: LUN
494 * @type: marker modifier
495 *
496 * Can be called from both normal and interrupt context.
497 *
cc3ef7bc 498 * Returns non-zero if a failure occurred, else zero.
1da177e4 499 */
3dbe756a 500static int
73208dfd
AC
501__qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
502 struct rsp_que *rsp, uint16_t loop_id,
9cb78c16 503 uint64_t lun, uint8_t type)
1da177e4 504{
2b6c0cee 505 mrk_entry_t *mrk;
8ae6d9c7 506 struct mrk_entry_24xx *mrk24 = NULL;
8ae6d9c7 507
e315cd28
AC
508 struct qla_hw_data *ha = vha->hw;
509 scsi_qla_host_t *base_vha = pci_get_drvdata(ha->pdev);
1da177e4 510
99b8212c 511 req = ha->req_q_map[0];
fa492630 512 mrk = (mrk_entry_t *)qla2x00_alloc_iocbs(vha, NULL);
2b6c0cee 513 if (mrk == NULL) {
7c3df132
SK
514 ql_log(ql_log_warn, base_vha, 0x3026,
515 "Failed to allocate Marker IOCB.\n");
1da177e4
LT
516
517 return (QLA_FUNCTION_FAILED);
518 }
519
2b6c0cee
AV
520 mrk->entry_type = MARKER_TYPE;
521 mrk->modifier = type;
1da177e4 522 if (type != MK_SYNC_ALL) {
bfd7334e 523 if (IS_FWI2_CAPABLE(ha)) {
2b6c0cee
AV
524 mrk24 = (struct mrk_entry_24xx *) mrk;
525 mrk24->nport_handle = cpu_to_le16(loop_id);
9cb78c16 526 int_to_scsilun(lun, (struct scsi_lun *)&mrk24->lun);
b797b6de 527 host_to_fcp_swap(mrk24->lun, sizeof(mrk24->lun));
e315cd28 528 mrk24->vp_index = vha->vp_idx;
2afa19a9 529 mrk24->handle = MAKE_HANDLE(req->id, mrk24->handle);
2b6c0cee
AV
530 } else {
531 SET_TARGET_ID(ha, mrk->target, loop_id);
9cb78c16 532 mrk->lun = cpu_to_le16((uint16_t)lun);
2b6c0cee 533 }
1da177e4
LT
534 }
535 wmb();
536
5162cf0c 537 qla2x00_start_iocbs(vha, req);
1da177e4
LT
538
539 return (QLA_SUCCESS);
540}
541
fa2a1ce5 542int
73208dfd 543qla2x00_marker(struct scsi_qla_host *vha, struct req_que *req,
9cb78c16 544 struct rsp_que *rsp, uint16_t loop_id, uint64_t lun,
73208dfd 545 uint8_t type)
1da177e4
LT
546{
547 int ret;
548 unsigned long flags = 0;
549
73208dfd
AC
550 spin_lock_irqsave(&vha->hw->hardware_lock, flags);
551 ret = __qla2x00_marker(vha, req, rsp, loop_id, lun, type);
552 spin_unlock_irqrestore(&vha->hw->hardware_lock, flags);
1da177e4
LT
553
554 return (ret);
555}
556
2d70c103
NB
557/*
558 * qla2x00_issue_marker
559 *
560 * Issue marker
561 * Caller CAN have hardware lock held as specified by ha_locked parameter.
562 * Might release it, then reaquire.
563 */
564int qla2x00_issue_marker(scsi_qla_host_t *vha, int ha_locked)
565{
566 if (ha_locked) {
567 if (__qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
568 MK_SYNC_ALL) != QLA_SUCCESS)
569 return QLA_FUNCTION_FAILED;
570 } else {
571 if (qla2x00_marker(vha, vha->req, vha->req->rsp, 0, 0,
572 MK_SYNC_ALL) != QLA_SUCCESS)
573 return QLA_FUNCTION_FAILED;
574 }
575 vha->marker_needed = 0;
576
577 return QLA_SUCCESS;
578}
579
5162cf0c
GM
580static inline int
581qla24xx_build_scsi_type_6_iocbs(srb_t *sp, struct cmd_type_6 *cmd_pkt,
582 uint16_t tot_dsds)
583{
584 uint32_t *cur_dsd = NULL;
585 scsi_qla_host_t *vha;
586 struct qla_hw_data *ha;
587 struct scsi_cmnd *cmd;
588 struct scatterlist *cur_seg;
589 uint32_t *dsd_seg;
590 void *next_dsd;
591 uint8_t avail_dsds;
592 uint8_t first_iocb = 1;
593 uint32_t dsd_list_len;
594 struct dsd_dma *dsd_ptr;
595 struct ct6_dsd *ctx;
1da177e4 596
9ba56b95 597 cmd = GET_CMD_SP(sp);
a9083016 598
5162cf0c
GM
599 /* Update entry type to indicate Command Type 3 IOCB */
600 *((uint32_t *)(&cmd_pkt->entry_type)) =
601 __constant_cpu_to_le32(COMMAND_TYPE_6);
602
603 /* No data transfer */
604 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
605 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
606 return 0;
607 }
608
609 vha = sp->fcport->vha;
610 ha = vha->hw;
611
612 /* Set transfer direction */
613 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
614 cmd_pkt->control_flags =
615 __constant_cpu_to_le16(CF_WRITE_DATA);
2be21fa2 616 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 617 vha->qla_stats.output_requests++;
5162cf0c
GM
618 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
619 cmd_pkt->control_flags =
620 __constant_cpu_to_le16(CF_READ_DATA);
2be21fa2 621 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 622 vha->qla_stats.input_requests++;
5162cf0c
GM
623 }
624
625 cur_seg = scsi_sglist(cmd);
9ba56b95 626 ctx = GET_CMD_CTX_SP(sp);
5162cf0c
GM
627
628 while (tot_dsds) {
629 avail_dsds = (tot_dsds > QLA_DSDS_PER_IOCB) ?
630 QLA_DSDS_PER_IOCB : tot_dsds;
631 tot_dsds -= avail_dsds;
632 dsd_list_len = (avail_dsds + 1) * QLA_DSD_SIZE;
633
634 dsd_ptr = list_first_entry(&ha->gbl_dsd_list,
635 struct dsd_dma, list);
636 next_dsd = dsd_ptr->dsd_addr;
637 list_del(&dsd_ptr->list);
638 ha->gbl_dsd_avail--;
639 list_add_tail(&dsd_ptr->list, &ctx->dsd_list);
640 ctx->dsd_use_cnt++;
641 ha->gbl_dsd_inuse++;
642
643 if (first_iocb) {
644 first_iocb = 0;
645 dsd_seg = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
646 *dsd_seg++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
647 *dsd_seg++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
648 cmd_pkt->fcp_data_dseg_len = cpu_to_le32(dsd_list_len);
73208dfd 649 } else {
5162cf0c
GM
650 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
651 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
652 *cur_dsd++ = cpu_to_le32(dsd_list_len);
653 }
654 cur_dsd = (uint32_t *)next_dsd;
655 while (avail_dsds) {
656 dma_addr_t sle_dma;
657
658 sle_dma = sg_dma_address(cur_seg);
659 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
660 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
661 *cur_dsd++ = cpu_to_le32(sg_dma_len(cur_seg));
662 cur_seg = sg_next(cur_seg);
663 avail_dsds--;
73208dfd 664 }
2b6c0cee
AV
665 }
666
5162cf0c
GM
667 /* Null termination */
668 *cur_dsd++ = 0;
669 *cur_dsd++ = 0;
670 *cur_dsd++ = 0;
671 cmd_pkt->control_flags |= CF_DATA_SEG_DESCR_ENABLE;
672 return 0;
2b6c0cee
AV
673}
674
5162cf0c
GM
675/*
676 * qla24xx_calc_dsd_lists() - Determine number of DSD list required
677 * for Command Type 6.
2b6c0cee
AV
678 *
679 * @dsds: number of data segment decriptors needed
680 *
5162cf0c 681 * Returns the number of dsd list needed to store @dsds.
2b6c0cee 682 */
a9083016 683inline uint16_t
5162cf0c 684qla24xx_calc_dsd_lists(uint16_t dsds)
2b6c0cee 685{
5162cf0c 686 uint16_t dsd_lists = 0;
2b6c0cee 687
5162cf0c
GM
688 dsd_lists = (dsds/QLA_DSDS_PER_IOCB);
689 if (dsds % QLA_DSDS_PER_IOCB)
690 dsd_lists++;
691 return dsd_lists;
2b6c0cee
AV
692}
693
5162cf0c 694
2b6c0cee
AV
695/**
696 * qla24xx_build_scsi_iocbs() - Build IOCB command utilizing Command Type 7
697 * IOCB types.
698 *
699 * @sp: SRB command to process
700 * @cmd_pkt: Command type 3 IOCB
701 * @tot_dsds: Total number of segments to transfer
702 */
a9083016 703inline void
2b6c0cee
AV
704qla24xx_build_scsi_iocbs(srb_t *sp, struct cmd_type_7 *cmd_pkt,
705 uint16_t tot_dsds)
706{
707 uint16_t avail_dsds;
708 uint32_t *cur_dsd;
e315cd28 709 scsi_qla_host_t *vha;
2b6c0cee 710 struct scsi_cmnd *cmd;
385d70b4
FT
711 struct scatterlist *sg;
712 int i;
73208dfd 713 struct req_que *req;
2b6c0cee 714
9ba56b95 715 cmd = GET_CMD_SP(sp);
2b6c0cee
AV
716
717 /* Update entry type to indicate Command Type 3 IOCB */
718 *((uint32_t *)(&cmd_pkt->entry_type)) =
719 __constant_cpu_to_le32(COMMAND_TYPE_7);
720
721 /* No data transfer */
385d70b4 722 if (!scsi_bufflen(cmd) || cmd->sc_data_direction == DMA_NONE) {
2b6c0cee
AV
723 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
724 return;
725 }
726
444786d7 727 vha = sp->fcport->vha;
67c2e93a 728 req = vha->req;
2b6c0cee
AV
729
730 /* Set transfer direction */
49fd462a 731 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2b6c0cee
AV
732 cmd_pkt->task_mgmt_flags =
733 __constant_cpu_to_le16(TMF_WRITE_DATA);
2be21fa2 734 vha->qla_stats.output_bytes += scsi_bufflen(cmd);
fabbb8df 735 vha->qla_stats.output_requests++;
49fd462a 736 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
2b6c0cee
AV
737 cmd_pkt->task_mgmt_flags =
738 __constant_cpu_to_le16(TMF_READ_DATA);
2be21fa2 739 vha->qla_stats.input_bytes += scsi_bufflen(cmd);
fabbb8df 740 vha->qla_stats.input_requests++;
49fd462a 741 }
2b6c0cee
AV
742
743 /* One DSD is available in the Command Type 3 IOCB */
744 avail_dsds = 1;
745 cur_dsd = (uint32_t *)&cmd_pkt->dseg_0_address;
746
747 /* Load data segments */
385d70b4
FT
748
749 scsi_for_each_sg(cmd, sg, tot_dsds, i) {
750 dma_addr_t sle_dma;
751 cont_a64_entry_t *cont_pkt;
752
753 /* Allocate additional continuation packets? */
754 if (avail_dsds == 0) {
755 /*
756 * Five DSDs are available in the Continuation
757 * Type 1 IOCB.
758 */
0d2aa38e 759 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
385d70b4
FT
760 cur_dsd = (uint32_t *)cont_pkt->dseg_0_address;
761 avail_dsds = 5;
2b6c0cee 762 }
385d70b4
FT
763
764 sle_dma = sg_dma_address(sg);
765 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
766 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
767 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
768 avail_dsds--;
2b6c0cee
AV
769 }
770}
771
bad75002
AE
772struct fw_dif_context {
773 uint32_t ref_tag;
774 uint16_t app_tag;
775 uint8_t ref_tag_mask[4]; /* Validation/Replacement Mask*/
776 uint8_t app_tag_mask[2]; /* Validation/Replacement Mask*/
777};
778
779/*
780 * qla24xx_set_t10dif_tags_from_cmd - Extract Ref and App tags from SCSI command
781 *
782 */
783static inline void
e02587d7 784qla24xx_set_t10dif_tags(srb_t *sp, struct fw_dif_context *pkt,
bad75002
AE
785 unsigned int protcnt)
786{
9ba56b95 787 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
bad75002
AE
788
789 switch (scsi_get_prot_type(cmd)) {
bad75002 790 case SCSI_PROT_DIF_TYPE0:
8cb2049c
AE
791 /*
792 * No check for ql2xenablehba_err_chk, as it would be an
793 * I/O error if hba tag generation is not done.
794 */
795 pkt->ref_tag = cpu_to_le32((uint32_t)
796 (0xffffffff & scsi_get_lba(cmd)));
e02587d7
AE
797
798 if (!qla2x00_hba_err_chk_enabled(sp))
799 break;
800
8cb2049c
AE
801 pkt->ref_tag_mask[0] = 0xff;
802 pkt->ref_tag_mask[1] = 0xff;
803 pkt->ref_tag_mask[2] = 0xff;
804 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
805 break;
806
807 /*
808 * For TYPE 2 protection: 16 bit GUARD + 32 bit REF tag has to
809 * match LBA in CDB + N
810 */
811 case SCSI_PROT_DIF_TYPE2:
e02587d7
AE
812 pkt->app_tag = __constant_cpu_to_le16(0);
813 pkt->app_tag_mask[0] = 0x0;
814 pkt->app_tag_mask[1] = 0x0;
0c470874
AE
815
816 pkt->ref_tag = cpu_to_le32((uint32_t)
817 (0xffffffff & scsi_get_lba(cmd)));
818
e02587d7
AE
819 if (!qla2x00_hba_err_chk_enabled(sp))
820 break;
821
0c470874
AE
822 /* enable ALL bytes of the ref tag */
823 pkt->ref_tag_mask[0] = 0xff;
824 pkt->ref_tag_mask[1] = 0xff;
825 pkt->ref_tag_mask[2] = 0xff;
826 pkt->ref_tag_mask[3] = 0xff;
bad75002
AE
827 break;
828
829 /* For Type 3 protection: 16 bit GUARD only */
830 case SCSI_PROT_DIF_TYPE3:
831 pkt->ref_tag_mask[0] = pkt->ref_tag_mask[1] =
832 pkt->ref_tag_mask[2] = pkt->ref_tag_mask[3] =
833 0x00;
834 break;
835
836 /*
837 * For TYpe 1 protection: 16 bit GUARD tag, 32 bit REF tag, and
838 * 16 bit app tag.
839 */
840 case SCSI_PROT_DIF_TYPE1:
e02587d7
AE
841 pkt->ref_tag = cpu_to_le32((uint32_t)
842 (0xffffffff & scsi_get_lba(cmd)));
843 pkt->app_tag = __constant_cpu_to_le16(0);
844 pkt->app_tag_mask[0] = 0x0;
845 pkt->app_tag_mask[1] = 0x0;
846
847 if (!qla2x00_hba_err_chk_enabled(sp))
bad75002
AE
848 break;
849
bad75002
AE
850 /* enable ALL bytes of the ref tag */
851 pkt->ref_tag_mask[0] = 0xff;
852 pkt->ref_tag_mask[1] = 0xff;
853 pkt->ref_tag_mask[2] = 0xff;
854 pkt->ref_tag_mask[3] = 0xff;
855 break;
856 }
bad75002
AE
857}
858
8cb2049c
AE
859struct qla2_sgx {
860 dma_addr_t dma_addr; /* OUT */
861 uint32_t dma_len; /* OUT */
862
863 uint32_t tot_bytes; /* IN */
864 struct scatterlist *cur_sg; /* IN */
865
866 /* for book keeping, bzero on initial invocation */
867 uint32_t bytes_consumed;
868 uint32_t num_bytes;
869 uint32_t tot_partial;
870
871 /* for debugging */
872 uint32_t num_sg;
873 srb_t *sp;
874};
875
876static int
877qla24xx_get_one_block_sg(uint32_t blk_sz, struct qla2_sgx *sgx,
878 uint32_t *partial)
879{
880 struct scatterlist *sg;
881 uint32_t cumulative_partial, sg_len;
882 dma_addr_t sg_dma_addr;
883
884 if (sgx->num_bytes == sgx->tot_bytes)
885 return 0;
886
887 sg = sgx->cur_sg;
888 cumulative_partial = sgx->tot_partial;
889
890 sg_dma_addr = sg_dma_address(sg);
891 sg_len = sg_dma_len(sg);
892
893 sgx->dma_addr = sg_dma_addr + sgx->bytes_consumed;
894
895 if ((cumulative_partial + (sg_len - sgx->bytes_consumed)) >= blk_sz) {
896 sgx->dma_len = (blk_sz - cumulative_partial);
897 sgx->tot_partial = 0;
898 sgx->num_bytes += blk_sz;
899 *partial = 0;
900 } else {
901 sgx->dma_len = sg_len - sgx->bytes_consumed;
902 sgx->tot_partial += sgx->dma_len;
903 *partial = 1;
904 }
905
906 sgx->bytes_consumed += sgx->dma_len;
907
908 if (sg_len == sgx->bytes_consumed) {
909 sg = sg_next(sg);
910 sgx->num_sg++;
911 sgx->cur_sg = sg;
912 sgx->bytes_consumed = 0;
913 }
914
915 return 1;
916}
917
f83adb61 918int
8cb2049c 919qla24xx_walk_and_build_sglist_no_difb(struct qla_hw_data *ha, srb_t *sp,
f83adb61 920 uint32_t *dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
8cb2049c
AE
921{
922 void *next_dsd;
923 uint8_t avail_dsds = 0;
924 uint32_t dsd_list_len;
925 struct dsd_dma *dsd_ptr;
926 struct scatterlist *sg_prot;
927 uint32_t *cur_dsd = dsd;
928 uint16_t used_dsds = tot_dsds;
929
f83adb61 930 uint32_t prot_int; /* protection interval */
8cb2049c
AE
931 uint32_t partial;
932 struct qla2_sgx sgx;
933 dma_addr_t sle_dma;
934 uint32_t sle_dma_len, tot_prot_dma_len = 0;
f83adb61
QT
935 struct scsi_cmnd *cmd;
936 struct scsi_qla_host *vha;
8cb2049c
AE
937
938 memset(&sgx, 0, sizeof(struct qla2_sgx));
f83adb61
QT
939 if (sp) {
940 vha = sp->fcport->vha;
941 cmd = GET_CMD_SP(sp);
942 prot_int = cmd->device->sector_size;
943
944 sgx.tot_bytes = scsi_bufflen(cmd);
945 sgx.cur_sg = scsi_sglist(cmd);
946 sgx.sp = sp;
947
948 sg_prot = scsi_prot_sglist(cmd);
949 } else if (tc) {
950 vha = tc->vha;
951 prot_int = tc->blk_sz;
952 sgx.tot_bytes = tc->bufflen;
953 sgx.cur_sg = tc->sg;
954 sg_prot = tc->prot_sg;
955 } else {
956 BUG();
957 return 1;
958 }
8cb2049c
AE
959
960 while (qla24xx_get_one_block_sg(prot_int, &sgx, &partial)) {
961
962 sle_dma = sgx.dma_addr;
963 sle_dma_len = sgx.dma_len;
964alloc_and_fill:
965 /* Allocate additional continuation packets? */
966 if (avail_dsds == 0) {
967 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
968 QLA_DSDS_PER_IOCB : used_dsds;
969 dsd_list_len = (avail_dsds + 1) * 12;
970 used_dsds -= avail_dsds;
971
972 /* allocate tracking DS */
973 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
974 if (!dsd_ptr)
975 return 1;
976
977 /* allocate new list */
978 dsd_ptr->dsd_addr = next_dsd =
979 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
980 &dsd_ptr->dsd_list_dma);
981
982 if (!next_dsd) {
983 /*
984 * Need to cleanup only this dsd_ptr, rest
985 * will be done by sp_free_dma()
986 */
987 kfree(dsd_ptr);
988 return 1;
989 }
990
f83adb61
QT
991 if (sp) {
992 list_add_tail(&dsd_ptr->list,
993 &((struct crc_context *)
994 sp->u.scmd.ctx)->dsd_list);
995
996 sp->flags |= SRB_CRC_CTX_DSD_VALID;
997 } else {
998 list_add_tail(&dsd_ptr->list,
999 &(tc->ctx->dsd_list));
1000 tc->ctx_dsd_alloced = 1;
1001 }
8cb2049c 1002
8cb2049c
AE
1003
1004 /* add new list to cmd iocb or last list */
1005 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1006 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1007 *cur_dsd++ = dsd_list_len;
1008 cur_dsd = (uint32_t *)next_dsd;
1009 }
1010 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1011 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1012 *cur_dsd++ = cpu_to_le32(sle_dma_len);
1013 avail_dsds--;
1014
1015 if (partial == 0) {
1016 /* Got a full protection interval */
1017 sle_dma = sg_dma_address(sg_prot) + tot_prot_dma_len;
1018 sle_dma_len = 8;
bad75002 1019
8cb2049c
AE
1020 tot_prot_dma_len += sle_dma_len;
1021 if (tot_prot_dma_len == sg_dma_len(sg_prot)) {
1022 tot_prot_dma_len = 0;
1023 sg_prot = sg_next(sg_prot);
1024 }
1025
1026 partial = 1; /* So as to not re-enter this block */
1027 goto alloc_and_fill;
1028 }
1029 }
1030 /* Null termination */
1031 *cur_dsd++ = 0;
1032 *cur_dsd++ = 0;
1033 *cur_dsd++ = 0;
1034 return 0;
1035}
5162cf0c 1036
f83adb61 1037int
bad75002 1038qla24xx_walk_and_build_sglist(struct qla_hw_data *ha, srb_t *sp, uint32_t *dsd,
f83adb61 1039 uint16_t tot_dsds, struct qla_tgt_cmd *tc)
bad75002
AE
1040{
1041 void *next_dsd;
1042 uint8_t avail_dsds = 0;
1043 uint32_t dsd_list_len;
1044 struct dsd_dma *dsd_ptr;
f83adb61 1045 struct scatterlist *sg, *sgl;
bad75002
AE
1046 uint32_t *cur_dsd = dsd;
1047 int i;
1048 uint16_t used_dsds = tot_dsds;
f83adb61
QT
1049 struct scsi_cmnd *cmd;
1050 struct scsi_qla_host *vha;
1051
1052 if (sp) {
1053 cmd = GET_CMD_SP(sp);
1054 sgl = scsi_sglist(cmd);
1055 vha = sp->fcport->vha;
1056 } else if (tc) {
1057 sgl = tc->sg;
1058 vha = tc->vha;
1059 } else {
1060 BUG();
1061 return 1;
1062 }
bad75002 1063
f83adb61
QT
1064
1065 for_each_sg(sgl, sg, tot_dsds, i) {
bad75002
AE
1066 dma_addr_t sle_dma;
1067
1068 /* Allocate additional continuation packets? */
1069 if (avail_dsds == 0) {
1070 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1071 QLA_DSDS_PER_IOCB : used_dsds;
1072 dsd_list_len = (avail_dsds + 1) * 12;
1073 used_dsds -= avail_dsds;
1074
1075 /* allocate tracking DS */
1076 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1077 if (!dsd_ptr)
1078 return 1;
1079
1080 /* allocate new list */
1081 dsd_ptr->dsd_addr = next_dsd =
1082 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1083 &dsd_ptr->dsd_list_dma);
1084
1085 if (!next_dsd) {
1086 /*
1087 * Need to cleanup only this dsd_ptr, rest
1088 * will be done by sp_free_dma()
1089 */
1090 kfree(dsd_ptr);
1091 return 1;
1092 }
1093
f83adb61
QT
1094 if (sp) {
1095 list_add_tail(&dsd_ptr->list,
1096 &((struct crc_context *)
1097 sp->u.scmd.ctx)->dsd_list);
bad75002 1098
f83adb61
QT
1099 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1100 } else {
1101 list_add_tail(&dsd_ptr->list,
1102 &(tc->ctx->dsd_list));
1103 tc->ctx_dsd_alloced = 1;
1104 }
bad75002
AE
1105
1106 /* add new list to cmd iocb or last list */
1107 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1108 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1109 *cur_dsd++ = dsd_list_len;
1110 cur_dsd = (uint32_t *)next_dsd;
1111 }
1112 sle_dma = sg_dma_address(sg);
9e522cd8 1113
bad75002
AE
1114 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1115 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1116 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1117 avail_dsds--;
1118
bad75002
AE
1119 }
1120 /* Null termination */
1121 *cur_dsd++ = 0;
1122 *cur_dsd++ = 0;
1123 *cur_dsd++ = 0;
1124 return 0;
1125}
1126
f83adb61 1127int
bad75002 1128qla24xx_walk_and_build_prot_sglist(struct qla_hw_data *ha, srb_t *sp,
f83adb61 1129 uint32_t *dsd, uint16_t tot_dsds, struct qla_tgt_cmd *tc)
bad75002
AE
1130{
1131 void *next_dsd;
1132 uint8_t avail_dsds = 0;
1133 uint32_t dsd_list_len;
1134 struct dsd_dma *dsd_ptr;
f83adb61 1135 struct scatterlist *sg, *sgl;
bad75002
AE
1136 int i;
1137 struct scsi_cmnd *cmd;
1138 uint32_t *cur_dsd = dsd;
f83adb61
QT
1139 uint16_t used_dsds = tot_dsds;
1140 struct scsi_qla_host *vha;
1141
1142 if (sp) {
1143 cmd = GET_CMD_SP(sp);
1144 sgl = scsi_prot_sglist(cmd);
1145 vha = sp->fcport->vha;
1146 } else if (tc) {
1147 vha = tc->vha;
1148 sgl = tc->prot_sg;
1149 } else {
1150 BUG();
1151 return 1;
1152 }
bad75002 1153
f83adb61
QT
1154 ql_dbg(ql_dbg_tgt, vha, 0xe021,
1155 "%s: enter\n", __func__);
1156
1157 for_each_sg(sgl, sg, tot_dsds, i) {
bad75002
AE
1158 dma_addr_t sle_dma;
1159
1160 /* Allocate additional continuation packets? */
1161 if (avail_dsds == 0) {
1162 avail_dsds = (used_dsds > QLA_DSDS_PER_IOCB) ?
1163 QLA_DSDS_PER_IOCB : used_dsds;
1164 dsd_list_len = (avail_dsds + 1) * 12;
1165 used_dsds -= avail_dsds;
1166
1167 /* allocate tracking DS */
1168 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
1169 if (!dsd_ptr)
1170 return 1;
1171
1172 /* allocate new list */
1173 dsd_ptr->dsd_addr = next_dsd =
1174 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC,
1175 &dsd_ptr->dsd_list_dma);
1176
1177 if (!next_dsd) {
1178 /*
1179 * Need to cleanup only this dsd_ptr, rest
1180 * will be done by sp_free_dma()
1181 */
1182 kfree(dsd_ptr);
1183 return 1;
1184 }
1185
f83adb61
QT
1186 if (sp) {
1187 list_add_tail(&dsd_ptr->list,
1188 &((struct crc_context *)
1189 sp->u.scmd.ctx)->dsd_list);
bad75002 1190
f83adb61
QT
1191 sp->flags |= SRB_CRC_CTX_DSD_VALID;
1192 } else {
1193 list_add_tail(&dsd_ptr->list,
1194 &(tc->ctx->dsd_list));
1195 tc->ctx_dsd_alloced = 1;
1196 }
bad75002
AE
1197
1198 /* add new list to cmd iocb or last list */
1199 *cur_dsd++ = cpu_to_le32(LSD(dsd_ptr->dsd_list_dma));
1200 *cur_dsd++ = cpu_to_le32(MSD(dsd_ptr->dsd_list_dma));
1201 *cur_dsd++ = dsd_list_len;
1202 cur_dsd = (uint32_t *)next_dsd;
1203 }
1204 sle_dma = sg_dma_address(sg);
9e522cd8 1205
bad75002
AE
1206 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
1207 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
1208 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
1209
bad75002
AE
1210 avail_dsds--;
1211 }
1212 /* Null termination */
1213 *cur_dsd++ = 0;
1214 *cur_dsd++ = 0;
1215 *cur_dsd++ = 0;
1216 return 0;
1217}
1218
1219/**
1220 * qla24xx_build_scsi_crc_2_iocbs() - Build IOCB command utilizing Command
1221 * Type 6 IOCB types.
1222 *
1223 * @sp: SRB command to process
1224 * @cmd_pkt: Command type 3 IOCB
1225 * @tot_dsds: Total number of segments to transfer
1226 */
1227static inline int
1228qla24xx_build_scsi_crc_2_iocbs(srb_t *sp, struct cmd_type_crc_2 *cmd_pkt,
1229 uint16_t tot_dsds, uint16_t tot_prot_dsds, uint16_t fw_prot_opts)
1230{
1231 uint32_t *cur_dsd, *fcp_dl;
1232 scsi_qla_host_t *vha;
1233 struct scsi_cmnd *cmd;
bad75002 1234 int sgc;
8cb2049c 1235 uint32_t total_bytes = 0;
bad75002
AE
1236 uint32_t data_bytes;
1237 uint32_t dif_bytes;
1238 uint8_t bundling = 1;
1239 uint16_t blk_size;
1240 uint8_t *clr_ptr;
1241 struct crc_context *crc_ctx_pkt = NULL;
1242 struct qla_hw_data *ha;
1243 uint8_t additional_fcpcdb_len;
1244 uint16_t fcp_cmnd_len;
1245 struct fcp_cmnd *fcp_cmnd;
1246 dma_addr_t crc_ctx_dma;
1247
9ba56b95 1248 cmd = GET_CMD_SP(sp);
bad75002
AE
1249
1250 sgc = 0;
1251 /* Update entry type to indicate Command Type CRC_2 IOCB */
1252 *((uint32_t *)(&cmd_pkt->entry_type)) =
1253 __constant_cpu_to_le32(COMMAND_TYPE_CRC_2);
1254
7c3df132
SK
1255 vha = sp->fcport->vha;
1256 ha = vha->hw;
1257
bad75002
AE
1258 /* No data transfer */
1259 data_bytes = scsi_bufflen(cmd);
1260 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
bad75002
AE
1261 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1262 return QLA_SUCCESS;
1263 }
1264
c6d39e23 1265 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
bad75002
AE
1266
1267 /* Set transfer direction */
1268 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1269 cmd_pkt->control_flags =
1270 __constant_cpu_to_le16(CF_WRITE_DATA);
1271 } else if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
1272 cmd_pkt->control_flags =
1273 __constant_cpu_to_le16(CF_READ_DATA);
1274 }
1275
9ba56b95
GM
1276 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1277 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP) ||
1278 (scsi_get_prot_op(cmd) == SCSI_PROT_READ_STRIP) ||
1279 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_INSERT))
bad75002
AE
1280 bundling = 0;
1281
1282 /* Allocate CRC context from global pool */
9ba56b95
GM
1283 crc_ctx_pkt = sp->u.scmd.ctx =
1284 dma_pool_alloc(ha->dl_dma_pool, GFP_ATOMIC, &crc_ctx_dma);
bad75002
AE
1285
1286 if (!crc_ctx_pkt)
1287 goto crc_queuing_error;
1288
1289 /* Zero out CTX area. */
1290 clr_ptr = (uint8_t *)crc_ctx_pkt;
1291 memset(clr_ptr, 0, sizeof(*crc_ctx_pkt));
1292
1293 crc_ctx_pkt->crc_ctx_dma = crc_ctx_dma;
1294
1295 sp->flags |= SRB_CRC_CTX_DMA_VALID;
1296
1297 /* Set handle */
1298 crc_ctx_pkt->handle = cmd_pkt->handle;
1299
1300 INIT_LIST_HEAD(&crc_ctx_pkt->dsd_list);
1301
e02587d7 1302 qla24xx_set_t10dif_tags(sp, (struct fw_dif_context *)
bad75002
AE
1303 &crc_ctx_pkt->ref_tag, tot_prot_dsds);
1304
1305 cmd_pkt->crc_context_address[0] = cpu_to_le32(LSD(crc_ctx_dma));
1306 cmd_pkt->crc_context_address[1] = cpu_to_le32(MSD(crc_ctx_dma));
1307 cmd_pkt->crc_context_len = CRC_CONTEXT_LEN_FW;
1308
1309 /* Determine SCSI command length -- align to 4 byte boundary */
1310 if (cmd->cmd_len > 16) {
bad75002
AE
1311 additional_fcpcdb_len = cmd->cmd_len - 16;
1312 if ((cmd->cmd_len % 4) != 0) {
1313 /* SCSI cmd > 16 bytes must be multiple of 4 */
1314 goto crc_queuing_error;
1315 }
1316 fcp_cmnd_len = 12 + cmd->cmd_len + 4;
1317 } else {
1318 additional_fcpcdb_len = 0;
1319 fcp_cmnd_len = 12 + 16 + 4;
1320 }
1321
1322 fcp_cmnd = &crc_ctx_pkt->fcp_cmnd;
1323
1324 fcp_cmnd->additional_cdb_len = additional_fcpcdb_len;
1325 if (cmd->sc_data_direction == DMA_TO_DEVICE)
1326 fcp_cmnd->additional_cdb_len |= 1;
1327 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
1328 fcp_cmnd->additional_cdb_len |= 2;
1329
9ba56b95 1330 int_to_scsilun(cmd->device->lun, &fcp_cmnd->lun);
bad75002
AE
1331 memcpy(fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
1332 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(fcp_cmnd_len);
1333 cmd_pkt->fcp_cmnd_dseg_address[0] = cpu_to_le32(
1334 LSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
1335 cmd_pkt->fcp_cmnd_dseg_address[1] = cpu_to_le32(
1336 MSD(crc_ctx_dma + CRC_CONTEXT_FCPCMND_OFF));
65155b37 1337 fcp_cmnd->task_management = 0;
50668633 1338 fcp_cmnd->task_attribute = TSK_SIMPLE;
ff2fc42e 1339
bad75002
AE
1340 cmd_pkt->fcp_rsp_dseg_len = 0; /* Let response come in status iocb */
1341
bad75002 1342 /* Compute dif len and adjust data len to incude protection */
bad75002
AE
1343 dif_bytes = 0;
1344 blk_size = cmd->device->sector_size;
8cb2049c
AE
1345 dif_bytes = (data_bytes / blk_size) * 8;
1346
9ba56b95 1347 switch (scsi_get_prot_op(GET_CMD_SP(sp))) {
8cb2049c
AE
1348 case SCSI_PROT_READ_INSERT:
1349 case SCSI_PROT_WRITE_STRIP:
1350 total_bytes = data_bytes;
1351 data_bytes += dif_bytes;
1352 break;
1353
1354 case SCSI_PROT_READ_STRIP:
1355 case SCSI_PROT_WRITE_INSERT:
1356 case SCSI_PROT_READ_PASS:
1357 case SCSI_PROT_WRITE_PASS:
1358 total_bytes = data_bytes + dif_bytes;
1359 break;
1360 default:
1361 BUG();
bad75002
AE
1362 }
1363
e02587d7 1364 if (!qla2x00_hba_err_chk_enabled(sp))
bad75002 1365 fw_prot_opts |= 0x10; /* Disable Guard tag checking */
9e522cd8
AE
1366 /* HBA error checking enabled */
1367 else if (IS_PI_UNINIT_CAPABLE(ha)) {
1368 if ((scsi_get_prot_type(GET_CMD_SP(sp)) == SCSI_PROT_DIF_TYPE1)
1369 || (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1370 SCSI_PROT_DIF_TYPE2))
1371 fw_prot_opts |= BIT_10;
1372 else if (scsi_get_prot_type(GET_CMD_SP(sp)) ==
1373 SCSI_PROT_DIF_TYPE3)
1374 fw_prot_opts |= BIT_11;
1375 }
bad75002
AE
1376
1377 if (!bundling) {
1378 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.nobundling.data_address;
1379 } else {
1380 /*
1381 * Configure Bundling if we need to fetch interlaving
1382 * protection PCI accesses
1383 */
1384 fw_prot_opts |= PO_ENABLE_DIF_BUNDLING;
1385 crc_ctx_pkt->u.bundling.dif_byte_count = cpu_to_le32(dif_bytes);
1386 crc_ctx_pkt->u.bundling.dseg_count = cpu_to_le16(tot_dsds -
1387 tot_prot_dsds);
1388 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.data_address;
1389 }
1390
1391 /* Finish the common fields of CRC pkt */
1392 crc_ctx_pkt->blk_size = cpu_to_le16(blk_size);
1393 crc_ctx_pkt->prot_opts = cpu_to_le16(fw_prot_opts);
1394 crc_ctx_pkt->byte_count = cpu_to_le32(data_bytes);
1395 crc_ctx_pkt->guard_seed = __constant_cpu_to_le16(0);
1396 /* Fibre channel byte count */
1397 cmd_pkt->byte_count = cpu_to_le32(total_bytes);
1398 fcp_dl = (uint32_t *)(crc_ctx_pkt->fcp_cmnd.cdb + 16 +
1399 additional_fcpcdb_len);
1400 *fcp_dl = htonl(total_bytes);
1401
0c470874 1402 if (!data_bytes || cmd->sc_data_direction == DMA_NONE) {
0c470874
AE
1403 cmd_pkt->byte_count = __constant_cpu_to_le32(0);
1404 return QLA_SUCCESS;
1405 }
bad75002
AE
1406 /* Walks data segments */
1407
1408 cmd_pkt->control_flags |=
1409 __constant_cpu_to_le16(CF_DATA_SEG_DESCR_ENABLE);
8cb2049c
AE
1410
1411 if (!bundling && tot_prot_dsds) {
1412 if (qla24xx_walk_and_build_sglist_no_difb(ha, sp,
f83adb61 1413 cur_dsd, tot_dsds, NULL))
8cb2049c
AE
1414 goto crc_queuing_error;
1415 } else if (qla24xx_walk_and_build_sglist(ha, sp, cur_dsd,
f83adb61 1416 (tot_dsds - tot_prot_dsds), NULL))
bad75002
AE
1417 goto crc_queuing_error;
1418
1419 if (bundling && tot_prot_dsds) {
1420 /* Walks dif segments */
bad75002
AE
1421 cmd_pkt->control_flags |=
1422 __constant_cpu_to_le16(CF_DIF_SEG_DESCR_ENABLE);
1423 cur_dsd = (uint32_t *) &crc_ctx_pkt->u.bundling.dif_address;
1424 if (qla24xx_walk_and_build_prot_sglist(ha, sp, cur_dsd,
f83adb61 1425 tot_prot_dsds, NULL))
bad75002
AE
1426 goto crc_queuing_error;
1427 }
1428 return QLA_SUCCESS;
1429
1430crc_queuing_error:
bad75002
AE
1431 /* Cleanup will be performed by the caller */
1432
1433 return QLA_FUNCTION_FAILED;
1434}
2b6c0cee
AV
1435
1436/**
1437 * qla24xx_start_scsi() - Send a SCSI command to the ISP
1438 * @sp: command to send to the ISP
1439 *
cc3ef7bc 1440 * Returns non-zero if a failure occurred, else zero.
2b6c0cee
AV
1441 */
1442int
1443qla24xx_start_scsi(srb_t *sp)
1444{
385d70b4 1445 int ret, nseg;
2b6c0cee 1446 unsigned long flags;
2b6c0cee
AV
1447 uint32_t *clr_ptr;
1448 uint32_t index;
1449 uint32_t handle;
1450 struct cmd_type_7 *cmd_pkt;
2b6c0cee
AV
1451 uint16_t cnt;
1452 uint16_t req_cnt;
1453 uint16_t tot_dsds;
73208dfd
AC
1454 struct req_que *req = NULL;
1455 struct rsp_que *rsp = NULL;
9ba56b95 1456 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
444786d7 1457 struct scsi_qla_host *vha = sp->fcport->vha;
73208dfd 1458 struct qla_hw_data *ha = vha->hw;
2b6c0cee
AV
1459
1460 /* Setup device pointers. */
1461 ret = 0;
73208dfd 1462
59e0b8b0
AC
1463 qla25xx_set_que(sp, &rsp);
1464 req = vha->req;
73208dfd 1465
2b6c0cee
AV
1466 /* So we know we haven't pci_map'ed anything yet */
1467 tot_dsds = 0;
1468
1469 /* Send marker if required */
e315cd28 1470 if (vha->marker_needed != 0) {
7c3df132
SK
1471 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1472 QLA_SUCCESS)
2b6c0cee 1473 return QLA_FUNCTION_FAILED;
e315cd28 1474 vha->marker_needed = 0;
2b6c0cee
AV
1475 }
1476
1477 /* Acquire ring specific lock */
e315cd28 1478 spin_lock_irqsave(&ha->hardware_lock, flags);
2b6c0cee
AV
1479
1480 /* Check for room in outstanding command list. */
e315cd28 1481 handle = req->current_outstanding_cmd;
8d93f550 1482 for (index = 1; index < req->num_outstanding_cmds; index++) {
2b6c0cee 1483 handle++;
8d93f550 1484 if (handle == req->num_outstanding_cmds)
2b6c0cee 1485 handle = 1;
e315cd28 1486 if (!req->outstanding_cmds[handle])
2b6c0cee
AV
1487 break;
1488 }
8d93f550 1489 if (index == req->num_outstanding_cmds)
2b6c0cee
AV
1490 goto queuing_error;
1491
1492 /* Map the sg table so we have an accurate count of sg entries needed */
2c3dfe3f
SJ
1493 if (scsi_sg_count(cmd)) {
1494 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1495 scsi_sg_count(cmd), cmd->sc_data_direction);
1496 if (unlikely(!nseg))
2b6c0cee 1497 goto queuing_error;
2c3dfe3f
SJ
1498 } else
1499 nseg = 0;
1500
385d70b4 1501 tot_dsds = nseg;
7c3df132 1502 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
e315cd28 1503 if (req->cnt < (req_cnt + 2)) {
7c6300e3
JC
1504 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1505 RD_REG_DWORD_RELAXED(req->req_q_out);
e315cd28
AC
1506 if (req->ring_index < cnt)
1507 req->cnt = cnt - req->ring_index;
2b6c0cee 1508 else
e315cd28
AC
1509 req->cnt = req->length -
1510 (req->ring_index - cnt);
a6eb3c9f
CL
1511 if (req->cnt < (req_cnt + 2))
1512 goto queuing_error;
2b6c0cee 1513 }
2b6c0cee
AV
1514
1515 /* Build command packet. */
e315cd28
AC
1516 req->current_outstanding_cmd = handle;
1517 req->outstanding_cmds[handle] = sp;
cf53b069 1518 sp->handle = handle;
9ba56b95 1519 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
e315cd28 1520 req->cnt -= req_cnt;
2b6c0cee 1521
e315cd28 1522 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2afa19a9 1523 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2b6c0cee
AV
1524
1525 /* Zero out remaining portion of packet. */
72df8325 1526 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2b6c0cee
AV
1527 clr_ptr = (uint32_t *)cmd_pkt + 2;
1528 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1529 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1530
1531 /* Set NPORT-ID and LUN number*/
1532 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1533 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1534 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1535 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1536 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
2b6c0cee 1537
9ba56b95 1538 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
0d4be124 1539 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2b6c0cee 1540
50668633 1541 cmd_pkt->task = TSK_SIMPLE;
ff2fc42e 1542
2b6c0cee
AV
1543 /* Load SCSI command packet. */
1544 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
1545 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
1546
385d70b4 1547 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2b6c0cee
AV
1548
1549 /* Build IOCB segments */
1550 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
1551
1552 /* Set total data segment count. */
1553 cmd_pkt->entry_count = (uint8_t)req_cnt;
2afa19a9
AC
1554 /* Specify response queue number where completion should happen */
1555 cmd_pkt->entry_status = (uint8_t) rsp->id;
2b6c0cee 1556 wmb();
2b6c0cee 1557 /* Adjust ring index. */
e315cd28
AC
1558 req->ring_index++;
1559 if (req->ring_index == req->length) {
1560 req->ring_index = 0;
1561 req->ring_ptr = req->ring;
2b6c0cee 1562 } else
e315cd28 1563 req->ring_ptr++;
2b6c0cee
AV
1564
1565 sp->flags |= SRB_DMA_VALID;
2b6c0cee
AV
1566
1567 /* Set chip new ring index. */
08029990
AV
1568 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1569 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
2b6c0cee 1570
4fdfefe5 1571 /* Manage unprocessed RIO/ZIO commands in response queue. */
e315cd28 1572 if (vha->flags.process_response_queue &&
73208dfd 1573 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2afa19a9 1574 qla24xx_process_response_queue(vha, rsp);
4fdfefe5 1575
e315cd28 1576 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1577 return QLA_SUCCESS;
1578
1579queuing_error:
385d70b4
FT
1580 if (tot_dsds)
1581 scsi_dma_unmap(cmd);
1582
e315cd28 1583 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2b6c0cee
AV
1584
1585 return QLA_FUNCTION_FAILED;
1da177e4 1586}
68ca949c 1587
bad75002
AE
1588/**
1589 * qla24xx_dif_start_scsi() - Send a SCSI command to the ISP
1590 * @sp: command to send to the ISP
1591 *
1592 * Returns non-zero if a failure occurred, else zero.
1593 */
1594int
1595qla24xx_dif_start_scsi(srb_t *sp)
1596{
1597 int nseg;
1598 unsigned long flags;
1599 uint32_t *clr_ptr;
1600 uint32_t index;
1601 uint32_t handle;
1602 uint16_t cnt;
1603 uint16_t req_cnt = 0;
1604 uint16_t tot_dsds;
1605 uint16_t tot_prot_dsds;
1606 uint16_t fw_prot_opts = 0;
1607 struct req_que *req = NULL;
1608 struct rsp_que *rsp = NULL;
9ba56b95 1609 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
bad75002
AE
1610 struct scsi_qla_host *vha = sp->fcport->vha;
1611 struct qla_hw_data *ha = vha->hw;
1612 struct cmd_type_crc_2 *cmd_pkt;
1613 uint32_t status = 0;
1614
1615#define QDSS_GOT_Q_SPACE BIT_0
1616
0c470874
AE
1617 /* Only process protection or >16 cdb in this routine */
1618 if (scsi_get_prot_op(cmd) == SCSI_PROT_NORMAL) {
1619 if (cmd->cmd_len <= 16)
1620 return qla24xx_start_scsi(sp);
1621 }
bad75002
AE
1622
1623 /* Setup device pointers. */
1624
1625 qla25xx_set_que(sp, &rsp);
1626 req = vha->req;
1627
1628 /* So we know we haven't pci_map'ed anything yet */
1629 tot_dsds = 0;
1630
1631 /* Send marker if required */
1632 if (vha->marker_needed != 0) {
1633 if (qla2x00_marker(vha, req, rsp, 0, 0, MK_SYNC_ALL) !=
1634 QLA_SUCCESS)
1635 return QLA_FUNCTION_FAILED;
1636 vha->marker_needed = 0;
1637 }
1638
1639 /* Acquire ring specific lock */
1640 spin_lock_irqsave(&ha->hardware_lock, flags);
1641
1642 /* Check for room in outstanding command list. */
1643 handle = req->current_outstanding_cmd;
8d93f550 1644 for (index = 1; index < req->num_outstanding_cmds; index++) {
bad75002 1645 handle++;
8d93f550 1646 if (handle == req->num_outstanding_cmds)
bad75002
AE
1647 handle = 1;
1648 if (!req->outstanding_cmds[handle])
1649 break;
1650 }
1651
8d93f550 1652 if (index == req->num_outstanding_cmds)
bad75002
AE
1653 goto queuing_error;
1654
1655 /* Compute number of required data segments */
1656 /* Map the sg table so we have an accurate count of sg entries needed */
1657 if (scsi_sg_count(cmd)) {
1658 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
1659 scsi_sg_count(cmd), cmd->sc_data_direction);
1660 if (unlikely(!nseg))
1661 goto queuing_error;
1662 else
1663 sp->flags |= SRB_DMA_VALID;
8cb2049c
AE
1664
1665 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1666 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1667 struct qla2_sgx sgx;
1668 uint32_t partial;
1669
1670 memset(&sgx, 0, sizeof(struct qla2_sgx));
1671 sgx.tot_bytes = scsi_bufflen(cmd);
1672 sgx.cur_sg = scsi_sglist(cmd);
1673 sgx.sp = sp;
1674
1675 nseg = 0;
1676 while (qla24xx_get_one_block_sg(
1677 cmd->device->sector_size, &sgx, &partial))
1678 nseg++;
1679 }
bad75002
AE
1680 } else
1681 nseg = 0;
1682
1683 /* number of required data segments */
1684 tot_dsds = nseg;
1685
1686 /* Compute number of required protection segments */
1687 if (qla24xx_configure_prot_mode(sp, &fw_prot_opts)) {
1688 nseg = dma_map_sg(&ha->pdev->dev, scsi_prot_sglist(cmd),
1689 scsi_prot_sg_count(cmd), cmd->sc_data_direction);
1690 if (unlikely(!nseg))
1691 goto queuing_error;
1692 else
1693 sp->flags |= SRB_CRC_PROT_DMA_VALID;
8cb2049c
AE
1694
1695 if ((scsi_get_prot_op(cmd) == SCSI_PROT_READ_INSERT) ||
1696 (scsi_get_prot_op(cmd) == SCSI_PROT_WRITE_STRIP)) {
1697 nseg = scsi_bufflen(cmd) / cmd->device->sector_size;
1698 }
bad75002
AE
1699 } else {
1700 nseg = 0;
1701 }
1702
1703 req_cnt = 1;
1704 /* Total Data and protection sg segment(s) */
1705 tot_prot_dsds = nseg;
1706 tot_dsds += nseg;
1707 if (req->cnt < (req_cnt + 2)) {
7c6300e3
JC
1708 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
1709 RD_REG_DWORD_RELAXED(req->req_q_out);
bad75002
AE
1710 if (req->ring_index < cnt)
1711 req->cnt = cnt - req->ring_index;
1712 else
1713 req->cnt = req->length -
1714 (req->ring_index - cnt);
a6eb3c9f
CL
1715 if (req->cnt < (req_cnt + 2))
1716 goto queuing_error;
bad75002
AE
1717 }
1718
bad75002
AE
1719 status |= QDSS_GOT_Q_SPACE;
1720
1721 /* Build header part of command packet (excluding the OPCODE). */
1722 req->current_outstanding_cmd = handle;
1723 req->outstanding_cmds[handle] = sp;
8cb2049c 1724 sp->handle = handle;
9ba56b95 1725 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
bad75002
AE
1726 req->cnt -= req_cnt;
1727
1728 /* Fill-in common area */
1729 cmd_pkt = (struct cmd_type_crc_2 *)req->ring_ptr;
1730 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
1731
1732 clr_ptr = (uint32_t *)cmd_pkt + 2;
1733 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
1734
1735 /* Set NPORT-ID and LUN number*/
1736 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1737 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
1738 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
1739 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
1740
9ba56b95 1741 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
bad75002
AE
1742 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
1743
1744 /* Total Data and protection segment(s) */
1745 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
1746
1747 /* Build IOCB segments and adjust for data protection segments */
1748 if (qla24xx_build_scsi_crc_2_iocbs(sp, (struct cmd_type_crc_2 *)
1749 req->ring_ptr, tot_dsds, tot_prot_dsds, fw_prot_opts) !=
1750 QLA_SUCCESS)
1751 goto queuing_error;
1752
1753 cmd_pkt->entry_count = (uint8_t)req_cnt;
1754 /* Specify response queue number where completion should happen */
1755 cmd_pkt->entry_status = (uint8_t) rsp->id;
1756 cmd_pkt->timeout = __constant_cpu_to_le16(0);
1757 wmb();
1758
1759 /* Adjust ring index. */
1760 req->ring_index++;
1761 if (req->ring_index == req->length) {
1762 req->ring_index = 0;
1763 req->ring_ptr = req->ring;
1764 } else
1765 req->ring_ptr++;
1766
1767 /* Set chip new ring index. */
1768 WRT_REG_DWORD(req->req_q_in, req->ring_index);
1769 RD_REG_DWORD_RELAXED(&ha->iobase->isp24.hccr);
1770
1771 /* Manage unprocessed RIO/ZIO commands in response queue. */
1772 if (vha->flags.process_response_queue &&
1773 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
1774 qla24xx_process_response_queue(vha, rsp);
1775
1776 spin_unlock_irqrestore(&ha->hardware_lock, flags);
1777
1778 return QLA_SUCCESS;
1779
1780queuing_error:
1781 if (status & QDSS_GOT_Q_SPACE) {
1782 req->outstanding_cmds[handle] = NULL;
1783 req->cnt += req_cnt;
1784 }
1785 /* Cleanup will be performed by the caller (queuecommand) */
1786
1787 spin_unlock_irqrestore(&ha->hardware_lock, flags);
bad75002
AE
1788 return QLA_FUNCTION_FAILED;
1789}
1790
1791
59e0b8b0 1792static void qla25xx_set_que(srb_t *sp, struct rsp_que **rsp)
68ca949c 1793{
9ba56b95 1794 struct scsi_cmnd *cmd = GET_CMD_SP(sp);
68ca949c
AC
1795 struct qla_hw_data *ha = sp->fcport->vha->hw;
1796 int affinity = cmd->request->cpu;
1797
7163ea81 1798 if (ha->flags.cpu_affinity_enabled && affinity >= 0 &&
59e0b8b0 1799 affinity < ha->max_rsp_queues - 1)
68ca949c 1800 *rsp = ha->rsp_q_map[affinity + 1];
59e0b8b0 1801 else
68ca949c 1802 *rsp = ha->rsp_q_map[0];
68ca949c 1803}
ac280b67
AV
1804
1805/* Generic Control-SRB manipulation functions. */
b6a029e1
AE
1806
1807/* hardware_lock assumed to be held. */
1808void *
1809qla2x00_alloc_iocbs_ready(scsi_qla_host_t *vha, srb_t *sp)
1810{
1811 if (qla2x00_reset_active(vha))
1812 return NULL;
1813
1814 return qla2x00_alloc_iocbs(vha, sp);
1815}
1816
d94d10e7
GM
1817void *
1818qla2x00_alloc_iocbs(scsi_qla_host_t *vha, srb_t *sp)
ac280b67 1819{
ac280b67
AV
1820 struct qla_hw_data *ha = vha->hw;
1821 struct req_que *req = ha->req_q_map[0];
1822 device_reg_t __iomem *reg = ISP_QUE_REG(ha, req->id);
1823 uint32_t index, handle;
1824 request_t *pkt;
1825 uint16_t cnt, req_cnt;
1826
1827 pkt = NULL;
1828 req_cnt = 1;
d94d10e7
GM
1829 handle = 0;
1830
1831 if (!sp)
1832 goto skip_cmd_array;
ac280b67
AV
1833
1834 /* Check for room in outstanding command list. */
1835 handle = req->current_outstanding_cmd;
4b4f30cc 1836 for (index = 1; index < req->num_outstanding_cmds; index++) {
ac280b67 1837 handle++;
8d93f550 1838 if (handle == req->num_outstanding_cmds)
ac280b67
AV
1839 handle = 1;
1840 if (!req->outstanding_cmds[handle])
1841 break;
1842 }
8d93f550 1843 if (index == req->num_outstanding_cmds) {
7c3df132 1844 ql_log(ql_log_warn, vha, 0x700b,
d6a03581 1845 "No room on outstanding cmd array.\n");
ac280b67 1846 goto queuing_error;
7c3df132 1847 }
ac280b67 1848
d94d10e7
GM
1849 /* Prep command array. */
1850 req->current_outstanding_cmd = handle;
1851 req->outstanding_cmds[handle] = sp;
1852 sp->handle = handle;
1853
5780790e 1854 /* Adjust entry-counts as needed. */
9ba56b95
GM
1855 if (sp->type != SRB_SCSI_CMD)
1856 req_cnt = sp->iocbs;
5780790e 1857
d94d10e7 1858skip_cmd_array:
ac280b67 1859 /* Check for room on request queue. */
94007037 1860 if (req->cnt < req_cnt + 2) {
f73cb695 1861 if (ha->mqenable || IS_QLA83XX(ha) || IS_QLA27XX(ha))
ac280b67 1862 cnt = RD_REG_DWORD(&reg->isp25mq.req_q_out);
7ec0effd 1863 else if (IS_P3P_TYPE(ha))
d94d10e7 1864 cnt = RD_REG_DWORD(&reg->isp82.req_q_out);
ac280b67
AV
1865 else if (IS_FWI2_CAPABLE(ha))
1866 cnt = RD_REG_DWORD(&reg->isp24.req_q_out);
8ae6d9c7
GM
1867 else if (IS_QLAFX00(ha))
1868 cnt = RD_REG_DWORD(&reg->ispfx00.req_q_out);
ac280b67
AV
1869 else
1870 cnt = qla2x00_debounce_register(
1871 ISP_REQ_Q_OUT(ha, &reg->isp));
1872
1873 if (req->ring_index < cnt)
1874 req->cnt = cnt - req->ring_index;
1875 else
1876 req->cnt = req->length -
1877 (req->ring_index - cnt);
1878 }
94007037 1879 if (req->cnt < req_cnt + 2)
ac280b67
AV
1880 goto queuing_error;
1881
1882 /* Prep packet */
ac280b67 1883 req->cnt -= req_cnt;
ac280b67
AV
1884 pkt = req->ring_ptr;
1885 memset(pkt, 0, REQUEST_ENTRY_SIZE);
8ae6d9c7 1886 if (IS_QLAFX00(ha)) {
1f8deefe
SK
1887 WRT_REG_BYTE((void __iomem *)&pkt->entry_count, req_cnt);
1888 WRT_REG_WORD((void __iomem *)&pkt->handle, handle);
8ae6d9c7
GM
1889 } else {
1890 pkt->entry_count = req_cnt;
1891 pkt->handle = handle;
1892 }
ac280b67
AV
1893
1894queuing_error:
1895 return pkt;
1896}
1897
ac280b67
AV
1898static void
1899qla24xx_login_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1900{
9ba56b95 1901 struct srb_iocb *lio = &sp->u.iocb_cmd;
ac280b67
AV
1902
1903 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1904 logio->control_flags = cpu_to_le16(LCF_COMMAND_PLOGI);
4916392b 1905 if (lio->u.logio.flags & SRB_LOGIN_COND_PLOGI)
ac280b67 1906 logio->control_flags |= cpu_to_le16(LCF_COND_PLOGI);
4916392b 1907 if (lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI)
ac280b67
AV
1908 logio->control_flags |= cpu_to_le16(LCF_SKIP_PRLI);
1909 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1910 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1911 logio->port_id[1] = sp->fcport->d_id.b.area;
1912 logio->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1913 logio->vp_index = sp->fcport->vha->vp_idx;
ac280b67
AV
1914}
1915
1916static void
1917qla2x00_login_iocb(srb_t *sp, struct mbx_entry *mbx)
1918{
1919 struct qla_hw_data *ha = sp->fcport->vha->hw;
9ba56b95 1920 struct srb_iocb *lio = &sp->u.iocb_cmd;
ac280b67
AV
1921 uint16_t opts;
1922
b963752f 1923 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1924 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1925 mbx->mb0 = cpu_to_le16(MBC_LOGIN_FABRIC_PORT);
4916392b
MI
1926 opts = lio->u.logio.flags & SRB_LOGIN_COND_PLOGI ? BIT_0 : 0;
1927 opts |= lio->u.logio.flags & SRB_LOGIN_SKIP_PRLI ? BIT_1 : 0;
ac280b67
AV
1928 if (HAS_EXTENDED_IDS(ha)) {
1929 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1930 mbx->mb10 = cpu_to_le16(opts);
1931 } else {
1932 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | opts);
1933 }
1934 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1935 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1936 sp->fcport->d_id.b.al_pa);
c6d39e23 1937 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
ac280b67
AV
1938}
1939
1940static void
1941qla24xx_logout_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1942{
1943 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1944 logio->control_flags =
1945 cpu_to_le16(LCF_COMMAND_LOGO|LCF_IMPL_LOGO);
1946 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
1947 logio->port_id[0] = sp->fcport->d_id.b.al_pa;
1948 logio->port_id[1] = sp->fcport->d_id.b.area;
1949 logio->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 1950 logio->vp_index = sp->fcport->vha->vp_idx;
ac280b67
AV
1951}
1952
1953static void
1954qla2x00_logout_iocb(srb_t *sp, struct mbx_entry *mbx)
1955{
1956 struct qla_hw_data *ha = sp->fcport->vha->hw;
1957
b963752f 1958 mbx->entry_type = MBX_IOCB_TYPE;
ac280b67
AV
1959 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1960 mbx->mb0 = cpu_to_le16(MBC_LOGOUT_FABRIC_PORT);
1961 mbx->mb1 = HAS_EXTENDED_IDS(ha) ?
1962 cpu_to_le16(sp->fcport->loop_id):
1963 cpu_to_le16(sp->fcport->loop_id << 8);
1964 mbx->mb2 = cpu_to_le16(sp->fcport->d_id.b.domain);
1965 mbx->mb3 = cpu_to_le16(sp->fcport->d_id.b.area << 8 |
1966 sp->fcport->d_id.b.al_pa);
c6d39e23 1967 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
ac280b67
AV
1968 /* Implicit: mbx->mbx10 = 0. */
1969}
1970
5ff1d584
AV
1971static void
1972qla24xx_adisc_iocb(srb_t *sp, struct logio_entry_24xx *logio)
1973{
1974 logio->entry_type = LOGINOUT_PORT_IOCB_TYPE;
1975 logio->control_flags = cpu_to_le16(LCF_COMMAND_ADISC);
1976 logio->nport_handle = cpu_to_le16(sp->fcport->loop_id);
c6d39e23 1977 logio->vp_index = sp->fcport->vha->vp_idx;
5ff1d584
AV
1978}
1979
1980static void
1981qla2x00_adisc_iocb(srb_t *sp, struct mbx_entry *mbx)
1982{
1983 struct qla_hw_data *ha = sp->fcport->vha->hw;
1984
1985 mbx->entry_type = MBX_IOCB_TYPE;
1986 SET_TARGET_ID(ha, mbx->loop_id, sp->fcport->loop_id);
1987 mbx->mb0 = cpu_to_le16(MBC_GET_PORT_DATABASE);
1988 if (HAS_EXTENDED_IDS(ha)) {
1989 mbx->mb1 = cpu_to_le16(sp->fcport->loop_id);
1990 mbx->mb10 = cpu_to_le16(BIT_0);
1991 } else {
1992 mbx->mb1 = cpu_to_le16((sp->fcport->loop_id << 8) | BIT_0);
1993 }
1994 mbx->mb2 = cpu_to_le16(MSW(ha->async_pd_dma));
1995 mbx->mb3 = cpu_to_le16(LSW(ha->async_pd_dma));
1996 mbx->mb6 = cpu_to_le16(MSW(MSD(ha->async_pd_dma)));
1997 mbx->mb7 = cpu_to_le16(LSW(MSD(ha->async_pd_dma)));
c6d39e23 1998 mbx->mb9 = cpu_to_le16(sp->fcport->vha->vp_idx);
5ff1d584
AV
1999}
2000
3822263e
MI
2001static void
2002qla24xx_tm_iocb(srb_t *sp, struct tsk_mgmt_entry *tsk)
2003{
2004 uint32_t flags;
9cb78c16 2005 uint64_t lun;
3822263e
MI
2006 struct fc_port *fcport = sp->fcport;
2007 scsi_qla_host_t *vha = fcport->vha;
2008 struct qla_hw_data *ha = vha->hw;
9ba56b95 2009 struct srb_iocb *iocb = &sp->u.iocb_cmd;
3822263e
MI
2010 struct req_que *req = vha->req;
2011
2012 flags = iocb->u.tmf.flags;
2013 lun = iocb->u.tmf.lun;
2014
2015 tsk->entry_type = TSK_MGMT_IOCB_TYPE;
2016 tsk->entry_count = 1;
2017 tsk->handle = MAKE_HANDLE(req->id, tsk->handle);
2018 tsk->nport_handle = cpu_to_le16(fcport->loop_id);
2019 tsk->timeout = cpu_to_le16(ha->r_a_tov / 10 * 2);
2020 tsk->control_flags = cpu_to_le32(flags);
2021 tsk->port_id[0] = fcport->d_id.b.al_pa;
2022 tsk->port_id[1] = fcport->d_id.b.area;
2023 tsk->port_id[2] = fcport->d_id.b.domain;
c6d39e23 2024 tsk->vp_index = fcport->vha->vp_idx;
3822263e
MI
2025
2026 if (flags == TCF_LUN_RESET) {
2027 int_to_scsilun(lun, &tsk->lun);
2028 host_to_fcp_swap((uint8_t *)&tsk->lun,
2029 sizeof(tsk->lun));
2030 }
2031}
2032
9a069e19
GM
2033static void
2034qla24xx_els_iocb(srb_t *sp, struct els_entry_24xx *els_iocb)
2035{
9ba56b95 2036 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9a069e19
GM
2037
2038 els_iocb->entry_type = ELS_IOCB_TYPE;
2039 els_iocb->entry_count = 1;
2040 els_iocb->sys_define = 0;
2041 els_iocb->entry_status = 0;
2042 els_iocb->handle = sp->handle;
2043 els_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2044 els_iocb->tx_dsd_count = __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
c6d39e23 2045 els_iocb->vp_index = sp->fcport->vha->vp_idx;
9a069e19
GM
2046 els_iocb->sof_type = EST_SOFI3;
2047 els_iocb->rx_dsd_count = __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2048
4916392b 2049 els_iocb->opcode =
9ba56b95 2050 sp->type == SRB_ELS_CMD_RPT ?
4916392b
MI
2051 bsg_job->request->rqst_data.r_els.els_code :
2052 bsg_job->request->rqst_data.h_els.command_code;
9a069e19
GM
2053 els_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2054 els_iocb->port_id[1] = sp->fcport->d_id.b.area;
2055 els_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2056 els_iocb->control_flags = 0;
2057 els_iocb->rx_byte_count =
2058 cpu_to_le32(bsg_job->reply_payload.payload_len);
2059 els_iocb->tx_byte_count =
2060 cpu_to_le32(bsg_job->request_payload.payload_len);
2061
2062 els_iocb->tx_address[0] = cpu_to_le32(LSD(sg_dma_address
2063 (bsg_job->request_payload.sg_list)));
2064 els_iocb->tx_address[1] = cpu_to_le32(MSD(sg_dma_address
2065 (bsg_job->request_payload.sg_list)));
2066 els_iocb->tx_len = cpu_to_le32(sg_dma_len
2067 (bsg_job->request_payload.sg_list));
2068
2069 els_iocb->rx_address[0] = cpu_to_le32(LSD(sg_dma_address
2070 (bsg_job->reply_payload.sg_list)));
2071 els_iocb->rx_address[1] = cpu_to_le32(MSD(sg_dma_address
2072 (bsg_job->reply_payload.sg_list)));
2073 els_iocb->rx_len = cpu_to_le32(sg_dma_len
2074 (bsg_job->reply_payload.sg_list));
fabbb8df
JC
2075
2076 sp->fcport->vha->qla_stats.control_requests++;
9a069e19
GM
2077}
2078
9bc4f4fb
HZ
2079static void
2080qla2x00_ct_iocb(srb_t *sp, ms_iocb_entry_t *ct_iocb)
2081{
2082 uint16_t avail_dsds;
2083 uint32_t *cur_dsd;
2084 struct scatterlist *sg;
2085 int index;
2086 uint16_t tot_dsds;
2087 scsi_qla_host_t *vha = sp->fcport->vha;
2088 struct qla_hw_data *ha = vha->hw;
9ba56b95 2089 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9bc4f4fb
HZ
2090 int loop_iterartion = 0;
2091 int cont_iocb_prsnt = 0;
2092 int entry_count = 1;
2093
2094 memset(ct_iocb, 0, sizeof(ms_iocb_entry_t));
2095 ct_iocb->entry_type = CT_IOCB_TYPE;
2096 ct_iocb->entry_status = 0;
2097 ct_iocb->handle1 = sp->handle;
2098 SET_TARGET_ID(ha, ct_iocb->loop_id, sp->fcport->loop_id);
2099 ct_iocb->status = __constant_cpu_to_le16(0);
2100 ct_iocb->control_flags = __constant_cpu_to_le16(0);
2101 ct_iocb->timeout = 0;
2102 ct_iocb->cmd_dsd_count =
2103 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2104 ct_iocb->total_dsd_count =
2105 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt + 1);
2106 ct_iocb->req_bytecount =
2107 cpu_to_le32(bsg_job->request_payload.payload_len);
2108 ct_iocb->rsp_bytecount =
2109 cpu_to_le32(bsg_job->reply_payload.payload_len);
2110
2111 ct_iocb->dseg_req_address[0] = cpu_to_le32(LSD(sg_dma_address
2112 (bsg_job->request_payload.sg_list)));
2113 ct_iocb->dseg_req_address[1] = cpu_to_le32(MSD(sg_dma_address
2114 (bsg_job->request_payload.sg_list)));
2115 ct_iocb->dseg_req_length = ct_iocb->req_bytecount;
2116
2117 ct_iocb->dseg_rsp_address[0] = cpu_to_le32(LSD(sg_dma_address
2118 (bsg_job->reply_payload.sg_list)));
2119 ct_iocb->dseg_rsp_address[1] = cpu_to_le32(MSD(sg_dma_address
2120 (bsg_job->reply_payload.sg_list)));
2121 ct_iocb->dseg_rsp_length = ct_iocb->rsp_bytecount;
2122
2123 avail_dsds = 1;
2124 cur_dsd = (uint32_t *)ct_iocb->dseg_rsp_address;
2125 index = 0;
2126 tot_dsds = bsg_job->reply_payload.sg_cnt;
2127
2128 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2129 dma_addr_t sle_dma;
2130 cont_a64_entry_t *cont_pkt;
2131
2132 /* Allocate additional continuation packets? */
2133 if (avail_dsds == 0) {
2134 /*
2135 * Five DSDs are available in the Cont.
2136 * Type 1 IOCB.
2137 */
0d2aa38e
GM
2138 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2139 vha->hw->req_q_map[0]);
9bc4f4fb
HZ
2140 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2141 avail_dsds = 5;
2142 cont_iocb_prsnt = 1;
2143 entry_count++;
2144 }
2145
2146 sle_dma = sg_dma_address(sg);
2147 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2148 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2149 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2150 loop_iterartion++;
2151 avail_dsds--;
2152 }
2153 ct_iocb->entry_count = entry_count;
fabbb8df
JC
2154
2155 sp->fcport->vha->qla_stats.control_requests++;
9bc4f4fb
HZ
2156}
2157
9a069e19
GM
2158static void
2159qla24xx_ct_iocb(srb_t *sp, struct ct_entry_24xx *ct_iocb)
2160{
2161 uint16_t avail_dsds;
2162 uint32_t *cur_dsd;
2163 struct scatterlist *sg;
2164 int index;
2165 uint16_t tot_dsds;
2166 scsi_qla_host_t *vha = sp->fcport->vha;
0d2aa38e 2167 struct qla_hw_data *ha = vha->hw;
9ba56b95 2168 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
9a069e19
GM
2169 int loop_iterartion = 0;
2170 int cont_iocb_prsnt = 0;
2171 int entry_count = 1;
2172
2173 ct_iocb->entry_type = CT_IOCB_TYPE;
2174 ct_iocb->entry_status = 0;
2175 ct_iocb->sys_define = 0;
2176 ct_iocb->handle = sp->handle;
2177
2178 ct_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
c6d39e23 2179 ct_iocb->vp_index = sp->fcport->vha->vp_idx;
9a069e19
GM
2180 ct_iocb->comp_status = __constant_cpu_to_le16(0);
2181
2182 ct_iocb->cmd_dsd_count =
2183 __constant_cpu_to_le16(bsg_job->request_payload.sg_cnt);
2184 ct_iocb->timeout = 0;
2185 ct_iocb->rsp_dsd_count =
2186 __constant_cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2187 ct_iocb->rsp_byte_count =
2188 cpu_to_le32(bsg_job->reply_payload.payload_len);
2189 ct_iocb->cmd_byte_count =
2190 cpu_to_le32(bsg_job->request_payload.payload_len);
2191 ct_iocb->dseg_0_address[0] = cpu_to_le32(LSD(sg_dma_address
2192 (bsg_job->request_payload.sg_list)));
2193 ct_iocb->dseg_0_address[1] = cpu_to_le32(MSD(sg_dma_address
2194 (bsg_job->request_payload.sg_list)));
2195 ct_iocb->dseg_0_len = cpu_to_le32(sg_dma_len
2196 (bsg_job->request_payload.sg_list));
2197
2198 avail_dsds = 1;
2199 cur_dsd = (uint32_t *)ct_iocb->dseg_1_address;
2200 index = 0;
2201 tot_dsds = bsg_job->reply_payload.sg_cnt;
2202
2203 for_each_sg(bsg_job->reply_payload.sg_list, sg, tot_dsds, index) {
2204 dma_addr_t sle_dma;
2205 cont_a64_entry_t *cont_pkt;
2206
2207 /* Allocate additional continuation packets? */
2208 if (avail_dsds == 0) {
2209 /*
2210 * Five DSDs are available in the Cont.
2211 * Type 1 IOCB.
2212 */
0d2aa38e
GM
2213 cont_pkt = qla2x00_prep_cont_type1_iocb(vha,
2214 ha->req_q_map[0]);
9a069e19
GM
2215 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2216 avail_dsds = 5;
2217 cont_iocb_prsnt = 1;
2218 entry_count++;
2219 }
2220
2221 sle_dma = sg_dma_address(sg);
2222 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2223 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2224 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2225 loop_iterartion++;
2226 avail_dsds--;
2227 }
2228 ct_iocb->entry_count = entry_count;
2229}
2230
5162cf0c
GM
2231/*
2232 * qla82xx_start_scsi() - Send a SCSI command to the ISP
2233 * @sp: command to send to the ISP
2234 *
2235 * Returns non-zero if a failure occurred, else zero.
2236 */
2237int
2238qla82xx_start_scsi(srb_t *sp)
2239{
2240 int ret, nseg;
2241 unsigned long flags;
2242 struct scsi_cmnd *cmd;
2243 uint32_t *clr_ptr;
2244 uint32_t index;
2245 uint32_t handle;
2246 uint16_t cnt;
2247 uint16_t req_cnt;
2248 uint16_t tot_dsds;
2249 struct device_reg_82xx __iomem *reg;
2250 uint32_t dbval;
2251 uint32_t *fcp_dl;
2252 uint8_t additional_cdb_len;
2253 struct ct6_dsd *ctx;
2254 struct scsi_qla_host *vha = sp->fcport->vha;
2255 struct qla_hw_data *ha = vha->hw;
2256 struct req_que *req = NULL;
2257 struct rsp_que *rsp = NULL;
5162cf0c
GM
2258
2259 /* Setup device pointers. */
2260 ret = 0;
2261 reg = &ha->iobase->isp82;
9ba56b95 2262 cmd = GET_CMD_SP(sp);
5162cf0c
GM
2263 req = vha->req;
2264 rsp = ha->rsp_q_map[0];
2265
2266 /* So we know we haven't pci_map'ed anything yet */
2267 tot_dsds = 0;
2268
2269 dbval = 0x04 | (ha->portnum << 5);
2270
2271 /* Send marker if required */
2272 if (vha->marker_needed != 0) {
2273 if (qla2x00_marker(vha, req,
2274 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS) {
2275 ql_log(ql_log_warn, vha, 0x300c,
2276 "qla2x00_marker failed for cmd=%p.\n", cmd);
2277 return QLA_FUNCTION_FAILED;
2278 }
2279 vha->marker_needed = 0;
2280 }
2281
2282 /* Acquire ring specific lock */
2283 spin_lock_irqsave(&ha->hardware_lock, flags);
2284
2285 /* Check for room in outstanding command list. */
2286 handle = req->current_outstanding_cmd;
8d93f550 2287 for (index = 1; index < req->num_outstanding_cmds; index++) {
5162cf0c 2288 handle++;
8d93f550 2289 if (handle == req->num_outstanding_cmds)
5162cf0c
GM
2290 handle = 1;
2291 if (!req->outstanding_cmds[handle])
2292 break;
2293 }
8d93f550 2294 if (index == req->num_outstanding_cmds)
5162cf0c
GM
2295 goto queuing_error;
2296
2297 /* Map the sg table so we have an accurate count of sg entries needed */
2298 if (scsi_sg_count(cmd)) {
2299 nseg = dma_map_sg(&ha->pdev->dev, scsi_sglist(cmd),
2300 scsi_sg_count(cmd), cmd->sc_data_direction);
2301 if (unlikely(!nseg))
2302 goto queuing_error;
2303 } else
2304 nseg = 0;
2305
2306 tot_dsds = nseg;
2307
2308 if (tot_dsds > ql2xshiftctondsd) {
2309 struct cmd_type_6 *cmd_pkt;
2310 uint16_t more_dsd_lists = 0;
2311 struct dsd_dma *dsd_ptr;
2312 uint16_t i;
2313
2314 more_dsd_lists = qla24xx_calc_dsd_lists(tot_dsds);
2315 if ((more_dsd_lists + ha->gbl_dsd_inuse) >= NUM_DSD_CHAIN) {
2316 ql_dbg(ql_dbg_io, vha, 0x300d,
2317 "Num of DSD list %d is than %d for cmd=%p.\n",
2318 more_dsd_lists + ha->gbl_dsd_inuse, NUM_DSD_CHAIN,
2319 cmd);
2320 goto queuing_error;
2321 }
2322
2323 if (more_dsd_lists <= ha->gbl_dsd_avail)
2324 goto sufficient_dsds;
2325 else
2326 more_dsd_lists -= ha->gbl_dsd_avail;
2327
2328 for (i = 0; i < more_dsd_lists; i++) {
2329 dsd_ptr = kzalloc(sizeof(struct dsd_dma), GFP_ATOMIC);
2330 if (!dsd_ptr) {
2331 ql_log(ql_log_fatal, vha, 0x300e,
2332 "Failed to allocate memory for dsd_dma "
2333 "for cmd=%p.\n", cmd);
2334 goto queuing_error;
2335 }
2336
2337 dsd_ptr->dsd_addr = dma_pool_alloc(ha->dl_dma_pool,
2338 GFP_ATOMIC, &dsd_ptr->dsd_list_dma);
2339 if (!dsd_ptr->dsd_addr) {
2340 kfree(dsd_ptr);
2341 ql_log(ql_log_fatal, vha, 0x300f,
2342 "Failed to allocate memory for dsd_addr "
2343 "for cmd=%p.\n", cmd);
2344 goto queuing_error;
2345 }
2346 list_add_tail(&dsd_ptr->list, &ha->gbl_dsd_list);
2347 ha->gbl_dsd_avail++;
2348 }
2349
2350sufficient_dsds:
2351 req_cnt = 1;
2352
2353 if (req->cnt < (req_cnt + 2)) {
2354 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2355 &reg->req_q_out[0]);
2356 if (req->ring_index < cnt)
2357 req->cnt = cnt - req->ring_index;
2358 else
2359 req->cnt = req->length -
2360 (req->ring_index - cnt);
a6eb3c9f
CL
2361 if (req->cnt < (req_cnt + 2))
2362 goto queuing_error;
5162cf0c
GM
2363 }
2364
9ba56b95
GM
2365 ctx = sp->u.scmd.ctx =
2366 mempool_alloc(ha->ctx_mempool, GFP_ATOMIC);
2367 if (!ctx) {
5162cf0c
GM
2368 ql_log(ql_log_fatal, vha, 0x3010,
2369 "Failed to allocate ctx for cmd=%p.\n", cmd);
2370 goto queuing_error;
2371 }
9ba56b95 2372
5162cf0c
GM
2373 memset(ctx, 0, sizeof(struct ct6_dsd));
2374 ctx->fcp_cmnd = dma_pool_alloc(ha->fcp_cmnd_dma_pool,
2375 GFP_ATOMIC, &ctx->fcp_cmnd_dma);
2376 if (!ctx->fcp_cmnd) {
2377 ql_log(ql_log_fatal, vha, 0x3011,
2378 "Failed to allocate fcp_cmnd for cmd=%p.\n", cmd);
841f97bf 2379 goto queuing_error;
5162cf0c
GM
2380 }
2381
2382 /* Initialize the DSD list and dma handle */
2383 INIT_LIST_HEAD(&ctx->dsd_list);
2384 ctx->dsd_use_cnt = 0;
2385
2386 if (cmd->cmd_len > 16) {
2387 additional_cdb_len = cmd->cmd_len - 16;
2388 if ((cmd->cmd_len % 4) != 0) {
2389 /* SCSI command bigger than 16 bytes must be
2390 * multiple of 4
2391 */
2392 ql_log(ql_log_warn, vha, 0x3012,
2393 "scsi cmd len %d not multiple of 4 "
2394 "for cmd=%p.\n", cmd->cmd_len, cmd);
2395 goto queuing_error_fcp_cmnd;
2396 }
2397 ctx->fcp_cmnd_len = 12 + cmd->cmd_len + 4;
2398 } else {
2399 additional_cdb_len = 0;
2400 ctx->fcp_cmnd_len = 12 + 16 + 4;
2401 }
2402
2403 cmd_pkt = (struct cmd_type_6 *)req->ring_ptr;
2404 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2405
2406 /* Zero out remaining portion of packet. */
2407 /* tagged queuing modifier -- default is TSK_SIMPLE (0). */
2408 clr_ptr = (uint32_t *)cmd_pkt + 2;
2409 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2410 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2411
2412 /* Set NPORT-ID and LUN number*/
2413 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2414 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2415 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2416 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 2417 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
5162cf0c
GM
2418
2419 /* Build IOCB segments */
2420 if (qla24xx_build_scsi_type_6_iocbs(sp, cmd_pkt, tot_dsds))
2421 goto queuing_error_fcp_cmnd;
2422
9ba56b95 2423 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
5162cf0c
GM
2424 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun, sizeof(cmd_pkt->lun));
2425
2426 /* build FCP_CMND IU */
2427 memset(ctx->fcp_cmnd, 0, sizeof(struct fcp_cmnd));
9ba56b95 2428 int_to_scsilun(cmd->device->lun, &ctx->fcp_cmnd->lun);
5162cf0c
GM
2429 ctx->fcp_cmnd->additional_cdb_len = additional_cdb_len;
2430
2431 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2432 ctx->fcp_cmnd->additional_cdb_len |= 1;
2433 else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
2434 ctx->fcp_cmnd->additional_cdb_len |= 2;
2435
a00f6296
SK
2436 /* Populate the FCP_PRIO. */
2437 if (ha->flags.fcp_prio_enabled)
2438 ctx->fcp_cmnd->task_attribute |=
2439 sp->fcport->fcp_prio << 3;
2440
5162cf0c
GM
2441 memcpy(ctx->fcp_cmnd->cdb, cmd->cmnd, cmd->cmd_len);
2442
2443 fcp_dl = (uint32_t *)(ctx->fcp_cmnd->cdb + 16 +
2444 additional_cdb_len);
2445 *fcp_dl = htonl((uint32_t)scsi_bufflen(cmd));
2446
2447 cmd_pkt->fcp_cmnd_dseg_len = cpu_to_le16(ctx->fcp_cmnd_len);
2448 cmd_pkt->fcp_cmnd_dseg_address[0] =
2449 cpu_to_le32(LSD(ctx->fcp_cmnd_dma));
2450 cmd_pkt->fcp_cmnd_dseg_address[1] =
2451 cpu_to_le32(MSD(ctx->fcp_cmnd_dma));
2452
2453 sp->flags |= SRB_FCP_CMND_DMA_VALID;
2454 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2455 /* Set total data segment count. */
2456 cmd_pkt->entry_count = (uint8_t)req_cnt;
2457 /* Specify response queue number where
2458 * completion should happen
2459 */
2460 cmd_pkt->entry_status = (uint8_t) rsp->id;
2461 } else {
2462 struct cmd_type_7 *cmd_pkt;
2463 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2464 if (req->cnt < (req_cnt + 2)) {
2465 cnt = (uint16_t)RD_REG_DWORD_RELAXED(
2466 &reg->req_q_out[0]);
2467 if (req->ring_index < cnt)
2468 req->cnt = cnt - req->ring_index;
2469 else
2470 req->cnt = req->length -
2471 (req->ring_index - cnt);
2472 }
2473 if (req->cnt < (req_cnt + 2))
2474 goto queuing_error;
2475
2476 cmd_pkt = (struct cmd_type_7 *)req->ring_ptr;
2477 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2478
2479 /* Zero out remaining portion of packet. */
2480 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2481 clr_ptr = (uint32_t *)cmd_pkt + 2;
2482 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2483 cmd_pkt->dseg_count = cpu_to_le16(tot_dsds);
2484
2485 /* Set NPORT-ID and LUN number*/
2486 cmd_pkt->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2487 cmd_pkt->port_id[0] = sp->fcport->d_id.b.al_pa;
2488 cmd_pkt->port_id[1] = sp->fcport->d_id.b.area;
2489 cmd_pkt->port_id[2] = sp->fcport->d_id.b.domain;
c6d39e23 2490 cmd_pkt->vp_index = sp->fcport->vha->vp_idx;
5162cf0c 2491
9ba56b95 2492 int_to_scsilun(cmd->device->lun, &cmd_pkt->lun);
5162cf0c 2493 host_to_fcp_swap((uint8_t *)&cmd_pkt->lun,
9ba56b95 2494 sizeof(cmd_pkt->lun));
5162cf0c 2495
a00f6296
SK
2496 /* Populate the FCP_PRIO. */
2497 if (ha->flags.fcp_prio_enabled)
2498 cmd_pkt->task |= sp->fcport->fcp_prio << 3;
2499
5162cf0c
GM
2500 /* Load SCSI command packet. */
2501 memcpy(cmd_pkt->fcp_cdb, cmd->cmnd, cmd->cmd_len);
2502 host_to_fcp_swap(cmd_pkt->fcp_cdb, sizeof(cmd_pkt->fcp_cdb));
2503
2504 cmd_pkt->byte_count = cpu_to_le32((uint32_t)scsi_bufflen(cmd));
2505
2506 /* Build IOCB segments */
2507 qla24xx_build_scsi_iocbs(sp, cmd_pkt, tot_dsds);
2508
2509 /* Set total data segment count. */
2510 cmd_pkt->entry_count = (uint8_t)req_cnt;
2511 /* Specify response queue number where
2512 * completion should happen.
2513 */
2514 cmd_pkt->entry_status = (uint8_t) rsp->id;
2515
2516 }
2517 /* Build command packet. */
2518 req->current_outstanding_cmd = handle;
2519 req->outstanding_cmds[handle] = sp;
2520 sp->handle = handle;
9ba56b95 2521 cmd->host_scribble = (unsigned char *)(unsigned long)handle;
5162cf0c
GM
2522 req->cnt -= req_cnt;
2523 wmb();
2524
2525 /* Adjust ring index. */
2526 req->ring_index++;
2527 if (req->ring_index == req->length) {
2528 req->ring_index = 0;
2529 req->ring_ptr = req->ring;
2530 } else
2531 req->ring_ptr++;
2532
2533 sp->flags |= SRB_DMA_VALID;
2534
2535 /* Set chip new ring index. */
2536 /* write, read and verify logic */
2537 dbval = dbval | (req->id << 8) | (req->ring_index << 16);
2538 if (ql2xdbwr)
2539 qla82xx_wr_32(ha, ha->nxdb_wr_ptr, dbval);
2540 else {
2541 WRT_REG_DWORD(
2542 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2543 dbval);
2544 wmb();
fa492630 2545 while (RD_REG_DWORD((void __iomem *)ha->nxdb_rd_ptr) != dbval) {
5162cf0c
GM
2546 WRT_REG_DWORD(
2547 (unsigned long __iomem *)ha->nxdb_wr_ptr,
2548 dbval);
2549 wmb();
2550 }
2551 }
2552
2553 /* Manage unprocessed RIO/ZIO commands in response queue. */
2554 if (vha->flags.process_response_queue &&
2555 rsp->ring_ptr->signature != RESPONSE_PROCESSED)
2556 qla24xx_process_response_queue(vha, rsp);
2557
2558 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2559 return QLA_SUCCESS;
2560
2561queuing_error_fcp_cmnd:
2562 dma_pool_free(ha->fcp_cmnd_dma_pool, ctx->fcp_cmnd, ctx->fcp_cmnd_dma);
2563queuing_error:
2564 if (tot_dsds)
2565 scsi_dma_unmap(cmd);
2566
9ba56b95
GM
2567 if (sp->u.scmd.ctx) {
2568 mempool_free(sp->u.scmd.ctx, ha->ctx_mempool);
2569 sp->u.scmd.ctx = NULL;
5162cf0c
GM
2570 }
2571 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2572
2573 return QLA_FUNCTION_FAILED;
2574}
2575
6d78e557 2576static void
4440e46d
AB
2577qla24xx_abort_iocb(srb_t *sp, struct abort_entry_24xx *abt_iocb)
2578{
2579 struct srb_iocb *aio = &sp->u.iocb_cmd;
2580 scsi_qla_host_t *vha = sp->fcport->vha;
2581 struct req_que *req = vha->req;
2582
2583 memset(abt_iocb, 0, sizeof(struct abort_entry_24xx));
2584 abt_iocb->entry_type = ABORT_IOCB_TYPE;
2585 abt_iocb->entry_count = 1;
2586 abt_iocb->handle = cpu_to_le32(MAKE_HANDLE(req->id, sp->handle));
2587 abt_iocb->nport_handle = cpu_to_le16(sp->fcport->loop_id);
2588 abt_iocb->handle_to_abort =
2589 cpu_to_le32(MAKE_HANDLE(req->id, aio->u.abt.cmd_hndl));
2590 abt_iocb->port_id[0] = sp->fcport->d_id.b.al_pa;
2591 abt_iocb->port_id[1] = sp->fcport->d_id.b.area;
2592 abt_iocb->port_id[2] = sp->fcport->d_id.b.domain;
2593 abt_iocb->vp_index = vha->vp_idx;
2594 abt_iocb->req_que_no = cpu_to_le16(req->id);
2595 /* Send the command to the firmware */
2596 wmb();
2597}
2598
ac280b67
AV
2599int
2600qla2x00_start_sp(srb_t *sp)
2601{
2602 int rval;
2603 struct qla_hw_data *ha = sp->fcport->vha->hw;
2604 void *pkt;
ac280b67
AV
2605 unsigned long flags;
2606
2607 rval = QLA_FUNCTION_FAILED;
2608 spin_lock_irqsave(&ha->hardware_lock, flags);
d94d10e7 2609 pkt = qla2x00_alloc_iocbs(sp->fcport->vha, sp);
7c3df132
SK
2610 if (!pkt) {
2611 ql_log(ql_log_warn, sp->fcport->vha, 0x700c,
2612 "qla2x00_alloc_iocbs failed.\n");
ac280b67 2613 goto done;
7c3df132 2614 }
ac280b67
AV
2615
2616 rval = QLA_SUCCESS;
9ba56b95 2617 switch (sp->type) {
ac280b67
AV
2618 case SRB_LOGIN_CMD:
2619 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2620 qla24xx_login_iocb(sp, pkt) :
ac280b67
AV
2621 qla2x00_login_iocb(sp, pkt);
2622 break;
2623 case SRB_LOGOUT_CMD:
2624 IS_FWI2_CAPABLE(ha) ?
5ff1d584 2625 qla24xx_logout_iocb(sp, pkt) :
ac280b67
AV
2626 qla2x00_logout_iocb(sp, pkt);
2627 break;
9a069e19
GM
2628 case SRB_ELS_CMD_RPT:
2629 case SRB_ELS_CMD_HST:
2630 qla24xx_els_iocb(sp, pkt);
2631 break;
2632 case SRB_CT_CMD:
9bc4f4fb 2633 IS_FWI2_CAPABLE(ha) ?
5780790e
AV
2634 qla24xx_ct_iocb(sp, pkt) :
2635 qla2x00_ct_iocb(sp, pkt);
9a069e19 2636 break;
5ff1d584
AV
2637 case SRB_ADISC_CMD:
2638 IS_FWI2_CAPABLE(ha) ?
2639 qla24xx_adisc_iocb(sp, pkt) :
2640 qla2x00_adisc_iocb(sp, pkt);
2641 break;
3822263e 2642 case SRB_TM_CMD:
8ae6d9c7
GM
2643 IS_QLAFX00(ha) ?
2644 qlafx00_tm_iocb(sp, pkt) :
2645 qla24xx_tm_iocb(sp, pkt);
2646 break;
2647 case SRB_FXIOCB_DCMD:
2648 case SRB_FXIOCB_BCMD:
2649 qlafx00_fxdisc_iocb(sp, pkt);
2650 break;
2651 case SRB_ABT_CMD:
4440e46d
AB
2652 IS_QLAFX00(ha) ?
2653 qlafx00_abort_iocb(sp, pkt) :
2654 qla24xx_abort_iocb(sp, pkt);
3822263e 2655 break;
ac280b67
AV
2656 default:
2657 break;
2658 }
2659
2660 wmb();
5162cf0c 2661 qla2x00_start_iocbs(sp->fcport->vha, ha->req_q_map[0]);
ac280b67
AV
2662done:
2663 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2664 return rval;
2665}
a9b6f722
SK
2666
2667static void
2668qla25xx_build_bidir_iocb(srb_t *sp, struct scsi_qla_host *vha,
2669 struct cmd_bidir *cmd_pkt, uint32_t tot_dsds)
2670{
2671 uint16_t avail_dsds;
2672 uint32_t *cur_dsd;
2673 uint32_t req_data_len = 0;
2674 uint32_t rsp_data_len = 0;
2675 struct scatterlist *sg;
2676 int index;
2677 int entry_count = 1;
2678 struct fc_bsg_job *bsg_job = sp->u.bsg_job;
2679
2680 /*Update entry type to indicate bidir command */
2681 *((uint32_t *)(&cmd_pkt->entry_type)) =
2682 __constant_cpu_to_le32(COMMAND_BIDIRECTIONAL);
2683
2684 /* Set the transfer direction, in this set both flags
2685 * Also set the BD_WRAP_BACK flag, firmware will take care
2686 * assigning DID=SID for outgoing pkts.
2687 */
2688 cmd_pkt->wr_dseg_count = cpu_to_le16(bsg_job->request_payload.sg_cnt);
2689 cmd_pkt->rd_dseg_count = cpu_to_le16(bsg_job->reply_payload.sg_cnt);
2690 cmd_pkt->control_flags =
2691 __constant_cpu_to_le16(BD_WRITE_DATA | BD_READ_DATA |
2692 BD_WRAP_BACK);
2693
2694 req_data_len = rsp_data_len = bsg_job->request_payload.payload_len;
2695 cmd_pkt->wr_byte_count = cpu_to_le32(req_data_len);
2696 cmd_pkt->rd_byte_count = cpu_to_le32(rsp_data_len);
2697 cmd_pkt->timeout = cpu_to_le16(qla2x00_get_async_timeout(vha) + 2);
2698
2699 vha->bidi_stats.transfer_bytes += req_data_len;
2700 vha->bidi_stats.io_count++;
2701
fabbb8df
JC
2702 vha->qla_stats.output_bytes += req_data_len;
2703 vha->qla_stats.output_requests++;
2704
a9b6f722
SK
2705 /* Only one dsd is available for bidirectional IOCB, remaining dsds
2706 * are bundled in continuation iocb
2707 */
2708 avail_dsds = 1;
2709 cur_dsd = (uint32_t *)&cmd_pkt->fcp_data_dseg_address;
2710
2711 index = 0;
2712
2713 for_each_sg(bsg_job->request_payload.sg_list, sg,
2714 bsg_job->request_payload.sg_cnt, index) {
2715 dma_addr_t sle_dma;
2716 cont_a64_entry_t *cont_pkt;
2717
2718 /* Allocate additional continuation packets */
2719 if (avail_dsds == 0) {
2720 /* Continuation type 1 IOCB can accomodate
2721 * 5 DSDS
2722 */
2723 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2724 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2725 avail_dsds = 5;
2726 entry_count++;
2727 }
2728 sle_dma = sg_dma_address(sg);
2729 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2730 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2731 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2732 avail_dsds--;
2733 }
2734 /* For read request DSD will always goes to continuation IOCB
2735 * and follow the write DSD. If there is room on the current IOCB
2736 * then it is added to that IOCB else new continuation IOCB is
2737 * allocated.
2738 */
2739 for_each_sg(bsg_job->reply_payload.sg_list, sg,
2740 bsg_job->reply_payload.sg_cnt, index) {
2741 dma_addr_t sle_dma;
2742 cont_a64_entry_t *cont_pkt;
2743
2744 /* Allocate additional continuation packets */
2745 if (avail_dsds == 0) {
2746 /* Continuation type 1 IOCB can accomodate
2747 * 5 DSDS
2748 */
2749 cont_pkt = qla2x00_prep_cont_type1_iocb(vha, vha->req);
2750 cur_dsd = (uint32_t *) cont_pkt->dseg_0_address;
2751 avail_dsds = 5;
2752 entry_count++;
2753 }
2754 sle_dma = sg_dma_address(sg);
2755 *cur_dsd++ = cpu_to_le32(LSD(sle_dma));
2756 *cur_dsd++ = cpu_to_le32(MSD(sle_dma));
2757 *cur_dsd++ = cpu_to_le32(sg_dma_len(sg));
2758 avail_dsds--;
2759 }
2760 /* This value should be same as number of IOCB required for this cmd */
2761 cmd_pkt->entry_count = entry_count;
2762}
2763
2764int
2765qla2x00_start_bidir(srb_t *sp, struct scsi_qla_host *vha, uint32_t tot_dsds)
2766{
2767
2768 struct qla_hw_data *ha = vha->hw;
2769 unsigned long flags;
2770 uint32_t handle;
2771 uint32_t index;
2772 uint16_t req_cnt;
2773 uint16_t cnt;
2774 uint32_t *clr_ptr;
2775 struct cmd_bidir *cmd_pkt = NULL;
2776 struct rsp_que *rsp;
2777 struct req_que *req;
2778 int rval = EXT_STATUS_OK;
a9b6f722
SK
2779
2780 rval = QLA_SUCCESS;
2781
2782 rsp = ha->rsp_q_map[0];
2783 req = vha->req;
2784
2785 /* Send marker if required */
2786 if (vha->marker_needed != 0) {
2787 if (qla2x00_marker(vha, req,
2788 rsp, 0, 0, MK_SYNC_ALL) != QLA_SUCCESS)
2789 return EXT_STATUS_MAILBOX;
2790 vha->marker_needed = 0;
2791 }
2792
2793 /* Acquire ring specific lock */
2794 spin_lock_irqsave(&ha->hardware_lock, flags);
2795
2796 /* Check for room in outstanding command list. */
2797 handle = req->current_outstanding_cmd;
8d93f550 2798 for (index = 1; index < req->num_outstanding_cmds; index++) {
a9b6f722 2799 handle++;
8d2b21db
BVA
2800 if (handle == req->num_outstanding_cmds)
2801 handle = 1;
2802 if (!req->outstanding_cmds[handle])
2803 break;
a9b6f722
SK
2804 }
2805
8d93f550 2806 if (index == req->num_outstanding_cmds) {
a9b6f722
SK
2807 rval = EXT_STATUS_BUSY;
2808 goto queuing_error;
2809 }
2810
2811 /* Calculate number of IOCB required */
2812 req_cnt = qla24xx_calc_iocbs(vha, tot_dsds);
2813
2814 /* Check for room on request queue. */
2815 if (req->cnt < req_cnt + 2) {
7c6300e3
JC
2816 cnt = IS_SHADOW_REG_CAPABLE(ha) ? *req->out_ptr :
2817 RD_REG_DWORD_RELAXED(req->req_q_out);
a9b6f722
SK
2818 if (req->ring_index < cnt)
2819 req->cnt = cnt - req->ring_index;
2820 else
2821 req->cnt = req->length -
2822 (req->ring_index - cnt);
2823 }
2824 if (req->cnt < req_cnt + 2) {
2825 rval = EXT_STATUS_BUSY;
2826 goto queuing_error;
2827 }
2828
2829 cmd_pkt = (struct cmd_bidir *)req->ring_ptr;
2830 cmd_pkt->handle = MAKE_HANDLE(req->id, handle);
2831
2832 /* Zero out remaining portion of packet. */
2833 /* tagged queuing modifier -- default is TSK_SIMPLE (0).*/
2834 clr_ptr = (uint32_t *)cmd_pkt + 2;
2835 memset(clr_ptr, 0, REQUEST_ENTRY_SIZE - 8);
2836
2837 /* Set NPORT-ID (of vha)*/
2838 cmd_pkt->nport_handle = cpu_to_le16(vha->self_login_loop_id);
2839 cmd_pkt->port_id[0] = vha->d_id.b.al_pa;
2840 cmd_pkt->port_id[1] = vha->d_id.b.area;
2841 cmd_pkt->port_id[2] = vha->d_id.b.domain;
2842
2843 qla25xx_build_bidir_iocb(sp, vha, cmd_pkt, tot_dsds);
2844 cmd_pkt->entry_status = (uint8_t) rsp->id;
2845 /* Build command packet. */
2846 req->current_outstanding_cmd = handle;
2847 req->outstanding_cmds[handle] = sp;
2848 sp->handle = handle;
2849 req->cnt -= req_cnt;
2850
2851 /* Send the command to the firmware */
2852 wmb();
2853 qla2x00_start_iocbs(vha, req);
2854queuing_error:
2855 spin_unlock_irqrestore(&ha->hardware_lock, flags);
2856 return rval;
2857}